relationgraph.class.inc.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. <?php
  2. // Copyright (C) 2015-2017 Combodo SARL
  3. //
  4. // This file is part of iTop.
  5. //
  6. // iTop is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // iTop is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with iTop. If not, see <http://www.gnu.org/licenses/>
  18. /**
  19. * Data structures (i.e. PHP classes) to build and use relation graphs
  20. *
  21. * @copyright Copyright (C) 2015-2017 Combodo SARL
  22. * @license http://opensource.org/licenses/AGPL-3.0
  23. *
  24. */
  25. require_once(APPROOT.'core/simplegraph.class.inc.php');
  26. /**
  27. * An object Node inside a RelationGraph
  28. */
  29. class RelationObjectNode extends GraphNode
  30. {
  31. public function __construct($oGraph, $oObject)
  32. {
  33. parent::__construct($oGraph, self::MakeId($oObject));
  34. $this->SetProperty('object', $oObject);
  35. $this->SetProperty('label', get_class($oObject).'::'.$oObject->GetKey().' ('.$oObject->Get('friendlyname').')');
  36. }
  37. /**
  38. * Make a normalized ID to ensure the uniqueness of such a node
  39. */
  40. public static function MakeId($oObject)
  41. {
  42. return get_class($oObject).'::'.$oObject->GetKey();
  43. }
  44. /**
  45. * Formatting for GraphViz
  46. */
  47. public function GetDotAttributes($bNoLabel = false)
  48. {
  49. $sDot = parent::GetDotAttributes();
  50. if ($this->GetProperty('developped', false))
  51. {
  52. $sDot .= ',fontcolor=black';
  53. }
  54. else
  55. {
  56. $sDot .= ',fontcolor=lightgrey';
  57. }
  58. if ($this->GetProperty('source', false) || $this->GetProperty('sink', false))
  59. {
  60. $sDot .= ',shape=rectangle';
  61. }
  62. if ($this->GetProperty('is_reached', false))
  63. {
  64. $sDot .= ',fillcolor="#ffdddd"';
  65. }
  66. else
  67. {
  68. $sDot .= ',fillcolor=white';
  69. }
  70. return $sDot;
  71. }
  72. /**
  73. * Recursively mark the objects nodes as reached, unless we get stopped by a redundancy node or a 'not allowed' node
  74. */
  75. public function ReachDown($sProperty, $value)
  76. {
  77. if (is_null($this->GetProperty($sProperty)) && ($this->GetProperty($sProperty.'_allowed') !== false))
  78. {
  79. $this->SetProperty($sProperty, $value);
  80. foreach ($this->GetOutgoingEdges() as $oOutgoingEdge)
  81. {
  82. // Recurse
  83. $oOutgoingEdge->GetSinkNode()->ReachDown($sProperty, $value);
  84. }
  85. }
  86. }
  87. }
  88. /**
  89. * An redundancy Node inside a RelationGraph
  90. */
  91. class RelationRedundancyNode extends GraphNode
  92. {
  93. public function __construct($oGraph, $sId, $iMinUp, $fThreshold)
  94. {
  95. parent::__construct($oGraph, $sId);
  96. $this->SetProperty('min_up', $iMinUp);
  97. $this->SetProperty('threshold', $fThreshold);
  98. }
  99. /**
  100. * Make a normalized ID to ensure the uniqueness of such a node
  101. */
  102. public static function MakeId($sRelCode, $sNeighbourId, $oSourceObject, $oSinkObject)
  103. {
  104. return 'redundancy-'.$sRelCode.'-'.$sNeighbourId.'-'.get_class($oSinkObject).'::'.$oSinkObject->GetKey();
  105. }
  106. /**
  107. * Formatting for GraphViz
  108. */
  109. public function GetDotAttributes($bNoLabel = false)
  110. {
  111. $sDisplayThreshold = sprintf('%.1f', $this->GetProperty('threshold'));
  112. $sDot = 'shape=doublecircle,fillcolor=indianred,fontcolor=papayawhip,label="'.$sDisplayThreshold.'"';
  113. return $sDot;
  114. }
  115. /**
  116. * Recursively mark the objects nodes as reached, unless we get stopped by a redundancy node
  117. */
  118. public function ReachDown($sProperty, $value)
  119. {
  120. $this->SetProperty($sProperty.'_count', $this->GetProperty($sProperty.'_count', 0) + 1);
  121. if ($this->GetProperty($sProperty.'_count') > $this->GetProperty('threshold'))
  122. {
  123. // Looping... though there should be only ONE SINGLE outgoing edge
  124. foreach ($this->GetOutgoingEdges() as $oOutgoingEdge)
  125. {
  126. // Recurse
  127. $oOutgoingEdge->GetSinkNode()->ReachDown($sProperty, $value);
  128. }
  129. }
  130. }
  131. }
  132. /**
  133. * Helper to name the edges in a unique way
  134. */
  135. class RelationEdge extends GraphEdge
  136. {
  137. public function __construct(SimpleGraph $oGraph, GraphNode $oSourceNode, GraphNode $oSinkNode, $bMustBeUnique = false)
  138. {
  139. $sId = $oSourceNode->GetId().'-to-'.$oSinkNode->GetId();
  140. parent::__construct($oGraph, $sId, $oSourceNode, $oSinkNode, $bMustBeUnique);
  141. }
  142. }
  143. /**
  144. * A graph representing the relations between objects
  145. * The graph is made of two types of nodes. Here is a list of the meaningful node properties
  146. * 1) RelationObjectNode
  147. * source: boolean, that node was added as a source node
  148. * sink: boolean, that node was added as a sink node
  149. * reached: boolean, that node has been marked as reached (impacted by the source nodes)
  150. * developped: boolean, that node has been visited to search for related objects
  151. * 1) RelationRedundancyNode
  152. * reached_count: int, the number of source nodes having reached=true
  153. * threshold: float, if reached_count > threshold, the sink nodes become reachable
  154. */
  155. class RelationGraph extends SimpleGraph
  156. {
  157. protected $aSourceNodes; // Index of source nodes (for a quicker access)
  158. protected $aSinkNodes; // Index of sink nodes (for a quicker access)
  159. protected $aRedundancySettings; // Cache of user settings
  160. protected $aContextSearches; // Context ("knowing that") stored as a hash array 'class' => DBObjectSearch
  161. public function __construct()
  162. {
  163. parent::__construct();
  164. $this->aSourceNodes = array();
  165. $this->aSinkNodes = array();
  166. $this->aRedundancySettings = array();
  167. $this->aContextSearches = array();
  168. }
  169. /**
  170. * Add an object that will be the starting point for building the relations downstream
  171. */
  172. public function AddSourceObject(DBObject $oObject)
  173. {
  174. $oSourceNode = new RelationObjectNode($this, $oObject);
  175. $oSourceNode->SetProperty('source', true);
  176. $this->aSourceNodes[$oSourceNode->GetId()] = $oSourceNode;
  177. }
  178. /**
  179. * Add an object that will be the starting point for building the relations uptream
  180. */
  181. public function AddSinkObject(DBObject$oObject)
  182. {
  183. $oSinkNode = new RelationObjectNode($this, $oObject);
  184. $oSinkNode->SetProperty('sink', true);
  185. $this->aSinkNodes[$oSinkNode->GetId()] = $oSinkNode;
  186. }
  187. /**
  188. * Add a 'context' OQL query, specifying extra objects to be marked as 'is_reached'
  189. * even though they are not part of the sources.
  190. * @param string $sOQL The OQL query defining the context objects
  191. */
  192. public function AddContextQuery($key, $sOQL)
  193. {
  194. if ($sOQL === '') return;
  195. $oSearch = DBObjectSearch::FromOQL($sOQL);
  196. $aAliases = $oSearch->GetSelectedClasses();
  197. if (count($aAliases) < 2 )
  198. {
  199. IssueLog::Error("Invalid context query '$sOQL'. A context query must contain at least two columns.");
  200. throw new Exception("Invalid context query '$sOQL'. A context query must contain at least two columns. Columns: ".implode(', ', $aAliases).'. ');
  201. }
  202. $aAliasNames = array_keys($aAliases);
  203. $sClassAlias = $oSearch->GetClassAlias();
  204. $oCondition = new BinaryExpression(new FieldExpression('id', $aAliasNames[0]), '=', new VariableExpression('id'));
  205. $oSearch->AddConditionExpression($oCondition);
  206. $sClass = $oSearch->GetClass();
  207. if (!array_key_exists($sClass, $this->aContextSearches))
  208. {
  209. $this->aContextSearches[$sClass] = array();
  210. }
  211. $this->aContextSearches[$sClass][] = array('key' => $key, 'search' => $oSearch);
  212. }
  213. /**
  214. * Determines if the given DBObject is part of a 'context'
  215. * @param DBObject $oObj
  216. * @return boolean
  217. */
  218. public function IsPartOfContext(DBObject $oObj, &$aRootCauses)
  219. {
  220. $bRet = false;
  221. $sFinalClass = get_class($oObj);
  222. $aParentClasses = MetaModel::EnumParentClasses($sFinalClass, ENUM_PARENT_CLASSES_ALL);
  223. foreach($aParentClasses as $sClass)
  224. {
  225. if (array_key_exists($sClass, $this->aContextSearches))
  226. {
  227. foreach($this->aContextSearches[$sClass] as $aContextQuery)
  228. {
  229. $aAliases = $aContextQuery['search']->GetSelectedClasses();
  230. $aAliasNames = array_keys($aAliases);
  231. $sRootCauseAlias = $aAliasNames[1]; // 1st column (=0) = object, second column = root cause
  232. $oSet = new DBObjectSet($aContextQuery['search'], array(), array('id' => $oObj->GetKey()));
  233. $oSet->OptimizeColumnLoad(array($aAliasNames[0] => array(), $aAliasNames[1] => array())); // Do not load any column... better do a reload than many joins
  234. while($aRow = $oSet->FetchAssoc())
  235. {
  236. if (!is_null($aRow[$sRootCauseAlias]))
  237. {
  238. if (!array_key_exists($aContextQuery['key'], $aRootCauses))
  239. {
  240. $aRootCauses[$aContextQuery['key']] = array();
  241. }
  242. $aRootCauses[$aContextQuery['key']][] = $aRow[$sRootCauseAlias];
  243. $bRet = true;
  244. }
  245. }
  246. }
  247. }
  248. }
  249. return $bRet;
  250. }
  251. /**
  252. * Build the graph downstream, and mark the nodes that can be reached from the source node
  253. */
  254. public function ComputeRelatedObjectsDown($sRelCode, $iMaxDepth, $bEnableRedundancy, $aUnreachableObjects = array())
  255. {
  256. //echo "<h5>Sources only...</h5>\n".$this->DumpAsHtmlImage()."<br/>\n";
  257. // Build the graph out of the sources
  258. foreach ($this->aSourceNodes as $oSourceNode)
  259. {
  260. $this->AddRelatedObjects($sRelCode, true, $oSourceNode, $iMaxDepth, $bEnableRedundancy);
  261. //echo "<h5>After processing of {$oSourceNode->GetId()}</h5>\n".$this->DumpAsHtmlImage()."<br/>\n";
  262. }
  263. // Mark the unreachable nodes
  264. foreach ($aUnreachableObjects as $oObj)
  265. {
  266. $sNodeId = RelationObjectNode::MakeId($oObj);
  267. $oNode = $this->GetNode($sNodeId);
  268. if($oNode)
  269. {
  270. $oNode->SetProperty('is_reached_allowed', false);
  271. }
  272. }
  273. // Determine the reached nodes
  274. foreach ($this->aSourceNodes as $oSourceNode)
  275. {
  276. $oSourceNode->ReachDown('is_reached', true);
  277. //echo "<h5>After reaching from {$oSourceNode->GetId()}</h5>\n".$this->DumpAsHtmlImage()."<br/>\n";
  278. }
  279. // Mark also the "context" nodes as reached and record the "root causes" for each node
  280. $oIterator = new RelationTypeIterator($this, 'Node');
  281. foreach($oIterator as $oNode)
  282. {
  283. $oObj = $oNode->GetProperty('object');
  284. $aRootCauses = array();
  285. if (!is_null($oObj) && $this->IsPartOfContext($oObj, $aRootCauses))
  286. {
  287. $oNode->SetProperty('context_root_causes', $aRootCauses);
  288. $oNode->ReachDown('is_reached', true);
  289. }
  290. }
  291. }
  292. /**
  293. * Build the graph upstream
  294. */
  295. public function ComputeRelatedObjectsUp($sRelCode, $iMaxDepth, $bEnableRedundancy)
  296. {
  297. //echo "<h5>Sinks only...</h5>\n".$this->DumpAsHtmlImage()."<br/>\n";
  298. // Build the graph out of the sinks
  299. foreach ($this->aSinkNodes as $oSinkNode)
  300. {
  301. $this->AddRelatedObjects($sRelCode, false, $oSinkNode, $iMaxDepth, $bEnableRedundancy);
  302. //echo "<h5>After processing of {$oSinkNode->GetId()}</h5>\n".$this->DumpAsHtmlImage()."<br/>\n";
  303. }
  304. // Mark also the "context" nodes as reached and record the "root causes" for each node
  305. $oIterator = new RelationTypeIterator($this, 'Node');
  306. foreach($oIterator as $oNode)
  307. {
  308. $oObj = $oNode->GetProperty('object');
  309. $aRootCauses = array();
  310. if (!is_null($oObj) && $this->IsPartOfContext($oObj, $aRootCauses))
  311. {
  312. $oNode->SetProperty('context_root_causes', $aRootCauses);
  313. $oNode->ReachDown('is_reached', true);
  314. }
  315. }
  316. }
  317. /**
  318. * Recursively find related objects, and add them into the graph
  319. *
  320. * @param string $sRelCode The code of the relation to use for the computation
  321. * @param boolean $bDown The direction: downstream or upstream
  322. * @param array $oObjectNode The node from which to compute the neighbours
  323. * @param int $iMaxDepth
  324. * @param boolean $bEnableReduncancy
  325. *
  326. * @return void
  327. */
  328. protected function AddRelatedObjects($sRelCode, $bDown, $oObjectNode, $iMaxDepth, $bEnableRedundancy)
  329. {
  330. if ($iMaxDepth > 0)
  331. {
  332. if ($oObjectNode instanceof RelationRedundancyNode)
  333. {
  334. // Note: this happens when recursing on an existing part of the graph
  335. // Skip that redundancy node
  336. $aRelatedEdges = $bDown ? $oObjectNode->GetOutgoingEdges() : $oObjectNode->GetIncomingEdges();
  337. foreach ($aRelatedEdges as $oRelatedEdge)
  338. {
  339. $oRelatedNode = $bDown ? $oRelatedEdge->GetSinkNode() : $oRelatedEdge->GetSourceNode();
  340. // Recurse (same depth)
  341. $this->AddRelatedObjects($sRelCode, $bDown, $oRelatedNode, $iMaxDepth, $bEnableRedundancy);
  342. }
  343. }
  344. elseif ($oObjectNode->GetProperty('developped', false))
  345. {
  346. // No need to execute the queries again... just dig into the nodes down/up to iMaxDepth
  347. //
  348. $aRelatedEdges = $bDown ? $oObjectNode->GetOutgoingEdges() : $oObjectNode->GetIncomingEdges();
  349. foreach ($aRelatedEdges as $oRelatedEdge)
  350. {
  351. $oRelatedNode = $bDown ? $oRelatedEdge->GetSinkNode() : $oRelatedEdge->GetSourceNode();
  352. // Recurse (decrement the depth)
  353. $this->AddRelatedObjects($sRelCode, $bDown, $oRelatedNode, $iMaxDepth - 1, $bEnableRedundancy);
  354. }
  355. }
  356. else
  357. {
  358. $oObjectNode->SetProperty('developped', true);
  359. $oObject = $oObjectNode->GetProperty('object');
  360. $iPreviousTimeLimit = ini_get('max_execution_time');
  361. $iLoopTimeLimit = MetaModel::GetConfig()->Get('max_execution_time_per_loop');
  362. foreach (MetaModel::EnumRelationQueries(get_class($oObject), $sRelCode, $bDown) as $sDummy => $aQueryInfo)
  363. {
  364. $sQuery = $bDown ? $aQueryInfo['sQueryDown'] : $aQueryInfo['sQueryUp'];
  365. try
  366. {
  367. $oFlt = DBObjectSearch::FromOQL($sQuery);
  368. $oObjSet = new DBObjectSet($oFlt, array(), $oObject->ToArgsForQuery());
  369. $oRelatedObj = $oObjSet->Fetch();
  370. }
  371. catch (Exception $e)
  372. {
  373. $sDirection = $bDown ? 'downstream' : 'upstream';
  374. throw new Exception("Wrong query ($sDirection) for the relation $sRelCode/{$aQueryInfo['sDefinedInClass']}/{$aQueryInfo['sNeighbour']}: ".$e->getMessage());
  375. }
  376. if ($oRelatedObj)
  377. {
  378. do
  379. {
  380. set_time_limit($iLoopTimeLimit);
  381. $sObjectRef = RelationObjectNode::MakeId($oRelatedObj);
  382. $oRelatedNode = $this->GetNode($sObjectRef);
  383. if (is_null($oRelatedNode))
  384. {
  385. $oRelatedNode = new RelationObjectNode($this, $oRelatedObj);
  386. }
  387. $oSourceNode = $bDown ? $oObjectNode : $oRelatedNode;
  388. $oSinkNode = $bDown ? $oRelatedNode : $oObjectNode;
  389. if ($bEnableRedundancy)
  390. {
  391. $oRedundancyNode = $this->ComputeRedundancy($sRelCode, $aQueryInfo, $oSourceNode, $oSinkNode);
  392. }
  393. else
  394. {
  395. $oRedundancyNode = null;
  396. }
  397. if (!$oRedundancyNode)
  398. {
  399. // Direct link (otherwise handled by ComputeRedundancy)
  400. new RelationEdge($this, $oSourceNode, $oSinkNode);
  401. }
  402. // Recurse
  403. $this->AddRelatedObjects($sRelCode, $bDown, $oRelatedNode, $iMaxDepth - 1, $bEnableRedundancy);
  404. }
  405. while ($oRelatedObj = $oObjSet->Fetch());
  406. }
  407. }
  408. set_time_limit($iPreviousTimeLimit);
  409. }
  410. }
  411. }
  412. /**
  413. * Determine if there is a redundancy (or use the existing one) and add the corresponding nodes/edges
  414. */
  415. protected function ComputeRedundancy($sRelCode, $aQueryInfo, $oFromNode, $oToNode)
  416. {
  417. $oRedundancyNode = null;
  418. $oObject = $oToNode->GetProperty('object');
  419. if ($this->IsRedundancyEnabled($sRelCode, $aQueryInfo, $oToNode))
  420. {
  421. $sUniqueNeighbourId = $aQueryInfo['sDefinedInClass'].'-'.$aQueryInfo['sNeighbour'];
  422. $sId = RelationRedundancyNode::MakeId($sRelCode, $sUniqueNeighbourId, $oFromNode->GetProperty('object'), $oToNode->GetProperty('object'));
  423. $oRedundancyNode = $this->GetNode($sId);
  424. if (is_null($oRedundancyNode))
  425. {
  426. // Get the upper neighbours
  427. $sQuery = $aQueryInfo['sQueryUp'];
  428. try
  429. {
  430. $oFlt = DBObjectSearch::FromOQL($sQuery);
  431. $oObjSet = new DBObjectSet($oFlt, array(), $oObject->ToArgsForQuery());
  432. $iCount = $oObjSet->Count();
  433. }
  434. catch (Exception $e)
  435. {
  436. throw new Exception("Wrong query (upstream) for the relation $sRelCode/{$aQueryInfo['sDefinedInClass']}/{$aQueryInfo['sNeighbour']}: ".$e->getMessage());
  437. }
  438. $iMinUp = $this->GetRedundancyMinUp($sRelCode, $aQueryInfo, $oToNode, $iCount);
  439. $fThreshold = max(0, $iCount - $iMinUp);
  440. $oRedundancyNode = new RelationRedundancyNode($this, $sId, $iMinUp, $fThreshold);
  441. new RelationEdge($this, $oRedundancyNode, $oToNode);
  442. while ($oUpperObj = $oObjSet->Fetch())
  443. {
  444. $sObjectRef = RelationObjectNode::MakeId($oUpperObj);
  445. $oUpperNode = $this->GetNode($sObjectRef);
  446. if (is_null($oUpperNode))
  447. {
  448. $oUpperNode = new RelationObjectNode($this, $oUpperObj);
  449. }
  450. new RelationEdge($this, $oUpperNode, $oRedundancyNode);
  451. }
  452. }
  453. }
  454. return $oRedundancyNode;
  455. }
  456. /**
  457. * Helper to determine the redundancy setting on a given relation
  458. */
  459. protected function IsRedundancyEnabled($sRelCode, $aQueryInfo, $oToNode)
  460. {
  461. $bRet = false;
  462. $oToObject = $oToNode->GetProperty('object');
  463. $oRedundancyAttDef = $this->FindRedundancyAttribute($sRelCode, $aQueryInfo, get_class($oToObject));
  464. if ($oRedundancyAttDef)
  465. {
  466. $sValue = $oToObject->Get($oRedundancyAttDef->GetCode());
  467. $bRet = $oRedundancyAttDef->IsEnabled($sValue);
  468. }
  469. return $bRet;
  470. }
  471. /**
  472. * Helper to determine the redundancy threshold, given the count of objects upstream
  473. */
  474. protected function GetRedundancyMinUp($sRelCode, $aQueryInfo, $oToNode, $iUpstreamObjects)
  475. {
  476. $iMinUp = 0;
  477. $oToObject = $oToNode->GetProperty('object');
  478. $oRedundancyAttDef = $this->FindRedundancyAttribute($sRelCode, $aQueryInfo, get_class($oToObject));
  479. if ($oRedundancyAttDef)
  480. {
  481. $sValue = $oToObject->Get($oRedundancyAttDef->GetCode());
  482. if ($oRedundancyAttDef->GetMinUpType($sValue) == 'count')
  483. {
  484. $iMinUp = $oRedundancyAttDef->GetMinUpValue($sValue);
  485. }
  486. else
  487. {
  488. $iMinUp = $iUpstreamObjects * $oRedundancyAttDef->GetMinUpValue($sValue) / 100;
  489. }
  490. }
  491. return $iMinUp;
  492. }
  493. /**
  494. * Helper to search for the redundancy attribute
  495. */
  496. protected function FindRedundancyAttribute($sRelCode, $aQueryInfo, $sClass)
  497. {
  498. $oRet = null;
  499. foreach (MetaModel::ListAttributeDefs($sClass) as $sAttCode => $oAttDef)
  500. {
  501. if ($oAttDef instanceof AttributeRedundancySettings)
  502. {
  503. if ($oAttDef->Get('relation_code') == $sRelCode)
  504. {
  505. if ($oAttDef->Get('from_class') == $aQueryInfo['sFromClass'])
  506. {
  507. if ($oAttDef->Get('neighbour_id') == $aQueryInfo['sNeighbour'])
  508. {
  509. $oRet = $oAttDef;
  510. break;
  511. }
  512. }
  513. }
  514. }
  515. }
  516. return $oRet;
  517. }
  518. /**
  519. * Get the objects referenced by the graph as a hash array: 'class' => array of objects
  520. * @return Ambigous <multitype:multitype: , unknown>
  521. */
  522. public function GetObjectsByClass()
  523. {
  524. $aResults = array();
  525. $oIterator = new RelationTypeIterator($this, 'Node');
  526. foreach($oIterator as $oNode)
  527. {
  528. $oObj = $oNode->GetProperty('object'); // Some nodes (Redundancy Nodes and Group) do not contain an object
  529. if ($oObj)
  530. {
  531. $sObjClass = get_class($oObj);
  532. if (!array_key_exists($sObjClass, $aResults))
  533. {
  534. $aResults[$sObjClass] = array();
  535. }
  536. $aResults[$sObjClass][] = $oObj;
  537. }
  538. }
  539. return $aResults;
  540. }
  541. }