use of org.drools.core.spi.AlphaNodeFieldConstraint in project drools by kiegroup.
the class PhreakFromNode method doLeftUpdates.
public void doLeftUpdates(FromNode fromNode, FromMemory fm, LeftTupleSink sink, InternalWorkingMemory wm, TupleSets<LeftTuple> srcLeftTuples, TupleSets<LeftTuple> trgLeftTuples, TupleSets<LeftTuple> stagedLeftTuples) {
BetaMemory bm = fm.getBetaMemory();
ContextEntry[] context = bm.getContext();
BetaConstraints betaConstraints = fromNode.getBetaConstraints();
AlphaNodeFieldConstraint[] alphaConstraints = fromNode.getAlphaConstraints();
DataProvider dataProvider = fromNode.getDataProvider();
Class<?> resultClass = fromNode.getResultClass();
for (LeftTuple leftTuple = srcLeftTuples.getUpdateFirst(); leftTuple != null; ) {
LeftTuple next = leftTuple.getStagedNext();
PropagationContext propagationContext = leftTuple.getPropagationContext();
final Map<Object, RightTuple> previousMatches = (Map<Object, RightTuple>) leftTuple.getContextObject();
final Map<Object, RightTuple> newMatches = new HashMap<Object, RightTuple>();
leftTuple.setContextObject(newMatches);
betaConstraints.updateFromTuple(context, wm, leftTuple);
FastIterator rightIt = LinkedList.fastIterator;
for (final java.util.Iterator<?> it = dataProvider.getResults(leftTuple, wm, propagationContext, fm.providerContext); it.hasNext(); ) {
final Object object = it.next();
if ((object == null) || !resultClass.isAssignableFrom(object.getClass())) {
// skip anything if it not assignable
continue;
}
RightTuple rightTuple = previousMatches.remove(object);
if (rightTuple == null) {
// new match, propagate assert
rightTuple = fromNode.createRightTuple(leftTuple, propagationContext, wm, object);
} else {
// previous match, so reevaluate and propagate modify
if (rightIt.next(rightTuple) != null) {
// handle the odd case where more than one object has the same hashcode/equals value
previousMatches.put(object, (RightTuple) rightIt.next(rightTuple));
rightTuple.setNext(null);
}
}
checkConstraintsAndPropagate(sink, leftTuple, rightTuple, alphaConstraints, betaConstraints, propagationContext, wm, fm, context, true, trgLeftTuples, stagedLeftTuples);
fromNode.addToCreatedHandlesMap(newMatches, rightTuple);
}
for (RightTuple rightTuple : previousMatches.values()) {
for (RightTuple current = rightTuple; current != null; current = (RightTuple) rightIt.next(current)) {
deleteChildLeftTuple(propagationContext, trgLeftTuples, stagedLeftTuples, current.getFirstChild());
}
}
leftTuple.clearStaged();
leftTuple = next;
}
betaConstraints.resetTuple(context);
}
use of org.drools.core.spi.AlphaNodeFieldConstraint in project drools by kiegroup.
the class MVELTest method testMapAccessorWithStaticFieldAccess.
@Test
public void testMapAccessorWithStaticFieldAccess() {
final String str = "" + "package org.drools.compiler.test \n" + "import " + Person.class.getCanonicalName() + "\n" + "import " + Address.class.getCanonicalName() + "\n" + "import " + TestEnum.class.getCanonicalName() + "\n" + "global java.util.List list \n" + "rule \"show\" \n" + "when \n" + " $m : Person( namedAddresses[TestEnum.ONE] == new Address('s1'), namedAddresses[TestEnum.ONE].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.getNamedAddresses().put(TestEnum.ONE, 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 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());
}
}
use of org.drools.core.spi.AlphaNodeFieldConstraint in project drools by kiegroup.
the class CollectBuilder method build.
/**
* @inheritDoc
*/
public void build(final BuildContext context, final BuildUtils utils, final RuleConditionElement rce) {
boolean existSubNetwort = false;
final Collect collect = (Collect) rce;
context.pushRuleComponent(collect);
final List<BetaNodeFieldConstraint> resultBetaConstraints = context.getBetaconstraints();
final List<AlphaNodeFieldConstraint> resultAlphaConstraints = context.getAlphaConstraints();
final Pattern sourcePattern = collect.getSourcePattern();
// get builder for the pattern
final ReteooComponentBuilder builder = utils.getBuilderFor(sourcePattern);
// save tuple source and pattern offset for later if needed
final LeftTupleSource tupleSource = context.getTupleSource();
final int currentPatternIndex = context.getCurrentPatternOffset();
// builds the source pattern
builder.build(context, utils, sourcePattern);
// if object source is null, then we need to adapt tuple source into a subnetwork
if (context.getObjectSource() == null) {
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;
}
BetaConstraints binder = utils.createBetaNodeConstraint(context, context.getBetaconstraints(), false);
// indexing for the results should be always disabled
BetaConstraints resultBinder = utils.createBetaNodeConstraint(context, resultBetaConstraints, true);
CollectAccumulator accumulator = new CollectAccumulator(collect, existSubNetwort);
Accumulate accumulate = new SingleAccumulate(sourcePattern, sourcePattern.getRequiredDeclarations(), accumulator);
AccumulateNode accNode = context.getComponentFactory().getNodeFactoryService().buildAccumulateNode(context.getNextId(), context.getTupleSource(), context.getObjectSource(), resultAlphaConstraints.toArray(new AlphaNodeFieldConstraint[resultAlphaConstraints.size()]), // source binder
binder, resultBinder, 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 FromBuilder method build.
/* (non-Javadoc)
* @see org.kie.reteoo.builder.ReteooComponentBuilder#build(org.kie.reteoo.builder.BuildContext, org.kie.reteoo.builder.BuildUtils, org.kie.rule.RuleConditionElement)
*/
public void build(final BuildContext context, final BuildUtils utils, final RuleConditionElement rce) {
final From from = (From) rce;
context.pushRuleComponent(from);
@SuppressWarnings("unchecked") BetaConstraints betaConstraints = utils.createBetaNodeConstraint(context, context.getBetaconstraints(), true);
AlphaNodeFieldConstraint[] alphaNodeFieldConstraints = context.getAlphaConstraints() != null ? context.getAlphaConstraints().toArray(new AlphaNodeFieldConstraint[context.getAlphaConstraints().size()]) : new AlphaNodeFieldConstraint[0];
NodeFactory nodeFactory = context.getComponentFactory().getNodeFactoryService();
FromNode fromNode = from.isReactive() ? nodeFactory.buildReactiveFromNode(context.getNextId(), from.getDataProvider(), context.getTupleSource(), alphaNodeFieldConstraints, betaConstraints, context.isTupleMemoryEnabled(), context, from) : nodeFactory.buildFromNode(context.getNextId(), from.getDataProvider(), context.getTupleSource(), alphaNodeFieldConstraints, betaConstraints, context.isTupleMemoryEnabled(), context, from);
context.setTupleSource(utils.attachNode(context, fromNode));
context.setAlphaConstraints(null);
context.setBetaconstraints(null);
context.popRuleComponent();
}
Aggregations