Search in sources :

Example 11 with AlphaNodeFieldConstraint

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

the class PhreakFromNode method doLeftInserts.

public void doLeftInserts(FromNode fromNode, FromMemory fm, LeftTupleSink sink, InternalWorkingMemory wm, TupleSets<LeftTuple> srcLeftTuples, TupleSets<LeftTuple> trgLeftTuples) {
    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.getInsertFirst(); leftTuple != null; ) {
        LeftTuple next = leftTuple.getStagedNext();
        PropagationContext propagationContext = leftTuple.getPropagationContext();
        Map<Object, RightTuple> matches = null;
        boolean useLeftMemory = RuleNetworkEvaluator.useLeftMemory(fromNode, leftTuple);
        if (useLeftMemory) {
            fm.getBetaMemory().getLeftTupleMemory().add(leftTuple);
            matches = new LinkedHashMap<Object, RightTuple>();
            leftTuple.setContextObject(matches);
        }
        betaConstraints.updateFromTuple(context, wm, leftTuple);
        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 = fromNode.createRightTuple(leftTuple, propagationContext, wm, object);
            checkConstraintsAndPropagate(sink, leftTuple, rightTuple, alphaConstraints, betaConstraints, propagationContext, wm, fm, context, useLeftMemory, trgLeftTuples, null);
            if (useLeftMemory) {
                fromNode.addToCreatedHandlesMap(matches, rightTuple);
            }
        }
        leftTuple.clearStaged();
        leftTuple = next;
    }
    betaConstraints.resetTuple(context);
}
Also used : BetaConstraints(org.drools.core.common.BetaConstraints) PropagationContext(org.drools.core.spi.PropagationContext) BetaMemory(org.drools.core.reteoo.BetaMemory) LeftTuple(org.drools.core.reteoo.LeftTuple) PhreakJoinNode.updateChildLeftTuple(org.drools.core.phreak.PhreakJoinNode.updateChildLeftTuple) RightTuple(org.drools.core.reteoo.RightTuple) ContextEntry(org.drools.core.rule.ContextEntry) DataProvider(org.drools.core.spi.DataProvider) AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint)

Example 12 with AlphaNodeFieldConstraint

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

the class PatternBuilder method createConstraints.

private Constraints createConstraints(BuildContext context, Pattern pattern) {
    Constraints constraints = new Constraints();
    // check if cross products for identity patterns should be disabled
    checkRemoveIdentities(context, pattern, constraints.betaConstraints);
    // checks if this pattern is nested inside a NOT CE
    final boolean isNegative = isNegative(context);
    for (Constraint constraint : pattern.getConstraints()) {
        switch(constraint.getType()) {
            case ALPHA:
                linkAlphaConstraint((AlphaNodeFieldConstraint) constraint, constraints.alphaConstraints);
                break;
            case BETA:
                linkBetaConstraint((BetaNodeFieldConstraint) constraint, constraints.betaConstraints);
                if (isNegative && context.getKnowledgeBase().getConfiguration().getEventProcessingMode() == EventProcessingOption.STREAM && pattern.getObjectType().isEvent() && constraint.isTemporal()) {
                    checkDelaying(context, constraint);
                }
                break;
            case XPATH:
                constraints.xpathConstraints.add((XpathConstraint) constraint);
                break;
            default:
                throw new RuntimeException("Unknown constraint type: " + constraint.getType() + ". This is a bug. Please contact development team.");
        }
    }
    return constraints;
}
Also used : InstanceNotEqualsConstraint(org.drools.core.common.InstanceNotEqualsConstraint) BetaNodeFieldConstraint(org.drools.core.spi.BetaNodeFieldConstraint) IntervalProviderConstraint(org.drools.core.rule.IntervalProviderConstraint) XpathConstraint(org.drools.core.rule.constraint.XpathConstraint) AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) Constraint(org.drools.core.spi.Constraint)

Example 13 with AlphaNodeFieldConstraint

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

the class ReactiveFromBuilder method build.

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];
    ReactiveFromNode node = context.getComponentFactory().getNodeFactoryService().buildReactiveFromNode(context.getNextId(), from.getDataProvider(), context.getTupleSource(), alphaNodeFieldConstraints, betaConstraints, context.isTupleMemoryEnabled(), context, from);
    context.setTupleSource(utils.attachNode(context, node));
    context.setAlphaConstraints(null);
    context.setBetaconstraints(null);
    context.incrementCurrentPatternOffset();
    int patternOffset = context.getCurrentPatternOffset();
    List<XpathConstraint> xpathConstraints = context.getXpathConstraints();
    for (XpathConstraint xpathConstraint : xpathConstraints) {
        for (XpathConstraint.XpathChunk chunk : xpathConstraint.getChunks()) {
            context.setAlphaConstraints(chunk.getAlphaConstraints());
            context.setBetaconstraints(chunk.getBetaConstraints());
            context.setXpathConstraints(chunk.getXpathConstraints());
            build(context, utils, chunk.asFrom());
        }
    }
    context.setCurrentPatternOffset(patternOffset);
}
Also used : XpathConstraint(org.drools.core.rule.constraint.XpathConstraint) BetaConstraints(org.drools.core.common.BetaConstraints) AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) ReactiveFromNode(org.drools.core.reteoo.ReactiveFromNode) From(org.drools.core.rule.From) AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) XpathConstraint(org.drools.core.rule.constraint.XpathConstraint)

