Re: JESS: How to use a Userfunction returning boolean in a LHS of a Jess rule

"Ernest Friedman-Hill" <[email protected]>
Newsgroups gmane.comp.java.jess
Message-ID <[email protected]>
Hi Ana,

First let me note that this isn't at all the way Jess is intended to  
work. It's supposed to be used to match data, not the results of  
calling functions. If you want to monitor the status of a function's  
return value over time, a simple Java loop would be far more convenient!

But assuming your actual intended application is more nuanced: in (1),  
the rule won't fire because pattern matching is always driven by  
working memory changes; if you write a rule like this one which  
matches no working memory elements, then Jess automatically makes it  
match "(initial-fact)", the fact that is asserted by the "reset"  
function, so you'll need to call that at some point. Also, rules only  
fire when you call "run". So your Java code might look like


	   engine.executeCommand("(load-project  file:/M:/MyJessTest/ 
osgi.myjesstest.test/simple-owl.pprj)");
            Batch.batch("myrules.clp", engine);
            engine.addUserfunction(new ReturnTrue());
            engine.reset();
	   engine.run();

(You might need to call "load-project" before the "reset()", and it  
would make sense to define the function before loading the rules,  
although that's not required.)

Now, for question (2), the only way to re-evaluate the function is to  
force re-evaluation of the (initial-fact) pattern. Calling  "reset"  
would do that, but it would also empty out working memory. In a real  
rule, you might have some real patterns, and re-evaluating one of  
those would do the trick -- modifying a fact that was matched on the  
rule's LHS before the function call.

PropertyChangeEvents have nothing directly to do with your question --  
that's a mechanism for telling Jess that a working memory element  
(i.e., a fact) has changed.


On Jul 28, 2010, at 5:42 AM, Ana Rossello Busquet wrote:

> Hi,
>
> I am beginner in Jess. I have a java application that uses  
> jess.Rete. I want to define a rule in jess that calls a Userfunction  
> in LHS. If the Userfunction returns true, the rule should be fired.  
> I have mainly two problems: 1- how do I write this rule in jess 2-  
> how do I notify my engine that the return value of the function has  
> changed.
>
> 1-      How do I write the rule in jess:
> I have created a this class that implements Userfunction:
>
> public class ReturnTrue implements Userfunction {
> @Override
> public Value call(ValueVector arg0, Context arg1) throws  
> JessException {
> return new Value(Funcall.TRUE);
> }
> @Override
> public String getName() {return "returnTrue";}
> }
>
> My main code, where jess.Rete is initialized and my rules and  
> Userfunctions are loaded:
>
> Rete engine = new Rete();
> engine.addUserpackage(new JessTabFunctions());
> try {
>            //my database is an ontology created with protege
> engine.executeCommand("(load-project  file:/M:/MyJessTest/ 
> osgi.myjesstest.test/simple-owl.pprj)");
>            Batch.batch("myrules.clp", engine);
>            engine.addUserfunction(new ReturnTrue());
> } catch (JessException e) {
> e.printStackTrace();
> }
>
> I have write my rule like this, but it does not seem to be working:
> (defrule trueFunction
>    "True Function"
>    (test (returnTrue))
>    =>
>     ((System.out) println "True"))
>
> What am I doing wrong?
>
> 2-  - how do I notify my engine that the return value of the  
> Userfunction has changed
>
> I have found a post that actually talks about this: Userfunction on  
> LHS called only once. In this post ejfried suggests two options:  
> idle-rule or JavaBean which support PropertyChangeEvents. Can some  
> on give more information on how to use PropertyChangeEvents or even  
> better an example code?
>
> Thanks!
>
> Ana
>

---------------------------------------------------------
Ernest Friedman-Hill
Informatics & Decision Sciences, Sandia National Laboratories
PO Box 969, MS 9012, 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].
--------------------------------------------------------------------
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.