graphviz.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. require_once('../application/application.inc.php');
  3. require_once('../application/itopwebpage.class.inc.php');
  4. require_once('../application/startup.inc.php');
  5. /**
  6. * Helper to generate a Graphviz code for displaying the life cycle of a class
  7. * @param string $sClass The class to display
  8. * @return string The Graph description in Graphviz/Dot syntax
  9. */
  10. function GraphvizLifecycle($sClass)
  11. {
  12. $sDotFileContent = "";
  13. $sStateAttCode = MetaModel::GetStateAttributeCode($sClass);
  14. if (empty($sStateAttCode))
  15. {
  16. //$oPage->p("no lifecycle for this class");
  17. }
  18. else
  19. {
  20. $aStates = MetaModel::EnumStates($sClass);
  21. $aStimuli = MetaModel::EnumStimuli($sClass);
  22. $sDotFileContent .= "digraph finite_state_machine {
  23. rankdir=LR;
  24. size=\"12,12\"
  25. node [ fontname=Verdana ];
  26. edge [ fontname=Verdana ];
  27. ";
  28. $aStatesLinks = array();
  29. foreach ($aStates as $sStateCode => $aStateDef)
  30. {
  31. $aStatesLinks[$sStateCode] = array('in' => 0, 'out' => 0);
  32. }
  33. foreach ($aStates as $sStateCode => $aStateDef)
  34. {
  35. $sStateLabel = $aStates[$sStateCode]['label'];
  36. $sStateDescription = $aStates[$sStateCode]['description'];
  37. foreach(MetaModel::EnumTransitions($sClass, $sStateCode) as $sStimulusCode => $aTransitionDef)
  38. {
  39. $aStatesLinks[$sStateCode]['out']++;
  40. $aStatesLinks[$aTransitionDef['target_state']]['in']++;
  41. $sStimulusLabel = $aStimuli[$sStimulusCode]->Get('label');
  42. $sTargetStateLabel = $aStates[$aTransitionDef['target_state']]['label'];
  43. $sDotFileContent .= "\t$sStateCode -> {$aTransitionDef['target_state']} [ label=\"$sStimulusLabel\"];\n";
  44. }
  45. }
  46. foreach($aStates as $sStateCode => $aStateDef)
  47. {
  48. $sStateLabel = str_replace(' ', '\n', $aStates[$sStateCode]['label']);
  49. if ( ($aStatesLinks[$sStateCode]['in'] == 0) || ($aStatesLinks[$sStateCode]['out'] == 0))
  50. {
  51. $sDotFileContent .= "\t$sStateCode [ shape=doublecircle,label=\"$sStateLabel\"];\n";
  52. }
  53. else
  54. {
  55. $sDotFileContent .= "\t$sStateCode [ shape=circle,label=\"$sStateLabel\"];\n";
  56. }
  57. }
  58. $sDotFileContent .= "}\n";
  59. }
  60. return $sDotFileContent;
  61. }
  62. $sClass = utils::ReadParam('class', 'bizIncidentTicket');
  63. $sDir = dirname(__FILE__);
  64. $sImageFilePath = $sDir."/../images/lifecycle/".$sClass.".png";
  65. if (file_exists("/iTop/Graphviz/bin/dot.exe"))
  66. {
  67. // create the file with Graphviz
  68. $sDotDescription = GraphvizLifecycle($sClass);
  69. $sDotFilePath = $sDir."/tmp-lifecycle.dot";
  70. $rFile = fopen($sDotFilePath, "w");
  71. fwrite($rFile, $sDotDescription);
  72. fclose($rFile);
  73. exec("/iTop/Graphviz/bin/dot.exe -Tpng < $sDotFilePath > $sImageFilePath");
  74. }
  75. header('Content-type: image/png');
  76. echo file_get_contents($sImageFilePath);
  77. ?>