Search in sources :

Example 1 with ClassObjectType

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

the class AccumulateNode method addAccFunctionDeclarationsToLeftMask.

private void addAccFunctionDeclarationsToLeftMask(InternalKnowledgeBase kbase, LeftTupleSource leftInput, Accumulate accumulate) {
    BitMask leftMask = getLeftInferredMask();
    ObjectType leftObjectType = leftInput.getObjectType();
    if (leftObjectType instanceof ClassObjectType) {
        TypeDeclaration typeDeclaration = kbase.getExactTypeDeclaration(((ClassObjectType) leftObjectType).getClassType());
        if (typeDeclaration != null && typeDeclaration.isPropertyReactive()) {
            List<String> accessibleProperties = typeDeclaration.getAccessibleProperties();
            for (Declaration decl : accumulate.getRequiredDeclarations()) {
                if (leftObjectType.equals(decl.getPattern().getObjectType())) {
                    leftMask = leftMask.setAll(calculatePositiveMask(typeDeclaration.getTypeClass(), decl.getPattern().getListenedProperties(), accessibleProperties));
                }
            }
        }
    }
    setLeftInferredMask(leftMask);
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) ObjectType(org.drools.core.spi.ObjectType) ClassObjectType(org.drools.core.base.ClassObjectType) BitMask(org.drools.core.util.bitmask.BitMask) TypeDeclaration(org.drools.core.rule.TypeDeclaration) Declaration(org.drools.core.rule.Declaration) TypeDeclaration(org.drools.core.rule.TypeDeclaration)

Example 2 with ClassObjectType

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

the class MarshallingTest method testSingleRuleSingleJoinNodePattern.

@Test
public void testSingleRuleSingleJoinNodePattern() throws Exception {
    String rule = "package org.drools.compiler.test;\n";
    rule += "import org.drools.compiler.Person\n";
    rule += "import org.drools.compiler.Cheese\n";
    rule += "global java.util.List list\n";
    rule += "rule \"Rule 1\"\n";
    rule += "when\n";
    rule += "    $c : Cheese( ) \n";
    rule += "    $p : Person( cheese == $c ) \n";
    rule += "then\n";
    rule += "    list.add( $p );\n";
    rule += "end";
    KieBase kBase = loadKnowledgeBaseFromString(rule);
    // Make sure the rete node map is created correctly
    Map<Integer, BaseNode> nodes = RuleBaseNodes.getNodeMap((InternalKnowledgeBase) kBase);
    assertEquals(5, nodes.size());
    assertEquals("Cheese", ((ClassObjectType) ((ObjectTypeNode) nodes.get(3)).getObjectType()).getClassType().getSimpleName());
    assertEquals("Person", ((ClassObjectType) ((ObjectTypeNode) nodes.get(5)).getObjectType()).getClassType().getSimpleName());
    assertTrue("Should end with JoinNode", nodes.get(6).getClass().getSimpleName().endsWith("JoinNode"));
    assertEquals("Rule 1", ((RuleTerminalNode) nodes.get(7)).getRule().getName());
    KieSession session = kBase.newKieSession();
    List list = new ArrayList();
    session.setGlobal("list", list);
    Cheese stilton = new Cheese("stilton", 25);
    Cheese brie = new Cheese("brie", 49);
    Person bobba = new Person("bobba fet", 32);
    bobba.setCheese(stilton);
    Person vadar = new Person("darth vadar", 32);
    session.insert(stilton);
    session.insert(bobba);
    session.insert(vadar);
    session.insert(brie);
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
    session.fireAllRules();
    assertEquals(1, ((List) session.getGlobal("list")).size());
    assertEquals(bobba, ((List) session.getGlobal("list")).get(0));
    Person c3po = new Person("c3p0", 32);
    c3po.setCheese(stilton);
    session.insert(c3po);
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
    session.fireAllRules();
    assertEquals(2, ((List) session.getGlobal("list")).size());
    assertEquals(c3po, ((List) session.getGlobal("list")).get(1));
    Person r2d2 = new Person("r2d2", 32);
    r2d2.setCheese(brie);
    session.insert(r2d2);
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
    session.fireAllRules();
    assertEquals(3, ((List) session.getGlobal("list")).size());
    assertEquals(r2d2, ((List) session.getGlobal("list")).get(2));
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) KieBase(org.kie.api.KieBase) BaseNode(org.drools.core.common.BaseNode) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) IteratorToList(org.drools.compiler.integrationtests.IteratorToList) Cheese(org.drools.compiler.Cheese) Person(org.drools.compiler.Person) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) Test(org.junit.Test)

Example 3 with ClassObjectType

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

the class MarshallingTest method testEmptyRule.

@Test
public void testEmptyRule() throws Exception {
    String rule = "package org.drools.compiler.test;\n";
    rule += "global java.util.List list\n";
    rule += "rule \"Rule 1\"\n";
    rule += "when\n";
    rule += "then\n";
    rule += "    list.add( \"fired\" );\n";
    rule += "end";
    KieBase kBase = loadKnowledgeBaseFromString(rule);
    // Make sure the rete node map is created correctly
    Map<Integer, BaseNode> nodes = RuleBaseNodes.getNodeMap((InternalKnowledgeBase) kBase);
    assertEquals(2, nodes.size());
    assertEquals("InitialFactImpl", ((ClassObjectType) ((ObjectTypeNode) nodes.get(2)).getObjectType()).getClassType().getSimpleName());
    assertEquals("Rule 1", ((RuleTerminalNode) nodes.get(4)).getRule().getName());
    KieSession session = kBase.newKieSession();
    List list = new ArrayList();
    session.setGlobal("list", list);
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
    session.fireAllRules();
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
    assertEquals(1, ((List) session.getGlobal("list")).size());
    assertEquals("fired", ((List) session.getGlobal("list")).get(0));
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) KieBase(org.kie.api.KieBase) BaseNode(org.drools.core.common.BaseNode) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) IteratorToList(org.drools.compiler.integrationtests.IteratorToList) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) Test(org.junit.Test)

