steffenbrand/dmn-decision-tables

PHP 库,用于以编程方式创建 DMN 决策表。

v1.0.1 2019-03-18 13:58 UTC

This package is auto-updated.

Last update: 2024-09-13 12:59:59 UTC


README

Build Scrutinizer Code Quality Latest Stable Version Total Downloads License

DMN 决策表

PHP 库,用于以编程方式创建 DMN 决策表

限制

  • 生成的表格已在 Camunda BPM 平台和 Camunda Cloud 上进行测试,但尚未在其他 BPMN 引擎上测试
  • 目前只测试了 FEEL 和 JUEL 表达式
  • 每个 .dmn 文件仅支持一个决策表
  • 不支持 DRD 功能
  • ID 是随机生成的,如下面的示例所示

如何安装

composer require steffenbrand/dmn-decision-tables

如何使用

以下示例展示了如何构建 Camunda Cloud 的示例表格

构建决策表

try {
    $decisionTable = DecisionTableBuilder::getInstance()
        ->setName('Dish')
        ->setDefinitionKey('decision')
        ->setHitPolicy(HitPolicy::UNIQUE_POLICY)
        ->addInput(new Input('Season', 'season', VariableType::STRING_TYPE))
        ->addInput(new Input('How many guests', 'guests', VariableType::INTEGER_TYPE))
        ->addOutput(new Output('Dish', 'dish', VariableType::STRING_TYPE))
        ->addRule(
            [
                new InputEntry('"Fall"', ExpressionLanguage::FEEL_LANGUAGE),
                new InputEntry('<= 8', ExpressionLanguage::FEEL_LANGUAGE)
            ],
            [new OutputEntry('"Spareribs"', ExpressionLanguage::JUEL_LANGUAGE)]
        )
        ->addRule(
            [new InputEntry('"Winter"'), new InputEntry('<= 8')],
            [new OutputEntry('"Roastbeef"')]
        )
        ->addRule(
            [new InputEntry('"Spring"'), new InputEntry('<= 4')],
            [new OutputEntry('"Dry Aged Gourmet Steak"')]
        )
        ->addRule(
            [new InputEntry('"Spring"'), new InputEntry('[5..8]')],
            [new OutputEntry('"Steak"')],
            'Save money'
        )
        ->addRule(
            [new InputEntry('"Fall", "Winter", "Spring"'), new InputEntry('> 8')],
            [new OutputEntry('"Stew"')],
            'Less effort'
        )
        ->addRule(
            [new InputEntry('"Summer"'), new InputEntry(null)],
            [new OutputEntry('"Light Salad and a nice Steak"')],
            'Hey, why not!?'
        )
        ->build();
} catch (DmnValidationException $e) {
    // Build method validates before it creates the DecisionTable.
    // Can be disabled, so feel free to validate yourself.
    // Use the DecisionTableValidator or inject your own validator.
} catch (DmnConversionException $e) {
    // Conversion to XML might fail because of invalid expressions or whatever.
}

转换为 DMN 字符串(XML)

$decisionTable->toDMN();

保存到文件系统

$decisionTable->saveFile('/my/path/and/filename.dmn');

结果看起来像什么

<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/DMN/20151101/dmn11.xsd" id="definitions" name="definitions" namespace="http://camunda.org/schema/1.0/dmn">
  <decision id="decision" name="Dish">
    <decisionTable id="decisionTable5990364503747" hitPolicy="UNIQUE">
      <input id="input599036450376b" label="Season">
        <inputExpression id="inputExpression5990364503774" typeRef="string">
          <text>season</text>
        </inputExpression>
      </input>
      <input id="input5990364503786" label="How many guests">
        <inputExpression id="inputExpression5990364503790" typeRef="integer">
          <text>guests</text>
        </inputExpression>
      </input>
      <output id="output59903645037c4" label="Dish" name="dish" typeRef="string"/>
      <rule id="rule59903645037de">
        <inputEntry id="inputEntry59903645037fb" expressionLanguage="feel">
          <text><![CDATA["Fall"]]></text>
        </inputEntry>
        <inputEntry id="inputEntry5990364503816" expressionLanguage="feel">
          <text><![CDATA[<= 8]]></text>
        </inputEntry>
        <outputEntry id="outputEntry5990364503833" expressionLanguage="juel">
          <text><![CDATA["Spareribs"]]></text>
        </outputEntry>
      </rule>
      <rule id="rule5990364503847">
        <inputEntry id="inputEntry5990364503880" expressionLanguage="feel">
          <text><![CDATA["Winter"]]></text>
        </inputEntry>
        <inputEntry id="inputEntry59903645038bf" expressionLanguage="feel">
          <text><![CDATA[<= 8]]></text>
        </inputEntry>
        <outputEntry id="outputEntry59903645038ee" expressionLanguage="juel">
          <text><![CDATA["Roastbeef"]]></text>
        </outputEntry>
      </rule>
      <rule id="rule59903645038fc">
        <inputEntry id="inputEntry599036450390a" expressionLanguage="feel">
          <text><![CDATA["Spring"]]></text>
        </inputEntry>
        <inputEntry id="inputEntry5990364503916" expressionLanguage="feel">
          <text><![CDATA[<= 4]]></text>
        </inputEntry>
        <outputEntry id="outputEntry5990364503922" expressionLanguage="juel">
          <text><![CDATA["Dry Aged Gourmet Steak"]]></text>
        </outputEntry>
      </rule>
      <rule id="rule5990364503930">
        <description>Save money</description>
        <inputEntry id="inputEntry599036450393e" expressionLanguage="feel">
          <text><![CDATA["Spring"]]></text>
        </inputEntry>
        <inputEntry id="inputEntry599036450394d" expressionLanguage="feel">
          <text><![CDATA[[5..8]]]></text>
        </inputEntry>
        <outputEntry id="outputEntry5990364503961" expressionLanguage="juel">
          <text><![CDATA["Steak"]]></text>
        </outputEntry>
      </rule>
      <rule id="rule599036450396e">
        <description>Less effort</description>
        <inputEntry id="inputEntry599036450397b" expressionLanguage="feel">
          <text><![CDATA["Fall", "Winter", "Spring"]]></text>
        </inputEntry>
        <inputEntry id="inputEntry5990364503985" expressionLanguage="feel">
          <text><![CDATA[> 8]]></text>
        </inputEntry>
        <outputEntry id="outputEntry5990364503991" expressionLanguage="juel">
          <text><![CDATA["Stew"]]></text>
        </outputEntry>
      </rule>
      <rule id="rule599036450399d">
        <description>Hey, why not!?</description>
        <inputEntry id="inputEntry59903645039ab" expressionLanguage="feel">
          <text><![CDATA["Summer"]]></text>
        </inputEntry>
        <inputEntry id="inputEntry59903645039b4">
          <text/>
        </inputEntry>
        <outputEntry id="outputEntry59903645039c0" expressionLanguage="juel">
          <text><![CDATA["Light Salad and a nice Steak"]]></text>
        </outputEntry>
      </rule>
    </decisionTable>
  </decision>
</definitions>

试试看

您可以随意下载 generated_with_dmn_decision_tables.dmn 并在 Camunda Cloud 中尝试使用