Search in sources :

Example 56 with ObjectTypeNode

use of org.drools.core.reteoo.ObjectTypeNode in project drools by kiegroup.

the class MVELTest method testNewConstructor.

@Test
public void testNewConstructor() {
    final String str = "" + "package org.drools.compiler.test \n" + "import " + Person.class.getCanonicalName() + "\n" + "import " + Address.class.getCanonicalName() + "\n" + "global java.util.List list \n" + "rule \"show\" \n" + "when  \n" + "    $m : Person( address == new Address('s1')) \n" + "then \n" + "    list.add('r1'); \n" + "end \n";
    final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    final InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    final KieSession ksession = createKnowledgeSession(kbase);
    final List list = new ArrayList();
    ksession.setGlobal("list", list);
    final Person p = new Person("yoda");
    p.setAddress(new Address("s1"));
    ksession.insert(p);
    ksession.fireAllRules();
    assertEquals("r1", list.get(0));
    // Check it was built with MVELReturnValueExpression constraint
    final List<ObjectTypeNode> nodes = ((InternalKnowledgeBase) kbase).getRete().getObjectTypeNodes();
    ObjectTypeNode node = null;
    for (final ObjectTypeNode n : nodes) {
        if (((ClassObjectType) n.getObjectType()).getClassType() == Person.class) {
            node = n;
            break;
        }
    }
    final AlphaNode alphanode = (AlphaNode) node.getObjectSinkPropagator().getSinks()[0];
    final AlphaNodeFieldConstraint constraint = alphanode.getConstraint();
    if (constraint instanceof MvelConstraint) {
        assertTrue(((MvelConstraint) constraint).getFieldExtractor() instanceof ClassFieldReader);
        final FieldValue r = ((MvelConstraint) constraint).getField();
        assertEquals(p.getAddress(), r.getValue());
    }
}
Also used : Address(org.drools.compiler.Address) ArrayList(java.util.ArrayList) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) AlphaNode(org.drools.core.reteoo.AlphaNode) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) ClassFieldReader(org.drools.core.base.ClassFieldReader) MVELObjectClassFieldReader(org.drools.core.base.extractors.MVELObjectClassFieldReader) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) FieldValue(org.drools.core.spi.FieldValue) Person(org.drools.compiler.Person) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 57 with ObjectTypeNode

use of org.drools.core.reteoo.ObjectTypeNode in project drools by kiegroup.

the class MemoryLeakTest method testStagedTupleLeak.

@Test
public void testStagedTupleLeak() throws Exception {
    // BZ-1056599
    String str = "rule R1 when\n" + "    $i : Integer()\n" + "then\n" + "    insertLogical( $i.toString() );\n" + "end\n" + "\n" + "rule R2 when\n" + "    $i : Integer()\n" + "then\n" + "    delete( $i );\n" + "end\n" + "\n" + "rule R3 when\n" + "    $l : Long()\n" + "    $s : String( this == $l.toString() )\n" + "then\n" + "end\n";
    KieBase kbase = new KieHelper().addContent(str, ResourceType.DRL).build();
    KieSession ksession = kbase.newKieSession();
    for (int i = 0; i < 10; i++) {
        ksession.insert(i);
        ksession.fireAllRules();
    }
    Rete rete = ((KnowledgeBaseImpl) kbase).getRete();
    JoinNode joinNode = null;
    for (ObjectTypeNode otn : rete.getObjectTypeNodes()) {
        if (String.class == otn.getObjectType().getValueType().getClassType()) {
            joinNode = (JoinNode) otn.getObjectSinkPropagator().getSinks()[0];
            break;
        }
    }
    assertNotNull(joinNode);
    InternalWorkingMemory wm = (InternalWorkingMemory) ksession;
    BetaMemory memory = (BetaMemory) wm.getNodeMemory(joinNode);
    TupleSets<RightTuple> stagedRightTuples = memory.getStagedRightTuples();
    assertNull(stagedRightTuples.getDeleteFirst());
    assertNull(stagedRightTuples.getInsertFirst());
}
Also used : Rete(org.drools.core.reteoo.Rete) JoinNode(org.drools.core.reteoo.JoinNode) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) KieHelper(org.kie.internal.utils.KieHelper) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) BetaMemory(org.drools.core.reteoo.BetaMemory) RightTuple(org.drools.core.reteoo.RightTuple) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 58 with ObjectTypeNode

