JESS: Is it possible to bind a Java object directly to a Jess variable without creating a new object?

2011-11-04 Thread Hunter McMillen
Hi everyone,

I am trying to assert to Jess that an object exists when I encounter a new
object in my Java program. Right now I am using a template to mirror that
object (i.e I have slot values for all of the Java objects fields)  but
this seems redundant to me. Is there any way to just bind a Java object to
a Jess variable without creating a new Java object?

A lot of the examples I see online and from JIA are of the form:
(bind ?map (new HashMap))

or

(call Classname method params...)

but these either create  a new object or call static methods. I already
have the Java object and just want to store it.

But I was hoping that there was someway I could do something like this:
public Rete engine = new Rete();

public void unitDiscovered()
{
Unit unit = some unit encountered; //Java object
engine.executeCommand((bind ?unit unit));
engine.executeCommand((assert ?unit));
}

Or would I have to use the store() and fetch() methods for this?

public void unitDiscovered()
{
Unit unit = some unit encountered; //Java object
engine.store(UNIT, unit);
engine.executeCommand((bind ?unit (fetch UNIT));
engine.executeCommand((assert ?unit));
}

Basically I want to know the best practice for binding Java objects to Jess
variables so I can assert them to the engine.

Thanks,
Hunter McMillen


RE: JESS: Is it possible to bind a Java object directly to a Jess variable without creating a new object?

2011-11-04 Thread Friedman-Hill, Ernest
There are (obviously) two options: (a) put the object somewhere accessible, and 
run Jess code that retrieves it, or (b) use Jess's Java API to set a Jess 
variable to contain the object. Either would work. The store/fetch mechanism is 
sort of an built-in easy way to do (a). Alternatively, say there's a global 
variable ?*x* defined in your Jess program. Then you can say

engine.getGlobalContext().setVariable(*x*, new Value(unit));

and your Jess code can later get the value of *x*.

If you don't like using variables this way, there's always directly invoking 
Jess functions from Java. Here we invoke 'add' to add the object to working 
memory directly:

new Funcall(add, engine).arg(new 
Value(unit)).execute(engine.getGlobalContext());



From: owner-jess-us...@sandia.gov [mailto:owner-jess-us...@sandia.gov] On 
Behalf Of Hunter McMillen
Sent: Friday, November 04, 2011 1:08 PM
To: jess-users
Subject: JESS: Is it possible to bind a Java object directly to a Jess variable 
without creating a new object?

Hi everyone,

I am trying to assert to Jess that an object exists when I encounter a new 
object in my Java program. Right now I am using a template to mirror that 
object (i.e I have slot values for all of the Java objects fields)  but this 
seems redundant to me. Is there any way to just bind a Java object to a Jess 
variable without creating a new Java object?

A lot of the examples I see online and from JIA are of the form:
(bind ?map (new HashMap))

or

(call Classname method params...)

but these either create  a new object or call static methods. I already have 
the Java object and just want to store it.

But I was hoping that there was someway I could do something like this:
public Rete engine = new Rete();

public void unitDiscovered()
{
Unit unit = some unit encountered; //Java object
engine.executeCommand((bind ?unit unit));
engine.executeCommand((assert ?unit));
}

Or would I have to use the store() and fetch() methods for this?

public void unitDiscovered()
{
Unit unit = some unit encountered; //Java object
engine.store(UNIT, unit);
engine.executeCommand((bind ?unit (fetch UNIT));
engine.executeCommand((assert ?unit));
}

Basically I want to know the best practice for binding Java objects to Jess 
variables so I can assert them to the engine.

Thanks,
Hunter McMillen