Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

else part of defrule

Help
rajiv
2012-01-05
2012-11-23
  • rajiv
    rajiv
    2012-01-05

    hi folks,

    I know it can be achieved making two rules, one which is satisfying conditions and the other just reverse of it
    but i would like to know is there is easier way to have same functionality

    example of having two rules to achieve else part:

    (defrule ifpart
    (value1 ?first)
    (value2 ?second)
    (test (> ?first ?second) => (printout  t " first is bigger") )

    (defrule elsepart
    (value1 ?first)
    (value2 ?second)
    (test (< ?first ?second) => (printout  t " second is bigger") )

    actually if rule is simple then it is little easy to write two rules, but when it becomes bigger then flooding the rule file is little trouble, so please let me know if i can….

     
  • Gary Riley
    Gary Riley
    2012-01-09

    In general, there's no else condition for a rule. In a simple case such as this, you can move the expression into the then portion of the rule, but in more complex cases you need a second rule:

    (defrule ifpart
    (value1 ?first)
    (value2 ?second)
    =>
    (if  (> ?first ?second) 
       then (printout  t " first is bigger") 
       else (if (> ?second ?first) then (printout  t " second is bigger"))))