use of org.drools.core.spi.KnowledgeHelper in project drools by kiegroup.
the class BaseMannersTest method getAllDone.
/**
* <pre>
* rule all_done() {
* Context context;
* when {
* context : Context( state == Context.PRINT_RESULTS )
* } then {
* }
* }
* </pre>
*
* @return
* @throws InvalidRuleException
*/
private RuleImpl getAllDone() throws InvalidRuleException {
final RuleImpl rule = new RuleImpl("alldone");
// -----------
// context : Context( state == Context.PRINT_RESULTS )
// -----------
final Pattern contextPattern = new Pattern(0, this.contextType);
contextPattern.addConstraint(getLiteralConstraint(contextPattern, "state", Context.PRINT_RESULTS));
rule.addPattern(contextPattern);
final Declaration contextDeclaration = rule.getDeclaration("context");
// ------------
//
// ------------
final Consequence consequence = new Consequence() {
public void evaluate(KnowledgeHelper drools, WorkingMemory workingMemory) throws ConsequenceException {
try {
System.err.println("all done");
} catch (Exception e) {
throw new ConsequenceException(e);
}
}
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
}
public void writeExternal(ObjectOutput out) throws IOException {
}
public String getName() {
return "default";
}
};
rule.setConsequence(consequence);
return rule;
}
use of org.drools.core.spi.KnowledgeHelper in project drools by kiegroup.
the class BaseMannersTest method getMakePath.
/**
* <pre>
* rule makePath() {
* Context context;
* int seatingId, seatingPid, pathSeat;
* String pathGuestName;
*
* when {
* Context( state == Context.MAKE_PATH )
* Seating( seatingId:id, seatingPid:pid, pathDone == false )
* Path( id == seatingPid, pathGuestName:guest, pathSeat:seat )
* (not Path( id == seatingId, guestName == pathGuestName )
* } else {
* drools.assert( new Path( seatingId, pathSeat, pathGuestName ) );
*
* }
* }
* </pre>
*
* @return
* @throws InvalidRuleException
*/
private RuleImpl getMakePath() throws InvalidRuleException {
final RuleImpl rule = new RuleImpl("makePath");
// -----------
// context : Context( state == Context.MAKE_PATH )
// -----------
final Pattern contextPattern = new Pattern(0, this.contextType);
contextPattern.addConstraint(getLiteralConstraint(contextPattern, "state", Context.MAKE_PATH));
rule.addPattern(contextPattern);
// ---------------
// Seating( seatingId:id, seatingPid:pid, pathDone == false )
// ---------------
final Pattern seatingPattern = new Pattern(1, this.seatingType);
setFieldDeclaration(seatingPattern, "id", "seatingId");
setFieldDeclaration(seatingPattern, "pid", "seatingPid");
seatingPattern.addConstraint(getLiteralConstraint(seatingPattern, "pathDone", false));
rule.addPattern(seatingPattern);
final Declaration seatingIdDeclaration = rule.getDeclaration("seatingId");
final Declaration seatingPidDeclaration = rule.getDeclaration("seatingPid");
// -----------
// Path( id == seatingPid, pathGuestName:guestName, pathSeat:seat )
// -----------
final Pattern pathPattern = new Pattern(2, this.pathType);
pathPattern.addConstraint(getBoundVariableConstraint(pathPattern, "id", seatingPidDeclaration, "=="));
setFieldDeclaration(pathPattern, "guestName", "pathGuestName");
setFieldDeclaration(pathPattern, "seat", "pathSeat");
rule.addPattern(pathPattern);
final Declaration pathGuestNameDeclaration = rule.getDeclaration("pathGuestName");
final Declaration pathSeatDeclaration = rule.getDeclaration("pathSeat");
// -------------
// (not Path( id == seatingId, guestName == pathGuestName )
// -------------
final Pattern notPathPattern = new Pattern(3, this.pathType);
notPathPattern.addConstraint(getBoundVariableConstraint(notPathPattern, "id", seatingIdDeclaration, "=="));
notPathPattern.addConstraint(getBoundVariableConstraint(notPathPattern, "guestName", pathGuestNameDeclaration, "=="));
final GroupElement not = GroupElementFactory.newNotInstance();
not.addChild(notPathPattern);
rule.addPattern(not);
// ------------
// drools.assert( new Path( id, pathName, pathSeat ) );
// ------------
final Consequence consequence = new Consequence() {
public void evaluate(KnowledgeHelper drools, WorkingMemory workingMemory) throws ConsequenceException {
try {
RuleImpl rule = drools.getRule();
LeftTuple tuple = (LeftTuple) drools.getTuple();
int id = seatingIdDeclaration.getExtractor().getIntValue((InternalWorkingMemory) workingMemory, tuple.get(seatingIdDeclaration).getObject());
int seat = pathSeatDeclaration.getExtractor().getIntValue((InternalWorkingMemory) workingMemory, tuple.get(pathSeatDeclaration).getObject());
String guestName = (String) drools.get(pathGuestNameDeclaration);
Path path = new Path(id, seat, guestName);
drools.insert(path);
// System.err.println( "make path : " + path );
} catch (Exception e) {
e.printStackTrace();
throw new ConsequenceException(e);
}
}
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
}
public void writeExternal(ObjectOutput out) throws IOException {
}
public String getName() {
return "default";
}
};
rule.setConsequence(consequence);
return rule;
}
use of org.drools.core.spi.KnowledgeHelper in project drools by kiegroup.
the class BaseMannersTest method getAreWeDone.
/**
* <pre>
* rule areWeDone() {
* Context context; LastSeat lastSear;
* when {
* context : Context( state == Context.CHECK_DONE )
* LastSeat( lastSeat: seat )
* Seating( rightSeat == lastSeat )
* } then {
* context.setState(Context.PRINT_RESULTS );
* }
* }
* </pre>
*
* @return
* @throws InvalidRuleException
*/
private RuleImpl getAreWeDone() throws InvalidRuleException {
final RuleImpl rule = new RuleImpl("areWeDone");
// -----------
// context : Context( state == Context.CHECK_DONE )
// -----------
final Pattern contextPattern = new Pattern(0, this.contextType, "context");
contextPattern.addConstraint(getLiteralConstraint(contextPattern, "state", Context.CHECK_DONE));
rule.addPattern(contextPattern);
final Declaration contextDeclaration = rule.getDeclaration("context");
// ---------------
// LastSeat( lastSeat: seat )
// ---------------
final Pattern lastSeatPattern = new Pattern(1, this.lastSeatType);
setFieldDeclaration(lastSeatPattern, "seat", "lastSeat");
rule.addPattern(lastSeatPattern);
final Declaration lastSeatDeclaration = rule.getDeclaration("lastSeat");
// -------------
// Seating( rightSeat == lastSeat )
// -------------
final Pattern seatingPattern = new Pattern(2, this.seatingType, null);
seatingPattern.addConstraint(getBoundVariableConstraint(seatingPattern, "rightSeat", lastSeatDeclaration, "=="));
rule.addPattern(seatingPattern);
// ------------
// context.setName( Context.PRINT_RESULTS );
// ------------
final Consequence consequence = new Consequence() {
public void evaluate(KnowledgeHelper drools, WorkingMemory workingMemory) throws ConsequenceException {
try {
RuleImpl rule = drools.getRule();
LeftTuple tuple = (LeftTuple) drools.getTuple();
Context context = (Context) drools.get(contextDeclaration);
context.setState(Context.PRINT_RESULTS);
drools.update(tuple.get(contextDeclaration), context);
// System.err.println( "We Are Done!!!" );
} catch (Exception e) {
throw new ConsequenceException(e);
}
}
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
}
public void writeExternal(ObjectOutput out) throws IOException {
}
public String getName() {
return "default";
}
};
rule.setConsequence(consequence);
return rule;
}
use of org.drools.core.spi.KnowledgeHelper in project drools by kiegroup.
the class CrossProductTest method setUp.
@Before
public void setUp() throws Exception {
final ObjectType list1ObjectType = new ClassObjectType(String.class);
final ObjectType list2ObjectType = new ClassObjectType(String.class);
final RuleImpl rule = new RuleImpl("rule-1");
final Pattern list1Pattern = new Pattern(0, list1ObjectType, "s1");
final Pattern list2Pattern = new Pattern(1, list2ObjectType, "s2");
rule.addPattern(list1Pattern);
rule.addPattern(list2Pattern);
final Declaration s1Declaration = rule.getDeclaration("s1");
final Declaration s2Declaration = rule.getDeclaration("s2");
this.values = new ArrayList();
rule.setConsequence(new Consequence() {
private static final long serialVersionUID = 510l;
public void evaluate(final KnowledgeHelper knowledgeHelper, final WorkingMemory workingMemory) throws Exception {
final String s1 = (String) knowledgeHelper.get(s1Declaration);
final String s2 = (String) knowledgeHelper.get(s2Declaration);
CrossProductTest.this.values.add(new String[] { s1, s2 });
}
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
}
public void writeExternal(ObjectOutput out) throws IOException {
}
public String getName() {
return "default";
}
});
this.pkg = new KnowledgePackageImpl("org.drools");
this.pkg.addRule(rule);
}
use of org.drools.core.spi.KnowledgeHelper in project drools by kiegroup.
the class ReteooRuleBuilderTest method testAddRuleWithPatterns.
@Test
public void testAddRuleWithPatterns() {
final RuleImpl rule = new RuleImpl("only patterns");
final Pattern c1 = new Pattern(0, new ClassObjectType(String.class));
final Pattern c2 = new Pattern(1, new ClassObjectType(String.class));
final Pattern c3 = new Pattern(2, new ClassObjectType(String.class));
final GroupElement lhsroot = GroupElementFactory.newAndInstance();
lhsroot.addChild(c1);
lhsroot.addChild(c2);
lhsroot.addChild(c3);
rule.setLhs(lhsroot);
final Consequence consequence = new Consequence() {
public void evaluate(KnowledgeHelper knowledgeHelper, WorkingMemory workingMemory) throws Exception {
System.out.println("Consequence!");
}
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
}
public void writeExternal(ObjectOutput out) throws IOException {
}
public String getName() {
return "default";
}
};
rule.setConsequence(consequence);
final List terminals = this.builder.addRule(rule, this.rulebase);
assertEquals("Rule must have a single terminal node", 1, terminals.size());
final RuleTerminalNode terminal = (RuleTerminalNode) terminals.get(0);
}
Aggregations