cosma / simple-state-machine
简单状态机
Requires
- php: >=5.3
- clue/graph: 0.7.*
Requires (Dev)
- phpunit/phpunit: ~4.3
This package is not auto-updated.
Last update: 2024-09-28 15:49:39 UTC
README
- 无超时的时间状态机。
- 状态可以修改一个数据对象,该对象将被注入到初始状态中。
- 状态机图可以被可视化为不同格式的UML图。
目录
安装
Simple State Machine可以通过Composer以cosma/simple-state-machine方式安装。
{
"require": {
"cosma/simple-state-machine": "1.0.*"
}
}
使用
让我们跟随一个简单的价格计算状态机的例子。
namespace \MyProject; /** * Simple State Machine */ $priceStateMachine = \Cosma\SimpleStateMachine\StateMachine('Price Calculator State Machine'); /** * Your Data object which can be modify by the State Machines * Has to implement the interface \Cosma\SimpleStateMachine\InterfaceData */ $price = new \YourProject\Price(); /** * Start State of the State Machine * Has to extends the abstract \Cosma\SimpleStateMachine\AbstractState */ $initialPriceState = \YourProject\PriceStateMachine\States\InitialPrice($price); /** * Simple State Machine cannot run without setting the start State */ $priceStateMachine->setState($initialPriceState); /** * Running the State Machine * During this process the Data object will be modified depending on teh configuration of the Machine */ $priceStateMachine->run(); /** * Retrieve the Data object at the end of the process */ $finalPrice = $priceStateMachine->getState()->getData(); /** * Generate the Diagram of the State Machine. * Choose the format */ $graphic = new Graphic('svg'); $diagramSVG = $priceStateMachine->draw($graphic); echo $diagramSVG;
参考
定义数据对象
数据对象可以被状态机转换和状态修改。
数据类必须实现接口 \Cosma\SimpleStateMachine\InterfaceData。
InterfaceData是一个空接口,但它用于强制类型提示。
namespace \MyProject\PriceStateMachine; class Price implements \Cosma\SimpleStateMachine\InterfaceData { /** * @var float */ private $value; public function __constructor() { $this->value = $this->getPriceFromDB(); } /** * getters, setters and other functions */ ... }
定义状态
所有状态必须扩展类 \Cosma\SimpleStateMachine\AbstractState
namespace \MyProject\PriceStateMachine\States; class AddVATState extends \Cosma\SimpleStateMachine\AbstractState { /** * Set the label for this State used in State Machine diagram */ public function getLabel() { return 'Add VAT Tax'; } /** * Modify the Data object */ protected function process() { $price = $this->getData(); $price->setValue($price->getValue() * 1.19); ... } /** * Configure the Transitions from this State to another States or itself in case of a loop * You may set in what Condition that Transition takes place * The order to check upon the validity of conditions and forward to next State is from up to down */ protected function configureAvailableTransitions() { $this->addTransition( '\YourProject\PriceStateMachine\States\AddDiscount', '\YourProject\PriceStateMachine\Conditions\IfGreaterThan1000' ); $this->addTransition('NewStateClass', 'ConditionClass'); $this->addTransition('\YourProject\PriceStateMachine\States\AddDiscount'); ... } }
定义条件
当没有条件或条件为真时,状态之间的转换是可能的。
所有条件都必须扩展 \Cosma\SimpleStateMachine\AbstractCondition 类
namespace namespace \MyProject\PriceStateMachine\Conditions; class SomeWildCondition extends \Cosma\SimpleStateMachine\AbstractCondition { /** * @return string */ public function getLabel() { return "Some Wild Condition"; } /** * @return bool */ public function isTrue() { $data = $this->getData(); return $this->checkSomething($data); } ... }
图形图
您可以轻松地可视化状态机图
namespace \MyProject; /** * Generate the Diagram of the State Machine. * Choose the format */ $graphic = new Graphic('svg'); $diagramSVG = $priceStateMachine->draw($graphic); echo $diagramSVG;
导出格式
输出以各种格式提供。
最常用的导出格式是
所有支持的格式都是DOT输出格式:bmp, canon, cgimage, cmap, cmapx, cmapx_np, dot, eps, exr, fig, gif, gv, icns, ico, imap, imap_np, ismap, jp2, jpe, jpeg, jpg, pct, pdf, pic, pict, plain, plain-ext, png, pov, ps, ps2, psd, sgi, svg, svgz, tga, tif, tiff, tk, vml, vmlz, x11, xdot, xdot1.2, xdot1.4, xlib
DOT语言
代表图形描述语言,您可以在这里了解更多信息
要充分利用样式属性,您需要了解DOT语言。
在定义条件或状态时,您可以轻松修改受保护的$styleAttributes属性并覆盖状态或条件的默认样式。
通过这种方式,您可以操作状态和条件的颜色、字体和形状
namespace \MyProject\PriceStateMachine\States; class MyState extends \Cosma\SimpleStateMachine\AbstractState { ... /** * An array of DOT attributes to overwrite the default style of a State/Condition */ protected $styleAttributes = array( 'fillcolor' => '#A8CE9F', 'style' => 'filled', 'fontcolor' => '#000000', 'fontsize' => 12, 'penwidth' => 1, ); ... }
DOT实用链接
测试
vendor/phpunit/phpunit/phpunit --coverage-text --coverage-html=tests/coverage tests
许可
在MIT许可下发布,请参阅LICENSE。