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);
}
}
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();
}
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());
}
}
Aggregations