簡體   English   中英

如何將這兩個規則與CLIPS結合?

[英]how to combine these two rules with CLIPS?

我在CLIPS中有兩個規則,如果它們都是真的,我想將它們組合在一起……雖然不確定如何去做。 我有一個稱為grant-eligible的屬性....我在想如果將其設置為TRUE那么我可以閱讀下一條規則,然后將'grant-eligible'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))

  )

這就是我現在要實施的

(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)
        )
    )

如果這兩個規則都成立,那么授予的補助金應該是9500,或者是5000,或者是4500...。

解決方案:(其中ff-grant-eligible grant- ff-grant-eligiblees-grant-eligible grant- es-grant-eligible是我的控制權...它們代表ff =女最終成績,es =優秀學生)

    (defrule female-finaid "rule for finaid applications for female students"
  ?f <- (application (app-decision ACCEPTED) (ff-grant-eligible TRUE)
    (gender F) (grade-entry Freshman) (country USA)

    (grant ?v_grant)
    )

  =>
  (modify ?f
            (grant (+ ?v_grant 5000))
            (ff-grant-eligible FALSE)
        )
    )

    (defrule great-students-finaid "rule for finaid applications for female students"
  ?f <- (application (app-decision ACCEPTED) (es-grant-eligible TRUE)
    (country USA)

    (grant ?v_grant)
    (gpa
                ?v_gpa&:(
                    >= ?v_gpa 4.0))
    )

  =>
  (modify ?f
            (grant (+ ?v_grant 4500))
            (es-grant-eligible FALSE)
        )
    )

您可以通過多種方式來控制程序的執行(例如,控制事實,顯着性,模塊)。 該答案將在應用程序處理階段使用控制事實(具有顯着性)。 我還將假設您具有與每個應用程序關聯的唯一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定義定義了application-stages順序,而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 ),您可以達到與我在這里所做的相同的效果,通常這是更可取的,但它需要更長的答案(並且這個答案已經相當長了)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM