I don't this there is an especially good way to do this within the
Jess language itself. The list-handling semantics are inherited from
CLIPS, and they're definitely not the most efficient thing in the
world.
You could write a simple Java Userfunction to do what you want,
something like
public class AppendList implements Userfunction, Serializable {
public String getName() { return "append"; }
public void call(ValueVector vv, Context c) throws JessException {
ValueVector list = vv.get(1).listValue(c);
Value addend = vv.get(2).resolveValue(c);
list.add(addend);
// Note my little hack to avoid allocating a Value here
return vv.get(1);
}
}
The thing to be careful of is never do this to a list that's
physically in working memory -- you could break things. It would work
fine in the situation you've shown.
I think Greenblatt, Alan wrote:
> I'm sure there's a simple and efficient way of doing this, but for
> some reason I'm unable to figure out the solution. On the LHS of my
> rule, I've matched a multislot. On the RHS I've asserted a new fact
> or list of facts and want to append that to the multislot. Something
> like the following:
>
> (defrule addFacts
> ?matchedFact <- (MyTemplate (factList $?facts))
>
> =>
> ; Initialize the list of new facts to be appended
> (bind ?newFacts (create$))
>
> (while (some condition)
> ; Assert the new fact
> (bind ?newFact (assert (Template2 (slot value)))
>
> ; Add the new fact to the list of facts to be appended
> (bind ?newFacts (create$ ?newFacts ?newFact)))
>
> (modify ?matchedFact (factList create$ (?facts ?newFacts))))
>
> There's a lot of list creation going on here that seems very wasteful
> when in fact I really only have one list that I want to keep appending
> to. I guess within the while loop I could use an ArrayList instead of
> a Jess list. Would that be more efficient or a better practice in
> such a case?
>
> Thanks for you help,
>
> - Alan
>
---------------------------------------------------------
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]
--------------------------------------------------------------------