Perhaps this will help?  Basically, this Userfunction allows you to
execute any arbitrary Jess function (whether it's a user function or
not) and evaluates it with the configured Executor.  You can control
how this is done by configuring the right executor.  For example,
configuring the instance of the user function below with:

BackgroundProcessor  bp = new BackgroundProcessor();
bp.setExecutor(java.util.concurrent.Executors.newFixedThreadPool(5));

will give you 5 background threads to use.   You can use this in Jess
like so:


(evaluate-in-background (some-function anArg anotherArg))

the function "some-function" will be evaluated asynchronously.

import java.util.concurrent.Executor;

import jess.*;

/**
 * @author Hal Hildebrand
 * Evaluates the passed Jess function call via an executor
 */
public class BackgroundProcessor implements Userfunction {
    protected Executor executor;

    public void setExecutor(Executor executor) {
        this.executor = executor;
    }

    /* (non-Javadoc)
     * @see jess.Userfunction#call(jess.ValueVector, jess.Context)
     */
    public Value call(ValueVector vv, final Context context) throws
JessException {
        final Funcall task = vv.get(1).funcallValue(context);
        executor.execute(new Runnable() {
            public void run() {
                try {
                    task.execute(context);
                } catch (JessException e) {
                    // Do your own error handling here ;)
                    e.printStackTrace();
                }
            }
        });
        return Funcall.NIL;
    }

    /* (non-Javadoc)
     * @see jess.Userfunction#getName()
     */
    public String getName() {
        return "evaluate-in-background";
    }
}

On Feb 17, 2009, at 12:52 PM, Matt Hutchinson wrote:

OK, thanks. However I am using a Userfunction - which takes a couple
of parameters. How do I start the new thread for it and pass it the
parameters?

The only way to do this was with constructor parameters, but the
Userfunction uses command line parameters.

Thanks
MH



On Thu, Feb 12, 2009 at 12:51 PM, Ernest Friedman-Hill <ejfr...@sandia.gov
> wrote:
Rules are fired sequentially, on the thread where you call "run". If
you want asynchronous execution, you could create a thread and run
your code off in its own thread. CHek out the "implement" function
in the "Jess Function List" chapter of the manual for an example of
this.




On Feb 12, 2009, at 11:20 AM, Matt Hutchinson wrote:

Hi everyone,

I did search on this but could not find an answer.

When multiple rules fire in JESS, it is JESS who decides the order
in which they fire - no worries. Although these fire in some order,
does the RHS of the first have to finish before the RHS of the next
rule is executed, and so on?

I was hoping that if several rules fire, their RHSs are all executed
one they fire, without any waiting (somewhat in parallel).


Thanks,
MH



---------------------------------------------------------
Ernest Friedman-Hill
Informatics & Decision Sciences          Phone: (925) 294-2154
Sandia National Labs
PO Box 969, MS 9012                            ejfr...@sandia.gov
Livermore, CA 94550                             http://www.jessrules.com





--------------------------------------------------------------------
To unsubscribe, send the words 'unsubscribe jess-users
y...@address.com'
in the BODY of a message to majord...@sandia.gov, NOT to the list
(use your own address!) List problems? Notify owner-jess-us...@sandia.gov
.
--------------------------------------------------------------------



Reply via email to