use of org.drools.core.rule.Pattern in project drools by kiegroup.
the class LogicTransformerTest method testProcessTree.
/**
* <pre>
* _/|\_
* __/ | \__
* / | \
* __/ | \__
* / | \
* And and Not
* / | \ / \ |
* a And d e Or i
* / \ / \
* b Not h Exists
* | |
* Not g
* |
* c
* </pre>
*
* It is important to ensure that the order of
* the elements is not changed after transformation
*
* <pre>
* Or
* _/ \__
* __/ \___
* / \__
* __/ \__
* / \__
* / \__
* | \
* And And
* /| | | | | \ /| | | | | \
* a b Not d e h Not a b Not d e Exists Not
* | | | | |
* Not i Not g i
* | |
* c c
* </pre>
*
* @throws IOException
* @throws ClassNotFoundException
*/
@Test
public void testProcessTree() throws IOException, ClassNotFoundException, InvalidPatternException {
final ObjectType type = new ClassObjectType(String.class);
final Pattern a = new Pattern(0, type, "a");
final Pattern b = new Pattern(1, type, "b");
final Pattern c = new Pattern(2, type, "c");
final Pattern d = new Pattern(3, type, "d");
final Pattern e = new Pattern(4, type, "e");
final Pattern g = new Pattern(5, type, "g");
final Pattern h = new Pattern(6, type, "h");
final Pattern i = new Pattern(7, type, "i");
final GroupElement and1 = GroupElementFactory.newAndInstance();
final GroupElement and2 = GroupElementFactory.newAndInstance();
and1.addChild(a);
and1.addChild(and2);
and2.addChild(b);
final GroupElement not1 = GroupElementFactory.newNotInstance();
final GroupElement not2 = GroupElementFactory.newNotInstance();
not1.addChild(not2);
not2.addChild(c);
and2.addChild(not1);
and1.addChild(d);
final GroupElement and3 = GroupElementFactory.newAndInstance();
and3.addChild(e);
final GroupElement or1 = GroupElementFactory.newOrInstance();
and3.addChild(or1);
final GroupElement exist1 = GroupElementFactory.newExistsInstance();
exist1.addChild(g);
or1.addChild(h);
or1.addChild(exist1);
final GroupElement not3 = GroupElementFactory.newNotInstance();
not3.addChild(i);
final GroupElement root = GroupElementFactory.newAndInstance();
root.addChild(and1);
root.addChild(and3);
root.addChild(not3);
final GroupElement[] result = LogicTransformer.getInstance().transform(root, Collections.EMPTY_MAP);
// ----------------------------------------------------------------------------------
// Now construct the result tree so we can test root against what it
// should look like
// ----------------------------------------------------------------------------------
// Get known correct tree
// The binary stream was created from a handchecked correct output
// Uncomment this when you need to output a new known correct tree
// result
final File testFile = new File("target/test/LogicTransformerTest_correct_processTree1.dat");
testFile.getParentFile().mkdirs();
DroolsStreamUtils.streamOut(new FileOutputStream(testFile), result);
final GroupElement[] correctResultRoot = (GroupElement[]) DroolsStreamUtils.streamIn(new FileInputStream(testFile));
// Make sure they are equal
for (int j = 0; j < correctResultRoot.length; j++) {
assertEquals(correctResultRoot[j], result[j]);
}
}
use of org.drools.core.rule.Pattern in project drools by kiegroup.
the class LogicTransformerTest method testNotOrTransformation.
/**
* This data structure is now valid
*
* (Not (OR (A B) ) )
*
* <pre>
* Not
* |
* or
* / \
* a b
* </pre>
*
* Should become:
*
* <pre>
* And
* / \
* Not Not
* | |
* a b
* </pre>
*/
@Test
public void testNotOrTransformation() throws InvalidPatternException {
final ObjectType type = new ClassObjectType(String.class);
final Pattern a = new Pattern(0, type, "a");
final Pattern b = new Pattern(1, type, "b");
final GroupElement parent = GroupElementFactory.newNotInstance();
final GroupElement or = GroupElementFactory.newOrInstance();
parent.addChild(or);
or.addChild(a);
or.addChild(b);
LogicTransformer.getInstance().applyOrTransformation(parent);
assertTrue(parent.isAnd());
assertEquals(2, parent.getChildren().size());
// we must ensure order
final GroupElement b1 = (GroupElement) parent.getChildren().get(0);
final GroupElement b2 = (GroupElement) parent.getChildren().get(1);
assertTrue(b1.isNot());
assertTrue(b2.isNot());
assertEquals(1, b1.getChildren().size());
assertEquals(a, b1.getChildren().get(0));
assertEquals(1, b2.getChildren().size());
assertEquals(b, b2.getChildren().get(0));
}
use of org.drools.core.rule.Pattern in project drools by kiegroup.
the class KnowledgeBuilderTest method testPredicateMethodCompare.
@Test
public void testPredicateMethodCompare() {
final KnowledgeBuilderImpl builder1 = new KnowledgeBuilderImpl();
final PackageDescr packageDescr1 = new PackageDescr("package1");
createPredicateRule(packageDescr1, "eval(x==y)");
builder1.addPackage(packageDescr1);
if (builder1.hasErrors()) {
fail(builder1.getErrors().toString());
}
final Pattern pattern1 = (Pattern) ((RuleImpl) builder1.getPackage("package1").getRules().iterator().next()).getLhs().getChildren().get(0);
final PredicateConstraint predicate1 = (PredicateConstraint) pattern1.getConstraints().get(0);
final KnowledgeBuilderImpl builder2 = new KnowledgeBuilderImpl();
final PackageDescr packageDescr2 = new PackageDescr("package2");
createPredicateRule(packageDescr2, "eval(x==y)");
builder2.addPackage(packageDescr2);
if (builder2.hasErrors()) {
fail(builder2.getErrors().toString());
}
final Pattern pattern2 = (Pattern) ((RuleImpl) builder2.getPackage("package2").getRules().iterator().next()).getLhs().getChildren().get(0);
final PredicateConstraint predicate2 = (PredicateConstraint) pattern2.getConstraints().get(0);
final KnowledgeBuilderImpl builder3 = new KnowledgeBuilderImpl();
if (builder3.hasErrors()) {
fail(builder3.getErrors().toString());
}
final PackageDescr packageDescr3 = new PackageDescr("package3");
createPredicateRule(packageDescr3, "eval(x!=y)");
builder3.addPackage(packageDescr3);
final Pattern pattern3 = (Pattern) ((RuleImpl) builder3.getPackage("package3").getRules().iterator().next()).getLhs().getChildren().get(0);
final PredicateConstraint predicate3 = (PredicateConstraint) pattern3.getConstraints().get(0);
assertEquals(predicate1, predicate2);
assertFalse(predicate1.equals(predicate3));
assertFalse(predicate2.equals(predicate3));
}
use of org.drools.core.rule.Pattern 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.Pattern in project drools by kiegroup.
the class KnowledgeBuilderTest method testOr.
@Test
public void testOr() throws Exception {
final KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
final RuleImpl rule = createRule(new OrDescr(), builder, "update(stilton);");
assertLength(0, builder.getErrors().getErrors());
final GroupElement lhs = rule.getLhs();
assertLength(1, lhs.getChildren());
final GroupElement or = (GroupElement) lhs.getChildren().get(0);
assertLength(1, or.getChildren());
final Pattern pattern = (Pattern) or.getChildren().get(0);
}
Aggregations