Skip to content

[Workflow] Add support for weighted transitions #60201

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

Open
wants to merge 1 commit into
base: 7.4
Choose a base branch
from

Conversation

lyrixx
Copy link
Member

@lyrixx lyrixx commented Apr 11, 2025

Q A
Branch? 7.3
Bug fix? no
New feature? yes
Deprecations? yes
Issues Fix #60107
License MIT

Allow to handle complex workflow like:

make_table:
    transitions:
        start:
            from: init
            to:
                -   place: prepare_leg
                    weight: 4
                -   place: prepare_top
                    weight: 1
                -   place: stopwatch_running
                    weight: 1
        build_leg:
            from: prepare_leg
            to: leg_created
        build_top:
            from: prepare_top
            to: top_created
        join:
            from:
                - place: leg_created
                  weight: 4
                - top_created
                - stopwatch_running
            to: finished
$definition = new Definition(
    [],
    [
        new Transition('start', 'init', [new Arc('prepare_leg', 4), 'prepare_top', 'stopwatch_running']),
        new Transition('build_leg', 'prepare_leg', 'leg_created'),
        new Transition('build_top', 'prepare_top', 'top_created'),
        new Transition('join', [new Arc('leg_created', 4), 'top_created', 'stopwatch_running'], 'finished'),
    ]
);

$subject = new Subject();
$workflow = new Workflow($definition);

$workflow->apply($subject, 'start');

$workflow->apply($subject, 'build_leg');
$workflow->apply($subject, 'build_top');
$workflow->apply($subject, 'build_leg');
$workflow->apply($subject, 'build_leg');
$workflow->apply($subject, 'build_leg');
$workflow->apply($subject, 'join');

Another example, based on https://demo-symfony-workflow.cleverapps.io/articles/show/1
workflow.webm

@lyrixx
Copy link
Member Author

lyrixx commented Apr 12, 2025

@bkosun Thanks for the review. I addressed your comments

throw new InvalidDefinitionException(\sprintf('A transition in StateMachine can only have one input. But the transition "%s" in StateMachine "%s" has %d inputs.', $transition->getName(), $name, \count($fromArcs)));
}
foreach ($transition->getToArcs() as $arc) {
if (1 < $arc->weight) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the ex message, should you compare with !== and not < ?

@@ -83,6 +83,11 @@ public function has(string $place): bool
return isset($this->places[$place]);
}

public function getTokenCount(string $place): int
{
return $this->places[$place] ?? 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps add php doc for the « places » array shape ?

<xsd:complexType name="arc">
<xsd:simpleContent>
<xsd:extension base="xsd:string">
<xsd:attribute name="weight" type="xsd:integer" use="optional" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use="optional" is the default and we generally don't put it explicitly in our schemas

return new Arc($arc, 1);
}

throw new \InvalidArgumentException(sprintf('The type of arc is invalid. Expected string or Arc, got "%s".', get_debug_type($arc)));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest using TypeError so that we get the same error type than if we use a parameter type Arc|string in the future

@fabpot fabpot modified the milestones: 7.3, 7.4 May 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Workflow] Unexpected marking with a complex workflow after transition
6 participants