Search in sources :

Example 16 with AlphaNodeFieldConstraint

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

the class WindowNode method modifyRightTuple.

@Override
public void modifyRightTuple(RightTuple rightTuple, PropagationContext context, InternalWorkingMemory workingMemory) {
    EventFactHandle originalFactHandle = (EventFactHandle) rightTuple.getFactHandle();
    EventFactHandle cloneFactHandle = (EventFactHandle) rightTuple.getContextObject();
    // make sure all fields are updated
    originalFactHandle.quickCloneUpdate(cloneFactHandle);
    // behavior modify
    boolean isAllowed = true;
    for (AlphaNodeFieldConstraint constraint : constraints) {
        if (!constraint.isAllowed(cloneFactHandle, workingMemory)) {
            isAllowed = false;
            break;
        }
    }
    if (isAllowed) {
        ModifyPreviousTuples modifyPreviousTuples = new ModifyPreviousTuples(cloneFactHandle.detachLinkedTuples());
        this.sink.propagateModifyObject(cloneFactHandle, modifyPreviousTuples, context, workingMemory);
        modifyPreviousTuples.retractTuples(context, workingMemory);
    } else {
        ObjectTypeNode.doRetractObject(cloneFactHandle, context, workingMemory);
    }
}
Also used : AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) EventFactHandle(org.drools.core.common.EventFactHandle)

Example 17 with AlphaNodeFieldConstraint

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

the class AccumulateBuilder method build.

/**
 * @inheritDoc
 */
public void build(final BuildContext context, final BuildUtils utils, final RuleConditionElement rce) {
    final Accumulate accumulate = (Accumulate) rce;
    boolean existSubNetwort = false;
    context.pushRuleComponent(accumulate);
    final List<BetaNodeFieldConstraint> resultBetaConstraints = context.getBetaconstraints();
    final List<AlphaNodeFieldConstraint> resultAlphaConstraints = context.getAlphaConstraints();
    RuleConditionElement source = accumulate.getSource();
    if (source instanceof GroupElement) {
        GroupElement ge = (GroupElement) source;
        if (ge.isAnd() && ge.getChildren().size() == 1) {
            source = ge.getChildren().get(0);
        }
    }
    // get builder for the pattern
    final ReteooComponentBuilder builder = utils.getBuilderFor(source);
    // save tuple source and current pattern offset for later if needed
    LeftTupleSource tupleSource = context.getTupleSource();
    final int currentPatternIndex = context.getCurrentPatternOffset();
    // builds the source pattern
    builder.build(context, utils, source);
    // if object source is null, then we need to adapt tuple source into a subnetwork
    if (context.getObjectSource() == null) {
        // attach right input adapter node to convert tuple source into an object source
        RightInputAdapterNode riaNode = context.getComponentFactory().getNodeFactoryService().buildRightInputNode(context.getNextId(), context.getTupleSource(), tupleSource, context);
        // attach right input adapter node to convert tuple source into an object source
        context.setObjectSource(utils.attachNode(context, riaNode));
        // restore tuple source from before the start of the sub network
        context.setTupleSource(tupleSource);
        // create a tuple start equals constraint and set it in the context
        final TupleStartEqualsConstraint constraint = TupleStartEqualsConstraint.getInstance();
        final List<BetaNodeFieldConstraint> betaConstraints = new ArrayList<BetaNodeFieldConstraint>();
        betaConstraints.add(constraint);
        context.setBetaconstraints(betaConstraints);
        existSubNetwort = true;
    }
    NodeFactory nfactory = context.getComponentFactory().getNodeFactoryService();
    final BetaConstraints resultsBinder = utils.createBetaNodeConstraint(context, resultBetaConstraints, true);
    final BetaConstraints sourceBinder = utils.createBetaNodeConstraint(context, context.getBetaconstraints(), false);
    AccumulateNode accNode = nfactory.buildAccumulateNode(context.getNextId(), context.getTupleSource(), context.getObjectSource(), resultAlphaConstraints.toArray(new AlphaNodeFieldConstraint[resultAlphaConstraints.size()]), sourceBinder, resultsBinder, accumulate, existSubNetwort, context);
    context.setTupleSource(utils.attachNode(context, accNode));
    // source pattern was bound, so nulling context
    context.setObjectSource(null);
    context.setCurrentPatternOffset(currentPatternIndex);
    context.popRuleComponent();
}
Also used : BetaConstraints(org.drools.core.common.BetaConstraints) AccumulateNode(org.drools.core.reteoo.AccumulateNode) GroupElement(org.drools.core.rule.GroupElement) ArrayList(java.util.ArrayList) TupleStartEqualsConstraint(org.drools.core.common.TupleStartEqualsConstraint) RuleConditionElement(org.drools.core.rule.RuleConditionElement) BetaNodeFieldConstraint(org.drools.core.spi.BetaNodeFieldConstraint) AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) BetaNodeFieldConstraint(org.drools.core.spi.BetaNodeFieldConstraint) TupleStartEqualsConstraint(org.drools.core.common.TupleStartEqualsConstraint) Accumulate(org.drools.core.rule.Accumulate) LeftTupleSource(org.drools.core.reteoo.LeftTupleSource) AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) RightInputAdapterNode(org.drools.core.reteoo.RightInputAdapterNode)

Example 18 with AlphaNodeFieldConstraint

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

the class JavaDialectTest method testEvalDetectionInAlphaNode.

@Test
public void testEvalDetectionInAlphaNode() {
    // Tests evals are generated and executed with Java dialect
    String drl = "";
    drl += "package org.drools.compiler.test\n";
    drl += "import org.drools.compiler.Person\n";
    drl += "global java.util.List list\n";
    drl += "rule test1\n";
    drl += "when\n";
    drl += "   $p1 : Person( eval( name \n != null ), name == ( new String(\"xxx\") ) )\n";
    drl += "then\n";
    drl += "end\n";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newReaderResource(new StringReader(drl)), ResourceType.DRL);
    KnowledgeBuilderErrors errors = kbuilder.getErrors();
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    assertFalse(kbuilder.hasErrors());
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    List<ObjectTypeNode> nodes = ((KnowledgeBaseImpl) kbase).getRete().getObjectTypeNodes();
    ObjectTypeNode node = null;
    for (ObjectTypeNode n : nodes) {
        if (((ClassObjectType) n.getObjectType()).getClassType() == Person.class) {
            node = n;
            break;
        }
    }
    AlphaNode alphanode = (AlphaNode) node.getObjectSinkPropagator().getSinks()[0];
    PredicateConstraint c = (PredicateConstraint) alphanode.getConstraint();
    assertTrue(c.getPredicateExpression() instanceof PredicateExpression);
    assertTrue(c.getPredicateExpression() instanceof CompiledInvoker);
    alphanode = (AlphaNode) alphanode.getObjectSinkPropagator().getSinks()[0];
    AlphaNodeFieldConstraint constraint = alphanode.getConstraint();
    if (constraint instanceof MvelConstraint) {
        FieldValue fieldVal = ((MvelConstraint) constraint).getField();
        assertEquals("xxx", fieldVal.getValue());
    }
}
Also used : KnowledgeBuilderErrors(org.kie.internal.builder.KnowledgeBuilderErrors) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) PredicateExpression(org.drools.core.spi.PredicateExpression) AlphaNode(org.drools.core.reteoo.AlphaNode) CompiledInvoker(org.drools.core.spi.CompiledInvoker) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) StringReader(java.io.StringReader) PredicateConstraint(org.drools.core.rule.PredicateConstraint) FieldValue(org.drools.core.spi.FieldValue) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

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