relationgraph.class.inc.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. <?php
  2. // Copyright (C) 2015 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 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()
  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
  74. */
  75. public function ReachDown()
  76. {
  77. if (!$this->GetProperty('is_reached', false))
  78. {
  79. $this->SetProperty('is_reached', true);
  80. foreach ($this->GetOutgoingEdges() as $oOutgoingEdge)
  81. {
  82. // Recurse
  83. $oOutgoingEdge->GetSinkNode()->ReachDown();
  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, $fThreshold)
  94. {
  95. parent::__construct($oGraph, $sId);
  96. $this->SetProperty('threshold', $fThreshold);
  97. $this->SetProperty('reach_count', 0);
  98. }
  99. /**
  100. * Make a normalized ID to ensure the uniqueness of such a node
  101. */
  102. public static function MakeId($sRelCode, $sNeighbourId, $oSinkObject)
  103. {
  104. return 'redundancy-'.$sRelCode.'-'.$sNeighbourId.'-'.get_class($oSinkObject).'::'.$oSinkObject->GetKey();
  105. }
  106. /**
  107. * Formatting for GraphViz
  108. */
  109. public function GetDotAttributes()
  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()
  119. {
  120. $this->SetProperty('reach_count', $this->GetProperty('reach_count', 0) + 1);
  121. if ($this->GetProperty('reach_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();
  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)
  138. {
  139. $sId = $oSourceNode->GetId().'-to-'.$oSinkNode->GetId();
  140. parent::__construct($oGraph, $sId, $oSourceNode, $oSinkNode);
  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. public function __construct()
  161. {
  162. parent::__construct();
  163. $this->aSourceNodes = array();
  164. $this->aSinkNodes = array();
  165. $this->aRedundancySettings = array();
  166. }
  167. /**
  168. * Add an object that will be the starting point for building the relations downstream
  169. */
  170. public function AddSourceObject(DBObject $oObject)
  171. {
  172. $oSourceNode = new RelationObjectNode($this, $oObject);
  173. $oSourceNode->SetProperty('source', true);
  174. $this->aSourceNodes[$oSourceNode->GetId()] = $oSourceNode;
  175. }
  176. /**
  177. * Add an object that will be the starting point for building the relations uptream
  178. */
  179. public function AddSinkObject(DBObject$oObject)
  180. {
  181. $oSinkNode = new RelationObjectNode($this, $oObject);
  182. $oSinkNode->SetProperty('sink', true);
  183. $this->aSinkNodes[$oSinkNode->GetId()] = $oSinkNode;
  184. }
  185. /**
  186. * Build the graph downstream, and mark the nodes that can be reached from the source node
  187. */
  188. public function ComputeRelatedObjectsDown($sRelCode, $iMaxDepth, $bEnableRedundancy)
  189. {
  190. //echo "<h5>Sources only...</h5>\n".$this->DumpAsHtmlImage()."<br/>\n";
  191. // Build the graph out of the sources
  192. foreach ($this->aSourceNodes as $oSourceNode)
  193. {
  194. $this->AddRelatedObjects($sRelCode, true, $oSourceNode, $iMaxDepth, $bEnableRedundancy);
  195. //echo "<h5>After processing of {$oSourceNode->GetId()}</h5>\n".$this->DumpAsHtmlImage()."<br/>\n";
  196. }
  197. // Determine the reached nodes
  198. foreach ($this->aSourceNodes as $oSourceNode)
  199. {
  200. $oSourceNode->ReachDown();
  201. //echo "<h5>After reaching from {$oSourceNode->GetId()}</h5>\n".$this->DumpAsHtmlImage()."<br/>\n";
  202. }
  203. }
  204. /**
  205. * Build the graph upstream
  206. */
  207. public function ComputeRelatedObjectsUp($sRelCode, $iMaxDepth, $bEnableRedundancy)
  208. {
  209. //echo "<h5>Sinks only...</h5>\n".$this->DumpAsHtmlImage()."<br/>\n";
  210. // Build the graph out of the sinks
  211. foreach ($this->aSinkNodes as $oSinkNode)
  212. {
  213. $this->AddRelatedObjects($sRelCode, false, $oSinkNode, $iMaxDepth, $bEnableRedundancy);
  214. //echo "<h5>After processing of {$oSinkNode->GetId()}</h5>\n".$this->DumpAsHtmlImage()."<br/>\n";
  215. }
  216. }
  217. /**
  218. * Recursively find related objects, and add them into the graph
  219. *
  220. * @param string $sRelCode The code of the relation to use for the computation
  221. * @param boolean $bDown The direction: downstream or upstream
  222. * @param array $oObjectNode The node from which to compute the neighbours
  223. * @param int $iMaxDepth
  224. * @param boolean $bEnableReduncancy
  225. *
  226. * @return void
  227. */
  228. protected function AddRelatedObjects($sRelCode, $bDown, $oObjectNode, $iMaxDepth, $bEnableRedundancy)
  229. {
  230. if ($iMaxDepth > 0)
  231. {
  232. if ($oObjectNode instanceof RelationRedundancyNode)
  233. {
  234. // Note: this happens when recursing on an existing part of the graph
  235. // Skip that redundancy node
  236. $aRelatedEdges = $bDown ? $oObjectNode->GetOutgoingEdges() : $oObjectNode->GetIncomingEdges();
  237. foreach ($aRelatedEdges as $oRelatedEdge)
  238. {
  239. $oRelatedNode = $bDown ? $oRelatedEdge->GetSinkNode() : $oRelatedEdge->GetSourceNode();
  240. // Recurse (same depth)
  241. $this->AddRelatedObjects($sRelCode, $bDown, $oRelatedNode, $iMaxDepth, $bEnableRedundancy);
  242. }
  243. }
  244. elseif ($oObjectNode->GetProperty('developped', false))
  245. {
  246. // No need to execute the queries again... just dig into the nodes down/up to iMaxDepth
  247. //
  248. $aRelatedEdges = $bDown ? $oObjectNode->GetOutgoingEdges() : $oObjectNode->GetIncomingEdges();
  249. foreach ($aRelatedEdges as $oRelatedEdge)
  250. {
  251. $oRelatedNode = $bDown ? $oRelatedEdge->GetSinkNode() : $oRelatedEdge->GetSourceNode();
  252. // Recurse (decrement the depth)
  253. $this->AddRelatedObjects($sRelCode, $bDown, $oRelatedNode, $iMaxDepth - 1, $bEnableRedundancy);
  254. }
  255. }
  256. else
  257. {
  258. $oObjectNode->SetProperty('developped', true);
  259. $oObject = $oObjectNode->GetProperty('object');
  260. foreach (MetaModel::EnumRelationQueries(get_class($oObject), $sRelCode, $bDown) as $sDummy => $aQueryInfo)
  261. {
  262. $sQuery = $bDown ? $aQueryInfo['sQueryDown'] : $aQueryInfo['sQueryUp'];
  263. try
  264. {
  265. $oFlt = DBObjectSearch::FromOQL($sQuery);
  266. $oObjSet = new DBObjectSet($oFlt, array(), $oObject->ToArgsForQuery());
  267. $oRelatedObj = $oObjSet->Fetch();
  268. }
  269. catch (Exception $e)
  270. {
  271. $sDirection = $bDown ? 'downstream' : 'upstream';
  272. throw new Exception("Wrong query ($sDirection) for the relation $sRelCode/{$aQueryInfo['sDefinedInClass']}/{$aQueryInfo['sNeighbour']}: ".$e->getMessage());
  273. }
  274. if ($oRelatedObj)
  275. {
  276. do
  277. {
  278. $sObjectRef = RelationObjectNode::MakeId($oRelatedObj);
  279. $oRelatedNode = $this->GetNode($sObjectRef);
  280. if (is_null($oRelatedNode))
  281. {
  282. $oRelatedNode = new RelationObjectNode($this, $oRelatedObj);
  283. }
  284. $oSourceNode = $bDown ? $oObjectNode : $oRelatedNode;
  285. $oSinkNode = $bDown ? $oRelatedNode : $oObjectNode;
  286. if ($bEnableRedundancy)
  287. {
  288. $oRedundancyNode = $this->ComputeRedundancy($sRelCode, $aQueryInfo, $oSourceNode, $oSinkNode);
  289. }
  290. else
  291. {
  292. $oRedundancyNode = null;
  293. }
  294. if (!$oRedundancyNode)
  295. {
  296. // Direct link (otherwise handled by ComputeRedundancy)
  297. $oEdge = new RelationEdge($this, $oSourceNode, $oSinkNode);
  298. }
  299. // Recurse
  300. $this->AddRelatedObjects($sRelCode, $bDown, $oRelatedNode, $iMaxDepth - 1, $bEnableRedundancy);
  301. }
  302. while ($oRelatedObj = $oObjSet->Fetch());
  303. }
  304. }
  305. }
  306. }
  307. }
  308. /**
  309. * Determine if there is a redundancy (or use the existing one) and add the corresponding nodes/edges
  310. */
  311. protected function ComputeRedundancy($sRelCode, $aQueryInfo, $oFromNode, $oToNode)
  312. {
  313. $oRedundancyNode = null;
  314. $oObject = $oToNode->GetProperty('object');
  315. if ($this->IsRedundancyEnabled($sRelCode, $aQueryInfo, $oToNode))
  316. {
  317. $sId = RelationRedundancyNode::MakeId($sRelCode, $aQueryInfo['sNeighbour'], $oToNode->GetProperty('object'));
  318. $oRedundancyNode = $this->GetNode($sId);
  319. if (is_null($oRedundancyNode))
  320. {
  321. // Get the upper neighbours
  322. $sQuery = $aQueryInfo['sQueryUp'];
  323. try
  324. {
  325. $oFlt = DBObjectSearch::FromOQL($sQuery);
  326. $oObjSet = new DBObjectSet($oFlt, array(), $oObject->ToArgsForQuery());
  327. $iCount = $oObjSet->Count();
  328. }
  329. catch (Exception $e)
  330. {
  331. throw new Exception("Wrong query (upstream) for the relation $sRelCode/{$aQueryInfo['sDefinedInClass']}/{$aQueryInfo['sNeighbour']}: ".$e->getMessage());
  332. }
  333. $fThreshold = $this->GetRedundancyThreshold($sRelCode, $aQueryInfo, $oToNode, $iCount);
  334. $oRedundancyNode = new RelationRedundancyNode($this, $sId, $fThreshold);
  335. new RelationEdge($this, $oRedundancyNode, $oToNode);
  336. while ($oUpperObj = $oObjSet->Fetch())
  337. {
  338. $sObjectRef = RelationObjectNode::MakeId($oUpperObj);
  339. $oUpperNode = $this->GetNode($sObjectRef);
  340. if (is_null($oUpperNode))
  341. {
  342. $oUpperNode = new RelationObjectNode($this, $oUpperObj);
  343. }
  344. new RelationEdge($this, $oUpperNode, $oRedundancyNode);
  345. }
  346. }
  347. }
  348. return $oRedundancyNode;
  349. }
  350. /**
  351. * Helper to determine the redundancy setting on a given relation
  352. */
  353. protected function IsRedundancyEnabled($sRelCode, $aQueryInfo, $oToNode)
  354. {
  355. $bRet = false;
  356. if (isset($aQueryInfo['sRedundancyEnabledMode']))
  357. {
  358. if ($aQueryInfo['sRedundancyEnabledMode'] == 'fixed')
  359. {
  360. $bRet = $aQueryInfo['bRedundancyEnabledValue'];
  361. }
  362. elseif ($aQueryInfo['sRedundancyEnabledMode'] == 'user')
  363. {
  364. $oUserSettings = $this->FindRedundancyUserSettings($sRelCode, $aQueryInfo, $oToNode);
  365. $bRet = $oUserSettings->Get('enabled');
  366. }
  367. }
  368. return $bRet;
  369. }
  370. /**
  371. * Helper to determine the redundancy threshold, given the count of objects upstream
  372. */
  373. protected function GetRedundancyThreshold($sRelCode, $aQueryInfo, $oToNode, $iUpstreamObjects)
  374. {
  375. $fThreshold = $iUpstreamObjects;
  376. if (isset($aQueryInfo['sRedundancyMinUpMode']))
  377. {
  378. if ($aQueryInfo['sRedundancyMinUpMode'] == 'fixed')
  379. {
  380. if ($aQueryInfo['sRedundancyMinUpType'] == 'count')
  381. {
  382. $fThreshold = $iUpstreamObjects - $aQueryInfo['iRedundancyMinUpValue'];
  383. }
  384. else // 'percent' assumed
  385. {
  386. $fThreshold = $iUpstreamObjects - ($iUpstreamObjects * $aQueryInfo['iRedundancyMinUpValue'] / 100);
  387. }
  388. }
  389. elseif ($aQueryInfo['sRedundancyMinUpMode'] == 'user')
  390. {
  391. $oUserSettings = $this->FindRedundancyUserSettings($sRelCode, $aQueryInfo, $oToNode);
  392. if ($oUserSettings->Get('min_up_type') == 'count')
  393. {
  394. $fThreshold = $iUpstreamObjects - $oUserSettings->Get('min_up_count');
  395. }
  396. else
  397. {
  398. $fThreshold = $iUpstreamObjects - ($iUpstreamObjects * $oUserSettings->Get('min_up_percent') / 100);
  399. }
  400. }
  401. }
  402. return max(0, $fThreshold);
  403. }
  404. /**
  405. * Helper to search for and cache the reduncancy user settings (could be an object NOT recorded in the DB)
  406. */
  407. protected function FindRedundancyUserSettings($sRelCode, $aQueryInfo, $oToNode)
  408. {
  409. $sNeighbourKey = $sRelCode.'/'.$aQueryInfo['sFromClass'].'/'.$aQueryInfo['sNeighbour'];
  410. if (isset($this->aRedundancySettings[$sNeighbourKey][$oToNode->GetId()]))
  411. {
  412. // Cache hit
  413. $oUserSettings = $this->aRedundancySettings[$sNeighbourKey][$oToNode->GetId()];
  414. }
  415. else
  416. {
  417. // Cache miss: build the entry
  418. $oUserSettings = RedundancySettings::GetSettings($sRelCode, $aQueryInfo, $oToNode->GetProperty('object'));
  419. $this->aRedundancySettings[$sNeighbourKey][$oToNode->GetId()] = $oUserSettings;
  420. }
  421. return $oUserSettings;
  422. }
  423. }