Search in sources :

Example 16 with ClassObjectType

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

the class JavaConsequenceBuilderPRAlwaysTest method setupTest.

private void setupTest(String consequence, Map<String, Object> namedConsequences) {
    InternalKnowledgePackage pkg = new KnowledgePackageImpl("org.drools");
    pkg.addImport(new ImportDeclaration("org.drools.compiler.Cheese"));
    KnowledgeBuilderConfigurationImpl conf = new KnowledgeBuilderConfigurationImpl();
    // Although it should be the default, for completeness we explicit this is the test cases how RHS should be rewritten as:
    conf.setOption(PropertySpecificOption.ALWAYS);
    KnowledgeBuilderImpl kBuilder = new KnowledgeBuilderImpl(pkg, conf);
    ruleDescr = new RuleDescr("test consequence builder");
    ruleDescr.setConsequence(consequence);
    for (Entry<String, Object> entry : namedConsequences.entrySet()) {
        ruleDescr.addNamedConsequences(entry.getKey(), entry.getValue());
    }
    RuleImpl rule = ruleDescr.toRule();
    PackageRegistry pkgRegistry = kBuilder.getPackageRegistry(pkg.getName());
    DialectCompiletimeRegistry reg = kBuilder.getPackageRegistry(pkg.getName()).getDialectCompiletimeRegistry();
    context = new RuleBuildContext(kBuilder, ruleDescr, reg, pkg, reg.getDialect(pkgRegistry.getDialect()));
    rule.addPattern(new Pattern(0, new ClassObjectType(Cheese.class), "$cheese"));
    Pattern p = new Pattern(1, new ClassObjectType(Person.class), "$persone");
    Declaration declr = p.addDeclaration("age");
    final InternalReadAccessor extractor = PatternBuilder.getFieldReadAccessor(context, new BindingDescr("age", "age"), p, "age", declr, true);
    rule.addPattern(p);
    context.getDeclarationResolver().pushOnBuildStack(rule.getLhs());
    context.getDialect().getConsequenceBuilder().build(context, RuleImpl.DEFAULT_CONSEQUENCE_NAME);
    for (String name : namedConsequences.keySet()) {
        context.getDialect().getConsequenceBuilder().build(context, name);
    }
    context.getDialect().addRule(context);
    pkgRegistry.getPackage().addRule(context.getRule());
    kBuilder.compileAll();
    kBuilder.reloadAll();
}
Also used : Pattern(org.drools.core.rule.Pattern) BindingDescr(org.drools.compiler.lang.descr.BindingDescr) RuleBuildContext(org.drools.compiler.rule.builder.RuleBuildContext) ClassObjectType(org.drools.core.base.ClassObjectType) KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) DialectCompiletimeRegistry(org.drools.compiler.compiler.DialectCompiletimeRegistry) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) PackageRegistry(org.drools.compiler.compiler.PackageRegistry) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) ImportDeclaration(org.drools.core.rule.ImportDeclaration) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) Declaration(org.drools.core.rule.Declaration) ImportDeclaration(org.drools.core.rule.ImportDeclaration) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) Person(org.drools.compiler.Person) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 17 with ClassObjectType

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

the class JavaConsequenceBuilderTest method setupTest.