Example 14 with AlphaNodeFieldConstraint

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

the class CompositeObjectSinkAdapter method removeObjectSink.

public ObjectSinkPropagator removeObjectSink(final ObjectSink sink) {
    // dirty it, so it'll rebuild on next get
    this.sinks = null;
    if (this.sinksMap != null) {
        this.sinksMap.remove(sink);
    }
    if (sink.getType() == NodeTypeEnums.AlphaNode) {
        final AlphaNode alphaNode = (AlphaNode) sink;
        final AlphaNodeFieldConstraint fieldConstraint = alphaNode.getConstraint();
        if (fieldConstraint instanceof IndexableConstraint) {
            final IndexableConstraint indexableConstraint = (IndexableConstraint) fieldConstraint;
            final FieldValue value = indexableConstraint.getField();
            if (isHashable(indexableConstraint)) {
                final InternalReadAccessor fieldAccessor = indexableConstraint.getFieldExtractor();
                final int index = fieldAccessor.getIndex();
                final FieldIndex fieldIndex = unregisterFieldIndex(index);
                if (fieldIndex.isHashed()) {
                    HashKey hashKey = new HashKey(index, value, fieldAccessor);
                    this.hashedSinkMap.remove(hashKey);
                    if (fieldIndex.getCount() <= this.alphaNodeHashingThreshold - 1) {
                        // we have less than three so unhash
                        unHashSinks(fieldIndex);
                    }
                } else {
                    this.hashableSinks.remove(alphaNode);
                }
                if (this.hashableSinks != null && this.hashableSinks.isEmpty()) {
                    this.hashableSinks = null;
                }
                return size() == 1 ? new SingleObjectSinkAdapter(getSinks()[0]) : this;
            }
        }
    }
    this.otherSinks.remove((ObjectSinkNode) sink);
    if (this.otherSinks.isEmpty()) {
        this.otherSinks = null;
    }
    return size() == 1 ? new SingleObjectSinkAdapter(getSinks()[0]) : this;
}
Also used : AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) IndexableConstraint(org.drools.core.rule.IndexableConstraint) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) FieldValue(org.drools.core.spi.FieldValue) AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) IndexableConstraint(org.drools.core.rule.IndexableConstraint)

Example 15 with AlphaNodeFieldConstraint

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

the class CompositeObjectSinkAdapter method hashSinks.

void hashSinks(final FieldIndex fieldIndex) {
    if (this.hashedSinkMap == null) {
        this.hashedSinkMap = new ObjectHashMap();
    }
    final int index = fieldIndex.getIndex();
    final InternalReadAccessor fieldReader = fieldIndex.getFieldExtractor();
    ObjectSinkNode currentSink = this.hashableSinks.getFirst();
    while (currentSink != null) {
        final AlphaNode alphaNode = (AlphaNode) currentSink;
        final AlphaNodeFieldConstraint fieldConstraint = alphaNode.getConstraint();
        final IndexableConstraint indexableConstraint = (IndexableConstraint) fieldConstraint;
        // position to the next sink now because alphaNode may be removed if the index is equal. If we were to do this
        // afterwards, currentSink.nextNode would be null
        currentSink = currentSink.getNextObjectSinkNode();
        // the right field index
        if (index == indexableConstraint.getFieldExtractor().getIndex()) {
            final FieldValue value = indexableConstraint.getField();
            this.hashedSinkMap.put(new HashKey(index, value, fieldReader), alphaNode);
            // remove the alpha from the possible candidates of hashable sinks since it is now hashed
            hashableSinks.remove(alphaNode);
        }
    }
    if (this.hashableSinks.isEmpty()) {
        this.hashableSinks = null;
    }
    fieldIndex.setHashed(true);
}
Also used : AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) IndexableConstraint(org.drools.core.rule.IndexableConstraint) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) ObjectHashMap(org.drools.core.util.ObjectHashMap) FieldValue(org.drools.core.spi.FieldValue) AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) IndexableConstraint(org.drools.core.rule.IndexableConstraint)

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