我在CLIPS中有两个规则,如果它们都是真的我想要合并......但不确定如何做到这一点。我有一个名为grant-eligible
的属性....我在想是否将其设置为TRUE
然后我可以阅读下一条规则,然后将'grant-eligible'
设置为{{1 ....但是,当我这样做时,似乎我的代码进入了无限循环...
所以这是我的规则:
FALSE
这是我现在尝试实施的那些
(defrule complete "rule for app completeness"
?f <- (application (transcript-received Yes) (app-complete FALSE)
(gpa
?v_gpa&:(
> ?v_gpa 0)))
=>
(modify ?f (app-complete TRUE)))
(defrule denied "rule for admission - DENIED"
?f <- (application (app-complete TRUE) (app-decision FALSE)
(gpa
?v_gpa&:(
< ?v_gpa 3.0))
(ssat
?v_ssat&:(
>= ?v_ssat 0.0))
)
=>
(modify ?f (app-decision DENIED))
)
(defrule accepted "rule for admission - ACCEPTED"
?f <- (application (app-complete TRUE) (app-decision FALSE)
(gpa
?v_gpa&:(
>= ?v_gpa 3.5))
(ssat
?v_ssat&:(
>= ?v_ssat 1500))
)
=>
(modify ?f (app-decision ACCEPTED))
)
如果这两条规则都属实,则授予的奖励应为9500,或者可能为5000或者可能为4500 ......任何想法?
解决方案:(其中(defrule female-finaid "rule for finaid applications for female students"
?f <- (application (app-decision ACCEPTED)
(gender F) (grade-entry Freshman) (country USA)
(grant-eligible TRUE)
(grant ?v_grant)
)
=>
(modify ?f
(grant (+ ?v_grant 5000))
(grant-eligible TRUE)
)
)
(defrule great-students-finaid "rule for finaid applications for female students"
?f <- (application (app-decision ACCEPTED)
(country USA)
(grant-eligible TRUE)
(grant ?v_grant)
(gpa
?v_gpa&:(
>= ?v_gpa 4.0))
)
=>
(modify ?f
(grant (+ ?v_grant 4500))
(grant-eligible FALSE)
)
)
和ff-grant-eligible
是我的控制事实......他们代表ff =女性finaid,es =优秀学生)
es-grant-eligible
答案 0 :(得分:2)
您可以通过多种方式控制程序的执行(例如,控制事实,突出性,模块)。这个答案将使用控制事实(有突出性)来处理应用程序的各个阶段。我还假设您有一个与每个应用程序关联的唯一id
插槽。
考虑以下事实和规则:
(deffacts application-stages "ordered sequence of stages for an application"
(stages app-received app-accept-reject app-evaluate-grants
app-apply-grants app-complete))
(defrule go-to-next-stage "Advances to the next application stage"
?stage <- (app-stage ?id ?current-stage)
(stages $? ?current-stage ?next-stage $?)
=>
(retract ?stage)
(assert (app-stage ?id ?next-stage))
(printout t "Application " ?id " moved from stage " ?current-stage
" to " ?next-stage "." crlf))
application-stages
deffact定义应用程序的阶段序列,go-to-next-stage
规则推进应用程序阶段。由于规则的显着性低于默认值(0),因此只有在没有与当前阶段对应的其他规则时才会执行该规则。因此,如果您的计划中没有其他规则,您将获得以下内容:
CLIPS> (reset)
CLIPS> (assert (app-stage app-001 app-received))
<Fact-2>
CLIPS> (run)
Application app-001 moved from stage app-received to app-accept-reject.
Application app-001 moved from stage app-accept-reject to app-evaluate-grants.
Application app-001 moved from stage app-evaluate-grants to app-apply-grants.
Application app-001 moved from stage app-apply-grants to app-complete.
CLIPS>
但是如果您有与特定阶段相关的任何规则,它们将首先执行。因此,您可以将规则添加到app-evaluate-grants
阶段,如下所示:
(defrule female-finaid "rule for finaid applications for female students"
(app-stage ?id app-evaluate-grants)
(application (app-decision ACCEPTED) (id ?id)
(gender F) (grade-entry Freshman) (country USA)
=>
(assert (grant ?id female 5000)))
您同样会添加great-student-finaid
规则。然后,app-apply-grants
阶段有一条规则:
(defrule apply-grant "Adds the amount of a grant to an application"
(app-stage ?id app-apply-grants)
?grant <- (grant ?id ? ?amount)
?app <- (application (id ?id) (grant ?v_grant))
=>
(retract ?grant)
(modify (?app (grant (+ ?v_grant ?amount))))
以这种方式对其进行建模的一个好处是,您不必在应用程序的数据中包含控制事实(例如grant-eligible
)。也就是说,您的控制逻辑与数据模型是分开的。请注意,通过使用CLIPS模块(通过defmodule
),您可以获得与我在此处所做的相同的效果,这通常是可取的,但它需要更长的答案(并且这个已经相当长)。 / p>