1

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.

Kara
  • 6,115
  • 16
  • 50
  • 57
Zain
  • 15
  • 3

2 Answers2

1

Maybe not the best way, but it's simple:

(defrule modify-something
?f <- (analysis (reasons $?c) (total ?t))))
=>
(modify ?f (reasons (create$ ?c "hey"))))
rpax
  • 4,468
  • 7
  • 33
  • 57
1

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.

laune
  • 31,114
  • 3
  • 29
  • 42
  • I sucessfully used the member$ function in each rule to not cause looping which I earlier encountered. My multislot is now sucessfully being modified but I am working on a second task of printing out the contents of the multislot? How to print out content of the multislot 'reasons' one line at a time? – Zain Mar 15 '14 at 09:48
  • I have printed out the contents of my multislot ok now, but the contents is all joined together on output lines. Is there a method to printout values of the multislot on individual lines respectfully. – Zain Mar 15 '14 at 11:32
  • You'll have to write a little deffunction, executing a `(foreach ?el ?list ...)`. See the Jess manual for details. – laune Mar 15 '14 at 12:42