找到你要的答案

Q:State machines and framework

Q:状态机与框架

I'm trying to understand bits and pieces of how frameworks are meant to be used. I'm working on a custom framework i.e. built by using essential components of Symphony2.

So are we understand MVC there is a controller that handles our routing methods. Beside this we have models & repo ( incase of Symphony2 type implementation ).

What i'm confused at is how do i integrate or implement state machines. I would like to know what part goes where.

I would like to use following library,

https://github.com/yohang/Finite

Now my current state is persisted in the table column.

So where do i implement state machine interface, models? Where do initialise state machine, controller?

我正试图理解如何使用框架的碎片和碎片。我工作在一个自定义的框架,即利用symphony2要素建立。

所以我们了解MVC有处理我们的路由方法的控制器。在这我们有模特&;回购(对symphony2型实施柜面)。

我困惑的是我如何整合或实现状态机。我想知道什么地方去哪里。

我想使用下列库,

https://github.com/yohang/finite

现在我的当前状态被保存在表列中。

那么我在哪里实现状态机接口,模型呢?在做初始化状态机控制器?

answer1: 回答1:

As far as the model goes, you simply have your model object implement the StatefullInterface. If, for example, you are using Doctrine 2 then your model object is just a plain php object. Implement the interface per your link and you are done.

For your state machines I would make custom state machines:

class MyStateMachine extends StateMachine
{
    public function __construct()
    {
        $this->addState(new State('s1', StateInterface::TYPE_INITIAL));
        $this->addState('s2');
        $this->addState('s3');
        $this->addState(new State('s4', StateInterface::TYPE_FINAL));

        $this->addTransition('t12', 's1', 's2');
        $this->addTransition('t23', 's2', 's3');
        $this->addTransition('t34', 's3', 's4');
        $this->addTransition('t42', 's4', 's2');

And then you could create and initialize the object in your controller. As an alternative use dependency injection and make a factory service.

I suppose you could also embed the state machine directly in your entity. Probably not a good idea.

The state machine you linked to seems a bit basic. Only supports one state per object? I have not checked the code but the transitions also seem pretty primitive. Make sure it fulfills your needs before investing too much time in it. I always just create custom machines.

至于模式,你只要有你的模型对象实施statefullinterface。如果,例如,你使用原则2那么你的模型对象只是一个普通的PHP对象。实现每个链接的接口,你就完成了。

对于你的状态机,我会定制状态机:

class MyStateMachine extends StateMachine
{
    public function __construct()
    {
        $this->addState(new State('s1', StateInterface::TYPE_INITIAL));
        $this->addState('s2');
        $this->addState('s3');
        $this->addState(new State('s4', StateInterface::TYPE_FINAL));

        $this->addTransition('t12', 's1', 's2');
        $this->addTransition('t23', 's2', 's3');
        $this->addTransition('t34', 's3', 's4');
        $this->addTransition('t42', 's4', 's2');

然后您可以创建和初始化控制器中的对象。作为替代使用依赖注入和使工厂服务。

我想你也可以直接嵌入状态机在你的实体。可能不是个好主意。

你连接的状态机看起来有点基础。只支持每个对象的一个状态?我没有检查代码,但转换似乎也相当原始。确保它满足你的需要之前投入太多的时间。我总是创建自定义机器。

answer2: 回答2:

You can implement your entity using a state machine, meaning the state machine will be placed in model layer and rest of the application will interact with it - read its state and invoke transitions.

The entity will provied API to invoke transitions, read its state and enumerate available transitions (like reflection, but with regard to current state). The rest of the application can ask individual instances of the entity what can be done next and show relevant parts of user interface. For example it can show a button to delete an entity which has delete transition available in its current state. And when the delete button is clicked, it will invoke the delete transition on given state machine. I found this approach very universal.

Some time ago I created Smalldb framework which implements such state machines. It implements model layer of a web application and lets you define state machines for individual entities and implement their transitions in PHP. It also checks which user is allowed to invoke which transitions and has few nice features like state machine visualization using Graphviz and REST API to directly interact with the state machines from JS (nice for SPA applications).

您可以使用状态机实现实体,这意味着状态机将被放置在模型层中,应用程序的其余部分将与它交互,读取状态并调用转换。

实体将提供API调用转换,读取其状态和枚举可用的转换(如反射,但对当前的状态)。应用程序的其余部分可以询问实体的各个实例,下一步可以做什么,并显示用户界面的相关部分。例如,它可以显示一个按钮来删除一个实体,该实体在当前状态中具有可删除转换。当单击“删除”按钮时,它将调用给定状态机上的“删除转换”。我发现这种方法非常普遍。

前一段时间我创造了smalldb框架,实现了状态机。它实现了一个Web应用模型层和让你定义单个实体的状态机,在PHP中实现他们的转变。它还检查允许用户调用的转换和有几个好的特点,如状态机使用Graphviz可视化和REST API直接从JS的状态机交互(好SPA应用)。

php  symfony2  state-machines  finite-state-machine