Skip to content

Commit

Permalink
Merge pull request #627 from tienvx/fix-expression-syntax-error
Browse files Browse the repository at this point in the history
Fix expression syntax error
  • Loading branch information
tienvx committed Jul 13, 2022
2 parents 2f3b5e4 + c73b6eb commit b3175d4
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 12 deletions.
2 changes: 2 additions & 0 deletions src/Resources/config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use SingleColorPetrinet\Service\GuardedTransitionService;
use SingleColorPetrinet\Service\GuardedTransitionServiceInterface;
use Symfony\Component\Messenger\MessageBusInterface;
use Tienvx\AssignmentsEvaluator\AssignmentsEvaluator;
use Tienvx\Bundle\MbtBundle\Channel\ChannelManager;
use Tienvx\Bundle\MbtBundle\Channel\ChannelManagerInterface;
use Tienvx\Bundle\MbtBundle\Command\CommandPreprocessor;
Expand Down Expand Up @@ -278,6 +279,7 @@
->args([
service(ColorfulFactoryInterface::class),
service(ExpressionLanguage::class),
service(AssignmentsEvaluator::class),
])
->alias(PetrinetHelperInterface::class, PetrinetHelper::class)

Expand Down
12 changes: 9 additions & 3 deletions src/Service/Petrinet/PetrinetHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use SingleColorPetrinet\Model\ColorfulFactoryInterface;
use SingleColorPetrinet\Model\ColorInterface;
use SingleColorPetrinet\Model\PetrinetInterface;
use Tienvx\AssignmentsEvaluator\AssignmentsEvaluator;
use Tienvx\Bundle\MbtBundle\Model\Model\Revision\PlaceInterface;
use Tienvx\Bundle\MbtBundle\Model\Model\Revision\TransitionInterface;
use Tienvx\Bundle\MbtBundle\Model\Model\RevisionInterface;
Expand All @@ -19,11 +20,16 @@ class PetrinetHelper implements PetrinetHelperInterface

protected ColorfulFactoryInterface $colorfulFactory;
protected ExpressionLanguage $expressionLanguage;
protected AssignmentsEvaluator $assignmentsEvaluator;

