ClassHierarchy.php
Go to the documentation of this file.
00001 <?php 00002 00003 00006 00032 class ClassHierarchy 00033 { 00035 public $classes = array(); 00036 00047 function __construct($rootClass) 00048 { 00049 $this->classes[$rootClass] = new ClassNode($rootClass, ""); 00050 } 00051 00052 function __destruct(){} 00053 00054 00066 public function addClassRelationship($class, $subClassOf) 00067 { 00068 // Make sure the relationship doesn't already exists 00069 if (isset($this->classes[$class]->subClassOf)) 00070 { 00071 foreach ($this->classes[$class]->subClassOf as $sc) 00072 { 00073 if ($sc->name == $subClassOf) 00074 { 00075 return; 00076 } 00077 } 00078 } 00079 00080 // First, check if the superClass exists. If it doesn't, we link it to owl:Thing 00081 if (!isset($this->classes[$subClassOf])) 00082 { 00083 $this->addClassRelationship($subClassOf, "http://www.w3.org/2002/07/owl#Thing"); 00084 } 00085 00086 // Then check if the class already belong to the structure. If it does, we only have to re-link the structure 00087 if (isset($this->classes[$class])) 00088 { 00089 $target = $this->classes[$class]; 00090 00091 $superClass = $this->classes[$subClassOf]; 00092 00093 array_push($superClass->superClassOf, $target); 00094 00095 array_push($target->subClassOf, $superClass); 00096 00097 00098 // Lets remove the owl:Thing link if it was existing (introduced at step 1). 00100 $newSubclassArray = array(); 00101 00102 foreach ($target->subClassOf as $sc) 00103 { 00104 if ($sc->name != "http://www.w3.org/2002/07/owl#Thing") 00105 { 00106 array_push($newSubclassArray, $sc); 00107 } 00108 else 00109 { 00110 // Remove the link from the subClassOf owl:Thing too! 00111 $newSuperClassArray = array(); 00112 00113 $owlThing = $this->classes["http://www.w3.org/2002/07/owl#Thing"]; 00114 00115 foreach ($owlThing->superClassOf as $sc) 00116 { 00117 if ($sc->name != $target->name) 00118 { 00119 array_push($newSuperClassArray, $sc); 00120 } 00121 } 00122 00123 $owlThing->superClassOf = $newSuperClassArray; 00124 } 00125 } 00126 00127 $target->subClassOf = $newSubclassArray; 00129 } 00130 else 00131 { 00132 // Otherwise we have a new node to add to the structure. 00133 $newClass = new ClassNode($class, $superClass); 00134 $this->classes[$class] = $newClass; 00135 00136 $superClass = $this->classes[$subClassOf]; 00137 00138 array_push($superClass->superClassOf, &$newClass); 00139 00140 array_push($newClass->subClassOf, $superClass); 00141 } 00142 } 00143 00156 public function getSuperClasses($class) 00157 { 00158 $superClasses = array(); 00159 $stack = array(); 00160 00161 if (isset($this->classes[$class])) 00162 { 00163 // Initialize the stack 00164 foreach ($this->classes[$class]->subClassOf as $sc) 00165 { 00166 if (array_search($sc, $stack) === FALSE) 00167 { 00168 array_push($stack, $sc); 00169 } 00170 } 00171 00172 while (count($stack) > 0) 00173 { 00174 $target = array_pop($stack); 00175 00176 array_push($superClasses, $target); 00177 00178 if (isset($target->subClassOf)) 00179 { 00180 foreach ($target->subClassOf as $sc) 00181 { 00182 if (array_search($sc, $stack) === FALSE) 00183 { 00184 array_push($stack, $sc); 00185 } 00186 } 00187 } 00188 } 00189 } 00190 00191 return $superClasses; 00192 } 00193 00206 public function getSubClasses($class) 00207 { 00208 $subClasses = array(); 00209 $stack = array(); 00210 00211 if (isset($this->classes[$class])) 00212 { 00213 // Initialize the stack 00214 foreach ($this->classes[$class]->superClassOf as $sc) 00215 { 00216 if (array_search($sc, $stack) === FALSE) 00217 { 00218 array_push($stack, $sc); 00219 } 00220 } 00221 00222 while (count($stack) > 0) 00223 { 00224 $target = array_pop($stack); 00225 00226 array_push($subClasses, $target); 00227 00228 if (isset($target->superClassOf)) 00229 { 00230 foreach ($target->superClassOf as $sc) 00231 { 00232 if (array_search($sc, $stack) === FALSE) 00233 { 00234 array_push($stack, $sc); 00235 } 00236 } 00237 } 00238 } 00239 } 00240 00241 return $subClasses; 00242 } 00243 00257 public function isSubClassOf($subClass, $superClass) 00258 { 00259 $superClasses = $this->getSuperClasses($subClass); 00260 00261 foreach ($superClasses as $sc) 00262 { 00263 if ($sc->name == $superClass) 00264 { 00265 return (TRUE); 00266 } 00267 } 00268 00269 return (FALSE); 00270 } 00271 } 00272 00281 class ClassNode 00282 { 00284 public $name = ""; 00285 00287 public $label = ""; 00288 00290 public $description = ""; 00291 00294 public $template = ""; 00295 00297 public $subClassOf = array(); // array of pointers 00298 00300 public $superClassOf = array(); // array of pointers 00301 00313 function __construct($name, $subClassOf) 00314 { 00315 $this->name = $name; 00316 00317 if (isset($subClassOf->name) && $subClassOf->name != "") 00318 { 00319 $this->subClassOf[$subClassOf->name] = $subClassOf; 00320 } 00321 } 00322 00323 function __destruct(){} 00324 } 00325 00327 00328 ?>
