mirror of
https://github.com/Combodo/iTop.git
synced 2026-04-30 22:18:46 +02:00
Replacement of the impact Flash based analysis graph by graphviz + Raphael + TCPDF. ALPHA version.
SVN:trunk[3554]
This commit is contained in:
@@ -153,6 +153,24 @@ class GraphNode extends GraphElement
|
||||
$this->aOutgoingEdges[$oEdge->GetId()] = $oEdge;
|
||||
}
|
||||
|
||||
/**
|
||||
* INTERNAL USE ONLY
|
||||
* @param GraphEdge $oEdge
|
||||
*/
|
||||
public function _RemoveIncomingEdge(GraphEdge $oEdge)
|
||||
{
|
||||
unset($this->aIncomingEdges[$oEdge->GetId()]);
|
||||
}
|
||||
|
||||
/**
|
||||
* INTERNAL USE ONLY
|
||||
* @param GraphEdge $oEdge
|
||||
*/
|
||||
public function _RemoveOutgoingEdge(GraphEdge $oEdge)
|
||||
{
|
||||
unset($this->aOutgoingEdges[$oEdge->GetId()]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the list of all incoming edges on the current node
|
||||
* @return Ambigous <multitype:, GraphEdge>
|
||||
@@ -171,6 +189,38 @@ class GraphNode extends GraphElement
|
||||
return $this->aOutgoingEdges;
|
||||
}
|
||||
|
||||
/**
|
||||
* Flood fill the chart with the given value for the specified property
|
||||
* @param string $sPropName The name of the property to set
|
||||
* @param mixed $value Teh value to set in the property
|
||||
* @param bool $bFloodDown Whether or not to fill in the downstream direction
|
||||
* @param bool $bFloodUp Whether or not to fill in the upstream direction
|
||||
*/
|
||||
public function FloodProperty($sPropName, $value, $bFloodDown, $bFloodUp)
|
||||
{
|
||||
if ($this->GetProperty($sPropName, null) == null)
|
||||
{
|
||||
// Property not already set, let's do it
|
||||
$this->SetProperty($sPropName, $value);
|
||||
if ($bFloodDown)
|
||||
{
|
||||
foreach($this->GetOutgoingEdges() as $oEdge)
|
||||
{
|
||||
$oEdge->SetProperty($sPropName, $value);
|
||||
$oEdge->GetSinkNode()->FloodProperty($sPropName, $value, $bFloodDown, $bFloodUp);
|
||||
}
|
||||
}
|
||||
if ($bFloodUp)
|
||||
{
|
||||
foreach($this->GetIncomingEdges() as $oEdge)
|
||||
{
|
||||
$oEdge->SetProperty($sPropName, $value);
|
||||
$oEdge->GetSourceNode()->FloodProperty($sPropName, $value, $bFloodDown, $bFloodUp);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -263,11 +313,30 @@ class SimpleGraph
|
||||
*/
|
||||
public function _AddNode(GraphNode $oNode)
|
||||
{
|
||||
if (array_key_exists($oNode->GetId(), $this->aNodes)) throw new SimpleGraphException('Cannot add node (id='.$oNode->GetId().') to the graph. A node with the same id already exists inthe graph.');
|
||||
if (array_key_exists($oNode->GetId(), $this->aNodes)) throw new SimpleGraphException('Cannot add node (id='.$oNode->GetId().') to the graph. A node with the same id already exists in the graph.');
|
||||
|
||||
$this->aNodes[$oNode->GetId()] = $oNode;
|
||||
}
|
||||
|
||||
/**
|
||||
* INTERNAL USE ONLY
|
||||
* @return Ambigous <multitype:, GraphNode>
|
||||
*/
|
||||
public function _RemoveNode(GraphNode $oNode)
|
||||
{
|
||||
if (!array_key_exists($oNode->GetId(), $this->aNodes)) throw new SimpleGraphException('Cannot remove the node (id='.$oNode->GetId().') from the graph. The node was not found in the graph.');
|
||||
|
||||
foreach($oNode->GetOutgoingEdges() as $oEdge)
|
||||
{
|
||||
$this->_RemoveEdge($oEdge);
|
||||
}
|
||||
foreach($oNode->GetIncomingEdges() as $oEdge)
|
||||
{
|
||||
$this->_RemoveEdge($oEdge);
|
||||
}
|
||||
unset($this->aNodes[$oNode->GetId()]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the node identified by $sId or null if not found
|
||||
* @param string $sId
|
||||
@@ -295,13 +364,28 @@ class SimpleGraph
|
||||
*/
|
||||
public function _AddEdge(GraphEdge $oEdge)
|
||||
{
|
||||
if (array_key_exists($oEdge->GetId(), $this->aEdges)) throw new SimpleGraphException('Cannot add edge (id='.$oEdge->GetId().') to the graph. An edge with the same id already exists inthe graph.');
|
||||
if (array_key_exists($oEdge->GetId(), $this->aEdges)) throw new SimpleGraphException('Cannot add edge (id='.$oEdge->GetId().') to the graph. An edge with the same id already exists in the graph.');
|
||||
|
||||
$this->aEdges[$oEdge->GetId()] = $oEdge;
|
||||
$oEdge->GetSourceNode()->_AddOutgoingEdge($oEdge);
|
||||
$oEdge->GetSinkNode()->_AddIncomingEdge($oEdge);
|
||||
}
|
||||
|
||||
/**
|
||||
* INTERNAL USE ONLY
|
||||
* @param GraphEdge $oEdge
|
||||
* @throws SimpleGraphException
|
||||
*/
|
||||
public function _RemoveEdge(GraphEdge $oEdge)
|
||||
{
|
||||
if (!array_key_exists($oEdge->GetId(), $this->aEdges)) throw new SimpleGraphException('Cannot remove edge (id='.$oEdge->GetId().') from the graph. The edge was not found.');
|
||||
|
||||
$oEdge->GetSourceNode()->_RemoveOutgoingEdge($oEdge);
|
||||
$oEdge->GetSinkNode()->_RemoveIncomingEdge($oEdge);
|
||||
|
||||
unset($this->aEdges[$oEdge->GetId()]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the edge indentified by $sId or null if not found
|
||||
* @param string $sId
|
||||
@@ -333,8 +417,9 @@ class SimpleGraph
|
||||
digraph finite_state_machine {
|
||||
graph [bgcolor = "transparent"];
|
||||
rankdir=LR;
|
||||
size="30,30"
|
||||
node [ fontname=Verdana style=filled fillcolor="#ffffcc" ];
|
||||
size="30,30";
|
||||
fontsize=8.0;
|
||||
node [ fontname=Verdana style=filled fillcolor="#ffffcc" fontsize=8.0 ];
|
||||
edge [ fontname=Verdana ];
|
||||
|
||||
EOF
|
||||
@@ -413,6 +498,62 @@ EOF
|
||||
return $sHtml;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the description of the graph as an embedded PNG image (using a data: url) as
|
||||
* generated by graphviz (requires graphviz to be installed on the machine and the path to
|
||||
* dot/dot.exe to be configured in the iTop configuration file)
|
||||
* Note: the function creates temporary files in APPROOT/data/tmp
|
||||
* @return string
|
||||
*/
|
||||
public function DumpAsXDot()
|
||||
{
|
||||
$sDotExecutable = MetaModel::GetConfig()->Get('graphviz_path');
|
||||
if (file_exists($sDotExecutable))
|
||||
{
|
||||
// create the file with Graphviz
|
||||
if (!is_dir(APPROOT."data"))
|
||||
{
|
||||
@mkdir(APPROOT."data");
|
||||
}
|
||||
if (!is_dir(APPROOT."data/tmp"))
|
||||
{
|
||||
@mkdir(APPROOT."data/tmp");
|
||||
}
|
||||
$sXdotFilePath = tempnam(APPROOT."data/tmp", 'xdot-');
|
||||
$sDotDescription = $this->GetDotDescription();
|
||||
$sDotFilePath = tempnam(APPROOT."data/tmp", 'dot-');
|
||||
|
||||
$rFile = @fopen($sDotFilePath, "w");
|
||||
@fwrite($rFile, $sDotDescription);
|
||||
@fclose($rFile);
|
||||
$aOutput = array();
|
||||
$CommandLine = "\"$sDotExecutable\" -v -Tdot < $sDotFilePath -o$sXdotFilePath 2>&1";
|
||||
|
||||
exec($CommandLine, $aOutput, $iRetCode);
|
||||
if ($iRetCode != 0)
|
||||
{
|
||||
$sHtml = '';
|
||||
$sHtml .= "<p><b>Error:</b></p>";
|
||||
$sHtml .= "<p>The command: <pre>$CommandLine</pre> returned $iRetCode</p>";
|
||||
$sHtml .= "<p>The output of the command is:<pre>\n".implode("\n", $aOutput)."</pre></p>";
|
||||
$sHtml .= "<hr>";
|
||||
$sHtml .= "<p>Content of the '".basename($sDotFilePath)."' file:<pre>\n$sDotDescription</pre>";
|
||||
}
|
||||
else
|
||||
{
|
||||
$sHtml = '<pre>'.file_get_contents($sXdotFilePath).'</pre>';
|
||||
@unlink($sImageFilePath);
|
||||
}
|
||||
@unlink($sXdotFilePath);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception('graphviz not found (executable path: '.$sDotExecutable.')');
|
||||
}
|
||||
return $sHtml;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the description of the graph as some HTML text
|
||||
* @return string
|
||||
@@ -456,6 +597,73 @@ EOF
|
||||
}
|
||||
return $sHtml;
|
||||
}
|
||||
|
||||
/**
|
||||
* Split the graph in a array of non connected subgraphs
|
||||
* @return multitype:SimpleGraph unknown
|
||||
*/
|
||||
public function GetSubgraphs()
|
||||
{
|
||||
$iNbColors = 0;
|
||||
$aResult = array();
|
||||
$oIterator = new RelationTypeIterator($this, 'Node');
|
||||
foreach($oIterator as $oNode)
|
||||
{
|
||||
$iPrevColor = $oNode->GetProperty('color', null);
|
||||
|
||||
if ($iPrevColor == null)
|
||||
{
|
||||
$iNbColors++; // Start a new color
|
||||
$oNode->FloodProperty('color', $iNbColors, true, true);
|
||||
}
|
||||
}
|
||||
if ($iNbColors == 1)
|
||||
{
|
||||
// Everything is connected together, only one subgraph
|
||||
$aResult[] = $this;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Let's reconstruct each separate graph
|
||||
$sClass = get_class($this);
|
||||
for($i = 1; $i <= $iNbColors; $i++)
|
||||
{
|
||||
$aResult[$i] = new $sClass();
|
||||
}
|
||||
|
||||
foreach($oIterator as $oNode)
|
||||
{
|
||||
$iNodeColor = $oNode->GetProperty('color');
|
||||
$aResult[$iNodeColor]->_AddNode($oNode);
|
||||
}
|
||||
|
||||
$oIter2 = new RelationTypeIterator($this, 'Edge');
|
||||
foreach($oIter2 as $oEdge)
|
||||
{
|
||||
$iEdgeColor = $oEdge->GetProperty('color');
|
||||
$aResult[$iEdgeColor]->_AddEdge($oEdge);
|
||||
}
|
||||
}
|
||||
return $aResult;
|
||||
}
|
||||
|
||||
/**
|
||||
* Merge back to subgraphs into one
|
||||
* @param SimpleGraph $oGraph
|
||||
*/
|
||||
public function Merge(SimpleGraph $oGraph)
|
||||
{
|
||||
$oIter1 = new RelationTypeIterator($oGraph, 'Node');
|
||||
foreach($oIter1 as $oNode)
|
||||
{
|
||||
$this->_AddNode($oNode);
|
||||
}
|
||||
$oIter2 = new RelationTypeIterator($oGraph, 'Edge');
|
||||
foreach($oIter2 as $oEdge)
|
||||
{
|
||||
$this->_AddEdge($oEdge);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user