PropertyHierarchy.php
Go to the documentation of this file.
00001 <?php 00002 00005 00028 class PropertyHierarchy 00029 { 00031 public $properties = array(); 00032 00043 function __construct($rootProperty) 00044 { 00045 $this->properties[$rootProperty] = new PropertyNode($rootProperty, ""); 00046 } 00047 00048 function __destruct() { } 00049 00061 public function addPropertyRelationship($property, $subPropertyOf) 00062 { 00063 // Make sure the relationship doesn't already exists 00064 00065 if(isset($this->properties[$property]->subPropertyOf)) 00066 { 00067 foreach($this->properties[$property]->subPropertyOf as $sp) 00068 { 00069 if($sp->name == $subPropertyOf) 00070 { 00071 return; 00072 } 00073 } 00074 } 00075 00076 // First, check if the superProperty exists. If it doesn't, we link it to owl:Thing 00077 if(!isset($this->properties[$subPropertyOf])) 00078 { 00079 $this->addPropertyRelationship($subPropertyOf, "http://www.w3.org/2002/07/owl#Thing"); 00080 } 00081 00082 // Then check if the property already belong to the structure. If it does, we only have to re-link the structure 00083 if(isset($this->properties[$property])) 00084 { 00085 $target = $this->properties[$property]; 00086 00087 $superProperty = $this->properties[$subPropertyOf]; 00088 00089 array_push($superProperty->superPropertyOf, $target); 00090 00091 array_push($target->subPropertyOf, $superProperty); 00092 00093 00094 // Lets remove the owl:Thing link if it was existing (introduced at step 1). 00096 $newSubpropertyArray = array(); 00097 00098 foreach($target->subPropertyOf as $sp) 00099 { 00100 if($sp->name != "http://www.w3.org/2002/07/owl#Thing") 00101 { 00102 array_push($newSubpropertyArray, $sp); 00103 } 00104 else 00105 { 00106 // Remove the link from the subPropertyOf owl:Thing too! 00107 $newSuperPropertyArray = array(); 00108 00109 $owlThing = $this->properties["http://www.w3.org/2002/07/owl#Thing"]; 00110 00111 foreach($owlThing->superPropertyOf as $sp) 00112 { 00113 if($sp->name != $target->name) 00114 { 00115 array_push($newSuperPropertyArray, $sp); 00116 } 00117 } 00118 00119 $owlThing->superPropertyOf = $newSuperPropertyArray; 00120 } 00121 } 00122 00123 $target->subPropertyOf = $newSubpropertyArray; 00125 } 00126 else 00127 { 00128 // Otherwise we have a new node to add to the structure. 00129 $newProperty = new PropertyNode($property, $superProperty); 00130 $this->properties[$property] = $newProperty; 00131 00132 $superProperty = $this->properties[$subPropertyOf]; 00133 00134 array_push($superProperty->superPropertyOf, &$newProperty); 00135 00136 array_push($newProperty->subPropertyOf, $superProperty); 00137 } 00138 } 00139 00152 public function getSuperProperties($property) 00153 { 00154 $superProperties = array(); 00155 $stack = array(); 00156 00157 if(isset($this->properties[$property])) 00158 { 00159 // Initialize the stack 00160 foreach($this->properties[$property]->subPropertyOf as $sp) 00161 { 00162 if(array_search($sp, $stack) === FALSE) 00163 { 00164 array_push($stack, $sp); 00165 } 00166 } 00167 00168 while(count($stack) > 0) 00169 { 00170 $target = array_pop($stack); 00171 00172 array_push($superProperties, $target); 00173 00174 if(isset($target->subPropertyOf)) 00175 { 00176 foreach($target->subPropertyOf as $sp) 00177 { 00178 if(array_search($sp, $stack) === FALSE) 00179 { 00180 array_push($stack, $sp); 00181 } 00182 } 00183 } 00184 } 00185 } 00186 00187 return $superProperties; 00188 } 00189 00202 public function getSubproperties($property) 00203 { 00204 $subproperties = array(); 00205 $stack = array(); 00206 00207 if(isset($this->properties[$property])) 00208 { 00209 // Initialize the stack 00210 foreach($this->properties[$property]->superPropertyOf as $sp) 00211 { 00212 if(array_search($sp, $stack) === FALSE) 00213 { 00214 array_push($stack, $sp); 00215 } 00216 } 00217 00218 while(count($stack) > 0) 00219 { 00220 $target = array_pop($stack); 00221 00222 array_push($subproperties, $target); 00223 00224 if(isset($target->superPropertyOf)) 00225 { 00226 foreach($target->superPropertyOf as $sp) 00227 { 00228 if(array_search($sp, $stack) === FALSE) 00229 { 00230 array_push($stack, $sp); 00231 } 00232 } 00233 } 00234 } 00235 } 00236 00237 return $subproperties; 00238 } 00239 00253 public function isSubPropertyOf($subproperty, $superProperty) 00254 { 00255 $superProperties = $this->getsuperProperties($subproperty); 00256 00257 foreach($superProperties as $sp) 00258 { 00259 if($sp->name == $superProperty) 00260 { 00261 return (TRUE); 00262 } 00263 } 00264 00265 return (FALSE); 00266 } 00267 00268 00281 public function inDomainOf($class) 00282 { 00283 $inDomain = array(); 00284 00285 foreach($this->properties as $property) 00286 { 00287 if(array_search($class, $property->domain) !== FALSE) 00288 { 00289 array_push($inDomain, $property->name); 00290 } 00291 } 00292 00293 return ($inDomain); 00294 } 00295 00308 public function inRangeOf($class) 00309 { 00310 $inRange = array(); 00311 00312 foreach($this->properties as $property) 00313 { 00314 if(array_search($class, $property->range) !== FALSE) 00315 { 00316 array_push($inRange, $property->name); 00317 } 00318 } 00319 00320 return ($inRange); 00321 } 00322 00323 public function getProperty($propertyURI) 00324 { 00325 foreach($this->properties as $property) 00326 { 00327 if($property->name == $propertyURI) 00328 { 00329 return ($property); 00330 } 00331 } 00332 00333 return (NULL); 00334 } 00335 } 00336 00337 00346 class propertyNode 00347 { 00349 public $name = ""; 00350 00352 public $label = ""; 00353 00355 public $description = ""; 00356 00357 /* public $displayCluster = "generic"; 00358 public $displayCardinality = "*"; 00359 public $displayKind = "string"; 00360 public $displayPriority = 1; 00361 */ 00363 public $subPropertyOf = array(); 00364 00366 public $superPropertyOf = array(); 00367 00369 public $domain = array(); 00370 00372 public $range = array(); 00373 00385 function __construct($name, $subPropertyOf) 00386 { 00387 $this->name = $name; 00388 00389 if(isset($subPropertyOf->name) && $subPropertyOf->name != "") 00390 { 00391 $this->subPropertyOf[$subPropertyOf->name] = $subPropertyOf; 00392 } 00393 } 00394 00395 function __destruct() { } 00396 } 00397 ?>
