Search in sources :

Example 96 with ClassObjectType

use of org.drools.core.base.ClassObjectType in project drools by kiegroup.

the class LogicTransformerTest method testMultipleOrAndOrTransformation.

/**
 * (a||b)&&c
 *
 * <pre>
 *                   And
 *                  /|\ \__
 *                _/ | \_  \_
 *               /   |   \   \
 *              or   |   or   not
 *             /   \ |  / \    |
 *            a    b c d   e   f
 * </pre>
 *
 * Should become (a&&c)||(b&&c)
 *
 * <pre>
 *                           /\
 *                         _/  \_
 *                        /      \
 *                      _/|       |\_
 *                   __/  |       |  \__
 *                __/     |       |     \__
 *               /        |       |        \
 *              and      and     and      and
 *             /||\     /||\     /||\     /||\
 *            a cd Not a ce Not b cd Not b ce Not
 *                  |        |        |        |
 *                  f        f        f        f
 * </pre>
 */
@Test
public void testMultipleOrAndOrTransformation() 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 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 f = new Pattern(5, type, "f");
    final GroupElement parent = GroupElementFactory.newAndInstance();
    final GroupElement or = GroupElementFactory.newOrInstance();
    or.addChild(a);
    or.addChild(b);
    parent.addChild(or);
    parent.addChild(c);
    final GroupElement or2 = GroupElementFactory.newOrInstance();
    or2.addChild(d);
    or2.addChild(e);
    parent.addChild(or2);
    final GroupElement not = GroupElementFactory.newNotInstance();
    not.addChild(f);
    parent.addChild(not);
    LogicTransformer.getInstance().applyOrTransformation(parent);
    assertEquals(GroupElement.Type.OR, parent.getType());
    assertLength(4, parent.getChildren());
    assertEquals(GroupElement.class, parent.getChildren().get(0).getClass());
    assertEquals(GroupElement.class, parent.getChildren().get(1).getClass());
    assertEquals(GroupElement.class, parent.getChildren().get(2).getClass());
    assertEquals(GroupElement.class, parent.getChildren().get(3).getClass());
    GroupElement and1 = (GroupElement) parent.getChildren().get(0);
    assertTrue(and1.isAnd());
    assertLength(4, and1.getChildren());
    assertEquals(a, and1.getChildren().get(0));
    assertEquals(c, and1.getChildren().get(1));
    assertEquals(d, and1.getChildren().get(2));
    assertEquals(not, and1.getChildren().get(3));
    and1 = (GroupElement) parent.getChildren().get(1);
    assertTrue(and1.isAnd());
    assertLength(4, and1.getChildren());
    assertEquals(a, and1.getChildren().get(0));
    assertEquals(c, and1.getChildren().get(1));
    assertEquals(e, and1.getChildren().get(2));
    assertEquals(not, and1.getChildren().get(3));
    and1 = (GroupElement) parent.getChildren().get(2);
    assertTrue(and1.isAnd());
    assertLength(4, and1.getChildren());
    assertEquals(b, and1.getChildren().get(0));
    assertEquals(c, and1.getChildren().get(1));
    assertEquals(d, and1.getChildren().get(2));
    assertEquals(not, and1.getChildren().get(3));
    and1 = (GroupElement) parent.getChildren().get(3);
    assertTrue(and1.isAnd());
    assertLength(4, and1.getChildren());
    assertEquals(b, and1.getChildren().get(0));
    assertEquals(c, and1.getChildren().get(1));
    assertEquals(e, and1.getChildren().get(2));
    assertEquals(not, and1.getChildren().get(3));
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) ObjectType(org.drools.core.spi.ObjectType) ClassObjectType(org.drools.core.base.ClassObjectType) Test(org.junit.Test)

Example 97 with ClassObjectType

use of org.drools.core.base.ClassObjectType in project drools by kiegroup.

the class LogicTransformerTest method testSingleOrAndOrTransformation.

