Search code examples
jessfact

How to modify a Jess multislot fact in working memory


I have some rules in my Jess code which i want to modify two facts in the working memory.

The facts which have been asserted are: (assert (analysis (reasons $?c) (total ?t))))

reasons $?c is a multislot and I want to add to this multislot if needed in rules.

For example: If a user drinks too much alcohol, I want the text "You are drinking to much alcohol which is unsafe." added as a field to the multislot (reasons $?c). How would I acheive this task. I have done a lot of research and tried several methods but they are not working correctly.


Solution

  • A little precaution should be made so that the rule doesn't loop:

    (defrule match
    ;;  (declare (no-loop true))
      ?t <- (Thing (what ?x))
      ?b <- (Box (id ?id)(things $?things&:(not (member$ ?x $?things)) ))
    =>
      (printout t ?id " not contains " ?x crlf)
      (modify ?b (things (list $?things ?x)))
    )
    

    Either you use the no-loop clause or, what is usually considered as the more astute approach, you use a constraint that ensures that the item the rule might add isn't already in the list; especially when a specific "reason" could be added by more than one rule.