RE: AW: JESS: How to match Pattern in Facts

2004-08-12 Thread calvin pevee
Hi, 

I am back, I tried out the store/fetch. BUT, it return
me only the latest fired facts. Why? I need both (any)
facts which is fired. Is like the variable
(fact-in-java) is overwritten. How can I get the
activated facts out, I mean all the fired facts?
Please advice.

merlinMain.r.executeCommand((deftemplate
adv-attributes +
(slot id) +
(multislot attribs)));

merlinMain.r.executeCommand((defrule
show-adv-attributes +
 ?fact - (adv-attributes (id ?id)
(attribs ?attribs)) +
= +
(store fact-in-java ?attribs) +
(printout t \-\
crlf) +
(printout t \Fact-id=\ ?fact crlf) +
(printout t \id=\ ?id crlf) +
(printout t \attribs=\ ?attribs
crlf)));

merlinMain.r.reset();

merlinMain.r.executeCommand((assert
(adv-attributes (id 1) (attribs low-budget;
merlinMain.r.executeCommand((assert
(adv-attributes (id 2) (attribs
control-message-frequency;

merlinMain.r.run();

String str1 =
merlinMain.r.fetch(fact-in-java).stringValue(merlinMain.r.getGlobalContext());

System.out.println(--+str1+--);




__
Do you Yahoo!?
New and Improved Yahoo! Mail - 100MB free storage!
http://promotions.yahoo.com/new_mail 


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]




Re: AW: JESS: How to match Pattern in Facts

2004-08-12 Thread ejfried
I think calvin pevee wrote:
 Hi, 
 
 I am back, I tried out the store/fetch. BUT, it return
 me only the latest fired facts. Why? I need both (any)
 facts which is fired. Is like the variable
 (fact-in-java) is overwritten. How can I get the
 activated facts out, I mean all the fired facts?
 Please advice.
 

store/fetch is just a thin layer over a HashMap. You put something in,
whatever was previously in there under the same key goes away.

Remember that from the Jess language, you have full access to all of
Java.  Your mainline Java code could store() an ArrayList under the
given key, and then your rule could, instead of calling (store), use
(fetch) to get that ArrayList, and call add to install more
results. This way all the results would accumulate in the ArrayList.

Of course, you could use a Set, or a Map, or whatever you
want. 



-
Ernest Friedman-Hill  
Advanced Software Research  Phone: (925) 294-2154
Sandia National LabsFAX:   (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]




RE: AW: JESS: How to match Pattern in Facts [details]

2004-08-12 Thread Jason Morris
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]