graphviz.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. // Copyright (C) 2010-2012 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. * Renders a graph of the class' lifecycle as a png (directly in the HTTP response)
  20. *
  21. * @copyright Copyright (C) 2010-2012 Combodo SARL
  22. * @license http://opensource.org/licenses/AGPL-3.0
  23. */
  24. require_once('../approot.inc.php');
  25. require_once(APPROOT.'/application/application.inc.php');
  26. require_once(APPROOT.'/application/itopwebpage.class.inc.php');
  27. require_once(APPROOT.'/application/startup.inc.php');
  28. require_once(APPROOT.'/application/utils.inc.php');
  29. require_once(APPROOT.'/application/loginwebpage.class.inc.php');
  30. LoginWebPage::DoLogin(); // Check user rights and prompt if needed
  31. /**
  32. * Escape a label (string) in a manner suitable for use with graphviz' DOT syntax
  33. * @param string $s The string to escape
  34. * @return string The escaped string
  35. */
  36. function GraphvizEscape($s)
  37. {
  38. $s = str_replace('"', '\\"', $s);
  39. return $s;
  40. }
  41. /**
  42. * Helper to generate a Graphviz code for displaying the life cycle of a class
  43. * @param string $sClass The class to display
  44. * @return string The Graph description in Graphviz/Dot syntax
  45. */
  46. function GraphvizLifecycle($sClass)
  47. {
  48. $sDotFileContent = "";
  49. $sStateAttCode = MetaModel::GetStateAttributeCode($sClass);
  50. if (empty($sStateAttCode))
  51. {
  52. //$oPage->p("no lifecycle for this class");
  53. }
  54. else
  55. {
  56. $aStates = MetaModel::EnumStates($sClass);
  57. $aStimuli = MetaModel::EnumStimuli($sClass);
  58. $sDotFileContent .= "digraph finite_state_machine {
  59. graph [bgcolor = \"transparent\"];
  60. rankdir=LR;
  61. size=\"12,12\"
  62. node [ fontname=Verdana style=filled fillcolor=\"#ffffff\" ];
  63. edge [ fontname=Verdana ];
  64. ";
  65. $aStatesLinks = array();
  66. foreach ($aStates as $sStateCode => $aStateDef)
  67. {
  68. $aStatesLinks[$sStateCode] = array('in' => 0, 'out' => 0);
  69. }
  70. foreach ($aStates as $sStateCode => $aStateDef)
  71. {
  72. $sStateLabel = MetaModel::GetStateLabel($sClass, $sStateCode);
  73. $sStateDescription = MetaModel::GetStateDescription($sClass, $sStateCode);
  74. foreach(MetaModel::EnumTransitions($sClass, $sStateCode) as $sStimulusCode => $aTransitionDef)
  75. {
  76. $aStatesLinks[$sStateCode]['out']++;
  77. $aStatesLinks[$aTransitionDef['target_state']]['in']++;
  78. $sStimulusLabel = $aStimuli[$sStimulusCode]->GetLabel();
  79. $sTargetStateLabel = MetaModel::GetStateLabel($sClass, $aTransitionDef['target_state']);
  80. $sDotFileContent .= "\t$sStateCode -> {$aTransitionDef['target_state']} [ label=\"".GraphvizEscape($sStimulusLabel)."\"];\n";
  81. }
  82. }
  83. foreach($aStates as $sStateCode => $aStateDef)
  84. {
  85. if (($aStatesLinks[$sStateCode]['out'] > 0) || ($aStatesLinks[$sStateCode]['in'] > 0))
  86. {
  87. // Show only reachable states
  88. $sStateLabel = str_replace(' ', '\n', MetaModel::GetStateLabel($sClass, $sStateCode));
  89. if ( ($aStatesLinks[$sStateCode]['in'] == 0) || ($aStatesLinks[$sStateCode]['out'] == 0))
  90. {
  91. // End or Start state, make it look different
  92. $sDotFileContent .= "\t$sStateCode [ shape=doublecircle,label=\"".GraphvizEscape($sStateLabel)."\"];\n";
  93. }
  94. else
  95. {
  96. $sDotFileContent .= "\t$sStateCode [ shape=circle,label=\"".GraphvizEscape($sStateLabel)."\"];\n";
  97. }
  98. }
  99. }
  100. $sDotFileContent .= "}\n";
  101. }
  102. return $sDotFileContent;
  103. }
  104. $sClass = utils::ReadParam('class', '', false, 'class');
  105. $oReflection = new ReflectionClass($sClass);
  106. $sDeclarationFile = $oReflection->getFileName();
  107. $sModuleDir = dirname($sDeclarationFile);
  108. $sImageFilePath = $sModuleDir."/lifecycle/".$sClass.".png";
  109. $sDotExecutable = MetaModel::GetConfig()->Get('graphviz_path');
  110. if (file_exists($sDotExecutable))
  111. {
  112. // create the file with Graphviz
  113. $sImageFilePath = APPROOT."data/lifecycle/".$sClass.".png";
  114. if (!is_dir(APPROOT."data"))
  115. {
  116. @mkdir(APPROOT."data");
  117. }
  118. if (!is_dir(APPROOT."data/lifecycle"))
  119. {
  120. @mkdir(APPROOT."data/lifecycle");
  121. }
  122. $sDotDescription = GraphvizLifecycle($sClass);
  123. $sDotFilePath = APPROOT."data/lifecycle/{$sClass}.dot";
  124. $rFile = @fopen($sDotFilePath, "w");
  125. @fwrite($rFile, $sDotDescription);
  126. @fclose($rFile);
  127. $aOutput = array();
  128. $CommandLine = "$sDotExecutable -v -Tpng < $sDotFilePath -o$sImageFilePath 2>&1";
  129. exec($CommandLine, $aOutput, $iRetCode);
  130. if ($iRetCode != 0)
  131. {
  132. header('Content-type: text/html');
  133. echo "<p><b>Error:</b></p>";
  134. echo "<p>The command: <pre>$CommandLine</pre> returned $iRetCode</p>";
  135. echo "<p>The output of the command is:<pre>\n".implode("\n", $aOutput)."</pre></p>";
  136. echo "<hr>";
  137. echo "<p>Content of the '".basename($sDotFilePath)."' file:<pre>\n$sDotDescription</pre>";
  138. }
  139. else
  140. {
  141. header('Content-type: image/png');
  142. echo file_get_contents($sImageFilePath);
  143. }
  144. @unlink($sDotFilePath);
  145. }
  146. else
  147. {
  148. header('Content-type: image/png');
  149. echo file_get_contents($sImageFilePath);
  150. }
  151. ?>