deffunctions和defulsles CLIPS之间的相关性

时间:2018-03-29 21:47:47

标签: clips

(deftemplate client
    (slot idClient (type INTEGER))
    (multislot nome)
    (multislot birthdate)
    (multislot registryCard)
    (multislot endOfRegistryCard))

(deffunction reservation(?idfunc)   
    (build (str-cat"(defrule existsClient
    (exists(client(idClient ?idfunc)))
    =>
    (printout t "exists" ?idfunc crlf))"
))

    (run)   
)

我做了这个deffunction并且我想看看是否存在具有作为参数接收的idfunc的客户端。会发生什么事情是内部的默认并没有处理这个变量的任何想法如何解决?

2 个答案:

答案 0 :(得分:0)

我认为你应该设置条件(断言你的idfunc),然后激活你的" existsClient"规则(定义为规则而不是在您的函数内)。在我看来,这将是一个更清晰的设计。

答案 1 :(得分:0)

通常你会直接定义你的规则而不是使用deffunction,但是这里你可以用两种方式来做:

CLIPS> (clear) ; Create rule and run with deffunction
CLIPS> 
(deftemplate client
   (slot idClient (type INTEGER))
   (multislot nome)
   (multislot birthdate)
   (multislot registryCard)
   (multislot endOfRegistryCard))
CLIPS> 
(deffunction reservation (?idfunc)   
   (build (str-cat 
      "(defrule existsClient
          (exists (client (idClient " ?idfunc ")))
          =>
          (printout t \"exists " ?idfunc "\" crlf))"))
   (assert (client (idClient ?idfunc)))
   (run))
CLIPS> (reservation 2)
exists 2
CLIPS> (ppdefrule existsClient)
(defrule MAIN::existsClient
   (exists
        (client (idClient 2)))
   =>
   (printout t "exists 2" crlf))
CLIPS> (clear) ; Create rule directly
CLIPS> 
(deftemplate client
   (slot idClient (type INTEGER))
   (multislot nome)
   (multislot birthdate)
   (multislot registryCard)
   (multislot endOfRegistryCard))
CLIPS> 
(defrule existsClient
   (exists (client (idClient 2)))
   =>
   (printout t "exists 2" crlf))
CLIPS> (ppdefrule existsClient)
(defrule MAIN::existsClient
   (exists
        (client (idClient 2)))
   =>
   (printout t "exists 2" crlf))
CLIPS> (assert (client (idClient 2)))
<Fact-1>
CLIPS> (run)
exists 2
CLIPS>