private void setupTest(String consequence, Map<String, Object> namedConsequences) {
    InternalKnowledgePackage pkg = new KnowledgePackageImpl("org.drools");
    pkg.addImport(new ImportDeclaration("org.drools.compiler.Cheese"));
    KnowledgeBuilderConfigurationImpl conf = new KnowledgeBuilderConfigurationImpl();
    // this test was originally intended with PropertyReactive.ALLOWED:
    conf.setOption(PropertySpecificOption.ALLOWED);
    KnowledgeBuilderImpl kBuilder = new KnowledgeBuilderImpl(pkg, conf);
    ruleDescr = new RuleDescr("test consequence builder");
    ruleDescr.setConsequence(consequence);
    for (Entry<String, Object> entry : namedConsequences.entrySet()) {
        ruleDescr.addNamedConsequences(entry.getKey(), entry.getValue());
    }
    RuleImpl rule = ruleDescr.toRule();
    PackageRegistry pkgRegistry = kBuilder.getPackageRegistry(pkg.getName());
    DialectCompiletimeRegistry reg = kBuilder.getPackageRegistry(pkg.getName()).getDialectCompiletimeRegistry();
    context = new RuleBuildContext(kBuilder, ruleDescr, reg, pkg, reg.getDialect(pkgRegistry.getDialect()));
    rule.addPattern(new Pattern(0, new ClassObjectType(Cheese.class), "$cheese"));
    Pattern p = new Pattern(1, new ClassObjectType(Person.class), "$persone");
    Declaration declr = p.addDeclaration("age");
    final InternalReadAccessor extractor = PatternBuilder.getFieldReadAccessor(context, new BindingDescr("age", "age"), p, "age", declr, true);
    rule.addPattern(p);
    context.getDeclarationResolver().pushOnBuildStack(rule.getLhs());
    context.getDialect().getConsequenceBuilder().build(context, RuleImpl.DEFAULT_CONSEQUENCE_NAME);
    for (String name : namedConsequences.keySet()) {
        context.getDialect().getConsequenceBuilder().build(context, name);
    }
    context.getDialect().addRule(context);
    pkgRegistry.getPackage().addRule(context.getRule());
    kBuilder.compileAll();
    kBuilder.reloadAll();
}
Also used : Pattern(org.drools.core.rule.Pattern) BindingDescr(org.drools.compiler.lang.descr.BindingDescr) RuleBuildContext(org.drools.compiler.rule.builder.RuleBuildContext) ClassObjectType(org.drools.core.base.ClassObjectType) KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) DialectCompiletimeRegistry(org.drools.compiler.compiler.DialectCompiletimeRegistry) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) PackageRegistry(org.drools.compiler.compiler.PackageRegistry) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) ImportDeclaration(org.drools.core.rule.ImportDeclaration) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) Declaration(org.drools.core.rule.Declaration) ImportDeclaration(org.drools.core.rule.ImportDeclaration) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) Person(org.drools.compiler.Person) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 18 with ClassObjectType

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

the class Misc2Test method testFromEPDontRequireLeftInput.

@Test
public void testFromEPDontRequireLeftInput() {
    // DROOLS-1014
    String drl = "rule R when\n" + "    $s1 : String() from entry-point \"xxx\"\n" + "then\n" + "end\n";
    KieBase kbase = new KieHelper().addContent(drl, ResourceType.DRL).build();
    Rete rete = ((KnowledgeBaseImpl) kbase).getRete();
    LeftInputAdapterNode liaNode = null;
    for (ObjectTypeNode otn : rete.getObjectTypeNodes()) {
        Class<?> otnType = ((ClassObjectType) otn.getObjectType()).getClassType();
        if (String.class == otnType) {
            assertEquals(1, otn.getObjectSinkPropagator().size());
        } else if (InitialFact.class.isAssignableFrom(otnType)) {
            assertEquals(0, otn.getObjectSinkPropagator().size());
        } else {
            fail("There shouldn't be other OTNs");
        }
    }
}
Also used : Rete(org.drools.core.reteoo.Rete) ClassObjectType(org.drools.core.base.ClassObjectType) KieBase(org.kie.api.KieBase) InitialFact(org.drools.core.InitialFact) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) KieHelper(org.kie.internal.utils.KieHelper) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) Test(org.junit.Test)

Example 19 with ClassObjectType

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

the class NodePositionInPathTest method test.

