ClassHierarchy.php
Go to the documentation of this file.
00001 <?php 00002 00005 00028 class ClassHierarchy 00029 { 00031 public $classes = array(); 00032 00043 function __construct($rootClass) 00044 { 00045 $this->classes[$rootClass] = new ClassNode($rootClass, ""); 00046 } 00047 00048 function __destruct() { } 00049 00050 00062 public function addClassRelationship($class, $subClassOf) 00063 { 00064 // Make sure the relationship doesn't already exists 00065 if(isset($this->classes[$class]->subClassOf)) 00066 { 00067 foreach($this->classes[$class]->subClassOf as $sc) 00068 { 00069 if($sc->name == $subClassOf) 00070 { 00071 return; 00072 } 00073 } 00074 } 00075 00076 // First, check if the superClass exists. If it doesn't, we link it to owl:Thing 00077 if(!isset($this->classes[$subClassOf])) 00078 { 00079 $this->addClassRelationship($subClassOf, "http://www.w3.org/2002/07/owl#Thing"); 00080 } 00081 00082 // Then check if the class already belong to the structure. If it does, we only have to re-link the structure 00083 if(isset($this->classes[$class])) 00084 { 00085 $target = $this->classes[$class]; 00086 00087 $superClass = $this->classes[$subClassOf]; 00088 00089 array_push($superClass->superClassOf, $target); 00090 00091 array_push($target->subClassOf, $superClass); 00092 00093 00094 // Lets remove the owl:Thing link if it was existing (introduced at step 1). 00096 $newSubclassArray = array(); 00097 00098 foreach($target->subClassOf as $sc) 00099 { 00100 if($sc->name != "http://www.w3.org/2002/07/owl#Thing") 00101 { 00102 array_push($newSubclassArray, $sc); 00103 } 00104 else 00105 { 00106 // Remove the link from the subClassOf owl:Thing too! 00107 $newSuperClassArray = array(); 00108 00109 $owlThing = $this->classes["http://www.w3.org/2002/07/owl#Thing"]; 00110 00111 foreach($owlThing->superClassOf as $sc) 00112 { 00113 if($sc->name != $target->name) 00114 { 00115 array_push($newSuperClassArray, $sc); 00116 } 00117 } 00118 00119 $owlThing->superClassOf = $newSuperClassArray; 00120 } 00121 } 00122 00123 $target->subClassOf = $newSubclassArray; 00125 } 00126 else 00127 { 00128 // Otherwise we have a new node to add to the structure. 00129 $newClass = new ClassNode($class, $superClass); 00130 $this->classes[$class] = $newClass; 00131 00132 $superClass = $this->classes[$subClassOf]; 00133 00134 array_push($superClass->superClassOf, &$newClass); 00135 00136 array_push($newClass->subClassOf, $superClass); 00137 } 00138 } 00139 00152 public function getSuperClasses($class) 00153 { 00154 $superClasses = array(); 00155 $stack = array(); 00156 00157 if(isset($this->classes[$class])) 00158 { 00159 // Initialize the stack 00160 foreach($this->classes[$class]->subClassOf as $sc) 00161 { 00162 if(array_search($sc, $stack) === FALSE) 00163 { 00164 array_push($stack, $sc); 00165 } 00166 } 00167 00168 while(count($stack) > 0) 00169 { 00170 $target = array_pop($stack); 00171 00172 array_push($superClasses, $target); 00173 00174 if(isset($target->subClassOf)) 00175 { 00176 foreach($target->subClassOf as $sc) 00177 { 00178 if(array_search($sc, $stack) === FALSE) 00179 { 00180 array_push($stack, $sc); 00181 } 00182 } 00183 } 00184 } 00185 } 00186 00187 return $superClasses; 00188 } 00189 00202 public function getSubClasses($class) 00203 { 00204 $subClasses = array(); 00205 $stack = array(); 00206 00207 if(isset($this->classes[$class])) 00208 { 00209 // Initialize the stack 00210 foreach($this->classes[$class]->superClassOf as $sc) 00211 { 00212 if(array_search($sc, $stack) === FALSE) 00213 { 00214 array_push($stack, $sc); 00215 } 00216 } 00217 00218 while(count($stack) > 0) 00219 { 00220 $target = array_pop($stack); 00221 00222 array_push($subClasses, $target); 00223 00224 if(isset($target->superClassOf)) 00225 { 00226 foreach($target->superClassOf as $sc) 00227 { 00228 if(array_search($sc, $stack) === FALSE) 00229 { 00230 array_push($stack, $sc); 00231 } 00232 } 00233 } 00234 } 00235 } 00236 00237 return $subClasses; 00238 } 00239 00253 public function isSubClassOf($subClass, $superClass) 00254 { 00255 $superClasses = $this->getSuperClasses($subClass); 00256 00257 foreach($superClasses as $sc) 00258 { 00259 if($sc->name == $superClass) 00260 { 00261 return (TRUE); 00262 } 00263 } 00264 00265 return (FALSE); 00266 } 00267 } 00268 00277 class ClassNode 00278 { 00280 public $name = ""; 00281 00283 public $label = ""; 00284 00286 public $description = ""; 00287 00289 public $subClassOf = array(); // array of pointers 00290 00292 public $superClassOf = array(); // array of pointers 00293 00305 function __construct($name, $subClassOf) 00306 { 00307 $this->name = $name; 00308 00309 if(isset($subClassOf->name) && $subClassOf->name != "") 00310 { 00311 $this->subClassOf[$subClassOf->name] = $subClassOf; 00312 } 00313 } 00314 00315 function __destruct() { } 00316 } 00317 00319 00320 ?>