use of org.drools.core.reteoo.ObjectTypeNode in project drools by kiegroup.

the class CepEspTest method testEventExpiration2.

@Test(timeout = 10000)
public void testEventExpiration2() throws Exception {
    // read in the source
    KieBaseConfiguration kbc = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kbc.setOption(EventProcessingOption.STREAM);
    KieBase kbase = loadKnowledgeBase(kbc, "test_CEP_EventExpiration2.drl");
    Map<ObjectType, ObjectTypeNode> objectTypeNodes = ((KnowledgeBaseImpl) kbase).getRete().getObjectTypeNodes(EntryPointId.DEFAULT);
    ObjectTypeNode node = objectTypeNodes.get(new ClassObjectType(StockTick.class));
    assertNotNull(node);
    // the expiration policy @expires(10m) should override the temporal operator usage
    assertEquals(TimeIntervalParser.parse("10m")[0] + 1, node.getExpirationOffset());
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) ObjectType(org.drools.core.spi.ObjectType) ClassObjectType(org.drools.core.base.ClassObjectType) ClassObjectType(org.drools.core.base.ClassObjectType) StockTick(org.drools.compiler.StockTick) KieBase(org.kie.api.KieBase) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) Test(org.junit.Test)

Example 59 with ObjectTypeNode

use of org.drools.core.reteoo.ObjectTypeNode in project drools by kiegroup.

the class AlphaNetworkModifyTest method testModifyWithLiaToAcc.

@Test
public void testModifyWithLiaToAcc() {
    // technically you can't have a modify with InitialFactImpl
    // But added test for completeness
    String str = "";
    str += "package org.simple \n";
    str += "import " + Person.class.getCanonicalName() + "\n";
    str += "import " + Cheese.class.getCanonicalName() + "\n";
    str += "import " + Cat.class.getCanonicalName() + "\n";
    str += "global java.util.List list \n";
    str += "rule x1 \n";
    str += "when \n";
    str += "    Object() from accumulate( $p : Person() and Cheese(), collectList( $p ) )\n";
    str += "    Person() \n";
    str += "then \n";
    str += "end  \n";
    str += "rule x2 \n";
    str += "when \n";
    str += "    Object() from accumulate( $ch : Cheese(), collectList( $ch ) )\n";
    str += "    Person() \n";
    str += "then \n";
    str += "end  \n";
    str += "rule x3 \n";
    str += "when \n";
    str += "    Object() from accumulate( $ch : Cheese(), collectList( $ch ) )\n";
    str += "    Person() \n";
    str += "then \n";
    str += "end  \n";
    str += "rule x4 \n";
    str += "when \n";
    str += "    Object() from accumulate( $ch : Cheese(), collectList( $ch ) )\n";
    str += "    Person() \n";
    str += "then \n";
    str += "end  \n";
    KieBase kbase = loadKnowledgeBaseFromString(str);
    KieSession wm = kbase.newKieSession();
    wm.fireAllRules();
    ObjectTypeNode otnInit = getObjectTypeNode(kbase, "InitialFactImpl");
    LeftInputAdapterNode liaNode = (LeftInputAdapterNode) otnInit.getObjectSinkPropagator().getSinks()[0];
    LeftTupleSink[] sinks = liaNode.getSinkPropagator().getSinks();
    assertEquals(0, sinks[0].getLeftInputOtnId().getId());
    assertEquals(1, sinks[1].getLeftInputOtnId().getId());
    assertEquals(2, sinks[2].getLeftInputOtnId().getId());
    ObjectTypeNode otnPerson = getObjectTypeNode(kbase, "Person");
    ObjectTypeNode otnCheese = getObjectTypeNode(kbase, "Cheese");
    assertEquals(0, otnPerson.getOtnIdCounter());
    assertEquals(0, otnCheese.getOtnIdCounter());
    wm.insert(new Person());
    wm.insert(new Cheese());
    wm.fireAllRules();
    assertEquals(3, otnPerson.getOtnIdCounter());
    assertEquals(2, otnCheese.getOtnIdCounter());
}
Also used : KieBase(org.kie.api.KieBase) Cat(org.drools.compiler.Cat) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) LeftTupleSink(org.drools.core.reteoo.LeftTupleSink) Cheese(org.drools.compiler.Cheese) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.compiler.Person) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) Test(org.junit.Test)

