When you pass a variable from Jess back to Java, what gets passed is a
Value object of type RU.VARIABLE. To get the actual value, you have to
"resolve" or look-up the variable in the execution context. This
happens automatically if you can a method like Value.stringValue(), or
you can do it explicitly using Value.resolveValue(). So the main
problem is just in your expectations for what the printout should be.
You're doing something odd on your rule RHS, but Jess's "lazy
execution" semantics are letting you get away with it. (user ?userID)
and (type-music ?classical) are being parsed as function calls. It's
up to your Userfunction to resolved each of its arguments, which would
result in those function calls being executed. In this case, there'd
be an error, since you haven't defined functions named (user) or
(type-music.) If you want to pass two-item lists as arguments, you
have to create the lists explicitly:
(notifyEvent (play-music-event (create$ user ?userID)
(create$ type-music "classical"))
Anyhow, given the chanmge above, you'll get the results you expect if
your Java code looked something like:
public Value call(ValueVector vv, Context context) throws JessException {
// We are expecting parameters on the following form
// (event-name (attribute-name attribute-value)+)
// Print out the contents of the event generated
System.out.println("AGGREGATION EVENT RECEIVED: " +
vv.get(0));
for (int i=1; i<vv.size(); ++i) {
Value v = vv.get(i);
if (v.type() == RU.LIST) {
System.out.print("(");
ValueVector list = v.listValue(context);
for (int j=0; j<list.size(); ++j) {
System.out.println(list.get(j).resolveValue(context));
System.out.print(" ");
}
System.out.print(")");
} else
System.out.print(v.resolveValue(context));
System.out.print(" ");
}
System.out.println();
I think D. Lopez-De-Ipina wrote:
> Hi,
>
> I am trying to implement a Event-Condition-Action Agent using
> Jess. Basically, events are mapped into facts that are inserted into a
> Jess engine. Several Jess rules correlate the events received and as a
> consequence generate another higher level event. This event is
> communicated to the Java program embedding the RETE engine through a
> UserFunction, namely notifyEvent.
>
> I have managed to get callbacks from Jess, but the problem is that it does
> not return me the values of the Jess variables I pass as arguments. For
> example, given the following rule in Jess:
>
> (defrule play-classical-music "Rule determining when user wants to listen
> classical music"
> (presence-event (personID ?userID))
> (not (presence-event (personID ~?userID)))
> =>
> (notifyEvent (play-music-event (user ?userID) (type-music
> "classical")))
> (printout t "play-music event asserted" crlf))
>
> The Java code receiving the function call is:
>
> public Value call(ValueVector vv, Context context) throws JessException {
> // We are expecting parameters on the following form
> // (event-name (attribute-name attribute-value)+)
> // Print out the contents of the event generated
> System.out.println("AGGREGATION EVENT RECEIVED: " +
> vv.get(0));
> System.out.println("CONTENTS: "+ vv.toString());
> }
>
>
> The results printed out are:
>
> AGGREGATION EVENT RECEIVED: notifyEvent
> CONTENTS: (notifyEvent (play-music-event (user ?userID) (type-music
> "classical")))
>
> How can I make to receive the value of the variable '?userID' ?
>
> My apologies if the question is too basic. I have been searching in the
> mailing list and I haven't managed to find a similar problem.
>
> Regards,
>
> -----
> Diego Lopez de Ipina
> http://www-lce.eng.cam.ac.uk/~dl231
>
>
>
> ---------------------------------------------------------------------
> 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]
> ---------------------------------------------------------------------
>
---------------------------------------------------------
Ernest Friedman-Hill
Distributed Systems Research Phone: (925) 294-2154
Sandia National Labs FAX: (925) 294-2234
Org. 8920, MS 9012 [EMAIL PROTECTED]
PO Box 969 http://herzberg.ca.sandia.gov
Livermore, CA 94550
---------------------------------------------------------------------
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]
---------------------------------------------------------------------