Clips.BuildRule 中出现错误

发布于 2024-12-05 03:40:31 字数 2611 浏览 0 评论 0原文

我正在使用 pyClips (这是嵌入在 python 中的 Clips)来构建知识 根据。我想通过调用函数向知识库文件添加新规则 实现此函数中显示的规则:

def normalRule(clips, i, path):
    rule_name = 'rule_' + str(i)
    rule_prec = '(phase ?phase)'
    rule_prec += '?address1 <-(Organ (name ?organ))'
    rule_prec += '?address2 <-(Surgical-Action (action ?action))'
    rule_prec += '?address3 <-(Medical-Devices (tool-name ?tool))'
    rule_body = '(if(=(str-compare ?*pickupEvent* "no")0) then'
    rule_body += '(bind ?predicted-action ?action)'
    rule_body += '(bind ?*current-action* ?predicted-action)'
    rule_body += '(bind ?ex-tool ?tool)'
    rule_body += 'else'
    rule_body += '(bind ?actual-action ?action)'
    rule_body += '(bind ?s-tool ?tool)'
    rule_body += '(retract ?address1 ?address2 ?address3)'
    rule_body += '(if (and (=(str-compare ?predicted-action ?actual-action)0)\
    (=(str-compare ?ex-tool ?s-tool) 0)) then'
    rule_body += '(updateAction ?*current-action* ?actual-action)'
    rule_body += 'else'
    rule_body += '( if (not(=(str-compare ?predicted-action ?actual-action)0)) then'
    rule_body += '(updateAction ?*current-action* ?actual-action)'
    rule_body += '(assert (You have mistake: incorrect action))'
    rule_body += '(bind ?*number-of-mistakes* (+ ?*number-of-mistakes* 1)))'
    rule_body += '(if (not(=(str-compare ?ex-tool ?s-tool) 0)) then'
    rule_body += '(updateAction ?*current-action* ?actual-action)'
    rule_body += '(assert (You have mistake: incorrect surgical device))'
    rule_body += '(bind ?*number-of-mistakes* (+ ?*number-of-mistakes* 1))'
    rule_body += ')))'

    rule = clips.BuildRule(rule_name, rule_prec, rule_body)

当我使用 Clips 应用它时,此函数内的规则工作正常,但现在我想 每次调用该函数时都会构建一个新规则。

但是当我调用这个函数时,我得到了一个错误:

Traceback (most recent call last):
File "C:\Users\DELL\My Documents\Aptana Studio 3 Workspace\
    Interface\src\ruleModel\ruleTypes.py", line 305,
    in <module> normalRule(clips, 1, "C:\Users\DELL\Desktop\bjareb\tt.py")          
File "C:\Users\DELL\My Documents\Aptana Studio 3 Workspace\
    Interface\src\ruleModel\ruleTypes.py", line 39,
    in normalRule rule = clips.BuildRule(rule_name, rule_prec, rule_body)
File "C:\Python26\lib\clips\_clips_wrap.py", line 149, in _WRAPPER return f(*args)
File "C:\Python26\lib\clips\_clips_wrap.py", line 238, in _WRAPPER return f(*newargs)
File "C:\Python26\lib\clips\_clips_wrap.py", line 3317, in BuildRule _c.build(construct)
    _clips.ClipsError: C08: syntax error, or unable to parse expression

所以请,我修改了很多次,规则没有任何问题 构造,当我使用 Clips 时它也有效,那么我该如何解决这个问题?

I am using pyClips (which is Clips embedded in python) for building a knowledge
base. I want to add a new rule to a knowledge base file by calling a function
that implements the rule shown in this function:

def normalRule(clips, i, path):
    rule_name = 'rule_' + str(i)
    rule_prec = '(phase ?phase)'
    rule_prec += '?address1 <-(Organ (name ?organ))'
    rule_prec += '?address2 <-(Surgical-Action (action ?action))'
    rule_prec += '?address3 <-(Medical-Devices (tool-name ?tool))'
    rule_body = '(if(=(str-compare ?*pickupEvent* "no")0) then'
    rule_body += '(bind ?predicted-action ?action)'
    rule_body += '(bind ?*current-action* ?predicted-action)'
    rule_body += '(bind ?ex-tool ?tool)'
    rule_body += 'else'
    rule_body += '(bind ?actual-action ?action)'
    rule_body += '(bind ?s-tool ?tool)'
    rule_body += '(retract ?address1 ?address2 ?address3)'
    rule_body += '(if (and (=(str-compare ?predicted-action ?actual-action)0)\
    (=(str-compare ?ex-tool ?s-tool) 0)) then'
    rule_body += '(updateAction ?*current-action* ?actual-action)'
    rule_body += 'else'
    rule_body += '( if (not(=(str-compare ?predicted-action ?actual-action)0)) then'
    rule_body += '(updateAction ?*current-action* ?actual-action)'
    rule_body += '(assert (You have mistake: incorrect action))'
    rule_body += '(bind ?*number-of-mistakes* (+ ?*number-of-mistakes* 1)))'
    rule_body += '(if (not(=(str-compare ?ex-tool ?s-tool) 0)) then'
    rule_body += '(updateAction ?*current-action* ?actual-action)'
    rule_body += '(assert (You have mistake: incorrect surgical device))'
    rule_body += '(bind ?*number-of-mistakes* (+ ?*number-of-mistakes* 1))'
    rule_body += ')))'

    rule = clips.BuildRule(rule_name, rule_prec, rule_body)

