relationgraph.class.inc.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. public static function MakeId($oObject)
  38. {
  39. return get_class($oObject).'::'.$oObject->GetKey();
  40. }
  41. }
  42. /**
  43. * An redundancy Node inside a RelationGraph
  44. */
  45. class RelationRedundancyNode extends GraphNode
  46. {
  47. public function GetDotAttributes()
  48. {
  49. $sDot = 'shape=point,label="'.$this->GetProperty('threshold').'"';
  50. return $sDot;
  51. // shape=point
  52. }
  53. }
  54. class RelationGraph extends SimpleGraph
  55. {
  56. /**
  57. * Recursively find related objects, and add them into the graph
  58. *
  59. * @param string $sRelCode The code of the relation to use for the computation
  60. * @param array $oObjectNode The node from which to compute the neighbours
  61. * @param int $iMaxDepth
  62. * @param boolean $bEnableReduncancy
  63. *
  64. * @return void
  65. */
  66. public function AddRelatedObjectsDown($sRelCode, $oObjectNode, $iMaxDepth, $bEnableRedundancy)
  67. {
  68. if ($iMaxDepth > 0)
  69. {
  70. $oObject = $oObjectNode->GetProperty('object');
  71. foreach (MetaModel::EnumRelationQueries(get_class($oObject), $sRelCode, true) as $sDummy => $aQueryInfo)
  72. {
  73. $sQuery = $aQueryInfo['sQueryDown'];
  74. try
  75. {
  76. $oFlt = DBObjectSearch::FromOQL($sQuery);
  77. $oObjSet = new DBObjectSet($oFlt, array(), $oObject->ToArgsForQuery());
  78. $oRelatedObj = $oObjSet->Fetch();
  79. }
  80. catch (Exception $e)
  81. {
  82. throw new Exception("Wrong query (downstream) for the relation $sRelCode/{$aQueryInfo['sDefinedInClass']}/{$aQueryInfo['sNeighbour']}: ".$e->getMessage());
  83. }
  84. if ($oRelatedObj)
  85. {
  86. do
  87. {
  88. $sObjectRef = RelationObjectNode::MakeId($oRelatedObj);
  89. $oRelatedNode = $this->GetNode($sObjectRef);
  90. if (is_null($oRelatedNode))
  91. {
  92. $oRelatedNode = new RelationObjectNode($this, $oRelatedObj);
  93. // Recurse
  94. $this->AddRelatedObjectsDown($sRelCode, $oRelatedNode, $iMaxDepth - 1, $bEnableRedundancy);
  95. }
  96. $oEdge = new GraphEdge($this, $oObjectNode->GetId().' to '.$oRelatedNode->GetId(), $oObjectNode, $oRelatedNode);
  97. }
  98. while ($oRelatedObj = $oObjSet->Fetch());
  99. }
  100. }
  101. }
  102. }
  103. }