I really don't understand why you're doing all this. What's wrong with
writing, for example,
(defrule addValueToMultislot
"add normal value to multislot"
?fact <- (Human (Name "b") (Pets $?p))
=>
(modify ?fact (Pets (create$ "l" $?p)))
(modify ?fact (Name "c")))
It's shorter, clearer, faster, and most importantly, *works
correctly.* Why go through all this difficult, complex, error-prone
Java coding to so something which is very simple to do in Jess? Every
one of your "helper" functions can be written much more easily and
clearly in a single Jess function call.
I think portalguy wrote:
> Hi,
> Thanks for pointing this out, i've since changed the helper code to look
> like what is shown below. Everything seems to work fine except for a message
> i get when running insertFactToMultislot method:
> "No overloading of method 'insertFactToMultislot'"
> Although the function works fine and inserts a new fact in the multislot
> specified.
> What do you think is causing this message to appear. The following shows the
> working memory after running each method
>
> Template
> -----------------
> (deftemplate Human
> (slot Name (type STRING))
> (multislot Ancestors)
> (multislot Pets)
> )
>
> Initial facts asserted
> -----------------------
> (assert
> (Human (Name "a")
> (Ancestors (assert (Human (Name "b")))(assert (Human (Name "c"))))
> (Pets "j" "k")
> ))
> f-0 (MAIN::initial-fact)
> f-1 (MAIN::Human (Name "b") (Ancestors ) (Pets ))
> f-2 (MAIN::Human (Name "c") (Ancestors ) (Pets ))
> f-3 (MAIN::Human (Name "a") (Ancestors <Fact-1> <Fact-2>) (Pets "j" "k"))
> For a total of 4 facts.
>
> running setSlotValue(works fine)
> --------------------
> (defrule setSlotValue
> "sets a slot value"
> ?fact <- (Human (Name "a"))
> =>
> (call ?jessHelper setSlotValue ?fact Name "b" 2)
> )
>
> f-0 (MAIN::initial-fact)
> f-1 (MAIN::Human (Name "b") (Ancestors ) (Pets ))
> f-2 (MAIN::Human (Name "c") (Ancestors ) (Pets ))
> f-3 (MAIN::Human (Name "b") (Ancestors <Fact-1> <Fact-2>) (Pets "j" "k"))
> For a total of 4 facts.
>
> running addValueToMultislot(works fine)
> --------------------
> (defrule addValueToMultislot
> "add normal value to multislot"
> ?fact <- (Human (Name "b"))
> =>
> (call ?jessHelper addValueToMultislot ?fact Pets "l" 2)
> (call ?jessHelper setSlotValue ?fact Name "c" 2)
> )
> f-0 (MAIN::initial-fact)
> f-1 (MAIN::Human (Name "b") (Ancestors ) (Pets ))
> f-2 (MAIN::Human (Name "c") (Ancestors ) (Pets ))
> f-3 (MAIN::Human (Name "c") (Ancestors <Fact-1> <Fact-2>) (Pets "j" "k"
> "l"))
> For a total of 4 facts.
>
> running insertFactToMultislot(works fine, except for the message "No
> overloading of method 'insertFactToMultislot'")
> --------------------
> (defrule insertFactToMultislot
> "inserts fact to multislot"
> ?fact <- (Human (Name "c"))
> =>
> (call ?jessHelper insertFactToMultislot ?fact Ancestors (assert (Human (Name
> "d"))))
> (call ?jessHelper setSlotValue ?fact Name "d" 2)
> )
> f-0 (MAIN::initial-fact)
> f-1 (MAIN::Human (Name "b") (Ancestors ) (Pets ))
> f-2 (MAIN::Human (Name "c") (Ancestors ) (Pets ))
> f-3 (MAIN::Human (Name "d") (Ancestors <Fact-1> <Fact-2> <Fact-4>) (Pets
> "j" "k" "l"))
> f-4 (MAIN::Human (Name "d") (Ancestors ) (Pets ))
> For a total of 5 facts.
>
>
> JessHelper class
> -----------------------
> package mypackage2;
> import java.util.*;
> import jess.*;
> import jess.awt.*;
> public class JessHelper
> {
> Rete engine=null;
> public JessHelper(Rete engine)
> {
> this.engine=engine;
> }
> public Fact getFactByTemplateSlotValue(String strTemplate, String strSlot,
> String strValue)
> {
> /*
> * Get the deftemplate
> * Get all the facts
> * Loop through facts and find one of deftemplate with name
> *
> */
> if(engine!=null)
> {
> Iterator facts = engine.listFacts();
> while(facts.hasNext())
> {
> Fact fact =(Fact)facts.next();
> Deftemplate template = fact.getDeftemplate();
> try
> {
> if((template.getBaseName().equals(strTemplate))&&(fact.getSlotValu
> e(strSlot).toString().equals(strValue)))
> {
> return fact;
> }
> }
> catch (JessException e)
> {
> System.out.println(e.getMessage() + " JessHelper:
> getFactByTemplateSlotValue");
> }
> }
> }
> return null;
> }
>
> public void setSlotValue(Fact fact,String slotName,String slotValue,int
> type)
> {
> try
> {
> //fact.setSlotValue(slotName,new Value(slotValue,type));
> engine.modify(fact,slotName,new Value(slotValue,type));
> }
> catch (JessException e)
> {
> System.out.println(e.getMessage() + " JessHelper: setSlotValue");
> }
> }
>
> public void addValueToMultislot(Fact fact,String multislotName,String
> slotValue,int type)
> {
> try
> {
> Value value=fact.getSlotValue(multislotName);
> ValueVector vecValue =value.listValue(engine.getGlobalContext());
> vecValue = (ValueVector) vecValue.clone();
> vecValue.add(new Value(slotValue,type));
> engine.modify(fact,multislotName,new Value(vecValue,RU.LIST));
> //fact.setSlotValue(multislotName,vecValue.);
> }
> catch (JessException e)
> {
> System.out.println(e.getMessage() + " JessHelper:
> addValueToMultislot");
> }
> }
> public void insertFactToMultislot(Fact fact,String multislotName,Fact
> factValue)
> {
> try
> {
> Value value=fact.getSlotValue(multislotName);
> ValueVector vecValue =value.listValue(engine.getGlobalContext());
> vecValue = (ValueVector) vecValue.clone();
> vecValue.add(new jess.FactIDValue(factValue));
> engine.modify(fact,multislotName,new Value(vecValue,RU.LIST));
>
> //fact.setSlotValue(multislotName,vecValue.);
> }
> catch (JessException e)
> {
> System.out.println(e.getMessage() + " JessHelper:
> insertFactToMultislot");
> }
> }
> public void setMultislotValue(Fact fact,String multislotName,String
> currentValue,String newValue,int newValueType)
> {
> try
> {
> Value value=fact.getSlotValue(multislotName);
> ValueVector vecValue =value.listValue(engine.getGlobalContext());
> int index=getIndexValueVector(vecValue,currentValue);
> if(index!=-1)
> vecValue.set(new Value(newValue,newValueType),index);
> }
> catch (JessException e)
> {
> System.out.println(e.getMessage() + " JessHelper:
> setValueOfMultislot");
> }
> }
> public void removeMultislotValue(Fact fact, String multislotName,String
> slotValue)
> {
> try
> {
> Value value=fact.getSlotValue(multislotName);
> ValueVector vecValue =value.listValue(engine.getGlobalContext());
> vecValue = (ValueVector) vecValue.clone();
> int index=getIndexValueVector(vecValue,slotValue);
> if(index!=-1)
> {
> vecValue.remove(index);
> engine.modify(fact,multislotName,new Value(vecValue,RU.LIST));
> }
> }
> catch (JessException e)
> {
> System.out.println(e.getMessage() + " JessHelper:
> removeMultislotValue");
> }
> }
> public void removeMultislotFactValue(Fact fact, String multislotName,Fact
> factToBeRemoved)
> {
> try
> {
> Value value=fact.getSlotValue(multislotName);
> ValueVector vecValue =value.listValue(engine.getGlobalContext());
> vecValue = (ValueVector) vecValue.clone();
> int index=getIndexValueVector(vecValue,factToBeRemoved);
> if(index!=-1)
> {
> vecValue.remove(index);
> engine.modify(fact,multislotName,new Value(vecValue,RU.LIST));
> }
> }
> catch (JessException e)
> {
> System.out.println(e.getMessage() + " JessHelper:
> removeMultislotValue");
> }
> }
> public int getIndexValueVector(ValueVector valueVector,String slotValue)
> {
> try
> {
> for (int i=0;i<valueVector.size();i++)
> {
> System.out.println(valueVector.get(i).atomValue(engine.getGlobalCont
> ext()) + " " +slotValue);
> //System.out.println(valueVector.get(i).variableValue(engine.getGlob
> alContext()) + " " +slotValue);
> if(valueVector.get(i).atomValue(engine.getGlobalContext()).equals(sl
> otValue))
> {
> return i;
> }
> }
> }
> catch (JessException e)
> {
> System.out.println(e.getMessage() + " JessHelper:
> getIndexValueVector");
> }
> return -1;
> }
> public int getIndexValueVector(ValueVector valueVector,Fact fact)
> {
> try
> {
> for (int i=0;i<valueVector.size();i++)
> {
> //System.out.println(valueVector.get(i).atomValue(engine.getGlobalCo
> ntext()) + " " +slotValue);
> System.out.println(valueVector.get(i).factValue(engine.getGlobalCont
> ext()) + " " +fact.toString());
> Fact
> f=(Fact)valueVector.get(i).factValue(engine.getGlobalContext());
> if(f.getFactId()==fact.getFactId())
> {
> return i;
> }
> }
> }
> catch (Exception e)
> {
> System.out.println(e.getMessage() + " JessHelper:
> getIndexValueVector");
> }
> return -1;
> }
> }
> -----Original Message-----
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
> Behalf Of [EMAIL PROTECTED]
> Sent: 19 January 2005 15:55
> To: [email protected]
> Subject: Re: JESS: Inserting a fact created using assert into a
> multislot in in java
>
>
> Well, the error is coming from this line:
>
> vecValue.add(new Value(factValue,RU.FACT));
>
> This is invoking the Value constructor that takes a ValueVector and a
> type, rather than the specific constructor that takes a Fact alone.
> If you did use the right constructor, though, you'd still get an
> error:
>
> Message: Cannot use jess.Value to represent fact-ids. You must use
> class jess.FactIDValue,
>
> as described in section 4.2.1 of the manual. In any case, the error
> message you actually did see should recognize this mistake and explain
> it; thanks for the report.
>
> Now, all that aside, I must state in the strongest possible terms that
> you *cannot* do this. You cannot, under any circumstances, make
> modifications to Fact objects in working memory except by using the
> modify function or the Rete.modify method. Doing so will corrupt
> working memory and result in undefined behavior (see manual section
> 4.7). To use Rete.modify(), you could rewrite your helper as
>
> Value value=fact.getSlotValue(multislotName);
> ValueVector vecValue =value.listValue(engine.getGlobalContext());
> vecValue = (ValueVector) vecValue.clone();
> vecValue.add(new FactIDValue(factValue));
> engine.modify(fact, multislotName, vecValue);
>
>
> I think portalguy wrote:
> [Charset iso-8859-1 unsupported, filtering to ASCII...]
> > I'm calling a java function to insert a fact (created in jess script using
> > assert) into a multislot, however I get the following message "Not a
> vector
> > type: "nil" JessHelper: insertFactToMultislot"
> > In the example below I'm basically adding an ancestor of type Human
> template
> > to a multislot field.
> >
> > Thanks in advance for the help.
> >
> >
> > JessScript
> > -----------
> > (deftemplate Human
> > (slot Name (type STRING))
> > (multislot Ancestors))
> >
> > (assert (Human (Name "a")
> > (Ancestors (assert (Human (Name "b")))(assert (Human (Name "c"))))
> > ))
> >
> > (defrule addToMS
> > "add to multislot"
> > ?fact <- (Human (Name "a"))
> > =>
> > (call ?jessHelper insertFactToMultislot ?fact Ancestors (assert (Human
> (Name
> > "d"))))
> > )
> >
> > java function
> > --------------
> > public void insertFactToMultislot(Fact fact,String multislotName,Fact
> > factValue)
> > {
> > try
> > {
> > Value value=fact.getSlotValue(multislotName);
> > ValueVector vecValue =value.listValue(engine.getGlobalContext());
> > vecValue.add(new Value(factValue,RU.FACT));
> > }
> > catch (JessException e)
> > {
> > System.out.println(e.getMessage() + " JessHelper:
> > insertFactToMultislot");
> > }
> > }
> >
> >
> >
> >
> > --------------------------------------------------------------------
> > 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
> Advanced Software Research Phone: (925) 294-2154
> Sandia National Labs FAX: (925) 294-2234
> PO Box 969, MS 9012 [EMAIL PROTECTED]
> Livermore, CA 94550 http://herzberg.ca.sandia.gov
>
> --------------------------------------------------------------------
> 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]
> --------------------------------------------------------------------
>
> --------------------------------------------------------------------
> 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
Advanced Software Research Phone: (925) 294-2154
Sandia National Labs FAX: (925) 294-2234
PO Box 969, MS 9012 [EMAIL PROTECTED]
Livermore, CA 94550 http://herzberg.ca.sandia.gov
--------------------------------------------------------------------
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]
--------------------------------------------------------------------