Search code examples
clips

Check if a CLIPS RHS Match condition happened


Im using CLIPS environment to create rules like this one:

(defrule rule1
(ACTIVE)
(AGE_YEARS ?ay)
=>
(if (< ?ay 15) then
    (assert (Please do not enter))
    )
(if (> ?ay 18) then
    (assert (Go ahead and enter))
    )
)

If I send the facts (ACTIVE) and (AGE_YEARS 10) I can use matches command (matches rule1) to see that it was activated as both conditions were true but I was wondering if are there any type of "matches" command to check from the RHS part, which condition matched in case any matched at all... as I could have sent (AGE_YEARS 17), the rule will still be fired but no RHS condition will match.


Solution

  • The matches command shows the current state of the matched conditions. It doesn't show what will happen if the actions of the rule are executed. If you create multiple rules, you can move the conditions from the RHS part of the rule to the LHS so the matches command will then show you if no rules are matched.

    CLIPS> 
    (defrule rule1-lt-15
       (ACTIVE)
       (AGE_YEARS ?ay&:(< ?ay 15))
       =>
       (assert (Please do not enter)))
    CLIPS>    
    (defrule rule1-gt-18
       (ACTIVE)
       (AGE_YEARS ?ay&:(> ?ay 18))
       =>
       (assert (Go ahead and enter)))
    CLIPS> (assert (ACTIVE))
    <Fact-1>
    CLIPS> (assert (AGE_YEARS 17))
    <Fact-2>
    CLIPS> (agenda)
    CLIPS> (assert (AGE_YEARS 10))
    <Fact-3>
    CLIPS> (agenda)
    0      rule1-lt-15: f-1,f-3
    For a total of 1 activation.
    CLIPS> 
    

    Alternatively, you could use facts to represent the conditions currently used in the RHS and then place those in the LHS so that you retain a single rule.

    CLIPS> (clear)
    CLIPS> 
    (deftemplate rhsc
       (multislot condition)
       (multislot action))
    CLIPS> 
    (deffacts rhs-conditions
       (rhsc (condition < 15) (action Please do not enter))
       (rhsc (condition > 18) (action Go ahead and enter)))
    CLIPS>    
    (defrule rule1
       (ACTIVE)
       (AGE_YEARS ?ay)
       (rhsc (condition ?o ?v) (action $?action))
       (test (funcall ?o ?ay ?v))
       =>
       (assert (result ?action)))
    CLIPS> (reset)
    CLIPS> (assert (ACTIVE))
    <Fact-3>
    CLIPS> (assert (AGE_YEARS 10))
    <Fact-4>
    CLIPS> (agenda)
    0      rule1: f-3,f-4,f-1
    For a total of 1 activation.
    CLIPS> (run)
    CLIPS> (facts)
    f-1     (rhsc (condition < 15) (action Please do not enter))
    f-2     (rhsc (condition > 18) (action Go ahead and enter))
    f-3     (ACTIVE)
    f-4     (AGE_YEARS 10)
    f-5     (result Please do not enter)
    For a total of 5 facts.
    CLIPS> (assert (AGE_YEARS 17))
    <Fact-6>
    CLIPS> (agenda)
    CLIPS> (assert (AGE_YEARS 19))
    <Fact-7>
    CLIPS> (agenda)
    0      rule1: f-3,f-7,f-2
    For a total of 1 activation.
    CLIPS> (run)
    CLIPS> (facts)
    f-1     (rhsc (condition < 15) (action Please do not enter))
    f-2     (rhsc (condition > 18) (action Go ahead and enter))
    f-3     (ACTIVE)
    f-4     (AGE_YEARS 10)
    f-5     (result Please do not enter)
    f-6     (AGE_YEARS 17)
    f-7     (AGE_YEARS 19)
    f-8     (result Go ahead and enter)
    For a total of 8 facts.
    CLIPS>