/**
 * (a||b)&&c
 *
 * <pre>
 *               and
 *               / \
 *              or  c
 *             /  \
 *            a    b
 * </pre>
 *
 * Should become (a&&c)||(b&&c)
 *
 * <pre>
 *
 *               or
 *              /  \
 *             /    \
 *            /      \
 *             and      and
 *          / \      / \
 *         a   c    b   c
 * </pre>
 */
@Test
public void testSingleOrAndOrTransformation() 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 Pattern c = new Pattern(2, type, "c");
    final GroupElement or = GroupElementFactory.newOrInstance();
    or.addChild(a);
    or.addChild(b);
    final GroupElement parent = GroupElementFactory.newAndInstance();
    parent.addChild(or);
    parent.addChild(c);
    LogicTransformer.getInstance().applyOrTransformation(parent);
    assertLength(2, parent.getChildren());
    assertEquals(GroupElement.class, parent.getChildren().get(0).getClass());
    assertEquals(GroupElement.class, parent.getChildren().get(1).getClass());
    final GroupElement and1 = (GroupElement) parent.getChildren().get(0);
    assertTrue(and1.isAnd());
    // transformation MUST keep the order
    assertEquals(a, and1.getChildren().get(0));
    assertEquals(c, and1.getChildren().get(1));
    final GroupElement and2 = (GroupElement) parent.getChildren().get(1);
    assertEquals(b, and2.getChildren().get(0));
    assertEquals(c, and2.getChildren().get(1));
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) ObjectType(org.drools.core.spi.ObjectType) ClassObjectType(org.drools.core.base.ClassObjectType) Test(org.junit.Test)

Example 98 with ClassObjectType

use of org.drools.core.base.ClassObjectType in project drools by kiegroup.

the class LogicTransformerTest method testTransform.

/**
 * /**
 *
 * <pre>
 *                         _/|\_
 *                      __/  |  \__
 *                     /     |     \
 *                  __/      |      \__
 *                 /         |         \
 *                And       or         And
 *               /  \       / \        /  \
 *             a    Or     d   e      Not OR
 *                 / \                |  / |
 *               b    c               f g Not
 *                                         |
 *                                         h
 *
 * </pre>
 *
 * Each And is a Rete sub rule
 *
 * <pre>
 *
 *       And___     And___      And___      And___        And__    And___       And___    And___
 *      ||| |  \   ||| |  \     ||| |  \   ||| |  \     ||| |  \  ||| |  \     ||| |  \  ||| |  \
 *      abd Not g  abd Not Not  abe Not g  abe Not Not  acd Not g acd Not Not  ace Not g ace Not Not
 *           |          |   |        |          |   |        |        |    |       |          |   |
 *           f          f   h        f          f   h        f        f    h       f          f   h
 *
 * </pre>
 *
 * @throws IOException
 * @throws ClassNotFoundException
 *
 * @throws IOException
 * @throws ClassNotFoundException
 */
@Test
public void testTransform() 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 f = new Pattern(5, type, "f");
    final Pattern g = new Pattern(6, type, "g");
    final Pattern h = new Pattern(7, type, "h");
    final GroupElement and = GroupElementFactory.newAndInstance();
    final GroupElement and1 = GroupElementFactory.newAndInstance();
    and1.addChild(a);
    final GroupElement or1 = GroupElementFactory.newOrInstance();
    or1.addChild(b);
    or1.addChild(c);
    and1.addChild(or1);
    and.addChild(and1);
    final GroupElement or2 = GroupElementFactory.newOrInstance();
    or2.addChild(d);
    or2.addChild(e);
    and.addChild(or2);
    final GroupElement and2 = GroupElementFactory.newAndInstance();
    final GroupElement not1 = GroupElementFactory.newNotInstance();
    not1.addChild(f);
    final GroupElement or3 = GroupElementFactory.newOrInstance();
    or3.addChild(g);
    final GroupElement not2 = GroupElementFactory.newNotInstance();
    not2.addChild(h);
    or3.addChild(not2);
    and2.addChild(not1);
    and2.addChild(or3);
    and.addChild(and2);
    final GroupElement[] ands = LogicTransformer.getInstance().transform(and, Collections.EMPTY_MAP);
    // Uncomment this when you need to output a new known correct tree
    // result
    final File testFile = new File("target/test/LogicTransformerTest_correct_transform1.dat");
    testFile.getParentFile().mkdirs();
    DroolsStreamUtils.streamOut(new FileOutputStream(testFile), ands);
    // Now check the main tree
    // Get known correct tree
    // The binary stream was created from a handchecked correct output
    final GroupElement[] correctResultAnds = (GroupElement[]) DroolsStreamUtils.streamIn(new FileInputStream(testFile));
    for (int j = 0; j < ands.length; j++) {
        assertEquals(correctResultAnds[j], ands[j]);
    }
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) ObjectType(org.drools.core.spi.ObjectType) ClassObjectType(org.drools.core.base.ClassObjectType) FileOutputStream(java.io.FileOutputStream) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 99 with ClassObjectType