@Test
public void test() {
    String drl = "rule R1 when\n" + "    Integer()\n" + "    exists( Integer() and Integer() )\n" + "    String()\n" + "then\n" + "end\n" + "rule R2 when\n" + "    Integer()\n" + "    exists( Integer() and String() )\n" + "then\n" + "end\n";
    KieBase kbase = new KieHelper().addContent(drl, ResourceType.DRL).build();
    ReteDumper.dumpRete(((InternalKnowledgeBase) kbase));
    Rete rete = ((KnowledgeBaseImpl) kbase).getRete();
    LeftInputAdapterNode liaNode = null;
    for (ObjectTypeNode otn : rete.getObjectTypeNodes()) {
        Class<?> otnType = ((ClassObjectType) otn.getObjectType()).getClassType();
        if (Integer.class == otnType) {
            liaNode = (LeftInputAdapterNode) otn.getObjectSinkPropagator().getSinks()[0];
        }
    }
    assertEquals(0, liaNode.getPositionInPath());
    LeftTupleSink[] liaSinks = liaNode.getSinkPropagator().getSinks();
    BetaNode join1 = (BetaNode) liaSinks[0];
    assertEquals(1, join1.getPositionInPath());
    ExistsNode ex1 = (ExistsNode) liaSinks[1];
    assertEquals(1, ex1.getPositionInPath());
    BetaNode join2 = (BetaNode) ex1.getSinkPropagator().getSinks()[0];
    assertEquals(2, join2.getPositionInPath());
    RuleTerminalNode rtn1 = (RuleTerminalNode) join2.getSinkPropagator().getSinks()[0];
    assertEquals(3, rtn1.getPositionInPath());
    ExistsNode ex2 = (ExistsNode) liaSinks[2];
    assertEquals(1, ex2.getPositionInPath());
    RuleTerminalNode rtn2 = (RuleTerminalNode) ex2.getSinkPropagator().getSinks()[0];
    assertEquals(2, rtn2.getPositionInPath());
    BetaNode join3 = (BetaNode) join1.getSinkPropagator().getSinks()[0];
    assertEquals(2, join3.getPositionInPath());
    RightInputAdapterNode ria1 = (RightInputAdapterNode) join3.getSinkPropagator().getSinks()[0];
    assertEquals(3, ria1.getPositionInPath());
    BetaNode join4 = (BetaNode) join1.getSinkPropagator().getSinks()[1];
    assertEquals(2, join4.getPositionInPath());
    RightInputAdapterNode ria2 = (RightInputAdapterNode) join4.getSinkPropagator().getSinks()[0];
    assertEquals(3, ria2.getPositionInPath());
    LeftTupleNode[] rtn1PathNodes = rtn1.getPathNodes();
    assertEquals(4, rtn1PathNodes.length);
    checkNodePosition(rtn1PathNodes, liaNode);
    checkNodePosition(rtn1PathNodes, ex1);
    checkNodePosition(rtn1PathNodes, join2);
    checkNodePosition(rtn1PathNodes, rtn1);
    LeftTupleNode[] rtn2PathNodes = rtn2.getPathNodes();
    assertEquals(3, rtn2PathNodes.length);
    checkNodePosition(rtn2PathNodes, liaNode);
    checkNodePosition(rtn2PathNodes, ex2);
    checkNodePosition(rtn2PathNodes, rtn2);
    LeftTupleNode[] ria1PathNodes = ria1.getPathNodes();
    assertEquals(4, ria1PathNodes.length);
    checkNodePosition(ria1PathNodes, liaNode);
    checkNodePosition(ria1PathNodes, join1);
    checkNodePosition(ria1PathNodes, join3);
    checkNodePosition(ria1PathNodes, ria1);
    LeftTupleNode[] ria2PathNodes = ria2.getPathNodes();
    assertEquals(4, ria2PathNodes.length);
    checkNodePosition(ria2PathNodes, liaNode);
    checkNodePosition(ria2PathNodes, join1);
    checkNodePosition(ria2PathNodes, join4);
    checkNodePosition(ria2PathNodes, ria2);
}
Also used : Rete(org.drools.core.reteoo.Rete) BetaNode(org.drools.core.reteoo.BetaNode) ClassObjectType(org.drools.core.base.ClassObjectType) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) LeftTupleSink(org.drools.core.reteoo.LeftTupleSink) KieHelper(org.kie.internal.utils.KieHelper) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) ExistsNode(org.drools.core.reteoo.ExistsNode) KieBase(org.kie.api.KieBase) LeftTupleNode(org.drools.core.reteoo.LeftTupleNode) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) RightInputAdapterNode(org.drools.core.reteoo.RightInputAdapterNode) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) Test(org.junit.Test)

Example 20 with ClassObjectType

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

the class PropertySpecificTest method testRtnSharedAlphaWithWatches.

