This commit is contained in:
Anne-Cath
2025-09-04 09:28:59 +02:00
parent 8e789d51ce
commit 10e2679887
10 changed files with 140 additions and 320 deletions

View File

@@ -1,21 +1,4 @@
<?php
// Copyright (C) 2015-2024 Combodo SAS
//
// This file is part of iTop.
//
// iTop is free software; you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// iTop is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with iTop. If not, see <http://www.gnu.org/licenses/>
/**
* Data structures (i.e. PHP classes) to build and use relation graphs
@@ -25,169 +8,6 @@
*
*/
require_once(APPROOT.'core/simplegraph.class.inc.php');
/**
* An object Node inside a RelationGraph
*/
class RelationObjectNode extends GraphNode
{
public function __construct($oGraph, $oObject)
{
parent::__construct($oGraph, self::MakeId($oObject));
$this->SetProperty('object', $oObject);
$this->SetProperty('label', get_class($oObject).'::'.$oObject->GetKey().' ('.$oObject->Get('friendlyname').')');
}
/**
* Make a normalized ID to ensure the uniqueness of such a node
*
* @param string $oObject
*
* @return string
*/
public static function MakeId($oObject)
{
return get_class($oObject).'::'.$oObject->GetKey();
}
/**
* Formatting for GraphViz
*
* @param bool $bNoLabel
*
* @return string
*/
public function GetDotAttributes($bNoLabel = false)
{
$sDot = parent::GetDotAttributes();
if ($this->GetProperty('developped', false))
{
$sDot .= ',fontcolor=black';
}
else
{
$sDot .= ',fontcolor=lightgrey';
}
if ($this->GetProperty('source', false) || $this->GetProperty('sink', false))
{
$sDot .= ',shape=rectangle';
}
if ($this->GetProperty('is_reached', false))
{
$sDot .= ',fillcolor="#ffdddd"';
}
else
{
$sDot .= ',fillcolor=white';
}
return $sDot;
}
/**
* Recursively mark the objects nodes as reached, unless we get stopped by a redundancy node or a 'not allowed' node
*
* @param string $sProperty
* @param $value
*/
public function ReachDown($sProperty, $value)
{
if (is_null($this->GetProperty($sProperty)) && ($this->GetProperty($sProperty.'_allowed') !== false))
{
$this->SetProperty($sProperty, $value);
foreach ($this->GetOutgoingEdges() as $oOutgoingEdge)
{
// Recurse
$oOutgoingEdge->GetSinkNode()->ReachDown($sProperty, $value);
}
}
}
}
/**
* An redundancy Node inside a RelationGraph
*/
class RelationRedundancyNode extends GraphNode
{
public function __construct($oGraph, $sId, $iMinUp, $fThreshold)
{
parent::__construct($oGraph, $sId);
$this->SetProperty('min_up', $iMinUp);
$this->SetProperty('threshold', $fThreshold);
}
/**
* Make a normalized ID to ensure the uniqueness of such a node
*
* @param string $sRelCode
* @param string $sNeighbourId
* @param $oSourceObject
* @param \DBObject $oSinkObject
*
* @return string
*/
public static function MakeId($sRelCode, $sNeighbourId, $oSourceObject, $oSinkObject)
{
return 'redundancy-'.$sRelCode.'-'.$sNeighbourId.'-'.get_class($oSinkObject).'::'.$oSinkObject->GetKey();
}
/**
* Formatting for GraphViz
*
* @param bool $bNoLabel
*
* @return string
*/
public function GetDotAttributes($bNoLabel = false)
{
$sDisplayThreshold = sprintf('%.1f', $this->GetProperty('threshold'));
$sDot = 'shape=doublecircle,fillcolor=indianred,fontcolor=papayawhip,label="'.$sDisplayThreshold.'"';
return $sDot;
}
/**
* Recursively mark the objects nodes as reached, unless we get stopped by a redundancy node
*
* @param string $sProperty
* @param $value
*/
public function ReachDown($sProperty, $value)
{
$this->SetProperty($sProperty.'_count', $this->GetProperty($sProperty.'_count', 0) + 1);
if ($this->GetProperty($sProperty.'_count') > $this->GetProperty('threshold'))
{
// Looping... though there should be only ONE SINGLE outgoing edge
foreach ($this->GetOutgoingEdges() as $oOutgoingEdge)
{
// Recurse
$oOutgoingEdge->GetSinkNode()->ReachDown($sProperty, $value);
}
}
}
}
/**
* Helper to name the edges in a unique way
*/
class RelationEdge extends GraphEdge
{
/**
* RelationEdge constructor.
*
* @param \SimpleGraph $oGraph
* @param \GraphNode $oSourceNode
* @param \GraphNode $oSinkNode
* @param bool $bMustBeUnique
*
* @throws \SimpleGraphException
*/
public function __construct(SimpleGraph $oGraph, GraphNode $oSourceNode, GraphNode $oSinkNode, $bMustBeUnique = false)
{
$sId = $oSourceNode->GetId().'-to-'.$oSinkNode->GetId();
parent::__construct($oGraph, $sId, $oSourceNode, $oSinkNode, $bMustBeUnique);
}
}
/**
* A graph representing the relations between objects