1
|
|
|
<?php |
2
|
|
|
namespace izzum\statemachine\utils; |
3
|
|
|
use izzum\statemachine\StateMachine; |
4
|
|
|
use izzum\statemachine\Transition; |
5
|
|
|
use izzum\statemachine\State; |
6
|
|
|
use izzum\statemachine\Exception; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* creates a uml statediagram in plantuml format for a statemachine. |
10
|
|
|
* |
11
|
|
|
* This mainly serves as a simple demo. |
12
|
|
|
* |
13
|
|
|
* More diagrams can be created from the data in a persistence layer. |
14
|
|
|
* for example: |
15
|
|
|
* - activity diagrams: which transitions has en entity actually made (and when) |
16
|
|
|
* - flow diagrams: a combination of the state diagram and the activity diagram |
17
|
|
|
* (show the full state diagram and highlight the states the entity has |
18
|
|
|
* actually gone through) |
19
|
|
|
* - heat maps: a count of every state for every entity. |
20
|
|
|
* |
21
|
|
|
* @link http://www.plantuml.com/plantuml/ for the generation of the diagram |
22
|
|
|
* after the output is created from createStateDiagram |
23
|
|
|
*/ |
24
|
|
|
class PlantUml { |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* create an alias for a state that has a valid plantuml syntax |
28
|
|
|
* |
29
|
|
|
* @param string $original |
30
|
|
|
* @return string |
31
|
|
|
*/ |
32
|
2 |
|
private function plantUmlStateAlias($original) |
33
|
|
|
{ |
34
|
2 |
|
$alias = ucfirst(implode("", array_map('ucfirst', explode("-", $original)))); |
35
|
2 |
|
return $alias; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* get skins for layout |
40
|
|
|
* |
41
|
|
|
* @return string |
42
|
|
|
* @link http://plantuml.sourceforge.net/skinparam.html |
43
|
|
|
* @link http://plantuml.com/classes.html#Skinparam |
44
|
|
|
*/ |
45
|
2 |
|
private function getPlantUmlSkins() |
46
|
|
|
{ |
47
|
|
|
$output = <<<SKINS |
48
|
|
|
skinparam state { |
49
|
|
|
FontColor black |
50
|
|
|
FontSize 11 |
51
|
|
|
FontStyle bold |
52
|
|
|
BackgroundColor orange |
53
|
|
|
BorderColor black |
54
|
|
|
ArrowColor red |
55
|
|
|
StartColor lime |
56
|
|
|
EndColor black |
57
|
|
|
|
58
|
|
|
} |
59
|
|
|
skinparam stateArrow { |
60
|
|
|
FontColor blue |
61
|
|
|
FontSize 9 |
62
|
|
|
FontStyle italic |
63
|
|
|
} |
64
|
|
|
skinparam stateAttribute { |
65
|
|
|
FontColor black |
66
|
|
|
FontSize 9 |
67
|
|
|
FontStyle italic |
68
|
|
|
} |
69
|
2 |
|
SKINS; |
70
|
2 |
|
return $output; |
71
|
|
|
} |
72
|
|
|
|
73
|
2 |
|
private static function escape($string) |
74
|
|
|
{ |
75
|
2 |
|
$string = addslashes($string); |
76
|
2 |
|
return $string; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* creates plantuml state output for a statemachine |
81
|
|
|
* |
82
|
|
|
* @param string $machine |
83
|
|
|
* @return string plant uml code, this can be used to render an image |
84
|
|
|
* @link http://www.plantuml.com/plantuml/ |
85
|
|
|
* @link http://plantuml.sourceforge.net/state.html |
86
|
|
|
* @throws Exception |
87
|
|
|
*/ |
88
|
2 |
|
public function createStateDiagram(StateMachine $machine) |
89
|
|
|
{ |
90
|
2 |
|
$transitions = $machine->getTransitions(); |
91
|
|
|
|
92
|
|
|
// all states are aliased so the plantuml parser can handle the names |
93
|
2 |
|
$aliases = array(); |
94
|
2 |
|
$end_states = array(); |
95
|
2 |
|
$EOL = "\\n\\" . PHP_EOL; /* for multiline stuff in plantuml */ |
96
|
2 |
|
$NEWLINE = PHP_EOL; |
97
|
|
|
|
98
|
|
|
// start with declaration |
99
|
2 |
|
$uml = "@startuml" . PHP_EOL; |
100
|
|
|
|
101
|
|
|
// skins for colors etc. |
102
|
2 |
|
$uml .= $this->getPlantUmlSkins() . PHP_EOL; |
103
|
|
|
|
104
|
|
|
// the order in which transitions are executed |
105
|
2 |
|
$order = array(); |
106
|
|
|
|
107
|
|
|
// create the diagram by drawing all transitions |
108
|
2 |
|
foreach ($transitions as $t) { |
109
|
|
|
// get states and state aliases (plantuml cannot work with certain |
110
|
|
|
// characters, so therefore we create an alias for the state name) |
111
|
2 |
|
$from = $t->getStateFrom(); |
112
|
2 |
|
$from_alias = $this->plantUmlStateAlias($from->getName()); |
113
|
2 |
|
$to = $t->getStateTo(); |
114
|
2 |
|
$to_alias = $this->plantUmlStateAlias($to->getName()); |
115
|
|
|
|
116
|
|
|
// get some names to display |
117
|
2 |
|
$command = $t->getCommandName(); |
118
|
2 |
|
$rule = self::escape($t->getRuleName()); |
119
|
2 |
|
$name_transition = $t->getName(); |
120
|
2 |
|
$description = $t->getDescription() ? ("description: '" . $t->getDescription() . "'" . $EOL) : ''; |
121
|
2 |
|
$event = $t->getEvent() ? ("event: '" . $t->getEvent() . "'" . $EOL) : ''; |
122
|
2 |
|
$f_description = $from->getDescription(); |
123
|
2 |
|
$t_description = $to->getDescription(); |
124
|
2 |
|
$f_exit = $from->getExitCommandName(); |
125
|
2 |
|
$f_entry = $from->getEntryCommandName(); |
126
|
2 |
|
$t_exit = $to->getExitCommandName(); |
127
|
2 |
|
$t_entry = $to->getEntryCommandName(); |
128
|
|
|
|
129
|
|
|
// only write aliases if not done before |
130
|
2 |
|
if (!isset($aliases [$from_alias])) { |
131
|
2 |
|
$uml .= 'state "' . $from . '" as ' . $from_alias . PHP_EOL; |
132
|
2 |
|
$uml .= "$from_alias: description: '" . $f_description . "'" . PHP_EOL; |
133
|
2 |
|
$uml .= "$from_alias: entry / '" . $f_entry . "'" . PHP_EOL; |
134
|
2 |
|
$uml .= "$from_alias: exit / '" . $f_exit . "'" . PHP_EOL; |
135
|
2 |
|
$aliases [$from_alias] = $from_alias; |
136
|
2 |
|
} |
137
|
|
|
|
138
|
|
|
// store order in which transitions will be handled |
139
|
2 |
|
if (!isset($order [$from_alias])) { |
140
|
2 |
|
$order [$from_alias] = 1; |
141
|
2 |
|
} else { |
142
|
2 |
|
$order [$from_alias] = $order [$from_alias] + 1; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
// get 'to' alias |
146
|
2 |
|
if (!isset($aliases [$to_alias])) { |
147
|
2 |
|
$uml .= 'state "' . $to . '" as ' . $to_alias . PHP_EOL; |
148
|
2 |
|
$aliases [$to_alias] = $to_alias; |
149
|
2 |
|
$uml .= "$to_alias: description: '" . $t_description . "'" . PHP_EOL; |
150
|
2 |
|
$uml .= "$to_alias: entry / '" . $t_entry . "'" . PHP_EOL; |
151
|
2 |
|
$uml .= "$to_alias: exit / '" . $t_exit . "'" . PHP_EOL; |
152
|
2 |
|
} |
153
|
|
|
|
154
|
|
|
// write transition information |
155
|
2 |
|
$uml .= $from_alias . ' --> ' . $to_alias; |
156
|
2 |
|
$uml .= " : <b><size:10>$name_transition</size></b>" . $EOL; |
157
|
2 |
|
$uml .= $event; |
158
|
2 |
|
$uml .= "transition order from '$from': <b>" . $order [$from_alias] . "</b>" . $EOL; |
159
|
2 |
|
$uml .= "rule/guard: '$rule'" . $EOL; |
160
|
2 |
|
$uml .= "command/action: '$command'" . $EOL; |
161
|
2 |
|
$uml .= $description; |
162
|
2 |
|
$uml .= PHP_EOL; |
163
|
|
|
|
164
|
|
|
// store possible end states aliases |
165
|
2 |
|
if ($t->getStateFrom()->isFinal()) { |
166
|
|
|
$end_states [$from_alias] = $from_alias; |
167
|
|
|
} |
168
|
2 |
|
if ($t->getStateTo()->isFinal()) { |
169
|
2 |
|
$end_states [$to_alias] = $to_alias; |
170
|
2 |
|
} |
171
|
2 |
|
} |
172
|
|
|
|
173
|
|
|
// only one begin state |
174
|
2 |
|
$initial = $machine->getInitialState(); |
175
|
2 |
|
$initial = $initial->getName(); |
176
|
2 |
|
$initial_alias = $this->plantUmlStateAlias($initial); |
177
|
2 |
|
if (!isset($aliases [$initial_alias])) { |
178
|
|
|
$uml .= 'state "' . $initial . '" as ' . $initial_alias . PHP_EOL; |
179
|
|
|
} |
180
|
2 |
|
$uml .= "[*] --> $initial_alias" . PHP_EOL; |
181
|
|
|
|
182
|
|
|
// note for initial alias with explanation |
183
|
2 |
|
$uml .= "note right of $initial_alias $NEWLINE"; |
184
|
2 |
|
$uml .= "state diagram for machine '" . $machine->getContext()->getMachine() . "'$NEWLINE"; |
185
|
2 |
|
$uml .= "created by izzum plantuml generator $NEWLINE"; |
186
|
2 |
|
$uml .= "@link http://plantuml.sourceforge.net/state.html\"" . $NEWLINE; |
187
|
2 |
|
$uml .= "end note" . $NEWLINE; |
188
|
|
|
|
189
|
|
|
// add end states to diagram |
190
|
2 |
|
foreach ($end_states as $end) { |
191
|
2 |
|
$uml .= "$end --> [*]" . PHP_EOL; |
192
|
2 |
|
} |
193
|
|
|
|
194
|
|
|
// close plantuml |
195
|
2 |
|
$uml .= "@enduml" . PHP_EOL; |
196
|
2 |
|
return $uml; |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|