public function __construct(ColorfulFactoryInterface $colorfulFactory, ExpressionLanguage $expressionLanguage)
{
public function __construct(
ColorfulFactoryInterface $colorfulFactory,
ExpressionLanguage $expressionLanguage,
AssignmentsEvaluator $assignmentsEvaluator
) {
$this->colorfulFactory = $colorfulFactory;
$this->expressionLanguage = $expressionLanguage;
$this->assignmentsEvaluator = $assignmentsEvaluator;
}

public function build(RevisionInterface $revision): PetrinetInterface
Expand Down Expand Up @@ -75,7 +81,7 @@ protected function getTransitions(RevisionInterface $revision, SingleColorPetrin
)
: null,
$transition->getExpression()
? fn (ColorInterface $color): array => (array) $this->expressionLanguage->evaluate(
? fn (ColorInterface $color): array => $this->assignmentsEvaluator->evaluate(
$transition->getExpression(),
$color->getValues()
)
Expand Down
4 changes: 2 additions & 2 deletions tests/Factory/Model/Revision/TransitionFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ protected function setUp(): void
$this->data = [
'label' => 'Transition 1',
'guard' => 'count > 1',
'expression' => '{count: count + 1}',
'expression' => 'count = count + 1',
'fromPlaces' => [1, 2],
'toPlaces' => [2, 3],
'commands' => [],
Expand All @@ -32,7 +32,7 @@ public function testCreateFromArray(): void
$transition = TransitionFactory::createFromArray($this->data);
$this->assertSame('Transition 1', $transition->getLabel());
$this->assertSame('count > 1', $transition->getGuard());
$this->assertSame('{count: count + 1}', $transition->getExpression());
$this->assertSame('count = count + 1', $transition->getExpression());
$this->assertSame([1, 2], $transition->getFromPlaces());
$this->assertSame([2, 3], $transition->getToPlaces());
$this->assertIsArray($transition->getCommands());
Expand Down
4 changes: 3 additions & 1 deletion tests/Generator/RandomGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use PHPUnit\Framework\TestCase;
use SingleColorPetrinet\Model\ColorfulFactory;
use SingleColorPetrinet\Service\GuardedTransitionService;
use Tienvx\AssignmentsEvaluator\AssignmentsEvaluator;
use Tienvx\Bundle\MbtBundle\Entity\Model\Revision;
use Tienvx\Bundle\MbtBundle\Entity\Task;
use Tienvx\Bundle\MbtBundle\Generator\GeneratorManager;
Expand Down Expand Up @@ -45,7 +46,8 @@ protected function setUp(): void
{
$factory = new ColorfulFactory();
$expressionLanguage = new ExpressionLanguage();
$petrinetHelper = new PetrinetHelper($factory, $expressionLanguage);
$assignmentsEvaluator = new AssignmentsEvaluator($expressionLanguage);
$petrinetHelper = new PetrinetHelper($factory, $expressionLanguage, $assignmentsEvaluator);
$markingHelper = new MarkingHelper($factory);
$modelHelper = new ModelHelper();
$transitionService = new GuardedTransitionService($factory);
Expand Down
8 changes: 4 additions & 4 deletions tests/Model/Model/Revision/TransitionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ protected function setUp(): void
$this->transition = $this->createTransition();
$this->transition->setLabel('transition label');
$this->transition->setGuard('count > 2');
$this->transition->setExpression('{count: count + 1}');
$this->transition->setExpression('count = count + 1');
$this->transition->setFromPlaces([1, 2, 3]);
$this->transition->setToPlaces([12, 23]);
$this->transition->setCommands([
Expand Down Expand Up @@ -61,18 +61,18 @@ public function testSerialize(): void
{
$className = get_class($this->transition);
// phpcs:ignore Generic.Files.LineLength
$this->assertSame('O:' . strlen($className) . ':"' . $className . '":6:{s:5:"label";s:16:"transition label";s:5:"guard";s:9:"count > 2";s:10:"expression";s:18:"{count: count + 1}";s:10:"fromPlaces";a:3:{i:0;i:1;i:1;i:2;i:2;i:3;}s:8:"toPlaces";a:2:{i:0;i:12;i:1;i:23;}s:8:"commands";a:2:{i:0;a:3:{s:7:"command";s:4:"type";s:6:"target";s:10:"css=.email";s:5:"value";s:16:"test@example.com";}i:1;a:3:{s:7:"command";s:5:"click";s:6:"target";s:9:"css=.link";s:5:"value";N;}}}', serialize($this->transition));
$this->assertSame('O:' . strlen($className) . ':"' . $className . '":6:{s:5:"label";s:16:"transition label";s:5:"guard";s:9:"count > 2";s:10:"expression";s:17:"count = count + 1";s:10:"fromPlaces";a:3:{i:0;i:1;i:1;i:2;i:2;i:3;}s:8:"toPlaces";a:2:{i:0;i:12;i:1;i:23;}s:8:"commands";a:2:{i:0;a:3:{s:7:"command";s:4:"type";s:6:"target";s:10:"css=.email";s:5:"value";s:16:"test@example.com";}i:1;a:3:{s:7:"command";s:5:"click";s:6:"target";s:9:"css=.link";s:5:"value";N;}}}', serialize($this->transition));
}

public function testUnerialize(): void
{
$className = get_class($this->transition);
// phpcs:ignore Generic.Files.LineLength
$transition = unserialize('O:' . strlen($className) . ':"' . $className . '":6:{s:5:"label";s:10:"Serialized";s:5:"guard";s:10:"count == 3";s:10:"expression";s:18:"{count: count - 1}";s:10:"fromPlaces";a:2:{i:0;i:1;i:1;i:4;}s:8:"toPlaces";a:1:{i:0;i:15;}s:8:"commands";a:1:{i:0;a:3:{s:7:"command";s:5:"store";s:6:"target";s:2:"55";s:5:"value";s:6:"number";}}}');
$transition = unserialize('O:' . strlen($className) . ':"' . $className . '":6:{s:5:"label";s:10:"Serialized";s:5:"guard";s:10:"count == 3";s:10:"expression";s:17:"count = count - 1";s:10:"fromPlaces";a:2:{i:0;i:1;i:1;i:4;}s:8:"toPlaces";a:1:{i:0;i:15;}s:8:"commands";a:1:{i:0;a:3:{s:7:"command";s:5:"store";s:6:"target";s:2:"55";s:5:"value";s:6:"number";}}}');
$this->assertInstanceOf(TransitionInterface::class, $transition);
$this->assertSame('Serialized', $transition->getLabel());
$this->assertSame('count == 3', $transition->getGuard());
$this->assertSame('{count: count - 1}', $transition->getExpression());
$this->assertSame('count = count - 1', $transition->getExpression());
$this->assertSame([1, 4], $transition->getFromPlaces());
$this->assertSame([15], $transition->getToPlaces());
$this->assertInstanceOf(CommandInterface::class, $transition->getCommands()[0]);
Expand Down
6 changes: 4 additions & 2 deletions tests/Service/Petrinet/PetrinetHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use SingleColorPetrinet\Model\ColorfulFactory;
use SingleColorPetrinet\Model\GuardedTransition as PetrinetTransition;
use SingleColorPetrinet\Model\Place as PetrinetPlace;
use Tienvx\AssignmentsEvaluator\AssignmentsEvaluator;
use Tienvx\Bundle\MbtBundle\Entity\Model\Revision;
use Tienvx\Bundle\MbtBundle\Service\ExpressionLanguage;
use Tienvx\Bundle\MbtBundle\Service\Petrinet\PetrinetHelper;
Expand All @@ -28,7 +29,8 @@ public function testBuild(): void
{
$factory = new ColorfulFactory();
$expressionLanguage = new ExpressionLanguage();
$helper = new PetrinetHelper($factory, $expressionLanguage);
$assignmentsEvaluator = new AssignmentsEvaluator($expressionLanguage);
$helper = new PetrinetHelper($factory, $expressionLanguage, $assignmentsEvaluator);

// Model revision
$revision = new Revision();
Expand All @@ -50,7 +52,7 @@ public function testBuild(): void
$transition1->setToPlaces([1, 2]);
$transition2->setFromPlaces([2]);
$transition2->setToPlaces([1]);
$transition3->setExpression('{count: count + 1, status: "open"}');
$transition3->setExpression('count = count + 1; status = "open"');
$transition3->setFromPlaces([1]);
$transition3->setToPlaces([0, 2]);

Expand Down

0 comments on commit b3175d4

Please sign in to comment.