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

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

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

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
Reply | Threaded
Open this post in threaded view
|

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

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: [hidden email] [mailto:[hidden email]] 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
Reply | Threaded
Open this post in threaded view
|

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

Donald Winston
In reply to this post by Hunter Mcmillen
Use "shadow" facts.

see http://www.jessrules.com/jesswiki/view?FactsVsShadowFacts

On Nov 4, 2011, at 1:08 PM, Hunter McMillen wrote:

> 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



--------------------------------------------------------------------
To unsubscribe, send the words 'unsubscribe jess-users [hidden email]'
in the BODY of a message to [hidden email], NOT to the list
(use your own address!) List problems? Notify [hidden email].
--------------------------------------------------------------------

Reply | Threaded
Open this post in threaded view
|

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

Hunter Mcmillen
In reply to this post by Friedman-Hill, Ernest
Thanks for your quick reply. After I manage to get my Java object into Jess, can I assert it as a fact? Then have rules lhs match on the existence of some object with attributes x, y, and z?

Hunter

On Fri, Nov 4, 2011 at 4:01 PM, Friedman-Hill, Ernest <[hidden email]> wrote:
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: [hidden email] [mailto:[hidden email]] 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

Reply | Threaded
Open this post in threaded view
|

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

Friedman-Hill, Ernest
That's what Rete.add() and Rete.definstance() do. If that's the goal, all of this other stuff is superfluous -- just call add() or definstance() from Java.
 

From: [hidden email] [mailto:[hidden email]] On Behalf Of Hunter McMillen
Sent: Friday, November 04, 2011 7:50 PM
To: jess-users
Subject: Re: JESS: Is it possible to bind a Java object directly to a Jess variable without creating a new object?

Thanks for your quick reply. After I manage to get my Java object into Jess, can I assert it as a fact? Then have rules lhs match on the existence of some object with attributes x, y, and z?

Hunter

On Fri, Nov 4, 2011 at 4:01 PM, Friedman-Hill, Ernest <[hidden email]> wrote:
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: [hidden email] [mailto:[hidden email]] 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

Reply | Threaded
Open this post in threaded view
|

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

Wolfgang Laun-2
In reply to this post by Hunter Mcmillen
I daresay, not all of us use the same terms for the same things ;-)

I'll use "Pojo" for a Java object; it contains data in named fields. I'll use "Fact" for an entitiy that has been inserted into Jess' Working Memory; it contains data in named slots. And then, there is a "shadow fact", which is a fact with a reference to a Pojo in a slot that is (by definition) called OBJECT; slot values are taken from the object's fields of the same name.

Quote: "I am trying to assert to Jess that an object exists when I encounter a new object in my Java program."

Comment: In any case, this must be done by creating/inserting a fact. As has been said, a shadow fact is the easiest way to do this; it's defined from the backing class, and it is inserted by adding the Pojo. Creating an equivalent fact implies that all slots must be filled, by hand-coded code, from the Pojo; there's no further connection between these two, which is bound to cause additional details.

Quote: "Is there any way to just bind a Java object to a Jess variable without creating a new Java object?"

Comment: This is not the same thing as "asserting to Jess". You can bind a Pojo to a Jess variable without inserting/asserting it. But this variable will not affect the firing of rules.

-W 


On 5 November 2011 00:49, Hunter McMillen <[hidden email]> wrote:
Thanks for your quick reply. After I manage to get my Java object into Jess, can I assert it as a fact? Then have rules lhs match on the existence of some object with attributes x, y, and z?

Hunter

On Fri, Nov 4, 2011 at 4:01 PM, Friedman-Hill, Ernest <[hidden email]> wrote:
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: [hidden email] [mailto:[hidden email]] 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