IF Statememnt

Frankie
2010-07-01
2012-11-23
  • Frankie
    Frankie
    2010-07-01

    Hi all. Is there a function to   terminate other IF conditions after 1 has been fullilled. Cas even though i get the desired output for a IF condition . IT goes on to process other IF functions in my program.

    See code below---->

    (deftemplate rule
       (multislot if)
       (multislot then))

    ;;;**************************
    ;;;* INFERENCE ENGINE RULES *
    ;;;**************************

    (defrule propagate-goal ""
       (goal is ?goal)
       (rule (if ?variable $?)
             (then ?goal ? ?value))
       =>
       (assert (goal is ?variable)))

    (defrule goal-satified ""
       (declare (salience 30))
       ?f <- (goal is ?goal)
       (variable ?goal ?value)
       (answer ? ?text ?goal)
       =>
       (retract ?f)
       (format t "%s%s%n" ?text ?value))

    (defrule remove-rule-no-match ""
       (declare (salience 20))
       (variable ?variable ?value)
       ?f <- (rule (if ?variable ? ~?value $?))
       =>
       (retract ?f))

    (defrule modify-rule-match ""
       (declare (salience 20))
       (variable ?variable ?value)
       ?f <- (rule (if ?variable ? ?value and $?rest))
       =>
       (modify ?f (if ?rest)))

    (defrule rule-satisfied ""
       (declare (salience 20))
       (variable ?variable ?value)
       ?f <- (rule (if ?variable ? ?value)
                   (then ?goal ? ?goal-value))
       =>
       (retract ?f)
       (assert (variable ?goal ?goal-value)))

    (defrule ask-question-no-legalvalues ""
       (declare (salience 10))
       (not (legalanswers $?))
       ?f1 <- (goal is ?variable)
       ?f2 <- (question ?variable ? ?text)
       =>
       (retract ?f1 ?f2)
       (format t "%s " ?text)
       (assert (variable ?variable (read))))

    (defrule ask-question-legalvalues ""
       (declare (salience 10))
       (legalanswers ? $?answers)
       ?f1 <- (goal is ?variable)
       ?f2 <- (question ?variable ? ?text)
       =>
       (retract ?f1)
       (format t "%s " ?text)
       (printout t ?answers " ")
       (bind ?reply (read))
       (if (member (lowcase ?reply) ?answers)
         then (assert (variable ?variable ?reply))
              (retract ?f2)
         else (assert (goal is ?variable))))

    ;;;***************************
    ;;;* DEFFACTS KNOWLEDGE BASE *
    ;;;***************************

    (deffacts knowledge-base
       (goal is type.risk)
       (legalanswers are yes no)
    (rule (if male is yes)
             (then sex is male))
    (rule (if male is no)
             (then sex is female))
    (question male is "Is the candidate male?")

    ;(legalanswers are 1 2 3 4)
    (rule (if sex is male and
              youth.group is yes )
             (then group is youth))
       (rule (if sex is male and
              youth.group is no)
             (then group is young/mid/senior))
       (question youth.group is "Is the candidate age between 17-24 inclusive?")

    (rule (if sex is male and
              young.group is yes and group is young/mid/senior)
             (then group.2 is young))
       (rule (if sex is male and
              young.group is no)
             (then group is mid/senior))
       (question young.group is "Is the candidate age between 25-34 inclusive?")

    (rule (if sex is male and
              mid.group is yes and group is mid/senior)
             (then group is mid))
       (rule (if sex is male and
              mid.group is no and group is mid/senior)
             (then group is senior))
       (question mid.group is "Is the candidate age between 35-54 inclusive?")

    (rule (if sex is male and group is youth and acciedent.level is yes )
             (then type.risk is high.risk.10%))
       (question acciedent.level is "Multiple accidents?")
       (answer is "This is a " type.risk)

    (rule (if sex is male and group is mid and acciedent.level is yes )
             (then type.risk is high.risk.10%))
       (question acciedent.level is "Multiple accidents?")
       (answer is "This is a " type.risk))

     
  • Gary Riley
    Gary Riley
    2010-07-04

    Take a look at the CLIPS wine example: http://clipsrules.svn.sourceforge.net/viewvc/clipsrules/examples/wine.clp?revision=75&view=markup.

    The questions in that example support the use of a precursor. For example:

    (question
       (attribute has-turkey)
       (precursors main-component is poultry)
       (the-question "Does the meal have turkey in it? ")
       (valid-answers yes no unknown))

    In this case, the "has turkey" question will only be asked if it's determined that the meal is poultry.

    In your case you could make the precursor to the mid.group question to be "young.group is no".