I think that Calvin Pevee wrote:
>>I tried out the store/fetch. BUT, it return me only the latest fired
facts.
Hi Calvin,
Ernest cut to the heart of the matter already, but in your code below, note
that every time the rule fires, the value of the stored variable
"fact-in-java" gets replaced, overwriting the previous value. You should
probably accumulate them in some sort of resizable collection like a Vector
or ArrayList.
r.executeCommand("(defrule show-adv-attributes"
+ " ?fact <- (adv-attributes (id ?id)(attribs
?attribs))"
+ "=>" + "(store fact-in-java ?attribs)"
>>How can I get the activated facts out, I mean all the fired facts?
It pays to be precise here. Do you mean "How do I get all the a)
activations; b) fact references; or c) attribute multivariables?
If you just want a collection of fact references that caused the rule to
fire, then you could rewrite your code (I've shortened the redirections for
clarity) something like this:
import jess.*;
import java.util.Vector;
public class TestBug3 {
public static void main(String[] args) {
TestBug3 tb = new TestBug3();
tb.run();
}
private Rete r;
public TestBug3() {
this.r = new Rete();
}
public void run() {
// Catch JessException!
try {
// Like Ernest suggested... add a resizable container.
Vector factRefs = new Vector();
// Store that reference in Jess
r.store("FACT-REFS", factRefs);
// Execute your commands
r.executeCommand("(deftemplate adv-attributes" + "(slot id)"
+ "(multislot attribs))");
r.executeCommand("(defrule show-adv-attributes"
+ " ?fact <- (adv-attributes (id ?id)(attribs
?attribs))"
+ "=>" + "(bind ?fact-refs (fetch FACT-REFS))"
+ "(bind ?ref (?fact getFactId))"
+ "(?fact-refs add ?ref)"
+ "(printout t \"-------------------------\" crlf)"
+ "(printout t \"Fact-id=\" ?fact crlf)"
+ "(printout t \"id=\" ?id crlf)"
+ "(printout t \"attribs=\" ?attribs crlf crlf))");
r.reset();
r.executeCommand("(assert (adv-attributes (id 1) (attribs
low-budget)))");
r.executeCommand("(assert (adv-attributes (id 2) (attribs
control-message-frequency)))");
r.run();
// Retrieve the stored fact refs - this may be redundant, but
you get the idea.
Value v = r.fetch("FACT-REFS");
Vector newRefs = (Vector)
v.externalAddressValue(r.getGlobalContext());
int length = newRefs.size();
for (int i = 0; i < length; i++) {
System.out.println("fact-ref:" + newRefs.get(i));
}
// Make sure that you are catching/handling JessException!
} catch (JessException jex) {
jex.printStackTrace(System.err);
}
}
}
This produces...
-------------------------
Fact-id=<Fact-2>
id=2
attribs=control-message-frequency
-------------------------
Fact-id=<Fact-1>
id=1
attribs=low-budget
fact-ref:2
fact-ref:1
You can do anything you want with the facts now that you have a list of
their references.
Hope this helps.
Cheers,
-Jason
------------------------
Jason Morris
Morris Technical Solutions
[EMAIL PROTECTED]
www.morristechnicalsolutions.com
fax/phone: 503.692.1088
--------------------------------------------------------------------
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]
--------------------------------------------------------------------