/** * {@inheritdoc} */ public function fire(TransitionInterface $transition, MarkingInterface $marking) { if (!$this->isEnabled($transition, $marking)) { throw new TransitionNotEnabledException('Cannot fire a disabled transition.'); } $inputArcs = $transition->getInputArcs(); $outputArcs = $transition->getOutputArcs(); // Remove tokens from the input places foreach ($inputArcs as $arc) { $arcWeight = $arc->getWeight(); $place = $arc->getPlace(); $placeMarking = $marking->getPlaceMarking($place); $tokens = $placeMarking->getTokens(); for ($i = 0; $i < $arcWeight; $i++) { $placeMarking->removeToken($tokens[$i]); } } // Add tokens to the output places foreach ($outputArcs as $arc) { $arcWeight = $arc->getWeight(); $place = $arc->getPlace(); $placeMarking = $marking->getPlaceMarking($place); if (null === $placeMarking) { $placeMarking = $this->factory->createPlaceMarking(); $placeMarking->setPlace($place); $marking->addPlaceMarking($placeMarking); } // Create the tokens $tokens = array(); for ($i = 0; $i < $arcWeight; $i++) { $tokens[] = $this->factory->createToken(); } $placeMarking->setTokens($tokens); } }
/** * Marks the place with the specified tokens. * * @param PlaceInterface $place * @param \Petrinet\Model\TokenInterface|array|integer $tokens * * @return MarkingBuilder * * @throws \InvalidArgumentException */ public function mark(PlaceInterface $place, $tokens) { if (is_int($tokens)) { $tokensCount = $tokens; $tokens = array(); for ($i = 0; $i < $tokensCount; $i++) { $tokens[] = $this->factory->createToken(); } } elseif ($tokens instanceof TokenInterface) { $tokens = array($tokens); } elseif (!is_array($tokens)) { throw new \InvalidArgumentException('The $tokens argument must be an array, integer or a Petrinet\\Model\\TokenInterface instance.'); } $placeMarking = $this->factory->createPlaceMarking(); $placeMarking->setPlace($place); $placeMarking->setTokens($tokens); $this->placeMarkings[] = $placeMarking; return $this; }
function it_builds_a_marking_with_three_place_markings(FactoryInterface $factory, MarkingInterface $marking, PlaceMarkingInterface $placeMarkingOne, PlaceMarkingInterface $placeMarkingTwo, PlaceMarkingInterface $placeMarkingThree, PlaceInterface $placeOne, PlaceInterface $placeTwo, PlaceInterface $placeThree, TokenInterface $token) { $factory->createMarking()->willReturn($marking); $factory->createPlaceMarking()->willReturn($placeMarkingOne, $placeMarkingTwo, $placeMarkingThree); $factory->createToken()->willReturn($token)->shouldBeCalledTimes(3); $marking->setPlaceMarkings(array($placeMarkingOne, $placeMarkingTwo, $placeMarkingThree))->shouldBeCalled(); $placeMarkingOne->setPlace($placeOne)->shouldBeCalled(); $placeMarkingOne->setTokens(array($token))->shouldBeCalled(); $placeMarkingTwo->setPlace($placeTwo)->shouldBeCalled(); $placeMarkingTwo->setTokens(array($token, $token))->shouldBeCalled(); $placeMarkingThree->setPlace($placeThree)->shouldBeCalled(); $placeMarkingThree->setTokens(array($token, $token, $token))->shouldBeCalled(); $this->beConstructedWith($factory); $this->mark($placeOne, $token); $this->mark($placeTwo, array($token, $token)); $this->mark($placeThree, 3); $this->getMarking()->shouldReturn($marking); }
function it_creates_the_output_places_marking_if_not_existing_when_firing_a_transition(FactoryInterface $factory, PlaceMarkingInterface $placeOneMarking, PlaceMarkingInterface $placeTwoMarking, MarkingInterface $marking, PlaceInterface $placeOne, PlaceInterface $placeTwo, TransitionInterface $transition, InputArcInterface $arcOne, OutputArcInterface $arcTwo, TokenInterface $token) { $placeOne->getOutputArcs()->willReturn(array($arcOne)); $arcOne->getPlace()->willReturn($placeOne); $arcOne->getTransition()->willReturn($transition); $arcOne->getWeight()->willReturn(1); $transition->getInputArcs()->willReturn(array($arcOne)); $transition->getOutputArcs()->willReturn(array($arcTwo)); $arcTwo->getTransition()->willReturn($transition); $arcTwo->getPlace()->willReturn($placeTwo); $arcTwo->getWeight()->willReturn(1); $factory->createToken()->willReturn($token)->shouldBeCalled(); $factory->createPlaceMarking()->willReturn($placeTwoMarking)->shouldBeCalled(); $placeOneMarking->getTokens()->willReturn(array($token)); $marking->getPlaceMarking($placeOne->getWrappedObject())->willReturn($placeOneMarking); $marking->getPlaceMarking($placeTwo->getWrappedObject())->willReturn(null); $placeOneMarking->removeToken($token)->shouldBeCalled(); $placeTwoMarking->setPlace($placeTwo)->shouldBeCalled(); $placeTwoMarking->setTokens(array($token))->shouldBeCalled(); $marking->addPlaceMarking($placeTwoMarking)->shouldBeCalled(); $this->beConstructedWith($factory); $this->fire($transition, $marking); }