PHP 中带守卫的有限状态机?

发布于 2024-12-01 19:48:44 字数 84 浏览 2 评论 0原文

有人知道在 PHP 中具有 guard 功能的有限状态机吗?

Does anybody know a finite state machine that has guard feature in PHP ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

奢望 2024-12-08 19:48:44

使用 PEAR 的 FSM (使用示例),如果守卫失败,您可以使用操作回调返回下一个状态,如下所示:

$payload = '';
$fsm = new FSM('STATE1', $payload);
function guard1($symbol, $payload) {
    if ($payload == 'something') {
        // Guard success, allow transition
        return;
    }
    else {
        // Guard fail, return to previous state
        return 'STATE1';
    }
}
$fsm->addTransition('SYMBOL1', 'STATE1', 'STATE2', 'guard1');

$fsm->process('SYMBOL1');

Using PEAR's FSM (usage example), you can use the action callback to return the next state if the guard fails, like so:

$payload = '';
$fsm = new FSM('STATE1', $payload);
function guard1($symbol, $payload) {
    if ($payload == 'something') {
        // Guard success, allow transition
        return;
    }
    else {
        // Guard fail, return to previous state
        return 'STATE1';
    }
}
$fsm->addTransition('SYMBOL1', 'STATE1', 'STATE2', 'guard1');

$fsm->process('SYMBOL1');
居里长安 2024-12-08 19:48:44

查看 ezComponents 工作流程。允许您设计包含许多对象的工作流程并添加条件和状态。

Look at ezComponents Workflow. Allows you to design workflows with many objects and add in conditionals and states.

千鲤 2024-12-08 19:48:44

查看:
https://github.com/chriswoodford/techne/tree/v0.2

我认为它具有您正在寻找的功能。您定义一个转换,然后可以关联一个在处理转换之前调用的闭包。这里有一个简单的例子:

定义你的 FSM

  $machine = new StateMachine\FiniteStateMachine();
  $machine->setInitialState('off');

定义转换

    $turnOff = new StateMachine\Transition('on', 'off');
    $turnOn = new StateMachine\Transition('off', 'on');

在打开转换中添加一个保护

    // flipping the switch on requires electricity
    $hasElectricity = true;
    $turnOn->before(function() use ($hasElectricity) {
        return $hasElectricity ? true : false;
    });

从关闭到打开

  $machine->flip();  
  echo $machine->getCurrentState();
  // prints 'on'  

返回到关闭的转换

  $machine->flip();  
  echo $machine->getCurrentState();
  // prints 'off'  

的转换如果 $hasElectricity 变量设置为 false,则

  // oops, forgot to pay that electricity bill
  $hasElectricity = false;

  $turnOn->before(function() use ($hasElectricity) {
      return $hasElectricity ? true : false;
  });

,结果将如下所示:然后,如果你尝试从关闭转换 ->为了

  $machine->flip();  
  echo $machine->getCurrentState();
  // prints 'off'  

确定转换完成的位置,您只需将之前的状态与当前状态进行比较。

check out:
https://github.com/chriswoodford/techne/tree/v0.2

I think it has the functionality that you're looking for. You define a transition and then can associate a closure that gets called before the transition is processed. here's a simple example:

Define your FSM

  $machine = new StateMachine\FiniteStateMachine();
  $machine->setInitialState('off');

Define the transitions

    $turnOff = new StateMachine\Transition('on', 'off');
    $turnOn = new StateMachine\Transition('off', 'on');

Add a guard to the turnOn transition

    // flipping the switch on requires electricity
    $hasElectricity = true;
    $turnOn->before(function() use ($hasElectricity) {
        return $hasElectricity ? true : false;
    });

Transition from off to on

  $machine->flip();  
  echo $machine->getCurrentState();
  // prints 'on'  

Transition back to off

  $machine->flip();  
  echo $machine->getCurrentState();
  // prints 'off'  

if the $hasElectricity variable had been set to false, the outcome would look like this:

  // oops, forgot to pay that electricity bill
  $hasElectricity = false;

  $turnOn->before(function() use ($hasElectricity) {
      return $hasElectricity ? true : false;
  });

Then, if you try to transition from Off -> On

  $machine->flip();  
  echo $machine->getCurrentState();
  // prints 'off'  

In order to determine where a transition was completed, you'd just have to compare the previous state to the current state.

一身仙ぐ女味 2024-12-08 19:48:44

Metabor 状态机中的条件
https://github.com/Metabor/Statemachine
可以用作 Guards(Transition 构造函数中的第三个参数)。
参见示例:
https://github.com/Metabor/Statemachine -示例/blob/master/示例/订单/流程/Pre payment.php

The Conditions in the Metabor Statemachine
https://github.com/Metabor/Statemachine
can be used as Guards (3rd parameter in Transition constructor).
See Example:
https://github.com/Metabor/Statemachine-Example/blob/master/Example/Order/Process/Prepayment.php

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文