Example 4 with ClassObjectType

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

the class MarshallingTest method testSinglePattern.

@Test
public void testSinglePattern() throws Exception {
    String rule = "package org.drools.compiler.test;\n";
    rule += "import org.drools.compiler.Person\n";
    rule += "global java.util.List list\n";
    rule += "rule \"Rule 1\"\n";
    rule += "when\n";
    rule += "    $p : Person( ) \n";
    rule += "then\n";
    rule += "    list.add( $p );\n";
    rule += "end";
    KieBase kBase = loadKnowledgeBaseFromString(rule);
    // Make sure the rete node map is created correctly
    Map<Integer, BaseNode> nodes = RuleBaseNodes.getNodeMap((InternalKnowledgeBase) kBase);
    assertEquals(3, nodes.size());
    assertEquals("Person", ((ClassObjectType) ((ObjectTypeNode) nodes.get(3)).getObjectType()).getClassType().getSimpleName());
    assertEquals("Rule 1", ((RuleTerminalNode) nodes.get(5)).getRule().getName());
    KieSession session = kBase.newKieSession();
    List list = new ArrayList();
    session.setGlobal("list", list);
    Person p = new Person("bobba fet", 32);
    session.insert(p);
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
    session.fireAllRules();
    assertEquals(1, ((List) session.getGlobal("list")).size());
    assertEquals(p, ((List) session.getGlobal("list")).get(0));
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) KieBase(org.kie.api.KieBase) BaseNode(org.drools.core.common.BaseNode) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) IteratorToList(org.drools.compiler.integrationtests.IteratorToList) Person(org.drools.compiler.Person) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) Test(org.junit.Test)

Example 5 with ClassObjectType

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

the class FromTest method testFromSharing.

@Test
public void testFromSharing() {
    // Keeping original test as non-property reactive by default, just allowed.
    String drl = "import " + ListsContainer.class.getCanonicalName() + "\n" + "global java.util.List output1;\n" + "global java.util.List output2;\n" + "rule R1 when\n" + "    ListsContainer( $list : list1 )\n" + "    $s : String( length == 2 ) from $list\n" + "then\n" + "    output1.add($s);\n" + "end\n" + "rule R2 when\n" + "    ListsContainer( $list : list2 )\n" + "    $s : String( length == 2 ) from $list\n" + "then\n" + "    output2.add($s);\n" + "end\n" + "rule R3 when\n" + "    ListsContainer( $list : list2 )\n" + "    $s : String( length == 2 ) from $list\n" + "then\n" + "    output2.add($s);\n" + "end\n";
    KieBase kbase = new KieHelper(PropertySpecificOption.ALLOWED).addContent(drl, ResourceType.DRL).build();
    KieSession ksession = kbase.newKieSession();
    ReteDumper.dumpRete(kbase);
    List<String> output1 = new ArrayList<String>();
    ksession.setGlobal("output1", output1);
    List<String> output2 = new ArrayList<String>();
    ksession.setGlobal("output2", output2);
    FactHandle fh = ksession.insert(new ListsContainer());
    ksession.fireAllRules();
    assertEquals("bb", output1.get(0));
    assertEquals("22", output2.get(0));
    assertEquals("22", output2.get(1));
    EntryPointNode epn = ((InternalKnowledgeBase) kbase).getRete().getEntryPointNodes().values().iterator().next();
    ObjectTypeNode otn = epn.getObjectTypeNodes().get(new ClassObjectType(ListsContainer.class));
    // There is only 1 LIA
    assertEquals(1, otn.getObjectSinkPropagator().size());
    LeftInputAdapterNode lian = (LeftInputAdapterNode) otn.getObjectSinkPropagator().getSinks()[0];
    // There are only 2 FromNodes since R2 and R3 are sharing the second From
    LeftTupleSink[] sinks = lian.getSinkPropagator().getSinks();
    assertEquals(2, sinks.length);
    // The first from has R1 has sink
    assertEquals(1, sinks[0].getSinkPropagator().size());
    // The second from has both R2 and R3 as sinks
    assertEquals(2, sinks[1].getSinkPropagator().size());
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) FactHandle(org.kie.api.runtime.rule.FactHandle) ArrayList(java.util.ArrayList) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) LeftTupleSink(org.drools.core.reteoo.LeftTupleSink) KieHelper(org.kie.internal.utils.KieHelper) EntryPointNode(org.drools.core.reteoo.EntryPointNode) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) Test(org.junit.Test)

Aggregations

ClassObjectType (org.drools.core.base.ClassObjectType)215 Test (org.junit.Test)131 Pattern (org.drools.core.rule.Pattern)82 Declaration (org.drools.core.rule.Declaration)62 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)60 ObjectType (org.drools.core.spi.ObjectType)48 KieBase (org.kie.api.KieBase)45 InternalReadAccessor (org.drools.core.spi.InternalReadAccessor)42 ArrayList (java.util.ArrayList)31 List (java.util.List)27 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)26 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)24 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)24 Cheese (org.drools.core.test.model.Cheese)24 InternalFactHandle (org.drools.core.common.InternalFactHandle)23 KieSession (org.kie.api.runtime.KieSession)23 BetaNode (org.drools.core.reteoo.BetaNode)21 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)21 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)20 EntryPointNode (org.drools.core.reteoo.EntryPointNode)19