JESS: [EXTERNAL] Adding facts that are instances of from-class deftemplates

classic Classic list List threaded Threaded
5 messages Options
hlc
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

JESS: [EXTERNAL] Adding facts that are instances of from-class deftemplates

hlc
Hi,

I am concerned with the at least two different ways in which you can add, to working memory, facts that are instances of from-class deftemplates.
Lets say I have:

(deftemplate FC (declare (from-class FC)))

I can add a shadow fact like this:

(bind ?fc (new FC))
(add ?fc)

This is the general approach described in Section 5.3.2 of the Jess manual.
But I can also simply go like:

(assert (FC))

I guess in this case I do not get a shadow fact, since the OBJECT slot is nil.

So, my question is: if my facts are not supposed to be changed from Java code, is there any difference in using each of these approaches?
Section 5.3 from the Jess manual does not even mention that instances of from-class deftemplates can be created using the second approach above.

Thanks!

Henrique

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: JESS: [EXTERNAL] Adding facts that are instances of from-class deftemplates

Friedman-Hill, Ernest

The *real* way to add shadow facts is using the “definstance” function. It has a number of options that aren’t available with “add”. The “add” function was added to Jess to support the simplified semantics of JSR-94 (the javax.rules API) but the intent is that most Jess users will use “definstance.”

 

Asserting a fact directly from a from-class template is like buying a doghouse; you don’t expect to go out the next morning and find that somehow it has a dog in it, right? It’s certainly something you can do, but in normal life it’s not very useful; usually you get a doghouse when you buy a dog, and “add” and “definstance” will both get you a doghouse for your Java object “dog” automatically.

 

From: [hidden email] [mailto:[hidden email]] On Behalf Of Henrique Lopes Cardoso
Sent: Friday, May 10, 2013 4:54 AM
To: jess-users
Subject: JESS: [EXTERNAL] Adding facts that are instances of from-class deftemplates

 

Hi,

 

I am concerned with the at least two different ways in which you can add, to working memory, facts that are instances of from-class deftemplates.

Lets say I have:

 

(deftemplate FC (declare (from-class FC)))

 

I can add a shadow fact like this:

 

(bind ?fc (new FC))

(add ?fc)

 

This is the general approach described in Section 5.3.2 of the Jess manual.

But I can also simply go like:

 

(assert (FC))

 

I guess in this case I do not get a shadow fact, since the OBJECT slot is nil.

 

So, my question is: if my facts are not supposed to be changed from Java code, is there any difference in using each of these approaches?

Section 5.3 from the Jess manual does not even mention that instances of from-class deftemplates can be created using the second approach above.

 

Thanks!

 

Henrique

 

hlc
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: JESS: [EXTERNAL] Adding facts that are instances of from-class deftemplates

hlc
Thanks for the dog analogy...

But going back to practical uses, one aspect that I like when creating a fact directly from a from-class template (i.e., a doghouse without a dog -- I can still make some reasoning over the doghouse) is that the Jess code to do it is much more readable and compact, since you provide values for named slots.
Something like:


(deftemplate Person (declare (from-class Person)))

(assert (Person (name "Henrique") (age 38)))

When using definstance, I would have to go:
    (bind ?p (new Person "Henrique" 38))    ; assuming there is such a constructor
    (definstance Person ?p)

or
    (bind ?p (new Person))
    (?p setName "Henrique")
    (?p setAge 38)
    (definstance Person ?p)

Now, when you have a class with many data members this becomes quite complex.

Any comments? Thanks!

Henrique



On Fri, May 10, 2013 at 3:21 PM, Friedman-Hill, Ernest <[hidden email]> wrote:

The *real* way to add shadow facts is using the “definstance” function. It has a number of options that aren’t available with “add”. The “add” function was added to Jess to support the simplified semantics of JSR-94 (the javax.rules API) but the intent is that most Jess users will use “definstance.”

 

Asserting a fact directly from a from-class template is like buying a doghouse; you don’t expect to go out the next morning and find that somehow it has a dog in it, right? It’s certainly something you can do, but in normal life it’s not very useful; usually you get a doghouse when you buy a dog, and “add” and “definstance” will both get you a doghouse for your Java object “dog” automatically.

 

From: [hidden email] [mailto:[hidden email]] On Behalf Of Henrique Lopes Cardoso
Sent: Friday, May 10, 2013 4:54 AM
To: jess-users
Subject: JESS: [EXTERNAL] Adding facts that are instances of from-class deftemplates

 

Hi,

 

I am concerned with the at least two different ways in which you can add, to working memory, facts that are instances of from-class deftemplates.

Lets say I have:

 

(deftemplate FC (declare (from-class FC)))

 

I can add a shadow fact like this:

 

(bind ?fc (new FC))

(add ?fc)

 

This is the general approach described in Section 5.3.2 of the Jess manual.

But I can also simply go like:

 

(assert (FC))

 

I guess in this case I do not get a shadow fact, since the OBJECT slot is nil.

 

So, my question is: if my facts are not supposed to be changed from Java code, is there any difference in using each of these approaches?

Section 5.3 from the Jess manual does not even mention that instances of from-class deftemplates can be created using the second approach above.

 

Thanks!

 

Henrique

 




--
----- - - - - -  -  -  -  -  -   -   -
Henrique Lopes Cardoso
DEI/FEUP
Rua Dr. Roberto Frias | 4200-465 Porto
PORTUGAL
 VoIP: (+351) 220413355
Phone: (+351) 225081400 ext.1315
[hidden email] | www.fe.up.pt/~hlc
----- - - - - -  -  -  -  -  -   -   -
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: JESS: [EXTERNAL] Adding facts that are instances of from-class deftemplates

Friedman-Hill, Ernest
The thing is that after this code:

  (deftemplate Person (declare (from-class Person)))
  (bind ?f (assert (Person (name "Henrique") (age 38))))

There's no way to transfer those property values to a Person object; i.e., if you then said

(modify ?f (OBJECT (new Person)))

Then the Person's name and age would NOT be "Henrique" and "38"; they'd be something else. The only way to get a shadow fact and a Java object synced up automatically is to let Jess copy the properties to the fact from the object.






--------------------------------------------------------------------
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].
--------------------------------------------------------------------

hlc
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: JESS: [EXTERNAL] Adding facts that are instances of from-class deftemplates

hlc
Got that, thanks a lot.

Henrique



On Thu, May 16, 2013 at 3:24 PM, Friedman-Hill, Ernest <[hidden email]> wrote:
The thing is that after this code:

  (deftemplate Person (declare (from-class Person)))
  (bind ?f (assert (Person (name "Henrique") (age 38))))

There's no way to transfer those property values to a Person object; i.e., if you then said

(modify ?f (OBJECT (new Person)))

Then the Person's name and age would NOT be "Henrique" and "38"; they'd be something else. The only way to get a shadow fact and a Java object synced up automatically is to let Jess copy the properties to the fact from the object.






--------------------------------------------------------------------
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].
--------------------------------------------------------------------




--
----- - - - - -  -  -  -  -  -   -   -
Henrique Lopes Cardoso
DEI/FEUP
Rua Dr. Roberto Frias | 4200-465 Porto
PORTUGAL
 VoIP: (+351) 220413355
Phone: (+351) 225081400 ext.1315
[hidden email] | www.fe.up.pt/~hlc
----- - - - - -  -  -  -  -  -   -   -
Loading...