Search in sources :

Example 26 with ObjectType

use of org.drools.core.spi.ObjectType 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 27 with ObjectType

use of org.drools.core.spi.ObjectType 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 28 with ObjectType

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

the class PatternTest method testDeclarationsFactTemplate.

@Test
public void testDeclarationsFactTemplate() throws Exception {
    InternalKnowledgePackage pkg = new KnowledgePackageImpl("org.store");
    final FieldTemplate cheeseName = new FieldTemplateImpl("name", 0, String.class);
    final FieldTemplate cheesePrice = new FieldTemplateImpl("price", 1, Integer.class);
    final FieldTemplate[] fields = new FieldTemplate[] { cheeseName, cheesePrice };
    final FactTemplate cheese = new FactTemplateImpl(pkg, "Cheese", fields);
    final ObjectType type = new FactTemplateObjectType(cheese);
    final Pattern col = new Pattern(0, type, "foo");
    final Declaration dec = col.getDeclaration();
    final InternalReadAccessor ext = dec.getExtractor();
    assertEquals(Fact.class, ext.getExtractToClass());
    final Fact stilton = cheese.createFact(10);
    stilton.setFieldValue("name", "stilton");
    stilton.setFieldValue("price", new Integer(200));
    assertEquals(stilton, dec.getValue(null, stilton));
}
Also used : FactTemplateImpl(org.drools.core.facttemplates.FactTemplateImpl) FieldTemplate(org.drools.core.facttemplates.FieldTemplate) FieldTemplateImpl(org.drools.core.facttemplates.FieldTemplateImpl) Fact(org.drools.core.facttemplates.Fact) ClassObjectType(org.drools.core.base.ClassObjectType) ObjectType(org.drools.core.spi.ObjectType) FactTemplateObjectType(org.drools.core.facttemplates.FactTemplateObjectType) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) FactTemplateObjectType(org.drools.core.facttemplates.FactTemplateObjectType) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) FactTemplate(org.drools.core.facttemplates.FactTemplate) Test(org.junit.Test)

Example 29 with ObjectType

use of org.drools.core.spi.ObjectType 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 30 with ObjectType

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

the class AbstractTerminalNode method initDeclaredMask.

public void initDeclaredMask(BuildContext context) {
    if (!(unwrapTupleSource() instanceof LeftInputAdapterNode)) {
        // RTN's not after LIANode are not relevant for property specific, so don't block anything.
        setDeclaredMask(AllSetBitMask.get());
        return;
    }
    Pattern pattern = context.getLastBuiltPatterns()[0];
    ObjectType objectType = pattern.getObjectType();
    if (!(objectType instanceof ClassObjectType)) {
        // InitialFact has no type declaration and cannot be property specific
        // Only ClassObjectType can use property specific
        setDeclaredMask(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
        setDeclaredMask(AllSetBitMask.get());
    } else {
        List<String> settableProperties = getAccessibleProperties(context.getKnowledgeBase(), objectClass);
        Class modifiedClass = ((ClassObjectType) pattern.getObjectType()).getClassType();
        setDeclaredMask(calculatePositiveMask(modifiedClass, pattern.getListenedProperties(), settableProperties));
        setNegativeMask(calculateNegativeMask(modifiedClass, pattern.getListenedProperties(), 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