Search in sources :

Example 6 with AlphaNodeFieldConstraint

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

the class PatternBuilder method buildAlphaNodeChain.

protected void buildAlphaNodeChain(BuildContext context, BuildUtils utils, Pattern pattern, List<AlphaNodeFieldConstraint> alphaConstraints) {
    for (final AlphaNodeFieldConstraint constraint : alphaConstraints) {
        context.pushRuleComponent(constraint);
        context.setObjectSource(utils.attachNode(context, context.getComponentFactory().getNodeFactoryService().buildAlphaNode(context.getNextId(), constraint, context.getObjectSource(), context)));
        context.popRuleComponent();
    }
}
Also used : AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint)

Example 7 with AlphaNodeFieldConstraint

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

the class WindowNode method assertObject.

public void assertObject(final InternalFactHandle factHandle, final PropagationContext pctx, final InternalWorkingMemory workingMemory) {
    EventFactHandle evFh = (EventFactHandle) factHandle;
    for (AlphaNodeFieldConstraint constraint : constraints) {
        if (!constraint.isAllowed(evFh, workingMemory)) {
            return;
        }
    }
    RightTuple rightTuple = new RightTupleImpl(evFh, this);
    rightTuple.setPropagationContext(pctx);
    // this is cloned, as we need to separate the child RightTuple references
    InternalFactHandle clonedFh = evFh.cloneAndLink();
    rightTuple.setContextObject(clonedFh);
    // process the behavior
    final WindowMemory memory = workingMemory.getNodeMemory(this);
    if (!behavior.assertFact(memory.behaviorContext, clonedFh, pctx, workingMemory)) {
        return;
    }
    this.sink.propagateAssertObject(clonedFh, pctx, workingMemory);
}
Also used : AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) WindowMemory(org.drools.core.reteoo.WindowNode.WindowMemory) EventFactHandle(org.drools.core.common.EventFactHandle) InternalFactHandle(org.drools.core.common.InternalFactHandle)

Example 8 with AlphaNodeFieldConstraint

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

the class MVELTest method testArrayAccessorWithStaticFieldAccess.

