Search in sources :

Example 11 with ObjectType

use of org.drools.core.spi.ObjectType 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) ClassObjectType(org.drools.core.base.ClassObjectType) FileOutputStream(java.io.FileOutputStream) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 12 with ObjectType

use of org.drools.core.spi.ObjectType in project drools by kiegroup.

the class LogicTransformerTest method testEliminateEmptyBranchesAndDuplications.

@Test
public void testEliminateEmptyBranchesAndDuplications() throws InvalidRuleException {
    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 GroupElement and1 = GroupElementFactory.newAndInstance();
    and1.addChild(a);
    and1.addChild(b);
    final GroupElement and2 = GroupElementFactory.newAndInstance();
    and2.addChild(c);
    and2.addChild(d);
    and1.addChild(and2);
    final GroupElement or = GroupElementFactory.newOrInstance();
    and1.addChild(or);
    final GroupElement[] result = LogicTransformer.getInstance().transform(and1, Collections.EMPTY_MAP);
    assertLength(1, result);
    assertLength(4, result[0].getChildren());
    // we must ensure order
    assertEquals(a, result[0].getChildren().get(0));
    assertEquals(b, result[0].getChildren().get(1));
    assertEquals(c, result[0].getChildren().get(2));
    assertEquals(d, result[0].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 13 with ObjectType

use of org.drools.core.spi.ObjectType 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) ClassObjectType(org.drools.core.base.ClassObjectType) Test(org.junit.Test)

Example 14 with ObjectType

use of org.drools.core.spi.ObjectType in project drools by kiegroup.

the class LogicTransformerTest method testExistOrTransformation.

/**
 * This data structure is now valid (Exists (OR (A B) ) )
 *
 * <pre>
 * Exists
 * |
 * or
 * / \
 * a b
 * </pre>
 *
 * Should become:
 *
 * <pre>
 * Not
 * |
 * And
 * / \
 * Not Not
 * | |
 * a b
 * </pre>
 */
@Test
public void testExistOrTransformation() 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.newExistsInstance();
    final GroupElement or = GroupElementFactory.newOrInstance();
    parent.addChild(or);
    or.addChild(a);
    or.addChild(b);
    LogicTransformer.getInstance().applyOrTransformation(parent);
    assertTrue(parent.isNot());
    assertEquals(1, parent.getChildren().size());
    final GroupElement and = (GroupElement) parent.getChildren().get(0);
    assertTrue(and.isAnd());
    assertEquals(2, and.getChildren().size());
    // we must ensure order
    final GroupElement b1 = (GroupElement) and.getChildren().get(0);
    final GroupElement b2 = (GroupElement) and.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) ClassObjectType(org.drools.core.base.ClassObjectType) Test(org.junit.Test)

Example 15 with ObjectType

use of org.drools.core.spi.ObjectType in project drools by kiegroup.

the class ObjectSource method initDeclaredMask.

public void initDeclaredMask(BuildContext context) {
    if (context == null || context.getLastBuiltPatterns() == null) {
        // only happens during unit tests
        declaredMask = AllSetBitMask.get();
        return;
    }
    Pattern pattern = context.getLastBuiltPatterns()[0];
    ObjectType objectType = pattern.getObjectType();
    if (!(objectType instanceof ClassObjectType)) {
        // Only ClassObjectType can use property specific
        declaredMask = AllSetBitMask.get();
        return;
    }
    Class objectClass = ((ClassObjectType) objectType).getClassType();
    TypeDeclaration typeDeclaration = context.getKnowledgeBase().getTypeDeclaration(objectClass);
    if (typeDeclaration == null || !typeDeclaration.isPropertyReactive()) {
        // if property specific is not on, then accept all modification propagations
        declaredMask = AllSetBitMask.get();
    } else {
        List<String> settableProperties = getAccessibleProperties(context.getKnowledgeBase(), objectClass);
        declaredMask = calculateDeclaredMask(objectClass, settableProperties);
    }
}
Also used : Pattern(org.drools.core.rule.Pattern) ClassObjectType(org.drools.core.base.ClassObjectType) ObjectType(org.drools.core.spi.ObjectType) ClassObjectType(org.drools.core.base.ClassObjectType) TypeDeclaration(org.drools.core.rule.TypeDeclaration)

Aggregations

ObjectType (org.drools.core.spi.ObjectType)33 ClassObjectType (org.drools.core.base.ClassObjectType)32 Test (org.junit.Test)17 Pattern (org.drools.core.rule.Pattern)13 Declaration (org.drools.core.rule.Declaration)7 FactTemplateObjectType (org.drools.core.facttemplates.FactTemplateObjectType)5 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)5 InternalReadAccessor (org.drools.core.spi.InternalReadAccessor)5 InternalFactHandle (org.drools.core.common.InternalFactHandle)4 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)4 KnowledgePackageImpl (org.drools.core.definitions.impl.KnowledgePackageImpl)4 TypeDeclaration (org.drools.core.rule.TypeDeclaration)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)3 DialectCompiletimeRegistry (org.drools.compiler.compiler.DialectCompiletimeRegistry)3 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)3 RuleBuildContext (org.drools.compiler.rule.builder.RuleBuildContext)3 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)3 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)3