You're right. A simple and accurate explanation of what's happening is that the current implementation of no-loop blocks all activations of a rule during the time that rule is firing. For a single-threaded system, this is correct, but for a multi-threaded system it's surprising, to say the least.

Thanks for pointing this out; it should be simple to give Jess 7.1 the expected behavior.


On Jan 17, 2008, at 6:19 AM, Chen, Yixi wrote:



<<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><JessMultipleAssertRun.clp>

---------------------------------------------------------
Ernest Friedman-Hill
Informatics & Decision Sciences          Phone: (925) 294-2154
Sandia National Labs                FAX:   (925) 294-2234
PO Box 969, MS 9012                 [EMAIL PROTECTED]
Livermore, CA 94550                 http://www.jessrules.com

--------------------------------------------------------------------
To unsubscribe, send the words 'unsubscribe jess-users [EMAIL PROTECTED]'
in the BODY of a message to [EMAIL PROTECTED], NOT to the list
(use your own address!) List problems? Notify [EMAIL PROTECTED]
--------------------------------------------------------------------

Reply via email to