/** * @see \MetaborStd\Statemachine\Factory\TransitionSelectorInterface::selectTransition() */ public function selectTransition(\Traversable $transitions) { $bestTransitions = array(); $bestScore = -1; foreach ($transitions as $transition) { $score = $this->calculcateScore($transition); if ($score > $bestScore) { $bestScore = $score; $bestTransitions = array($transition); } elseif ($score == $bestScore) { $bestTransitions[] = $transition; } } return $this->innerSelector->selectTransition(new \ArrayIterator($bestTransitions)); }
/** * @param \ArrayAccess $context * @param EventInterface $event */ protected function doCheckTransitions(\ArrayAccess $context, EventInterface $event = null) { try { $transitions = $this->currentState->getTransitions(); $activeTransitions = new ActiveTransitionFilter($transitions, $this->getSubject(), $context, $event); $this->selectedTransition = $this->transitonSelector->selectTransition($activeTransitions); if ($this->selectedTransition) { $targetState = $this->selectedTransition->getTargetState(); if ($this->currentState != $targetState) { $this->lastState = $this->currentState; $this->currentState = $targetState; $this->notify(); $this->selectedTransition = null; $this->lastState = null; } $this->checkTransitions(); } } catch (\Exception $exception) { $message = 'Exception was thrown when doing a transition from current state "' . $this->currentState->getName() . '"'; if ($this->currentEvent instanceof NamedInterface) { $message .= ' with event "' . $this->currentEvent->getName() . '"'; } throw new \RuntimeException($message, 0, $exception); } }
/** * @see \MetaborStd\Statemachine\Factory\TransitionSelectorInterface::selectTransition() */ public function selectTransition(\Traversable $transitions) { $bestTransitions = array(); $bestWeight = null; foreach ($transitions as $transition) { if ($transition instanceof WeightedInterface) { $weight = $transition->getWeight(); $diff = $weight - $bestWeight; if ($bestWeight === null || $diff >= $this->epsilon) { $bestWeight = $weight; $bestTransitions = array($transition); } elseif (abs($diff) < $this->epsilon) { $bestTransitions[] = $transition; } } } return $this->innerSelector->selectTransition(new \ArrayIterator($bestTransitions)); }
/** * * @param ArrayAccess $context * @param EventInterface $event */ protected function doCheckTransitions(ArrayAccess $context, EventInterface $event = null) { $transitions = $this->currentState->getTransitions(); $activeTransitions = new ActiveTransitionFilter($transitions, $this->getSubject(), $context, $event); $this->selectedTransition = $this->transitonSelector->selectTransition($activeTransitions); if ($this->selectedTransition) { $this->beforeTransition($this->getSubject(), $this->selectedTransition->getTargetState()->getName()); $this->currentState = $this->selectedTransition->getTargetState(); $this->notify(); $this->selectedTransition = null; $this->checkTransitions(); } }