graphviz.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. // Copyright (C) 2010 Combodo SARL
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; version 3 of the License.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. /**
  17. * Renders a graph of the class' lifecycle as a png (directly in the HTTP response)
  18. *
  19. * @author Erwan Taloc <erwan.taloc@combodo.com>
  20. * @author Romain Quetiez <romain.quetiez@combodo.com>
  21. * @author Denis Flaven <denis.flaven@combodo.com>
  22. * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
  23. */
  24. require_once('../application/application.inc.php');
  25. require_once('../application/itopwebpage.class.inc.php');
  26. require_once('../application/startup.inc.php');
  27. require_once('../application/utils.inc.php');
  28. require_once('../application/loginwebpage.class.inc.php');
  29. LoginWebPage::DoLogin(); // Check user rights and prompt if needed
  30. /**
  31. * Escape a label (string) in a manner suitable for use with graphviz' DOT syntax
  32. * @param string $s The string to escape
  33. * @return string The escaped string
  34. */
  35. function GraphvizEscape($s)
  36. {
  37. $s = str_replace('"', '\\"', $s);
  38. return $s;
  39. }
  40. /**
  41. * Helper to generate a Graphviz code for displaying the life cycle of a class
  42. * @param string $sClass The class to display
  43. * @return string The Graph description in Graphviz/Dot syntax
  44. */
  45. function GraphvizLifecycle($sClass)
  46. {
  47. $sDotFileContent = "";
  48. $sStateAttCode = MetaModel::GetStateAttributeCode($sClass);
  49. if (empty($sStateAttCode))
  50. {
  51. //$oPage->p("no lifecycle for this class");
  52. }
  53. else
  54. {
  55. $aStates = MetaModel::EnumStates($sClass);
  56. $aStimuli = MetaModel::EnumStimuli($sClass);
  57. $sDotFileContent .= "digraph finite_state_machine {
  58. rankdir=LR;
  59. size=\"12,12\"
  60. node [ fontname=Verdana ];
  61. edge [ fontname=Verdana ];
  62. ";
  63. $aStatesLinks = array();
  64. foreach ($aStates as $sStateCode => $aStateDef)
  65. {
  66. $aStatesLinks[$sStateCode] = array('in' => 0, 'out' => 0);
  67. }
  68. foreach ($aStates as $sStateCode => $aStateDef)
  69. {
  70. $sStateLabel = MetaModel::GetStateLabel($sClass, $sStateCode);
  71. $sStateDescription = MetaModel::GetStateDescription($sClass, $sStateCode);
  72. foreach(MetaModel::EnumTransitions($sClass, $sStateCode) as $sStimulusCode => $aTransitionDef)
  73. {
  74. $aStatesLinks[$sStateCode]['out']++;
  75. $aStatesLinks[$aTransitionDef['target_state']]['in']++;
  76. $sStimulusLabel = $aStimuli[$sStimulusCode]->GetLabel();
  77. $sTargetStateLabel = MetaModel::GetStateLabel($sClass, $aTransitionDef['target_state']);
  78. $sDotFileContent .= "\t$sStateCode -> {$aTransitionDef['target_state']} [ label=\"".GraphvizEscape($sStimulusLabel)."\"];\n";
  79. }
  80. }
  81. foreach($aStates as $sStateCode => $aStateDef)
  82. {
  83. if (($aStatesLinks[$sStateCode]['out'] > 0) || ($aStatesLinks[$sStateCode]['in'] > 0))
  84. {
  85. // Show only reachable states
  86. $sStateLabel = str_replace(' ', '\n', MetaModel::GetStateLabel($sClass, $sStateCode));
  87. if ( ($aStatesLinks[$sStateCode]['in'] == 0) || ($aStatesLinks[$sStateCode]['out'] == 0))
  88. {
  89. // End or Start state, make it look different
  90. $sDotFileContent .= "\t$sStateCode [ shape=doublecircle,label=\"".GraphvizEscape($sStateLabel)."\"];\n";
  91. }
  92. else
  93. {
  94. $sDotFileContent .= "\t$sStateCode [ shape=circle,label=\"".GraphvizEscape($sStateLabel)."\"];\n";
  95. }
  96. }
  97. }
  98. $sDotFileContent .= "}\n";
  99. }
  100. return $sDotFileContent;
  101. }
  102. $sClass = utils::ReadParam('class', 'bizIncidentTicket');
  103. $sDir = dirname(__FILE__);
  104. $sImageFilePath = $sDir."/../images/lifecycle/".$sClass.".png";
  105. $sDotExecutable = utils::GetConfig()->Get('graphviz_path');
  106. if (file_exists($sDotExecutable))
  107. {
  108. // create the file with Graphviz
  109. $sDotDescription = GraphvizLifecycle($sClass);
  110. $sDotFilePath = $sDir."/tmp-lifecycle.dot";
  111. // From now on, fail silently, since the image file may
  112. // already exist and we should not "pollute" the page's output
  113. // with warnings in case we are unable to refresh the image
  114. $rFile = @fopen($sDotFilePath, "w");
  115. @fwrite($rFile, $sDotDescription);
  116. @fclose($rFile);
  117. //echo "<p>Executing command: <tt>$sDotExecutable -Tpng < $sDotFilePath > $sImageFilePath</tt></p>\n";
  118. @exec("$sDotExecutable -Tpng < $sDotFilePath > $sImageFilePath");
  119. }
  120. header('Content-type: image/png');
  121. echo file_get_contents($sImageFilePath);
  122. ?>