JESS: Would like to know why runQueryStar would not return any results
"Felix Chan" <[email protected]> Sun, 17 Jul 2011 17:30:40 -0700
| Newsgroups | gmane.comp.java.jess |
|---|---|
| Message-ID | <31423_1311004757_p6IFxG1J001674_CAEg6sM8yL1LnYiQp-OJvbNfR1tNfMJa_m0pz-bnh+_DbywDu1g@mail.gmail.com> |
Hi all,
I am wondering if someone could help me understand why runQueryStar would
not work for me in a certain scenario. Thank you in advance!
First, I have the following script. It works as expected:
(deftemplate DimmerSwitch (slot label) (slot brightness))
(defquery search-dimmer-switch
(declare (variables ?lbl))
(DimmerSwitch (label ?lbl) (brightness ?b))
)
(defrule set-brightness-to-10-and-15
(not (DimmerSwitch))
=>
(assert (DimmerSwitch (label "Ceiling Light") (brightness 10)))
(assert (DimmerSwitch (label "Bathroom Light") (brightness 5)))
)
(reset)
(run)
(facts)
(bind ?result (run-query* search-dimmer-switch "Ceiling Light"))
(while (?result next)
(printout t (?result getString lbl) " | " (?result getInt b) crlf)
)
The output is as follows. No problem there.
f-0 (MAIN::initial-fact)
f-1 (MAIN::DimmerSwitch (label "Ceiling Light") (brightness 10))
f-2 (MAIN::DimmerSwitch (label "Bathroom Light") (brightness 5))
For a total of 3 facts in module MAIN.
Ceiling Light | 10
-----------------------------------------------------------
Then I delete the portion of the script from (reset) on down, with just the
following:
(deftemplate DimmerSwitch (slot label) (slot brightness))
(defquery search-dimmer-switch
(declare (variables ?lbl))
(DimmerSwitch (label ?lbl) (brightness ?b))
)
(defrule set-brightness-to-10-and-15
(not (DimmerSwitch))
=>
(assert (DimmerSwitch (label "Ceiling Light") (brightness 10)))
(assert (DimmerSwitch (label "Bathroom Light") (brightness 5)))
)
Then I use java to run the query as follows. "test.clp" contains the above
script obviously.
package control;
import jess.*;
public class TestApp {
public static void main(String[] args) throws JessException {
Rete engine = new Rete();
engine.batch("C:\\Users\\Administrator\\workspace\\ShadowFactTest\\src\\control\\test.clp");
engine.reset();
engine.eval("(facts)");
engine.run();
engine.eval("(facts)");
QueryResult result = engine.runQueryStar("search-dimmer-switch", new
ValueVector().add("Ceiling Light"));
while (result.next()) {
System.out.println(result.getString("lbl") + " | " + result.getInt("b"));
}
}
}
The output is as follows. The Rete Engine ran fine. The rule fired. But
there's nothing coming back from runQueryStar.
f-0 (MAIN::initial-fact)
For a total of 1 facts in module MAIN.
f-0 (MAIN::initial-fact)
f-1 (MAIN::DimmerSwitch (label "Ceiling Light") (brightness 10))
f-2 (MAIN::DimmerSwitch (label "Bathroom Light") (brightness 5))
For a total of 3 facts in module MAIN.
---------------------------------------------------------
Now I change the query parameter to "brightness", an integer field.
(deftemplate DimmerSwitch (slot label) (slot brightness))
(defquery search-dimmer-switch
(declare (variables ?b))
(DimmerSwitch (label ?lbl) (brightness ?b))
)
(defrule set-brightness-to-10-and-15
(not (DimmerSwitch))
=>
(assert (DimmerSwitch (label "Ceiling Light") (brightness 10)))
(assert (DimmerSwitch (label "Bathroom Light") (brightness 5)))
)
---------------------
and use the following java program instead:
package control;
import jess.*;
public class TestApp {
public static void main(String[] args) throws JessException {
Rete engine = new Rete();
engine.batch("C:\\Users\\Administrator\\workspace\\ShadowFactTest\\src\\control\\test.clp");
engine.reset();
engine.eval("(facts)");
engine.run();
engine.eval("(facts)");
QueryResult result = engine.runQueryStar("search-dimmer-switch", new
ValueVector().add(10));
while (result.next()) {
System.out.println(result.getString("lbl") + " | " + result.getInt("b"));
}
}
}
---------------
Now the output looks normal:
f-0 (MAIN::initial-fact)
For a total of 1 facts in module MAIN.
f-0 (MAIN::initial-fact)
f-1 (MAIN::DimmerSwitch (label "Ceiling Light") (brightness 10))
f-2 (MAIN::DimmerSwitch (label "Bathroom Light") (brightness 5))
For a total of 3 facts in module MAIN.
Ceiling Light | 10
--------------------------
Could someone explain why this query won't work?
QueryResult result = engine.runQueryStar("search-dimmer-switch", new
ValueVector().add("Ceiling Light"));
Many thanks.
Felix