arcturial / fsm
简单的有限状态机
dev-master
2014-06-10 09:05 UTC
Requires
- php: >=5.3
- clue/graph: *
This package is not auto-updated.
Last update: 2024-09-14 15:12:08 UTC
README
这个库提供了一个简单的集成有限状态机。状态机由状态/转换/触发器组成。
- 状态
状态定义了...机器在处理过程中任何给定点上可以处于的状态。状态通过转换连接。
$stateCreated = new State('created', StateInterface::TYPE_INITIAL); $stateImported = new State('imported'); $stateLive = new State('imported', StateInterface::TYPE_FINAL);
状态类型包括 TYPE_INITIAL
、TYPE_INTERMEDIATE
、TYPE_FINAL
。
动作
状态可以包含动作。动作是需要执行的设置任务,以便成功转换。例如,"当一笔付款进入待处理状态时,我们需要通知支持顾问。"
$stateImported = new State('imported'); $stateImported->addAction(new NotifyAction()); $stateImported->addAction(new TwitterAction());
- 转换
转换是不同状态之间的逻辑链接(路径)。转换还可以有转移条件。转移条件需要评估为 true
才能转换到新状态。通过指定初始状态和需要转换到的状态来构建转换。
$transitionCreatedImported = new Transition($stateCreated, $stateImported); $transitionImportedLive = new Transition($stateImported, $stateLive);
条件
转换可以有一个可选的条件。这个条件必须评估为 true。条件可以轻松地由不同的状态重复使用。
class ImportPermissions implements \FSM\StateMachine\Condition\ConditionInterface { public function check() { return true; // Return true/false depending on the outcome of your condition } } $transitionCreatedImported = new Transition($stateCreated, $stateImported); $transitionCreatedImported->addCondition(new ImportPermissions()); $transitionImportedLive = new Transition($stateImported, $stateLive);
- 触发器
触发器是某些可以触发的特定事件,这将尝试推动状态机向前。触发器可以与一系列不同的转换相关联,并将自动确定要遵循的转换路径。
$stateMachine->addTrigger('triggerName', array($transitionCreatedImported, $transitionImportedLive));
- 使用示例
将这些全部组合起来,状态机将看起来像这样
// Set up a condition class ImportPermissions implements \FSM\StateMachine\Condition\ConditionInterface { public function check() { return true; // Return true/false depending on the outcome of your condition } } // Set up a machine class ImportMachine extends \FSM\StateMachine\StateMachine { public function __construct($name) { // Potentially inject some custom dependencies? parent::__construct($name); } protected function configureMachine() { $stateCreated = new State('created', StateInterface::TYPE_INITIAL); $stateImported = new State('imported'); $stateLive = new State('imported', StateInterface::TYPE_FINAL); $transitionCreatedImported = new Transition($stateCreated, $stateImported); $transitionCreatedImported->addCondition(new ImportPermissions()); $transitionImportedLive = new Transition($stateImported, $stateLive); // Add the supported machine transitions $this->addTransition($transitionCreatedImported); $this->addTransition($transitionImportedLive); // Add machine triggers $this->addTrigger('import', array($transitionCreatedImported, $transitionImportedLive)); $this->addTrigger('live', array($transitionImportedLive)); // The initial state can be determined by external dependencies // if required $this->setCurrentState('created'); } } $machine = new ImportMachine(); echo $machine->getCurrentState(); // echo 'created' // Trigger machine import $machine->trigger('import'); echo $machine->getCurrentState(); // echo 'imported' (update the ImportPermissions return to false will echo 'created') // Trigger machine 'mark as live' $machine->trigger('live'); echo $machine->getCurrentState(); // echo 'live'
由于 "import" 触发器包含 import
和 live
转换,因此此触发器可以 "深度解析"。这意味着它可以直接从导入状态变为在线状态。要启用深度解析,您可以传递 $machine->trigger('import', true);
- 更多示例
要查看更深入的示例和状态图的视觉表示,您可以在浏览器中查看 tests/FSM/StateMachine/Stage/Graph.php。您需要可选地下载 GraphViz 库以生成更详细的图像。