I am having a serious trouble with the rule engine Jess. The problem that I am facing is with the Conditional Element Accumulate. I am trying to write a rule which gives me a list of words - according to some criteria - from my working memory but it shows me all the phases of the list - first an empty list then a list with one element and it goes on- which it does so by firing again and again the same rule. But I know and sure that the working memory does not change while this rule fires.
And also strangely I wrote the same rule hours ago and it was giving, as a result, only one list which has more than one elements.
Do you have any suggestions on what I am doing wrong?
Please help! Here is the code :
(defrule show-me
(declare (salience -11)
(no-loop TRUE))
?my-list <- (accumulate (bind ?list (new java.util.ArrayList)) ;initializer
(?list add ?sWord) ; action
?list ;result
(ourObject (sourceWord ?sWord) ; CE
{complementType == COMPLEMENTO-OGGETTO-PARTITIVO}))
=>
(printout t "complementType" (?my-list toString) crlf))
And this is the result that I am getting :
complementType[ un , po , di , acqua , tua ]
complementType[ un , po , acqua , tua ]
complementType[ un , acqua , tua ]
complementType[ acqua , tua ]
complementType[ acqua ]
complementType[]
And I need only this :
complementType[ un , po , di , acqua , tua ]
p.s. Sorry for the code appearance but it did not permit me to paste it as it is.
Most probably you are inserting ourObject facts while the engine is running. One way would be to call run in one thread (which blocks) and then insert facts in another thread. This will create activations immediately, which will then be executed in reverse order, since conflict resolution prefers newer activations over older ones.
If you execute either of these two insertion variants. the rule fires as expected, once.
;;(deffacts facts
;; (ourObject (sourceWord un)(type X))
;; (ourObject (sourceWord po)(type X))
;; (ourObject (sourceWord di)(type X))
;; (ourObject (sourceWord acqua)(type X)))
(reset)
(assert (ourObject (sourceWord un)(type X)))
(assert (ourObject (sourceWord po)(type X)))
(assert (ourObject (sourceWord di)(type X)))
(assert (ourObject (sourceWord acqua)(type X)))
(run)