use of org.drools.core.base.ClassObjectType in project drools by kiegroup.

the class PatternTest method testDeclarationsObjectType.

@Test
public void testDeclarationsObjectType() throws Exception {
    final ObjectType type = new ClassObjectType(Cheese.class);
    final Pattern col = new Pattern(0, type, "foo");
    final Declaration dec = col.getDeclaration();
    final InternalReadAccessor ext = dec.getExtractor();
    assertEquals(Cheese.class, ext.getExtractToClass());
    final Cheese stilton = new Cheese("stilton", 42);
    assertEquals(stilton, dec.getValue(null, stilton));
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) ObjectType(org.drools.core.spi.ObjectType) FactTemplateObjectType(org.drools.core.facttemplates.FactTemplateObjectType) ClassObjectType(org.drools.core.base.ClassObjectType) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) Cheese(org.drools.core.test.model.Cheese) Test(org.junit.Test)

Example 100 with ClassObjectType

use of org.drools.core.base.ClassObjectType in project drools by kiegroup.

the class LeftLeftTupleIndexHashTableIteratorTest method getConstraint.

protected BetaNodeFieldConstraint getConstraint(String identifier, Operator operator, String fieldName, Class clazz) {
    ClassFieldAccessorStore store = new ClassFieldAccessorStore();
    store.setClassFieldAccessorCache(new ClassFieldAccessorCache(Thread.currentThread().getContextClassLoader()));
    store.setEagerWire(true);
    InternalReadAccessor extractor = store.getReader(clazz, fieldName);
    Declaration declaration = new Declaration(identifier, extractor, new Pattern(0, new ClassObjectType(clazz)));
    String expression = fieldName + " " + operator.getOperatorString() + " " + declaration.getIdentifier();
    return new MvelConstraintTestUtil(expression, declaration, extractor);
}
Also used : ClassFieldAccessorCache(org.drools.core.base.ClassFieldAccessorCache) Pattern(org.drools.core.rule.Pattern) ClassObjectType(org.drools.core.base.ClassObjectType) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) ClassFieldAccessorStore(org.drools.core.base.ClassFieldAccessorStore) Declaration(org.drools.core.rule.Declaration) MvelConstraintTestUtil(org.drools.core.rule.MvelConstraintTestUtil)

Aggregations

ClassObjectType (org.drools.core.base.ClassObjectType)123 Test (org.junit.Test)76 Pattern (org.drools.core.rule.Pattern)37 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)34 KieBase (org.kie.api.KieBase)32 Declaration (org.drools.core.rule.Declaration)28 ObjectType (org.drools.core.spi.ObjectType)28 InternalReadAccessor (org.drools.core.spi.InternalReadAccessor)24 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)23 BetaNode (org.drools.core.reteoo.BetaNode)17 AlphaNode (org.drools.core.reteoo.AlphaNode)15 ArrayList (java.util.ArrayList)14 List (java.util.List)13 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)13 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)13 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)13 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)13 Cheese (org.drools.core.test.model.Cheese)13 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)12 InternalFactHandle (org.drools.core.common.InternalFactHandle)12