graphviz.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. * Helper to generate a Graphviz code for displaying the life cycle of a class
  32. * @param string $sClass The class to display
  33. * @return string The Graph description in Graphviz/Dot syntax
  34. */
  35. function GraphvizLifecycle($sClass)
  36. {
  37. $sDotFileContent = "";
  38. $sStateAttCode = MetaModel::GetStateAttributeCode($sClass);
  39. if (empty($sStateAttCode))
  40. {
  41. //$oPage->p("no lifecycle for this class");
  42. }
  43. else
  44. {
  45. $aStates = MetaModel::EnumStates($sClass);
  46. $aStimuli = MetaModel::EnumStimuli($sClass);
  47. $sDotFileContent .= "digraph finite_state_machine {
  48. rankdir=LR;
  49. size=\"12,12\"
  50. node [ fontname=Verdana ];
  51. edge [ fontname=Verdana ];
  52. ";
  53. $aStatesLinks = array();
  54. foreach ($aStates as $sStateCode => $aStateDef)
  55. {
  56. $aStatesLinks[$sStateCode] = array('in' => 0, 'out' => 0);
  57. }
  58. foreach ($aStates as $sStateCode => $aStateDef)
  59. {
  60. $sStateLabel = MetaModel::GetStateLabel($sClass, $sStateCode);
  61. $sStateDescription = MetaModel::GetStateDescription($sClass, $sStateCode);
  62. foreach(MetaModel::EnumTransitions($sClass, $sStateCode) as $sStimulusCode => $aTransitionDef)
  63. {
  64. $aStatesLinks[$sStateCode]['out']++;
  65. $aStatesLinks[$aTransitionDef['target_state']]['in']++;
  66. $sStimulusLabel = $aStimuli[$sStimulusCode]->GetLabel();
  67. $sTargetStateLabel = MetaModel::GetStateLabel($sClass, $aTransitionDef['target_state']);
  68. $sDotFileContent .= "\t$sStateCode -> {$aTransitionDef['target_state']} [ label=\"$sStimulusLabel\"];\n";
  69. }
  70. }
  71. foreach($aStates as $sStateCode => $aStateDef)
  72. {
  73. if (($aStatesLinks[$sStateCode]['out'] > 0) || ($aStatesLinks[$sStateCode]['in'] > 0))
  74. {
  75. // Show only reachable states
  76. $sStateLabel = str_replace(' ', '\n', MetaModel::GetStateLabel($sClass, $sStateCode));
  77. if ( ($aStatesLinks[$sStateCode]['in'] == 0) || ($aStatesLinks[$sStateCode]['out'] == 0))
  78. {
  79. // End or Start state, make it look different
  80. $sDotFileContent .= "\t$sStateCode [ shape=doublecircle,label=\"$sStateLabel\"];\n";
  81. }
  82. else
  83. {
  84. $sDotFileContent .= "\t$sStateCode [ shape=circle,label=\"$sStateLabel\"];\n";
  85. }
  86. }
  87. }
  88. $sDotFileContent .= "}\n";
  89. }
  90. return $sDotFileContent;
  91. }
  92. $sClass = utils::ReadParam('class', 'bizIncidentTicket');
  93. $sDir = dirname(__FILE__);
  94. $sImageFilePath = $sDir."/../images/lifecycle/".$sClass.".png";
  95. $sDotExecutable = utils::GetConfig()->Get('graphviz_path');
  96. if (file_exists($sDotExecutable))
  97. {
  98. // create the file with Graphviz
  99. $sDotDescription = GraphvizLifecycle($sClass);
  100. $sDotFilePath = $sDir."/tmp-lifecycle.dot";
  101. // From now on, fail silently, since the image file may
  102. // already exist and we should not "pollute" the page's output
  103. // with warnings in case we are unable to refresh the image
  104. $rFile = @fopen($sDotFilePath, "w");
  105. @fwrite($rFile, $sDotDescription);
  106. @fclose($rFile);
  107. //echo "<p>Executing command: <tt>$sDotExecutable -Tpng < $sDotFilePath > $sImageFilePath</tt></p>\n";
  108. @exec("$sDotExecutable -Tpng < $sDotFilePath > $sImageFilePath");
  109. }
  110. header('Content-type: image/png');
  111. echo file_get_contents($sImageFilePath);
  112. ?>