Search in sources :

Example 81 with Pattern

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]);
    }
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) ObjectType(org.drools.core.spi.ObjectType) Pattern(org.drools.core.rule.Pattern) ClassObjectType(org.drools.core.base.ClassObjectType) GroupElement(org.drools.core.rule.GroupElement) FileOutputStream(java.io.FileOutputStream) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 82 with Pattern

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));
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) ObjectType(org.drools.core.spi.ObjectType) Pattern(org.drools.core.rule.Pattern) ClassObjectType(org.drools.core.base.ClassObjectType) GroupElement(org.drools.core.rule.GroupElement) Test(org.junit.Test)

Example 83 with Pattern

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));
}
Also used : Pattern(org.drools.core.rule.Pattern) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) PredicateConstraint(org.drools.core.rule.PredicateConstraint) PackageDescr(org.drools.drl.ast.descr.PackageDescr) Test(org.junit.Test)

Example 84 with Pattern

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);
}
Also used : NotDescr(org.drools.drl.ast.descr.NotDescr) Pattern(org.drools.core.rule.Pattern) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) GroupElement(org.drools.core.rule.GroupElement) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) Test(org.junit.Test)

Example 85 with Pattern

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);
}
Also used : Pattern(org.drools.core.rule.Pattern) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) GroupElement(org.drools.core.rule.GroupElement) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) OrDescr(org.drools.drl.ast.descr.OrDescr) Test(org.junit.Test)

Aggregations

Pattern (org.drools.core.rule.Pattern)190 ClassObjectType (org.drools.core.base.ClassObjectType)103 Test (org.junit.Test)98 Declaration (org.drools.core.rule.Declaration)79 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)69 GroupElement (org.drools.core.rule.GroupElement)49 InternalReadAccessor (org.drools.core.spi.InternalReadAccessor)45 ArrayList (java.util.ArrayList)35 IOException (java.io.IOException)33 ObjectType (org.drools.core.spi.ObjectType)32 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)28 Variable (org.drools.model.Variable)25 LambdaConstraint (org.drools.modelcompiler.constraints.LambdaConstraint)25 List (java.util.List)24 Accumulate (org.drools.core.rule.Accumulate)24 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)23 KieBase (org.kie.api.KieBase)23 Collection (java.util.Collection)21 QueryImpl (org.drools.core.rule.QueryImpl)21 KieSession (org.kie.api.runtime.KieSession)21