且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用ClipsPy以编程方式修改事实栏

更新时间:2023-11-30 17:49:40

environment.build 方法用于在引擎内构建构造(defruledeftemplate等).要执行CLIPS代码,您需要使用 environment.eval .

The environment.build method is for building constructs (defrule, deftemplate, etc.) within the engine. To execute CLIPS code, you need to use environment.eval.

在CLIPS 6.30中,一旦声明就无法更改事实(6.40为此添加了API).唯一的方法是收回旧的,并用更新的值声明新的.

In CLIPS 6.30 it's not possible to change a fact once asserted (6.40 added APIs for that). Only way to do so is by retracting the old one and asserting a new one with updated values.

def modify_fact(fact):
    """Modify a template fact."""
    fact.retract()

    new_fact = fact.template.new_fact()
    new_fact.update(dict(fact))  # copy over old fact slot values

    new_fact["s_1"] = clips.Symbol("v_2") 

    new_fact.assertit()

CLIPS提供了与modify命令完全相同的命令:撤回事实并使用新值对其进行断言.但是,不能通过environment.eval来使用它,因为事实索引不能通过API来使用.如果要修改规则中的事实,***直接使用modify命令.

CLIPS provides the modify command which does the exact same: retracts the fact and asserts it with the new value. Nevertheless, it cannot be used via environment.eval as fact indexes cannot be used via the API. If you want to modify a fact within a rule, you'd better use the modify command directly.

(defrule rule_1
  ?p <- (t (s_1 ?v))
  =>
  (modify ?p (s_1 v_2)))