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 时也可以,那么我该如何解决这个问题?

4

1 回答 1

0

首先,您必须小心程序中缺少的空格。需要 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())
于 2014-03-19T19:09:30.133 回答