Example 60 with ObjectTypeNode

use of org.drools.core.reteoo.ObjectTypeNode in project drools by kiegroup.

the class PatternBuilder method attachObjectTypeNode.

private void attachObjectTypeNode(final BuildContext context, final BuildUtils utils, final Pattern pattern) {
    boolean objectMemory = context.isObjectTypeNodeMemoryEnabled();
    ObjectType objectType = pattern.getObjectType();
    if (pattern.getObjectType() instanceof ClassObjectType) {
        // Is this the query node, if so we don't want any memory
        if (DroolsQuery.class == ((ClassObjectType) pattern.getObjectType()).getClassType()) {
            context.setTupleMemoryEnabled(false);
            context.setObjectTypeNodeMemoryEnabled(false);
        }
    }
    ObjectTypeNode otn = context.getComponentFactory().getNodeFactoryService().buildObjectTypeNode(context.getNextId(), (EntryPointNode) context.getObjectSource(), objectType, context);
    if (objectType.isEvent() && EventProcessingOption.STREAM.equals(context.getKnowledgeBase().getConfiguration().getEventProcessingMode())) {
        ExpirationSpec expirationSpec = getExpirationForType(context, objectType);
        if (expirationSpec.offset != NEVER_EXPIRES && expirationSpec.hard) {
            // hard expiration is set, so use it
            otn.setExpirationOffset(expirationSpec.offset);
        } else {
            // otherwise calculate it based on behaviours and temporal constraints
            long offset = NEVER_EXPIRES;
            for (Behavior behavior : pattern.getBehaviors()) {
                if (behavior.getExpirationOffset() != NEVER_EXPIRES) {
                    offset = Math.max(behavior.getExpirationOffset(), offset);
                }
            }
            // if there's no implicit expiration uses the (eventually set) soft one
            if (offset == NEVER_EXPIRES && !expirationSpec.hard) {
                offset = expirationSpec.offset;
            }
            long distance = context.getExpirationOffset(pattern);
            if (distance == NEVER_EXPIRES) {
                // it means the rules have no temporal constraints, or
                // the constraints require events to be hold forever. In this
                // case, we allow type declarations to override the implicit
                // expiration offset by defining an expiration policy with the
                // @expires tag
                otn.setExpirationOffset(offset);
            } else {
                otn.setExpirationOffset(Math.max(distance, offset));
            }
        }
    }
    context.setObjectSource(utils.attachNode(context, otn));
    context.setObjectTypeNodeMemoryEnabled(objectMemory);
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) ObjectType(org.drools.core.spi.ObjectType) ClassObjectType(org.drools.core.base.ClassObjectType) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) Behavior(org.drools.core.rule.Behavior)

Aggregations

ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)116 Test (org.junit.Test)100 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)69 KieBase (org.kie.api.KieBase)64 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)60 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)40 ClassObjectType (org.drools.core.base.ClassObjectType)34 JoinNode (org.drools.core.reteoo.JoinNode)31 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)30 AlphaNode (org.drools.core.reteoo.AlphaNode)29 ArrayList (java.util.ArrayList)28 BetaNode (org.drools.core.reteoo.BetaNode)27 BetaMemory (org.drools.core.reteoo.BetaMemory)24 List (java.util.List)23 SegmentMemory (org.drools.core.reteoo.SegmentMemory)23 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)23 LiaNodeMemory (org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory)22 KieSession (org.kie.api.runtime.KieSession)20 Person (org.drools.compiler.Person)17 EntryPointNode (org.drools.core.reteoo.EntryPointNode)16