The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

PSUnderwood (3) [Avatar] Offline
#1
I'm trying to get the sample program in Jess In Action translated into Java. The problem is in the defrule for find-solution, which does not seem to fire. All the facts are asserted, as they are in the Jess version of the program, but no rules are activated. I'm sure this is a trivial problem, but I'm stumped. Java code follows:

package dilemma;
import jess.*;
public class Bandersnatch {
private static final String[] GOLFERS = {"Fred", "Joe", "Bob", "Tom"};
private static final String[] PANTS_COLORS = {"red", "blue", "plaid", "orange"};
private static final int[] POSITIONS = {1,2,3,4};

private Deftemplate pantsColor;
private Deftemplate position;
private Deffacts facts;

private Rete r;

Bandersnatch(Rete r){
this.r = r;
}

void doIt(){
try{
r.watchAll();
r.reset();
r.eval("(rules)");
r.run();
}
catch(JessException e){
e.printStackTrace();
}
}

void createFacts(){
try{
facts = new Deffacts("some-facts","the facts",r);
for(String g : GOLFERS){
for(String c : PANTS_COLORS){
Fact f = new Fact("pants-color",r);
f.setSlotValue("of", new Value(g, RU.STRING));
f.setSlotValue("is", new Value(c, RU.STRING));
facts.addFact(f);
}
for(int p : POSITIONS){
Fact f = new Fact("position",r);
f.setSlotValue("of", new Value(g, RU.STRING));
f.setSlotValue("is", new Value(p, RU.INTEGER));
facts.addFact(f);
}
}
r.addDeffacts(facts);
}
catch(JessException je){
System.out.println(je.toString());
}
}

void createTemplates(){
try{
pantsColor = new Deftemplate("pants-color", "Golfer pants color",r);
Value v1 = new Value("",RU.STRING);
pantsColor.addSlot("of", v1, "STRING");
v1 = new Value(0,RU.INTEGER);
pantsColor.addSlot("is", v1, "INTEGER");
r.addDeftemplate(pantsColor);

position = new Deftemplate("position", "Golfer position",r);
v1 = new Value("",RU.STRING);
position.addSlot("of", v1, "STRING");
v1 = new Value(0,RU.INTEGER);
position.addSlot("is", v1, "INTEGER");
r.addDeftemplate(position);
}
catch(JessException je){
System.out.println(je.getMessage());
}
}

void define_solve(){
String find_solution = new String("(defrule find-solution" +
" (position (of Fred) (is ?p1))" +
" (pants-color (of Fred) (is ?c1))" +
" (position (of ?n&~Fred) (is ?p&smilieeq ?p (+ ?p1 1))))" +
" (pants-color (of ?n&~Fred) (is blue&~?c1))" +
" (position (of Joe) (is ?p2&2&~?p1))" +
" (pants-color (of Joe) (is ?c2&~?c1))" +
" (position (of Bob) (is ?p3&~?p1&~?p&~?p2))" +
" (pants-color (of Bob&~?n) (is plaid&?c3&~?c1&~?c2))" +
" (position (of Tom&~?n) (is ~1&~4&?p4&~?p1&~?p2&~?p3))" +
" (pants-color (of Tom) (is ?c4&~orange&~blue&~?c1&~?c2&~?c3))" +
" =>" +
" (printout t Fred " " ?p1 " " ?c1 crlf)" +
" (printout t Joe " " ?p2 " " ?c2 crlf)" +
" (printout t Bob " " ?p3 " " ?c3 crlf)" +
" (printout t Tom " " ?p4 " " ?c4 crlf) )");
try{
r.eval(find_solution);
}
catch(JessException e){
e.printStackTrace();
}
}

public static void main(String[] args){
Bandersnatch b = new Bandersnatch(new Rete());
b.createTemplates();
b.createFacts();
b.define_solve();
b.doIt();
}
}
ejfried (326) [Avatar] Offline
#2
Re: Jess In Action Example in Java
One problem I see is that when you create the Fact objects, you store the slot values as RU.STRING data; but the rule is written to match symbols (RU.ATOM in Jess 6, RU.SYMBOL in Jess 7). Since these are two different types the rule won't match.
PSUnderwood (3) [Avatar] Offline
#3
Re: Jess In Action Example in Java
That did the trick. Thanks!