You're right that there's no direct method you can call to create a
definstance from Java, although the same is true of many other Jess
functions. Instead you can either do something like what you've done
below, or create and use a Funcall object.

First, an improvement on the scheme below that doesn't require a rule!

  rete.store("FOO", obj);
  rete.executeCommand("(definstance class-name (fetch FOO) dynamic)");

Second, the Funcall approach. First, build the funcall:

  Funcall f = new Funcall("definstance", rete);
  f.add(new Value("class-name", RU.ATOM));
  f.add(new Value(obj));
  f.add(new Value("dynamic", RU.ATOM));

Then you can execute it once:

  f.execute(rete.getGlobalContext());

Then you can reuse it as many times as necessary:

  f.set(newObj, 2); // replace obj with newObj
  f.execute(rete.getGlobalContext());
  
Wrap these last two lines in a method called definstance(Object)
someplace, make f a member of the method's class,and you're all set. 


I think Alan Littleford wrote:
> Forgive me if I've missed something obvious but:
> 
>     I  cannot see a way of 'Definstancing' from within a Userfunction --
> the Definstance functionality within Jess seems private. Is this by
> design, or am I just mssing something very obvious? As it is now I
> simply assert a fact    'foo(OBJECT ?obj)' from my user function ( where
> ?obj is the address of my Bean created within my user function) and then
> I have a rule within Jess along the lines of :
> 
> (defrule defitup
>   ?f<-(foo (OBJECT ?obj))
> =>
>  (definstance <?class-name> ?obj dynamic)
>  (retract ?f) ;; don't need it any more
> )
> 
> which although it works fine seems to be laborious at best.  BTW I'm
> using Jess 5.1  (still intending to move to Jess 6 at some point!)
> 
> Thanks to anyone who can shed light on this
> 
> Alanl
> 
> ---------------------------------------------------------------------
> 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]
---------------------------------------------------------------------

Reply via email to