graphviz.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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('../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. rankdir=LR;
  60. size=\"12,12\"
  61. node [ fontname=Verdana ];
  62. edge [ fontname=Verdana ];
  63. ";
  64. $aStatesLinks = array();
  65. foreach ($aStates as $sStateCode => $aStateDef)
  66. {
  67. $aStatesLinks[$sStateCode] = array('in' => 0, 'out' => 0);
  68. }
  69. foreach ($aStates as $sStateCode => $aStateDef)
  70. {
  71. $sStateLabel = MetaModel::GetStateLabel($sClass, $sStateCode);
  72. $sStateDescription = MetaModel::GetStateDescription($sClass, $sStateCode);
  73. foreach(MetaModel::EnumTransitions($sClass, $sStateCode) as $sStimulusCode => $aTransitionDef)
  74. {
  75. $aStatesLinks[$sStateCode]['out']++;
  76. $aStatesLinks[$aTransitionDef['target_state']]['in']++;
  77. $sStimulusLabel = $aStimuli[$sStimulusCode]->GetLabel();
  78. $sTargetStateLabel = MetaModel::GetStateLabel($sClass, $aTransitionDef['target_state']);
  79. $sDotFileContent .= "\t$sStateCode -> {$aTransitionDef['target_state']} [ label=\"".GraphvizEscape($sStimulusLabel)."\"];\n";
  80. }
  81. }
  82. foreach($aStates as $sStateCode => $aStateDef)
  83. {
  84. if (($aStatesLinks[$sStateCode]['out'] > 0) || ($aStatesLinks[$sStateCode]['in'] > 0))
  85. {
  86. // Show only reachable states
  87. $sStateLabel = str_replace(' ', '\n', MetaModel::GetStateLabel($sClass, $sStateCode));
  88. if ( ($aStatesLinks[$sStateCode]['in'] == 0) || ($aStatesLinks[$sStateCode]['out'] == 0))
  89. {
  90. // End or Start state, make it look different
  91. $sDotFileContent .= "\t$sStateCode [ shape=doublecircle,label=\"".GraphvizEscape($sStateLabel)."\"];\n";
  92. }
  93. else
  94. {
  95. $sDotFileContent .= "\t$sStateCode [ shape=circle,label=\"".GraphvizEscape($sStateLabel)."\"];\n";
  96. }
  97. }
  98. }
  99. $sDotFileContent .= "}\n";
  100. }
  101. return $sDotFileContent;
  102. }
  103. $sClass = utils::ReadParam('class', 'bizIncidentTicket');
  104. $sDir = dirname(__FILE__);
  105. $sImageFilePath = $sDir."/../images/lifecycle/".$sClass.".png";
  106. $sDotExecutable = MetaModel::GetConfig()->Get('graphviz_path');
  107. if (file_exists($sDotExecutable))
  108. {
  109. // create the file with Graphviz
  110. $sDotDescription = GraphvizLifecycle($sClass);
  111. $sDotFilePath = $sDir."/tmp-lifecycle.dot";
  112. // From now on, fail silently, since the image file may
  113. // already exist and we should not "pollute" the page's output
  114. // with warnings in case we are unable to refresh the image
  115. $rFile = @fopen($sDotFilePath, "w");
  116. @fwrite($rFile, $sDotDescription);
  117. @fclose($rFile);
  118. //echo "<p>Executing command: <tt>$sDotExecutable -Tpng < $sDotFilePath > $sImageFilePath</tt></p>\n";
  119. @exec("$sDotExecutable -Tpng < $sDotFilePath > $sImageFilePath");
  120. }
  121. header('Content-type: image/png');
  122. echo file_get_contents($sImageFilePath);
  123. ?>