graphviz.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 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. /**
  28. * Helper to generate a Graphviz code for displaying the life cycle of a class
  29. * @param string $sClass The class to display
  30. * @return string The Graph description in Graphviz/Dot syntax
  31. */
  32. function GraphvizLifecycle($sClass)
  33. {
  34. $sDotFileContent = "";
  35. $sStateAttCode = MetaModel::GetStateAttributeCode($sClass);
  36. if (empty($sStateAttCode))
  37. {
  38. //$oPage->p("no lifecycle for this class");
  39. }
  40. else
  41. {
  42. $aStates = MetaModel::EnumStates($sClass);
  43. $aStimuli = MetaModel::EnumStimuli($sClass);
  44. $sDotFileContent .= "digraph finite_state_machine {
  45. rankdir=LR;
  46. size=\"12,12\"
  47. node [ fontname=Verdana ];
  48. edge [ fontname=Verdana ];
  49. ";
  50. $aStatesLinks = array();
  51. foreach ($aStates as $sStateCode => $aStateDef)
  52. {
  53. $aStatesLinks[$sStateCode] = array('in' => 0, 'out' => 0);
  54. }
  55. foreach ($aStates as $sStateCode => $aStateDef)
  56. {
  57. $sStateLabel = MetaModel::GetStateLabel($sClass, $sStateCode);
  58. $sStateDescription = MetaModel::GetStateDescription($sClass, $sStateCode);
  59. foreach(MetaModel::EnumTransitions($sClass, $sStateCode) as $sStimulusCode => $aTransitionDef)
  60. {
  61. $aStatesLinks[$sStateCode]['out']++;
  62. $aStatesLinks[$aTransitionDef['target_state']]['in']++;
  63. $sStimulusLabel = $aStimuli[$sStimulusCode]->GetLabel();
  64. $sTargetStateLabel = MetaModel::GetStateLabel($sClass, $aTransitionDef['target_state']);
  65. $sDotFileContent .= "\t$sStateCode -> {$aTransitionDef['target_state']} [ label=\"$sStimulusLabel\"];\n";
  66. }
  67. }
  68. foreach($aStates as $sStateCode => $aStateDef)
  69. {
  70. $sStateLabel = str_replace(' ', '\n', MetaModel::GetStateLabel($sClass, $sStateCode));
  71. if ( ($aStatesLinks[$sStateCode]['in'] == 0) || ($aStatesLinks[$sStateCode]['out'] == 0))
  72. {
  73. $sDotFileContent .= "\t$sStateCode [ shape=doublecircle,label=\"$sStateLabel\"];\n";
  74. }
  75. else
  76. {
  77. $sDotFileContent .= "\t$sStateCode [ shape=circle,label=\"$sStateLabel\"];\n";
  78. }
  79. }
  80. $sDotFileContent .= "}\n";
  81. }
  82. return $sDotFileContent;
  83. }
  84. $sClass = utils::ReadParam('class', 'bizIncidentTicket');
  85. $sDir = dirname(__FILE__);
  86. $sImageFilePath = $sDir."/../images/lifecycle/".$sClass.".png";
  87. if (file_exists("/iTop/Graphviz/bin/dot.exe"))
  88. {
  89. // create the file with Graphviz
  90. $sDotDescription = GraphvizLifecycle($sClass);
  91. $sDotFilePath = $sDir."/tmp-lifecycle.dot";
  92. $rFile = fopen($sDotFilePath, "w");
  93. fwrite($rFile, $sDotDescription);
  94. fclose($rFile);
  95. exec("/iTop/Graphviz/bin/dot.exe -Tpng < $sDotFilePath > $sImageFilePath");
  96. }
  97. header('Content-type: image/png');
  98. echo file_get_contents($sImageFilePath);
  99. ?>