@Test
public void testArrayAccessorWithStaticFieldAccess() {
    final String str = "" + "package org.drools.compiler.test \n" + "import " + Person.class.getCanonicalName() + "\n" + "import " + Address.class.getCanonicalName() + "\n" + "import " + Triangle.class.getCanonicalName() + "\n" + "global java.util.List list \n" + "rule \"show\" \n" + "when  \n" + "    $m : Person( addresses[Triangle.ZERO] == new Address('s1'), addresses[Triangle.ZERO].street == new Address('s1').street ) \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.addAddress(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;
        }
    }
    AlphaNode alphanode = (AlphaNode) node.getObjectSinkPropagator().getSinks()[0];
    AlphaNodeFieldConstraint constraint = alphanode.getConstraint();
    if (constraint instanceof MvelConstraint) {
        assertTrue(((MvelConstraint) alphanode.getConstraint()).getFieldExtractor() instanceof MVELObjectClassFieldReader);
        assertEquals(new Address("s1"), ((MvelConstraint) alphanode.getConstraint()).getField().getValue());
    }
    alphanode = (AlphaNode) alphanode.getObjectSinkPropagator().getSinks()[0];
    constraint = alphanode.getConstraint();
    if (constraint instanceof MvelConstraint) {
        assertTrue(((MvelConstraint) alphanode.getConstraint()).getFieldExtractor() instanceof MVELObjectClassFieldReader);
        assertEquals(new Address("s1").getStreet(), ((MvelConstraint) alphanode.getConstraint()).getField().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) MVELObjectClassFieldReader(org.drools.core.base.extractors.MVELObjectClassFieldReader) AlphaNode(org.drools.core.reteoo.AlphaNode) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Person(org.drools.compiler.Person) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 9 with AlphaNodeFieldConstraint

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

the class MVELTest method testArrayAccessorWithGenerics.

@Test
public void testArrayAccessorWithGenerics() {
    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( addresses[0] == new Address('s1'), addresses[0].street == new Address('s1').street ) \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.addAddress(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;
        }
    }
    AlphaNode alphanode = (AlphaNode) node.getObjectSinkPropagator().getSinks()[0];
    AlphaNodeFieldConstraint constraint = alphanode.getConstraint();
    if (constraint instanceof MvelConstraint) {
        assertTrue(((MvelConstraint) constraint).getFieldExtractor() instanceof MVELObjectClassFieldReader);
        assertEquals(new Address("s1"), ((MvelConstraint) constraint).getField().getValue());
    }
    alphanode = (AlphaNode) alphanode.getObjectSinkPropagator().getSinks()[0];
    constraint = alphanode.getConstraint();
    if (constraint instanceof MvelConstraint) {
        assertTrue(((MvelConstraint) constraint).getFieldExtractor() instanceof MVELObjectClassFieldReader);
        assertEquals(new Address("s1").getStreet(), ((MvelConstraint) constraint).getField().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) MVELObjectClassFieldReader(org.drools.core.base.extractors.MVELObjectClassFieldReader) AlphaNode(org.drools.core.reteoo.AlphaNode) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Person(org.drools.compiler.Person) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 10 with AlphaNodeFieldConstraint

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

the class PhreakAccumulateNode method evaluateResultConstraints.

private void evaluateResultConstraints(final AccumulateNode accNode, final LeftTupleSink sink, final Accumulate accumulate, final LeftTuple leftTuple, final PropagationContext context, final InternalWorkingMemory workingMemory, final AccumulateMemory memory, final AccumulateContext accctx, final TupleSets<LeftTuple> trgLeftTuples, final TupleSets<LeftTuple> stagedLeftTuples) {
    // get the actual result
    Object result = accumulate.getResult(memory.workingMemoryContext, accctx.context, leftTuple, workingMemory);
    if (result == null) {
        return;
    }
    if (accctx.getResultFactHandle() == null) {
        final InternalFactHandle handle = accNode.createResultFactHandle(context, workingMemory, leftTuple, result);
        accctx.setResultFactHandle(handle);
        accctx.setResultLeftTuple(sink.createLeftTuple(handle, leftTuple, sink));
    } else {
        accctx.getResultFactHandle().setObject(result);
    }
    // First alpha node filters
    AlphaNodeFieldConstraint[] resultConstraints = accNode.getResultConstraints();
    BetaConstraints resultBinder = accNode.getResultBinder();
    boolean isAllowed = true;
    for (AlphaNodeFieldConstraint resultConstraint : resultConstraints) {
        if (!resultConstraint.isAllowed(accctx.resultFactHandle, workingMemory)) {
            isAllowed = false;
            break;
        }
    }
    if (isAllowed) {
        resultBinder.updateFromTuple(memory.resultsContext, workingMemory, leftTuple);
        if (!resultBinder.isAllowedCachedLeft(memory.resultsContext, accctx.getResultFactHandle())) {
            isAllowed = false;
        }
        resultBinder.resetTuple(memory.resultsContext);
    }
    LeftTuple childLeftTuple = accctx.getResultLeftTuple();
    if (accctx.getPropagationContext() != null) {
        childLeftTuple.setPropagationContext(accctx.getPropagationContext());
        accctx.setPropagationContext(null);
    } else {
        childLeftTuple.setPropagationContext(leftTuple.getPropagationContext());
    }
    if (accctx.propagated) {
        normalizeStagedTuples(stagedLeftTuples, childLeftTuple);
        if (isAllowed) {
            // modify
            trgLeftTuples.addUpdate(childLeftTuple);
        } else {
            // retract
            trgLeftTuples.addDelete(childLeftTuple);
            accctx.propagated = false;
        }
    } else if (isAllowed) {
        // assert
        trgLeftTuples.addInsert(childLeftTuple);
        accctx.propagated = true;
    }
}
Also used : AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) BetaConstraints(org.drools.core.common.BetaConstraints) InternalFactHandle(org.drools.core.common.InternalFactHandle) LeftTuple(org.drools.core.reteoo.LeftTuple)

Aggregations

AlphaNodeFieldConstraint (org.drools.core.spi.AlphaNodeFieldConstraint)18 BetaConstraints (org.drools.core.common.BetaConstraints)7 ArrayList (java.util.ArrayList)6 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)5 AlphaNode (org.drools.core.reteoo.AlphaNode)5 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)5 MvelConstraint (org.drools.core.rule.constraint.MvelConstraint)5 Test (org.junit.Test)5 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)5 List (java.util.List)4 Address (org.drools.compiler.Address)4 Person (org.drools.compiler.Person)4 MVELObjectClassFieldReader (org.drools.core.base.extractors.MVELObjectClassFieldReader)4 KieSession (org.kie.api.runtime.KieSession)4 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)4 LeftTuple (org.drools.core.reteoo.LeftTuple)3 BetaNodeFieldConstraint (org.drools.core.spi.BetaNodeFieldConstraint)3 FieldValue (org.drools.core.spi.FieldValue)3 EventFactHandle (org.drools.core.common.EventFactHandle)2 InternalFactHandle (org.drools.core.common.InternalFactHandle)2