No response yet, so I'll take it:
Conor Power wrote:
>
> Hi,
> I have a question regarding how to use the attribute
> of a java object in the LHS of a rule.
>
> I have a java object to be used in the JESS engine as in the
> readme -
>
> Foo foo = new Foo();
> engine.executeCommand("(defglobal ?*x* = 0)");
> Funcall f = new Funcall("bind", engine);
> f.add(new Value(RU.putAtom("*x*"), RU.VARIABLE));
> f.add(new Value(foo, RU.EXTERNAL_ADDRESS));
> f.simpleExecute(f, rete.globalContext());
The above, as I understand, will make a global var *x* bound to
Foo foo. Is this what you want? definstance's don't get
(reset), so they may give you what you're after.
> The object has a member variable called - cmp
>
> I want a rule to fire depending on the value of the cmp member variable
>
> I envisage using something like follows
>
> (defrule r1
> (get ?*x* cmp)
> =>
> (DO SOMETHING)
> )
>
> Is this okay??
Another alternative to your approach is defclass/definstance, in
the event that you have several instances of Foo. The code is:
(defclass foo package.Foo)
and the definstance
Funcall f = new Funcall("definstance", rete);
f.add(new Value(RU.putAtom("foo"), RU.ATOM));
f.add(new Value(foo, RU.EXTERNAL_ADDRESS));
f.simpleExecute(f, rete.globalContext());
The above defclass/definstance works with Java Beans, which is
any class that supports PropertyChange listener/events, and has
methods like getAttribute() and setAttribute(). If you
defclass/definstance a Bean you can do things like
(get ?foo attribute)
and
(set ?foo attrubute <something>)
You would define a method in Foo called
int getCmp()
{
return cmp;
}
and, if you want it,
void setCmp( int newCmp )
{
cmp = newCmp;
}
/* don't forget property change stuff! */
(defrule r1
(foo (OBJECT ?foo))
(test (= 1 (get ?foo cmp))
=>
(do-stuff)
)
> The next problem I have is that, I set the member variable cmp in the
> right side
> of another rule. In this situation will the above rule check (get) the
> new value and
> test it. Do I need to add "notify changed" functions to the Foo object?
>
> Is my methodology correct?
Not sure if your proposed idea would work. First, Jess wouldn't
have any way to magically know that cmp changed, so it wouldn't
re-check in the LHS. That's what the PropertyChange stuff is for
(email me privately if you need more on this; it was covered a
week or so ago). Second, it's been my (exceedingly recent)
experience that Jess is really cool about handling Beans. You
may not need the defclass/definstance stuff, if you make Foo a
Bean.
If you haven't done anything with Beans please don't shun them --
it's really simple, involving naming conventions (the new Java
reflection is great). I did all this after-the-fact, and it made
my life about a zillion times easier. YMMV
Also, you may need to
rete.addUserpackage( new jess.reflect.ReflectFunctions() );
Sorry about the length -- please let me know if I'm
mistaken/misguided/etc.
--
Austin http://havoc.gtf.gatech.edu/austin Slave to The Man
---------------------------------------------------------------------
To unsubscribe, send the words 'unsubscribe jess-users [EMAIL PROTECTED]'
in the BODY of a message to [EMAIL PROTECTED], NOT to the
list. List problems? Notify [EMAIL PROTECTED]
---------------------------------------------------------------------