fibonacci.python.drl

来自「drools 一个开放源码的规则引擎」· DRL 代码 · 共 74 行

DRL
74
字号
<?xml version="1.0"?>

<rule-set name="fibonacci"
          xmlns="http://drools.org/rules"
          xmlns:python="http://drools.org/semantics/python"
          xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
          xs:schemaLocation="http://drools.org/rules rules.xsd
                             http://drools.org/semantics/python python.xsd">

  <import>from org.drools.io import Fibonacci</import>
  
  <application-data identifier="map">java.util.Map</application-data>      

  <rule name="Bootstrap 1"   salience="20">
    <parameter identifier="f">
      <class>Fibonacci</class>
    </parameter>
    <python:condition>f.getSequence() == 1</python:condition>
    <python:condition>f.getValue() == -1</python:condition>
    <python:consequence>
      f.setValue( 1 )
      drools.modifyObject( f )
    </python:consequence>
  </rule>

  <rule name="Bootstrap 2">
    <parameter identifier="f">
      <class>Fibonacci</class>
    </parameter>
    <python:condition>f.getSequence() == 2</python:condition>
    <python:condition>f.getValue() == -1</python:condition>
    <python:consequence>
      f.setValue( 1 )
      drools.modifyObject( f )
    </python:consequence>
  </rule>

  <rule name="Recurse" salience="10">
    <parameter identifier="f">
      <class>Fibonacci</class>
    </parameter>
    <python:condition>f.getValue() == -1</python:condition>
    <python:consequence>
      drools.assertObject( Fibonacci( f.getSequence() - 1 ) )
    </python:consequence>
  </rule>

  <rule name="Calculate">
    <parameter identifier="f1">
      <class>Fibonacci</class>
    </parameter>
    <parameter identifier="f2">
      <class>Fibonacci</class>
    </parameter>
    <parameter identifier="f3">
      <class>Fibonacci</class>
    </parameter>
    <python:condition>f2.getSequence() == (f1.getSequence() + 1)</python:condition>
    <python:condition>f3.getSequence() == (f2.getSequence() + 1)</python:condition>
    <python:condition>f1.getValue() != -1</python:condition>
    <python:condition>f2.getValue() != -1</python:condition>
    <python:condition>f3.getValue() == -1</python:condition>
    <python:consequence>
      from java.lang import Long
      f3.setValue( f1.getValue() + f2.getValue() )
      drools.modifyObject( f3 )
      drools.retractObject( f1 )
      map.put("value", Long( f3.getValue() ) )
    </python:consequence>
  </rule>

</rule-set>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?