@Test
public void testRtnSharedAlphaWithWatches() {
    String rule1 = "A( a == 10, b == 15 ) @watch(c, !a)";
    String rule2 = "A( a == 10, i == 20 ) @watch(s, !i)";
    KieBase kbase = getKnowledgeBase(rule1, rule2);
    InternalWorkingMemory wm = ((InternalWorkingMemory) kbase.newKieSession());
    ObjectTypeNode otn = getObjectTypeNode(kbase, "A");
    assertNotNull(otn);
    Class classType = ((ClassObjectType) otn.getObjectType()).getClassType();
    List<String> sp = getSettableProperties(wm, otn);
    AlphaNode alphaNode1 = (AlphaNode) otn.getObjectSinkPropagator().getSinks()[0];
    assertEquals(calculatePositiveMask(classType, list("a"), sp), alphaNode1.getDeclaredMask());
    assertEquals(calculatePositiveMask(classType, list("a", "b", "c", "s", "i"), sp), alphaNode1.getInferredMask());
    // first share
    AlphaNode alphaNode1_1 = (AlphaNode) alphaNode1.getObjectSinkPropagator().getSinks()[0];
    assertEquals(calculatePositiveMask(classType, list("b"), sp), alphaNode1_1.getDeclaredMask());
    assertEquals(calculatePositiveMask(classType, list("a", "b", "c"), sp), alphaNode1_1.getInferredMask());
    LeftInputAdapterNode liaNode1 = (LeftInputAdapterNode) alphaNode1_1.getObjectSinkPropagator().getSinks()[0];
    RuleTerminalNode rtNode1 = (RuleTerminalNode) liaNode1.getSinkPropagator().getSinks()[0];
    assertEquals(calculatePositiveMask(classType, list("c"), sp), rtNode1.getDeclaredMask());
    assertEquals(calculatePositiveMask(classType, list("b", "c"), sp), rtNode1.getInferredMask());
    assertEquals(calculateNegativeMask(classType, list("!a"), sp), rtNode1.getNegativeMask());
    // second share
    AlphaNode alphaNode1_2 = (AlphaNode) alphaNode1.getObjectSinkPropagator().getSinks()[1];
    assertEquals(calculatePositiveMask(classType, list("i"), sp), alphaNode1_2.getDeclaredMask());
    assertEquals(calculatePositiveMask(classType, list("a", "i", "s"), sp), alphaNode1_2.getInferredMask());
    LeftInputAdapterNode liaNode2 = (LeftInputAdapterNode) alphaNode1_2.getObjectSinkPropagator().getSinks()[0];
    RuleTerminalNode rtNode2 = (RuleTerminalNode) liaNode2.getSinkPropagator().getSinks()[0];
    assertEquals(calculatePositiveMask(classType, list("s"), sp), rtNode2.getDeclaredMask());
    assertEquals(calculatePositiveMask(classType, list("a", "s"), sp), rtNode2.getInferredMask());
    assertEquals(calculateNegativeMask(classType, list("!i"), sp), rtNode2.getNegativeMask());
    // test rule removal
    kbase.removeRule("org.drools.compiler.integrationtests", "r0");
    assertEquals(calculatePositiveMask(classType, list("a"), sp), alphaNode1.getDeclaredMask());
    assertEquals(calculatePositiveMask(classType, list("a", "i", "s"), sp), alphaNode1.getInferredMask());
    assertEquals(calculatePositiveMask(classType, list("i"), sp), alphaNode1_2.getDeclaredMask());
    assertEquals(calculatePositiveMask(classType, list("a", "i", "s"), sp), alphaNode1_2.getInferredMask());
    assertEquals(calculatePositiveMask(classType, list("s"), sp), rtNode2.getDeclaredMask());
    assertEquals(calculatePositiveMask(classType, list("a", "s"), sp), rtNode2.getInferredMask());
    assertEquals(calculateNegativeMask(classType, list("!i"), sp), rtNode2.getNegativeMask());
    // have to rebuild to remove r1
    kbase = getKnowledgeBase(rule1, rule2);
    kbase.removeRule("org.drools.compiler.integrationtests", "r1");
    otn = getObjectTypeNode(kbase, "A");
    alphaNode1 = (AlphaNode) otn.getObjectSinkPropagator().getSinks()[0];
    assertEquals(calculatePositiveMask(classType, list("a"), sp), alphaNode1.getDeclaredMask());
    assertEquals(calculatePositiveMask(classType, list("a", "b", "c"), sp), alphaNode1.getInferredMask());
    alphaNode1_1 = (AlphaNode) alphaNode1.getObjectSinkPropagator().getSinks()[0];
    assertEquals(calculatePositiveMask(classType, list("b"), sp), alphaNode1_1.getDeclaredMask());
    assertEquals(calculatePositiveMask(classType, list("a", "b", "c"), sp), alphaNode1_1.getInferredMask());
    liaNode1 = (LeftInputAdapterNode) alphaNode1_1.getObjectSinkPropagator().getSinks()[0];
    rtNode1 = (RuleTerminalNode) liaNode1.getSinkPropagator().getSinks()[0];
    assertEquals(calculatePositiveMask(classType, list("c"), sp), rtNode1.getDeclaredMask());
    assertEquals(calculatePositiveMask(classType, list("b", "c"), sp), rtNode1.getInferredMask());
    assertEquals(calculateNegativeMask(classType, list("!a"), sp), rtNode1.getNegativeMask());
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) ClassObjectType(org.drools.core.base.ClassObjectType) KieBase(org.kie.api.KieBase) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) AlphaNode(org.drools.core.reteoo.AlphaNode) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) Test(org.junit.Test)

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