00001 <?php
00022 class SMWExpData extends SMWExpElement {
00027 protected $m_subject;
00033 protected $m_children = array();
00038 protected $m_edges = array();
00039
00044 public function __construct( SMWExpResource $subject ) {
00045 parent::__construct( $subject->getDataItem() );
00046 $this->m_subject = $subject;
00047 }
00048
00055 public static function makeCollection( array $elements ) {
00056 if ( count( $elements ) == 0 ) {
00057 return new SMWExpData( SMWExporter::getSpecialNsResource( 'rdf', 'nil' ) );
00058 } else {
00059 $rdftype = SMWExporter::getSpecialNsResource( 'rdf', 'type' );
00060 $rdffirst = SMWExporter::getSpecialNsResource( 'rdf', 'first' );
00061 $rdfrest = SMWExporter::getSpecialNsResource( 'rdf', 'rest' );
00062 $result = new SMWExpData( new SMWExpResource( '' ) );
00063 $result->addPropertyObjectValue( $rdftype, new SMWExpData( SMWExporter::getSpecialNsResource( 'rdf', 'List' ) ) );
00064 $result->addPropertyObjectValue( $rdffirst, array_shift( $elements ) );
00065 $result->addPropertyObjectValue( $rdfrest, SMWExpData::makeCollection( $elements ) );
00066 return $result;
00067 }
00068 }
00069
00075 public function getSubject() {
00076 return $this->m_subject;
00077 }
00078
00087 public function addPropertyObjectValue( SMWExpNsResource $property, SMWExpElement $child ) {
00088 if ( !array_key_exists( $property->getUri(), $this->m_edges ) ) {
00089 $this->m_children[$property->getUri()] = array();
00090 $this->m_edges[$property->getUri()] = $property;
00091 }
00092 $this->m_children[$property->getUri()][] = $child;
00093 }
00094
00101 public function getProperties() {
00102 return $this->m_edges;
00103 }
00104
00111 public function getValues( SMWExpResource $property ) {
00112 if ( array_key_exists( $property->getUri(), $this->m_children ) ) {
00113 return $this->m_children[$property->getUri()];
00114 } else {
00115 return array();
00116 }
00117 }
00118
00127 public function getSpecialValues( $namespaceId, $localName ) {
00128 $pe = SMWExporter::getSpecialNsResource( $namespaceId, $localName );
00129 return $this->getValues( $pe );
00130 }
00131
00144 public function extractMainType() {
00145 $pe = SMWExporter::getSpecialNsResource( 'rdf', 'type' );
00146 if ( array_key_exists( $pe->getUri(), $this->m_children ) ) {
00147 $result = array_shift( $this->m_children[$pe->getUri()] );
00148 if ( count( $this->m_children[$pe->getUri()] ) == 0 ) {
00149 unset( $this->m_edges[$pe->getUri()] );
00150 unset( $this->m_children[$pe->getUri()] );
00151 }
00152 return ( $result instanceof SMWExpData ) ? $result->getSubject() : $result;
00153 } else {
00154 return SMWExporter::getSpecialNsResource( 'rdf', 'Resource' );
00155 }
00156 }
00157
00168 public function getCollection() {
00169 $rdftypeUri = SMWExporter::getSpecialNsResource( 'rdf', 'type' )->getUri();
00170 $rdffirstUri = SMWExporter::getSpecialNsResource( 'rdf', 'first' )->getUri();
00171 $rdfrestUri = SMWExporter::getSpecialNsResource( 'rdf', 'rest' )->getUri();
00172 $rdfnilUri = SMWExporter::getSpecialNsResource( 'rdf', 'nil' )->getUri();
00173
00174 if ( ( $this->m_subject->isBlankNode() ) &&
00175 ( count( $this->m_children ) == 3 ) &&
00176 ( array_key_exists( $rdftypeUri, $this->m_children ) ) &&
00177 ( count( $this->m_children[$rdftypeUri] ) == 1 ) &&
00178 ( array_key_exists( $rdffirstUri, $this->m_children ) ) &&
00179 ( count( $this->m_children[$rdffirstUri] ) == 1 ) &&
00180 !( end( $this->m_children[$rdffirstUri] ) instanceof SMWExpLiteral ) &&
00181
00182 ( array_key_exists( $rdfrestUri, $this->m_children ) ) &&
00183 ( count( $this->m_children[$rdfrestUri] ) == 1 ) ) {
00184 $typedata = end( $this->m_children[$rdftypeUri] );
00185 $rdflistUri = SMWExporter::getSpecialNsResource( 'rdf', 'List' )->getUri();
00186 if ( $typedata->getSubject()->getUri() == $rdflistUri ) {
00187 $first = end( $this->m_children[$rdffirstUri] );
00188 $rest = end( $this->m_children[$rdfrestUri] );
00189 if ( $rest instanceof SMWExpData ) {
00190 $restlist = $rest->getCollection();
00191 if ( $restlist === false ) {
00192 return false;
00193 } else {
00194 array_unshift( $restlist, $first );
00195 return $restlist;
00196 }
00197 } elseif ( ( $rest instanceof SMWExpResource ) &&
00198 ( $rest->getUri() == $rdfnilUri ) ) {
00199 return array( $first );
00200 } else {
00201 return false;
00202 }
00203 } else {
00204 return false;
00205 }
00206 } elseif ( ( count( $this->m_children ) == 0 ) && ( $this->m_subject->getUri() == $rdfnilUri ) ) {
00207 return array();
00208 } else {
00209 return false;
00210 }
00211 }
00212
00219 public function getTripleList( SMWExpElement $subject = null ) {
00220 global $smwgBnodeCount;
00221 if ( !isset( $smwgBnodeCount ) ) {
00222 $smwgBnodeCount = 0;
00223 }
00224
00225 if ( $subject == null ) {
00226 $subject = $this->m_subject;
00227 }
00228
00229 $result = array();
00230
00231 foreach ( $this->m_edges as $key => $edge ) {
00232 foreach ( $this->m_children[$key] as $childElement ) {
00233 if ( $childElement instanceof SMWExpData ) {
00234 $childSubject = $childElement->getSubject();
00235 } else {
00236 $childSubject = $childElement;
00237 }
00238
00239 if ( ( $childSubject instanceof SMWExpResource ) &&
00240 ( $childSubject->isBlankNode() ) ) {
00241
00242 $childSubject = new SMWExpResource( '_' . $smwgBnodeCount++, $childSubject->getDataItem() );
00243 }
00244
00245 $result[] = array( $subject, $edge, $childSubject );
00246 if ( $childElement instanceof SMWExpData ) {
00247 $result = array_merge( $result, $childElement->getTripleList( $childSubject ) );
00248 }
00249 }
00250 }
00251
00252 return $result;
00253 }
00254
00255 }