The rule inside this function works fine when i apply it using Clips, but now I want to
build a new rule every time the function is being called.

But when I called this function, I got an error:

Traceback (most recent call last):
File "C:\Users\DELL\My Documents\Aptana Studio 3 Workspace\
    Interface\src\ruleModel\ruleTypes.py", line 305,
    in <module> normalRule(clips, 1, "C:\Users\DELL\Desktop\bjareb\tt.py")          
File "C:\Users\DELL\My Documents\Aptana Studio 3 Workspace\
    Interface\src\ruleModel\ruleTypes.py", line 39,
    in normalRule rule = clips.BuildRule(rule_name, rule_prec, rule_body)
File "C:\Python26\lib\clips\_clips_wrap.py", line 149, in _WRAPPER return f(*args)
File "C:\Python26\lib\clips\_clips_wrap.py", line 238, in _WRAPPER return f(*newargs)
File "C:\Python26\lib\clips\_clips_wrap.py", line 3317, in BuildRule _c.build(construct)
    _clips.ClipsError: C08: syntax error, or unable to parse expression

So please, I revised it many times and there is nothing wrong in the rule
construction, also it works when I use Clips, so how do I solve this?

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

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

发布评论

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

评论(1

梦过后 2024-12-12 03:40:31

首先,您必须小心程序中缺少的空格。需要 Eoln 以及比较语句的空格。因此,您的程序应如下所示:

rule_prec = '(phase ?phase)\n'
rule_prec += '?address1 <-(Organ (name ?organ))\n'
rule_prec += '?address2 <-(Surgical-Action (action ?action))\n'
rule_prec += '?address3 <-(Medical-Devices (tool-name ?tool))\n'
rule_body = '(if(=(str-compare ?*pickupEvent* "no") 0) then\n'
rule_body += '(bind ?predicted-action ?action)\n'
rule_body += '(bind ?*current-action* ?predicted-action)\n'
rule_body += '(bind ?ex-tool ?tool)\n'
rule_body += 'else\n'
rule_body += '(bind ?actual-action ?action)\n'
rule_body += '(bind ?s-tool ?tool)\n'
rule_body += '(retract ?address1 ?address2 ?address3)\n'
rule_body += '(if (and (= (str-compare ?predicted-action ?actual-action) 0) \
(=(str-compare ?ex-tool ?s-tool) 0)) then\n'
rule_body += '(updateAction ?*current-action* ?actual-action)\n'
rule_body += 'else\n'
rule_body += '( if (not (= (str-compare ?predicted-action ?actual-action) 0)) then\n'
rule_body += '(updateAction ?*current-action* ?actual-action)\n'
rule_body += '(assert (You have mistake: incorrect action))\n'
rule_body += '(bind ?*number-of-mistakes* (+ ?*number-of-mistakes* 1)))\n'
rule_body += '(if (not (= (str-compare ?ex-tool ?s-tool) 0)) then\n'
rule_body += '(updateAction ?*current-action* ?actual-action)\n'
rule_body += '(assert (You have mistake: incorrect surgical device))\n'
rule_body += '(bind ?*number-of-mistakes* (+ ?*number-of-mistakes* 1))\n'
rule_body += ')))\n'

其次,如果您正在使用 PyClips 并且对 CLIPS 感到满意,请使用以下行以获得您的规则的真正 CLIPS 错误消息。我尝试解决您的问题,但您的规则模式和操作部分存在几个问题。

import logging.config
engine = clips.Environment()
try:
rule_name = 'rule_1'
rule_prec = '(phase ?phase)\n'
rule_prec += '?address1 <-(Organ (name ?organ))\n'
rule_prec += '?address2 <-(Surgical-Action (action ?action))\n'
rule_prec += '?address3 <-(Medical-Devices (tool-name ?tool))\n'
rule_body = '(if(=(str-compare ?*pickupEvent* "no") 0) then\n'
rule_body += '(bind ?predicted-action ?action)\n'
rule_body += '(bind ?*current-action* ?predicted-action)\n'
rule_body += '(bind ?ex-tool ?tool)\n'
rule_body += 'else\n'
rule_body += '(bind ?actual-action ?action)\n'
rule_body += '(bind ?s-tool ?tool)\n'
rule_body += '(retract ?address1 ?address2 ?address3)\n'
rule_body += '(if (and (= (str-compare ?predicted-action ?actual-action) 0) \
(=(str-compare ?ex-tool ?s-tool) 0)) then\n'
rule_body += '(updateAction ?*current-action* ?actual-action)\n'
rule_body += 'else\n'
rule_body += '( if (not (= (str-compare ?predicted-action ?actual-action) 0)) then\n'
rule_body += '(updateAction ?*current-action* ?actual-action)\n'
rule_body += '(assert (You have mistake: incorrect action))\n'
rule_body += '(bind ?*number-of-mistakes* (+ ?*number-of-mistakes* 1)))\n'
rule_body += '(if (not (= (str-compare ?ex-tool ?s-tool) 0)) then\n'
rule_body += '(updateAction ?*current-action* ?actual-action)\n'
rule_body += '(assert (You have mistake: incorrect surgical device))\n'
rule_body += '(bind ?*number-of-mistakes* (+ ?*number-of-mistakes* 1))\n'
rule_body += ')))\n'

rule=clips.BuildRule(rule_name, rule_prec, rule_body,"test rule")
    except clips.ClipsError:
logging.error(clips.ErrorStream.Read())

First of all you have to be careful with whitespaces that you are missing in your program. Eoln's are needed as well as spaces for your comparison statements. So, your program should look like this:

rule_prec = '(phase ?phase)\n'
rule_prec += '?address1 <-(Organ (name ?organ))\n'
rule_prec += '?address2 <-(Surgical-Action (action ?action))\n'
rule_prec += '?address3 <-(Medical-Devices (tool-name ?tool))\n'
rule_body = '(if(=(str-compare ?*pickupEvent* "no") 0) then\n'
rule_body += '(bind ?predicted-action ?action)\n'
rule_body += '(bind ?*current-action* ?predicted-action)\n'
rule_body += '(bind ?ex-tool ?tool)\n'
rule_body += 'else\n'
rule_body += '(bind ?actual-action ?action)\n'
rule_body += '(bind ?s-tool ?tool)\n'
rule_body += '(retract ?address1 ?address2 ?address3)\n'
rule_body += '(if (and (= (str-compare ?predicted-action ?actual-action) 0) \
(=(str-compare ?ex-tool ?s-tool) 0)) then\n'
rule_body += '(updateAction ?*current-action* ?actual-action)\n'
rule_body += 'else\n'
rule_body += '( if (not (= (str-compare ?predicted-action ?actual-action) 0)) then\n'
rule_body += '(updateAction ?*current-action* ?actual-action)\n'
rule_body += '(assert (You have mistake: incorrect action))\n'
rule_body += '(bind ?*number-of-mistakes* (+ ?*number-of-mistakes* 1)))\n'
rule_body += '(if (not (= (str-compare ?ex-tool ?s-tool) 0)) then\n'
rule_body += '(updateAction ?*current-action* ?actual-action)\n'
rule_body += '(assert (You have mistake: incorrect surgical device))\n'
rule_body += '(bind ?*number-of-mistakes* (+ ?*number-of-mistakes* 1))\n'
rule_body += ')))\n'

Second, if you are using PyClips and feel comfortable with CLIPS, use the following lines in order to get the real CLIPS error message for your rule. I tried solving your problem but there are several problems in your rule's pattern and action parts.

import logging.config
engine = clips.Environment()
try:
rule_name = 'rule_1'
rule_prec = '(phase ?phase)\n'
rule_prec += '?address1 <-(Organ (name ?organ))\n'
rule_prec += '?address2 <-(Surgical-Action (action ?action))\n'
rule_prec += '?address3 <-(Medical-Devices (tool-name ?tool))\n'
rule_body = '(if(=(str-compare ?*pickupEvent* "no") 0) then\n'
rule_body += '(bind ?predicted-action ?action)\n'
rule_body += '(bind ?*current-action* ?predicted-action)\n'
rule_body += '(bind ?ex-tool ?tool)\n'
rule_body += 'else\n'
rule_body += '(bind ?actual-action ?action)\n'
rule_body += '(bind ?s-tool ?tool)\n'
rule_body += '(retract ?address1 ?address2 ?address3)\n'
rule_body += '(if (and (= (str-compare ?predicted-action ?actual-action) 0) \
(=(str-compare ?ex-tool ?s-tool) 0)) then\n'
rule_body += '(updateAction ?*current-action* ?actual-action)\n'
rule_body += 'else\n'
rule_body += '( if (not (= (str-compare ?predicted-action ?actual-action) 0)) then\n'
rule_body += '(updateAction ?*current-action* ?actual-action)\n'
rule_body += '(assert (You have mistake: incorrect action))\n'
rule_body += '(bind ?*number-of-mistakes* (+ ?*number-of-mistakes* 1)))\n'
rule_body += '(if (not (= (str-compare ?ex-tool ?s-tool) 0)) then\n'
rule_body += '(updateAction ?*current-action* ?actual-action)\n'
rule_body += '(assert (You have mistake: incorrect surgical device))\n'
rule_body += '(bind ?*number-of-mistakes* (+ ?*number-of-mistakes* 1))\n'
rule_body += ')))\n'

rule=clips.BuildRule(rule_name, rule_prec, rule_body,"test rule")
    except clips.ClipsError:
logging.error(clips.ErrorStream.Read())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文