-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Workflow] Made GraphvizDumper supports StateMachine #20497
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@@ -126,21 +126,20 @@ private function addTransitions(array $transitions) | |||
private function findEdges(Definition $definition) | |||
{ | |||
$dotEdges = array(); | |||
$foundEdge = function ($from, $to, $direction) use (&$dotEdges) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$foundEdge
is not really a good name for what the function is doing imo.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$addEdge
?
Can we add a test to prevent regressions? |
This indeed needs tests to be accepted Status: needs work |
6a4abe1
to
d40e38d
Compare
Fixed now, the problem came from the way transitions are configured for state machines. Status: needs review |
@@ -84,7 +84,7 @@ private function findTransitions(Definition $definition) | |||
$transitions = array(); | |||
|
|||
foreach ($definition->getTransitions() as $transition) { | |||
$transitions[] = array( | |||
$transitions[$transition->getName()] = array( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks wrong to me. Transition names are not unique in the definition (the only requirement is that transition starting from a given node have unique names)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@stof This is what this key is about, removing duplicated transition names, since they generate useless code in the dot
file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If they have the same name, they have different from/to, so they should both be rendered on the graph separately (otherwise, the graph will lie).
Btw, for a finite state machine, we could change the graph to render transitions as edges directly (optionally, as rendering the graph in workflow mode is still useful), as they are known to have a single from and a single to
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If they have the same name, they have different from/to, so they should both be rendered on the graph separately (otherwise, the graph will lie).
That not true for the way state machines have been implemented. It splits the transition that has many tos in many transitions with the same name.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@HeahDude But this class is also able to dump Workflow graph.
'to' => $transition->getName(), | ||
'direction' => 'from', | ||
); | ||
$getEdge($from, $transition->getName(), 'from'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getEdge
is a weird name, as it does not get anything. It adds something
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok l'll change it to addName
, it seems you all agree with that :)
@@ -126,21 +126,20 @@ private function addTransitions(array $transitions) | |||
private function findEdges(Definition $definition) | |||
{ | |||
$dotEdges = array(); | |||
$getEdge = function ($from, $to, $direction) use (&$dotEdges) { | |||
$dotEdges[$from.$to.$direction] = array( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This kind of "hash" could collide.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They should, this is the point of it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the thing is that your dumping of the StateMachine graph does not represent the workflow being defined by the state machine, as you represent a single transition with multiple input and output instead of several transitions (the fact that the DI config of the StateMachine allows to define these transitions in a shorted way by grouping them does not mean they are the same transition in the StateMachine)
Your dumping is lying about the actual workflow being run.
The bug in the GraphvizDumper currently is that it uses transition names to identify them in the dot file, while they are not unique identifiers anymore (they were at the time the dumper was written).
Btw, this is why I said we should have another dumper being tailored only at state machines, and dumping them into a graph being more readable. This graph would represent transitions as edges on the graph (which is not possible in generic workflows, as transitions can have multiple input and output). But I would make this another dumper, or an option in this dumper, because dumping the StateMachine as a generic workflow graph can be useful too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright! Thank you again for your explanation, I'll work on that soon!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Btw, this is why I said we should have another dumper being tailored only at state machines, and dumping them into a graph being more readable
👍
Awesome thanks! Closing then. |
This PR was merged into the 3.3-dev branch. Discussion ---------- [Workflow] Fixed graphviz dumper for state machine | Q | A | ------------- | --- | Branch? | master | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #20497 | License | MIT | Doc PR | ~ --- Before:  After:  --- Script: ```php <?php require __DIR__.'/vendor/autoload.php'; use Symfony\Component\Workflow\Definition; use Symfony\Component\Workflow\Dumper\StateMachineGraphvizDumper; use Symfony\Component\Workflow\Marking; use Symfony\Component\Workflow\StateMachine; use Symfony\Component\Workflow\Transition; $places = array('a', 'b', 'c', 'd'); $transitions[] = new Transition('t1', 'a', 'b'); $transitions[] = new Transition('t1', 'd', 'b'); $transitions[] = new Transition('t2', 'b', 'c'); $transitions[] = new Transition('t3', 'b', 'd'); $definition = new Definition($places, $transitions); $marking = new Marking(['d' => 1]); echo (new StateMachineGraphvizDumper())->dump($definition, $marking); ``` Commits ------- 1e92e02 [Workflow] Fixed graphviz dumper for state machine
This PR was submitted for the master branch but it was merged into the 3.2 branch instead (closes #20583). Discussion ---------- [Workflow] Fixed graphviz dumper for state machine | Q | A | ------------- | --- | Branch? | master | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #20497 | License | MIT | Doc PR | ~ --- Before:  After:  --- Script: ```php <?php require __DIR__.'/vendor/autoload.php'; use Symfony\Component\Workflow\Definition; use Symfony\Component\Workflow\Dumper\StateMachineGraphvizDumper; use Symfony\Component\Workflow\Marking; use Symfony\Component\Workflow\StateMachine; use Symfony\Component\Workflow\Transition; $places = array('a', 'b', 'c', 'd'); $transitions[] = new Transition('t1', 'a', 'b'); $transitions[] = new Transition('t1', 'd', 'b'); $transitions[] = new Transition('t2', 'b', 'c'); $transitions[] = new Transition('t3', 'b', 'd'); $definition = new Definition($places, $transitions); $marking = new Marking(['d' => 1]); echo (new StateMachineGraphvizDumper())->dump($definition, $marking); ``` Commits ------- 330c069 [Workflow] Fixed graphviz dumper for state machine
Before:

After:
