use of org.drools.core.rule.GroupElement in project drools by kiegroup.
the class KnowledgeBuilderTest method testExists.
@Test
public void testExists() throws Exception {
KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
// Make sure we can't accessa variable bound inside the not node
RuleImpl rule = createRule(new ExistsDescr(), builder, "update(stilton);");
assertTrue(builder.hasErrors());
builder = new KnowledgeBuilderImpl();
rule = createRule(new ExistsDescr(), builder, "");
assertEquals(0, builder.getErrors().getErrors().length);
final GroupElement lhs = rule.getLhs();
assertLength(1, lhs.getChildren());
final GroupElement exists = (GroupElement) lhs.getChildren().get(0);
assertLength(1, exists.getChildren());
final Pattern pattern = (Pattern) exists.getChildren().get(0);
}
use of org.drools.core.rule.GroupElement in project drools by kiegroup.
the class KnowledgeBuilderTest method testAnd.
@Test
public void testAnd() throws Exception {
final KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
final RuleImpl rule = createRule(new AndDescr(), builder, "update(stilton);");
assertLength(0, builder.getErrors().getErrors());
final GroupElement lhs = rule.getLhs();
assertLength(1, lhs.getChildren());
final GroupElement and = (GroupElement) lhs.getChildren().get(0);
assertLength(1, and.getChildren());
final Pattern pattern = (Pattern) and.getChildren().get(0);
}
use of org.drools.core.rule.GroupElement in project drools by kiegroup.
the class KnowledgeBuilderTest method testNot.
@Test
public void testNot() throws Exception {
KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
// Make sure we can't accessa variable bound inside the not node
RuleImpl rule = createRule(new NotDescr(), builder, "update(stilton);");
assertTrue(builder.hasErrors());
builder = new KnowledgeBuilderImpl();
rule = createRule(new NotDescr(), builder, "");
assertEquals(0, builder.getErrors().getErrors().length);
final GroupElement lhs = rule.getLhs();
assertLength(1, lhs.getChildren());
final GroupElement not = (GroupElement) lhs.getChildren().get(0);
assertLength(1, not.getChildren());
final Pattern pattern = (Pattern) not.getChildren().get(0);
}
use of org.drools.core.rule.GroupElement in project drools by kiegroup.
the class DroolsObjectIOTest method testFileIO.
@Test
public void testFileIO() throws Exception {
FooBar fooBar1 = new FooBar();
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
new ObjectOutputStream(byteArrayOut).writeObject(fooBar1);
ByteArrayInputStream byteArrayIn = new ByteArrayInputStream(byteArrayOut.toByteArray());
FooBar fooBar2 = (FooBar) new ObjectInputStream(byteArrayIn).readObject();
final File testFile = new File("target/test/DroolsObjectIOTest_testFileIO.dat");
testFile.getParentFile().mkdirs();
GroupElement testGroupElement = new GroupElement();
DroolsStreamUtils.streamOut(new FileOutputStream(testFile), testGroupElement);
InputStream fis = new FileInputStream(testFile);
GroupElement streamedGroupElement = (GroupElement) DroolsStreamUtils.streamIn(new FileInputStream(testFile));
assertEquals(streamedGroupElement, testGroupElement);
}
use of org.drools.core.rule.GroupElement 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;
}
Aggregations