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