relationgraph.class.inc.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  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. $oSearch->SetArchiveMode(false); // Exclude archived objects anytime
  197. $aAliases = $oSearch->GetSelectedClasses();
  198. if (count($aAliases) < 2 )
  199. {
  200. IssueLog::Error("Invalid context query '$sOQL'. A context query must contain at least two columns.");
  201. throw new Exception("Invalid context query '$sOQL'. A context query must contain at least two columns. Columns: ".implode(', ', $aAliases).'. ');
  202. }
  203. $aAliasNames = array_keys($aAliases);
  204. $sClassAlias = $oSearch->GetClassAlias();
  205. $oCondition = new BinaryExpression(new FieldExpression('id', $aAliasNames[0]), '=', new VariableExpression('id'));
  206. $oSearch->AddConditionExpression($oCondition);
  207. $sClass = $oSearch->GetClass();
  208. if (!array_key_exists($sClass, $this->aContextSearches))
  209. {
  210. $this->aContextSearches[$sClass] = array();
  211. }
  212. $this->aContextSearches[$sClass][] = array('key' => $key, 'search' => $oSearch);
  213. }
  214. /**
  215. * Determines if the given DBObject is part of a 'context'
  216. * @param DBObject $oObj
  217. * @return boolean
  218. */
  219. public function IsPartOfContext(DBObject $oObj, &$aRootCauses)
  220. {
  221. $bRet = false;
  222. $sFinalClass = get_class($oObj);
  223. $aParentClasses = MetaModel::EnumParentClasses($sFinalClass, ENUM_PARENT_CLASSES_ALL);
  224. foreach($aParentClasses as $sClass)
  225. {
  226. if (array_key_exists($sClass, $this->aContextSearches))
  227. {
  228. foreach($this->aContextSearches[$sClass] as $aContextQuery)
  229. {
  230. $aAliases = $aContextQuery['search']->GetSelectedClasses();
  231. $aAliasNames = array_keys($aAliases);
  232. $sRootCauseAlias = $aAliasNames[1]; // 1st column (=0) = object, second column = root cause
  233. $oSet = new DBObjectSet($aContextQuery['search'], array(), array('id' => $oObj->GetKey()));
  234. $oSet->OptimizeColumnLoad(array($aAliasNames[0] => array(), $aAliasNames[1] => array())); // Do not load any column... better do a reload than many joins
  235. while($aRow = $oSet->FetchAssoc())
  236. {
  237. if (!is_null($aRow[$sRootCauseAlias]))
  238. {
  239. if (!array_key_exists($aContextQuery['key'], $aRootCauses))
  240. {
  241. $aRootCauses[$aContextQuery['key']] = array();
  242. }
  243. $aRootCauses[$aContextQuery['key']][] = $aRow[$sRootCauseAlias];
  244. $bRet = true;
  245. }
  246. }
  247. }
  248. }
  249. }
  250. return $bRet;
  251. }
  252. /**
  253. * Build the graph downstream, and mark the nodes that can be reached from the source node
  254. */
  255. public function ComputeRelatedObjectsDown($sRelCode, $iMaxDepth, $bEnableRedundancy, $aUnreachableObjects = array())
  256. {
  257. //echo "<h5>Sources only...</h5>\n".$this->DumpAsHtmlImage()."<br/>\n";
  258. // Build the graph out of the sources
  259. foreach ($this->aSourceNodes as $oSourceNode)
  260. {
  261. $this->AddRelatedObjects($sRelCode, true, $oSourceNode, $iMaxDepth, $bEnableRedundancy);
  262. //echo "<h5>After processing of {$oSourceNode->GetId()}</h5>\n".$this->DumpAsHtmlImage()."<br/>\n";
  263. }
  264. // Mark the unreachable nodes
  265. foreach ($aUnreachableObjects as $oObj)
  266. {
  267. $sNodeId = RelationObjectNode::MakeId($oObj);
  268. $oNode = $this->GetNode($sNodeId);
  269. if($oNode)
  270. {
  271. $oNode->SetProperty('is_reached_allowed', false);
  272. }
  273. }
  274. // Determine the reached nodes
  275. foreach ($this->aSourceNodes as $oSourceNode)
  276. {
  277. $oSourceNode->ReachDown('is_reached', true);
  278. //echo "<h5>After reaching from {$oSourceNode->GetId()}</h5>\n".$this->DumpAsHtmlImage()."<br/>\n";
  279. }
  280. // Mark also the "context" nodes as reached and record the "root causes" for each node
  281. $oIterator = new RelationTypeIterator($this, 'Node');
  282. foreach($oIterator as $oNode)
  283. {
  284. $oObj = $oNode->GetProperty('object');
  285. $aRootCauses = array();
  286. if (!is_null($oObj) && $this->IsPartOfContext($oObj, $aRootCauses))
  287. {
  288. $oNode->SetProperty('context_root_causes', $aRootCauses);
  289. $oNode->ReachDown('is_reached', true);
  290. }
  291. }
  292. }
  293. /**
  294. * Build the graph upstream
  295. */
  296. public function ComputeRelatedObjectsUp($sRelCode, $iMaxDepth, $bEnableRedundancy)
  297. {
  298. //echo "<h5>Sinks only...</h5>\n".$this->DumpAsHtmlImage()."<br/>\n";
  299. // Build the graph out of the sinks
  300. foreach ($this->aSinkNodes as $oSinkNode)
  301. {
  302. $this->AddRelatedObjects($sRelCode, false, $oSinkNode, $iMaxDepth, $bEnableRedundancy);
  303. //echo "<h5>After processing of {$oSinkNode->GetId()}</h5>\n".$this->DumpAsHtmlImage()."<br/>\n";
  304. }
  305. // Mark also the "context" nodes as reached and record the "root causes" for each node
  306. $oIterator = new RelationTypeIterator($this, 'Node');
  307. foreach($oIterator as $oNode)
  308. {
  309. $oObj = $oNode->GetProperty('object');
  310. $aRootCauses = array();
  311. if (!is_null($oObj) && $this->IsPartOfContext($oObj, $aRootCauses))
  312. {
  313. $oNode->SetProperty('context_root_causes', $aRootCauses);
  314. $oNode->ReachDown('is_reached', true);
  315. }
  316. }
  317. }
  318. /**
  319. * Recursively find related objects, and add them into the graph
  320. *
  321. * @param string $sRelCode The code of the relation to use for the computation
  322. * @param boolean $bDown The direction: downstream or upstream
  323. * @param array $oObjectNode The node from which to compute the neighbours
  324. * @param int $iMaxDepth
  325. * @param boolean $bEnableReduncancy
  326. *
  327. * @return void
  328. */
  329. protected function AddRelatedObjects($sRelCode, $bDown, $oObjectNode, $iMaxDepth, $bEnableRedundancy)
  330. {
  331. if ($iMaxDepth > 0)
  332. {
  333. if ($oObjectNode instanceof RelationRedundancyNode)
  334. {
  335. // Note: this happens when recursing on an existing part of the graph
  336. // Skip that redundancy node
  337. $aRelatedEdges = $bDown ? $oObjectNode->GetOutgoingEdges() : $oObjectNode->GetIncomingEdges();
  338. foreach ($aRelatedEdges as $oRelatedEdge)
  339. {
  340. $oRelatedNode = $bDown ? $oRelatedEdge->GetSinkNode() : $oRelatedEdge->GetSourceNode();
  341. // Recurse (same depth)
  342. $this->AddRelatedObjects($sRelCode, $bDown, $oRelatedNode, $iMaxDepth, $bEnableRedundancy);
  343. }
  344. }
  345. elseif ($oObjectNode->GetProperty('developped', false))
  346. {
  347. // No need to execute the queries again... just dig into the nodes down/up to iMaxDepth
  348. //
  349. $aRelatedEdges = $bDown ? $oObjectNode->GetOutgoingEdges() : $oObjectNode->GetIncomingEdges();
  350. foreach ($aRelatedEdges as $oRelatedEdge)
  351. {
  352. $oRelatedNode = $bDown ? $oRelatedEdge->GetSinkNode() : $oRelatedEdge->GetSourceNode();
  353. // Recurse (decrement the depth)
  354. $this->AddRelatedObjects($sRelCode, $bDown, $oRelatedNode, $iMaxDepth - 1, $bEnableRedundancy);
  355. }
  356. }
  357. else
  358. {
  359. $oObjectNode->SetProperty('developped', true);
  360. $oObject = $oObjectNode->GetProperty('object');
  361. $iPreviousTimeLimit = ini_get('max_execution_time');
  362. $iLoopTimeLimit = MetaModel::GetConfig()->Get('max_execution_time_per_loop');
  363. foreach (MetaModel::EnumRelationQueries(get_class($oObject), $sRelCode, $bDown) as $sDummy => $aQueryInfo)
  364. {
  365. $sQuery = $bDown ? $aQueryInfo['sQueryDown'] : $aQueryInfo['sQueryUp'];
  366. try
  367. {
  368. $oFlt = DBObjectSearch::FromOQL($sQuery);
  369. $oFlt->SetArchiveMode(false); // Exclude archived objects anytime
  370. $oObjSet = new DBObjectSet($oFlt, array(), $oObject->ToArgsForQuery());
  371. $oRelatedObj = $oObjSet->Fetch();
  372. }
  373. catch (Exception $e)
  374. {
  375. $sDirection = $bDown ? 'downstream' : 'upstream';
  376. throw new Exception("Wrong query ($sDirection) for the relation $sRelCode/{$aQueryInfo['sDefinedInClass']}/{$aQueryInfo['sNeighbour']}: ".$e->getMessage());
  377. }
  378. if ($oRelatedObj)
  379. {
  380. do
  381. {
  382. set_time_limit($iLoopTimeLimit);
  383. $sObjectRef = RelationObjectNode::MakeId($oRelatedObj);
  384. $oRelatedNode = $this->GetNode($sObjectRef);
  385. if (is_null($oRelatedNode))
  386. {
  387. $oRelatedNode = new RelationObjectNode($this, $oRelatedObj);
  388. }
  389. $oSourceNode = $bDown ? $oObjectNode : $oRelatedNode;
  390. $oSinkNode = $bDown ? $oRelatedNode : $oObjectNode;
  391. if ($bEnableRedundancy)
  392. {
  393. $oRedundancyNode = $this->ComputeRedundancy($sRelCode, $aQueryInfo, $oSourceNode, $oSinkNode);
  394. }
  395. else
  396. {
  397. $oRedundancyNode = null;
  398. }
  399. if (!$oRedundancyNode)
  400. {
  401. // Direct link (otherwise handled by ComputeRedundancy)
  402. new RelationEdge($this, $oSourceNode, $oSinkNode);
  403. }
  404. // Recurse
  405. $this->AddRelatedObjects($sRelCode, $bDown, $oRelatedNode, $iMaxDepth - 1, $bEnableRedundancy);
  406. }
  407. while ($oRelatedObj = $oObjSet->Fetch());
  408. }
  409. }
  410. set_time_limit($iPreviousTimeLimit);
  411. }
  412. }
  413. }
  414. /**
  415. * Determine if there is a redundancy (or use the existing one) and add the corresponding nodes/edges
  416. */
  417. protected function ComputeRedundancy($sRelCode, $aQueryInfo, $oFromNode, $oToNode)
  418. {
  419. $oRedundancyNode = null;
  420. $oObject = $oToNode->GetProperty('object');
  421. if ($this->IsRedundancyEnabled($sRelCode, $aQueryInfo, $oToNode))
  422. {
  423. $sUniqueNeighbourId = $aQueryInfo['sDefinedInClass'].'-'.$aQueryInfo['sNeighbour'];
  424. $sId = RelationRedundancyNode::MakeId($sRelCode, $sUniqueNeighbourId, $oFromNode->GetProperty('object'), $oToNode->GetProperty('object'));
  425. $oRedundancyNode = $this->GetNode($sId);
  426. if (is_null($oRedundancyNode))
  427. {
  428. // Get the upper neighbours
  429. $sQuery = $aQueryInfo['sQueryUp'];
  430. try
  431. {
  432. $oFlt = DBObjectSearch::FromOQL($sQuery);
  433. $oFlt->SetArchiveMode(false); // Exclude archived objects anytime
  434. $oObjSet = new DBObjectSet($oFlt, array(), $oObject->ToArgsForQuery());
  435. $iCount = $oObjSet->Count();
  436. }
  437. catch (Exception $e)
  438. {
  439. throw new Exception("Wrong query (upstream) for the relation $sRelCode/{$aQueryInfo['sDefinedInClass']}/{$aQueryInfo['sNeighbour']}: ".$e->getMessage());
  440. }
  441. $iMinUp = $this->GetRedundancyMinUp($sRelCode, $aQueryInfo, $oToNode, $iCount);
  442. $fThreshold = max(0, $iCount - $iMinUp);
  443. $oRedundancyNode = new RelationRedundancyNode($this, $sId, $iMinUp, $fThreshold);
  444. new RelationEdge($this, $oRedundancyNode, $oToNode);
  445. while ($oUpperObj = $oObjSet->Fetch())
  446. {
  447. $sObjectRef = RelationObjectNode::MakeId($oUpperObj);
  448. $oUpperNode = $this->GetNode($sObjectRef);
  449. if (is_null($oUpperNode))
  450. {
  451. $oUpperNode = new RelationObjectNode($this, $oUpperObj);
  452. }
  453. new RelationEdge($this, $oUpperNode, $oRedundancyNode);
  454. }
  455. }
  456. }
  457. return $oRedundancyNode;
  458. }
  459. /**
  460. * Helper to determine the redundancy setting on a given relation
  461. */
  462. protected function IsRedundancyEnabled($sRelCode, $aQueryInfo, $oToNode)
  463. {
  464. $bRet = false;
  465. $oToObject = $oToNode->GetProperty('object');
  466. $oRedundancyAttDef = $this->FindRedundancyAttribute($sRelCode, $aQueryInfo, get_class($oToObject));
  467. if ($oRedundancyAttDef)
  468. {
  469. $sValue = $oToObject->Get($oRedundancyAttDef->GetCode());
  470. $bRet = $oRedundancyAttDef->IsEnabled($sValue);
  471. }
  472. return $bRet;
  473. }
  474. /**
  475. * Helper to determine the redundancy threshold, given the count of objects upstream
  476. */
  477. protected function GetRedundancyMinUp($sRelCode, $aQueryInfo, $oToNode, $iUpstreamObjects)
  478. {
  479. $iMinUp = 0;
  480. $oToObject = $oToNode->GetProperty('object');
  481. $oRedundancyAttDef = $this->FindRedundancyAttribute($sRelCode, $aQueryInfo, get_class($oToObject));
  482. if ($oRedundancyAttDef)
  483. {
  484. $sValue = $oToObject->Get($oRedundancyAttDef->GetCode());
  485. if ($oRedundancyAttDef->GetMinUpType($sValue) == 'count')
  486. {
  487. $iMinUp = $oRedundancyAttDef->GetMinUpValue($sValue);
  488. }
  489. else
  490. {
  491. $iMinUp = $iUpstreamObjects * $oRedundancyAttDef->GetMinUpValue($sValue) / 100;
  492. }
  493. }
  494. return $iMinUp;
  495. }
  496. /**
  497. * Helper to search for the redundancy attribute
  498. */
  499. protected function FindRedundancyAttribute($sRelCode, $aQueryInfo, $sClass)
  500. {
  501. $oRet = null;
  502. foreach (MetaModel::ListAttributeDefs($sClass) as $sAttCode => $oAttDef)
  503. {
  504. if ($oAttDef instanceof AttributeRedundancySettings)
  505. {
  506. if ($oAttDef->Get('relation_code') == $sRelCode)
  507. {
  508. if ($oAttDef->Get('from_class') == $aQueryInfo['sFromClass'])
  509. {
  510. if ($oAttDef->Get('neighbour_id') == $aQueryInfo['sNeighbour'])
  511. {
  512. $oRet = $oAttDef;
  513. break;
  514. }
  515. }
  516. }
  517. }
  518. }
  519. return $oRet;
  520. }
  521. /**
  522. * Get the objects referenced by the graph as a hash array: 'class' => array of objects
  523. * @return Ambigous <multitype:multitype: , unknown>
  524. */
  525. public function GetObjectsByClass()
  526. {
  527. $aResults = array();
  528. $oIterator = new RelationTypeIterator($this, 'Node');
  529. foreach($oIterator as $oNode)
  530. {
  531. $oObj = $oNode->GetProperty('object'); // Some nodes (Redundancy Nodes and Group) do not contain an object
  532. if ($oObj)
  533. {
  534. $sObjClass = get_class($oObj);
  535. if (!array_key_exists($sObjClass, $aResults))
  536. {
  537. $aResults[$sObjClass] = array();
  538. }
  539. $aResults[$sObjClass][] = $oObj;
  540. }
  541. }
  542. return $aResults;
  543. }
  544. }