> <<JessMultiThreadAssert.java>> <<JessMultipleAssertRun.clp>>
> Dear all,
> I am working with Jess under multiple threads environment. More than
> two threads will assert facts and run the engine at the same time. The
> system works well with rules without no-loop declaration. But if a
> rule contains no-loop declaration, sometimes it can not activate and
> fire correctly with several facts.
> I write two files to show what I found. I both attach them and paste
> their content in the mail body. The java file open two threads,
> EvenThread assert facts with value 2,4,6,8,10; OddThread assert facts
> with value 1,3,5,7,9. In my clp file, only one rule written, so it
> should fire 10 times for the 10 facts. But actually, it may fire 8, 9
> or 10 times. If I remove the no-loop declaration (in my sample it is
> not required, I only use it to explain my issue), the rule exactly
> fire 10 times. Why?
> My jess version is 7.0.
>
> Thank you
>
> // java file start. My java Class, enable two threads to assert facts
> and run engine
> public class JessMultiThreadAssert {
>
> public static void main(String[] args) {
>
> Rete rete = new Rete();
> OddThread odd = new OddThread(rete);
> EvenThread even = new EvenThread(rete);
>
> try{
> rete.batch("JessMultipleAssertRun.clp");
> }catch (Exception e){
> e.printStackTrace();
> }
> even.start();
> odd.start();
> }
>
> }
> /**
> * assert facts with even number, from 2 to 10
> * */
> class EvenThread extends Thread
> {
> private Rete rete = null;
> public EvenThread(Rete rete){
> this.rete = rete;
> }
> public void run(){
> try{
> long i=2;
> while(i<=10){
> rete.eval("(assert (item1 (value
> "+i+")))");
> rete.run();
> i+=2;
> }
> }catch (Exception e){
> e.printStackTrace();
> }
> }
> }
> /**
> * assert facts with odd number, from 1 to 9
> * */
> class OddThread extends Thread
> {
> private Rete rete = null;
> public OddThread(Rete rete){
> this.rete = rete;
> }
> public void run(){
> try{
> long i=1;
> while(i<=10){
> rete.eval("(assert (item1 (value
> "+i+")))");
> rete.run();
> i+=2;
> }
> }catch (Exception e){
> e.printStackTrace();
> }
> }
> }
> // java file end
>
>
> ;;;;;;;;;;; clp file start, my JessMultipleAssertRun.clp file
> (defglobal ?*num* = 0)
>
> (deftemplate item1 (slot value (type Long)))
>
> (defrule many-matches
> (declare (no-loop TRUE) )
> ?fact1<-(item1 (value ?a&:(> ?a 0) ))
> =>
> (modify ?fact1 (value -1))
> (bind ?*num* (+ ?*num* 1))
> (printout t ?*num* crlf)
> )
>
> (reset)
>
> ;;;;;;;;;; clp file end
>
>
>
JessMultiThreadAssert.java
Description: JessMultiThreadAssert.java
JessMultipleAssertRun.clp
Description: JessMultipleAssertRun.clp
