Search in sources :

Example 1 with Activation

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

the class ProtobufOutputMarshaller method writeAgenda.

private static void writeAgenda(MarshallerWriteContext context, ProtobufMessages.RuleData.Builder _ksb) throws IOException {
    InternalWorkingMemory wm = context.wm;
    InternalAgenda agenda = wm.getAgenda();
    org.drools.core.marshalling.impl.ProtobufMessages.Agenda.Builder _ab = ProtobufMessages.Agenda.newBuilder();
    AgendaGroup[] agendaGroups = agenda.getAgendaGroupsMap().values().toArray(new AgendaGroup[agenda.getAgendaGroupsMap().size()]);
    Arrays.sort(agendaGroups, AgendaGroupSorter.instance);
    for (AgendaGroup ag : agendaGroups) {
        AgendaGroupQueueImpl group = (AgendaGroupQueueImpl) ag;
        org.drools.core.marshalling.impl.ProtobufMessages.Agenda.AgendaGroup.Builder _agb = ProtobufMessages.Agenda.AgendaGroup.newBuilder();
        _agb.setName(group.getName()).setIsActive(group.isActive()).setIsAutoDeactivate(group.isAutoDeactivate()).setClearedForRecency(group.getClearedForRecency()).setHasRuleFlowLister(group.isRuleFlowListener()).setActivatedForRecency(group.getActivatedForRecency());
        Map<Long, String> nodeInstances = group.getNodeInstances();
        for (Map.Entry<Long, String> entry : nodeInstances.entrySet()) {
            org.drools.core.marshalling.impl.ProtobufMessages.Agenda.AgendaGroup.NodeInstance.Builder _nib = ProtobufMessages.Agenda.AgendaGroup.NodeInstance.newBuilder();
            _nib.setProcessInstanceId(entry.getKey());
            _nib.setNodeInstanceId(entry.getValue());
            _agb.addNodeInstance(_nib.build());
        }
        _ab.addAgendaGroup(_agb.build());
    }
    org.drools.core.marshalling.impl.ProtobufMessages.Agenda.FocusStack.Builder _fsb = ProtobufMessages.Agenda.FocusStack.newBuilder();
    LinkedList<AgendaGroup> focusStack = agenda.getStackList();
    for (AgendaGroup group : focusStack) {
        _fsb.addGroupName(group.getName());
    }
    _ab.setFocusStack(_fsb.build());
    // serialize all dormant activations
    org.drools.core.util.Iterator it = ActivationIterator.iterator(wm);
    List<org.drools.core.spi.Activation> dormant = new ArrayList<org.drools.core.spi.Activation>();
    for (org.drools.core.spi.Activation item = (org.drools.core.spi.Activation) it.next(); item != null; item = (org.drools.core.spi.Activation) it.next()) {
        if (!item.isQueued()) {
            dormant.add(item);
        }
    }
    Collections.sort(dormant, ActivationsSorter.INSTANCE);
    for (org.drools.core.spi.Activation activation : dormant) {
        _ab.addMatch(writeActivation(context, (AgendaItem) activation));
    }
    // serialize all network evaluator activations
    for (Activation activation : agenda.getActivations()) {
        if (activation.isRuleAgendaItem()) {
            // serialize it
            _ab.addRuleActivation(writeActivation(context, (AgendaItem) activation));
        }
    }
    _ksb.setAgenda(_ab.build());
}
Also used : AgendaGroup(org.drools.core.spi.AgendaGroup) ArrayList(java.util.ArrayList) AgendaGroupQueueImpl(org.drools.core.common.AgendaGroupQueueImpl) Activation(org.drools.core.spi.Activation) ByteString(com.google.protobuf.ByteString) RuleAgendaItem(org.drools.core.phreak.RuleAgendaItem) AgendaItem(org.drools.core.common.AgendaItem) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) InternalAgenda(org.drools.core.common.InternalAgenda) Activation(org.drools.core.spi.Activation) InternalAgenda(org.drools.core.common.InternalAgenda) Map(java.util.Map) ObjectHashMap(org.drools.core.util.ObjectHashMap)

Example 2 with Activation

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

the class ProtobufOutputMarshaller method writeBeliefSet.

private static void writeBeliefSet(MarshallerWriteContext context, BeliefSet beliefSet, org.drools.core.marshalling.impl.ProtobufMessages.EqualityKey.Builder _key) throws IOException {
    ProtobufMessages.BeliefSet.Builder _beliefSet = ProtobufMessages.BeliefSet.newBuilder();
    _beliefSet.setHandleId(beliefSet.getFactHandle().getId());
    ObjectMarshallingStrategyStore objectMarshallingStrategyStore = context.objectMarshallingStrategyStore;
    // for ( LinkedListEntry node = (LinkedListEntry) beliefSet.getFirst(); node != null; node = (LinkedListEntry) node.getNext() ) {
    FastIterator it = beliefSet.iterator();
    for (LinkedListEntry node = (LinkedListEntry) beliefSet.getFirst(); node != null; node = (LinkedListEntry) it.next(node)) {
        LogicalDependency belief = (LogicalDependency) node.getObject();
        ProtobufMessages.LogicalDependency.Builder _logicalDependency = ProtobufMessages.LogicalDependency.newBuilder();
        // _belief.setActivation( value )
        LogicalDependency dependency = (LogicalDependency) node.getObject();
        org.drools.core.spi.Activation activation = dependency.getJustifier();
        ProtobufMessages.Activation _activation = ProtobufMessages.Activation.newBuilder().setPackageName(activation.getRule().getPackage()).setRuleName(activation.getRule().getName()).setTuple(PersisterHelper.createTuple(activation.getTuple())).build();
        _logicalDependency.setActivation(_activation);
        if (belief.getObject() != null) {
            ObjectMarshallingStrategy strategy = objectMarshallingStrategyStore.getStrategyObject(belief.getObject());
            Integer index = context.getStrategyIndex(strategy);
            _logicalDependency.setObjectStrategyIndex(index);
            _logicalDependency.setObject(ByteString.copyFrom(strategy.marshal(context.strategyContext.get(strategy), context, belief.getObject())));
        }
        if (belief.getMode() != null) {
            ObjectMarshallingStrategy strategy = objectMarshallingStrategyStore.getStrategyObject(belief.getMode());
            Integer index = context.getStrategyIndex(strategy);
            _logicalDependency.setValueStrategyIndex(index);
            _logicalDependency.setValue(ByteString.copyFrom(strategy.marshal(context.strategyContext.get(strategy), context, belief.getMode())));
        }
        _beliefSet.addLogicalDependency(_logicalDependency.build());
    }
    _key.setBeliefSet(_beliefSet);
}
Also used : ObjectMarshallingStrategyStore(org.kie.api.marshalling.ObjectMarshallingStrategyStore) LinkedListEntry(org.drools.core.util.LinkedListEntry) ObjectMarshallingStrategy(org.kie.api.marshalling.ObjectMarshallingStrategy) Activation(org.drools.core.spi.Activation) BeliefSet(org.drools.core.beliefsystem.BeliefSet) LogicalDependency(org.drools.core.common.LogicalDependency) FastIterator(org.drools.core.util.FastIterator)

Example 3 with Activation

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

the class ProtobufInputMarshaller method readBeliefSet.

private static void readBeliefSet(MarshallerReaderContext context, TruthMaintenanceSystem tms, EqualityKey key, ProtobufMessages.EqualityKey _key) throws IOException, ClassNotFoundException {
    if (_key.hasBeliefSet()) {
        ProtobufMessages.BeliefSet _beliefSet = _key.getBeliefSet();
        InternalFactHandle handle = (InternalFactHandle) context.handles.get(_key.getHandleId());
        // phreak might serialize empty belief sets, so he have to handle it during deserialization
        if (_beliefSet.getLogicalDependencyCount() > 0) {
            for (ProtobufMessages.LogicalDependency _logicalDependency : _beliefSet.getLogicalDependencyList()) {
                ProtobufMessages.Activation _activation = _logicalDependency.getActivation();
                Activation activation = (Activation) context.filter.getTuplesCache().get(PersisterHelper.createActivationKey(_activation.getPackageName(), _activation.getRuleName(), _activation.getTuple())).getContextObject();
                Object object = null;
                ObjectMarshallingStrategy strategy = null;
                if (_logicalDependency.hasObjectStrategyIndex()) {
                    strategy = context.usedStrategies.get(_logicalDependency.getObjectStrategyIndex());
                    object = strategy.unmarshal(context.strategyContexts.get(strategy), context, _logicalDependency.getObject().toByteArray(), (context.kBase == null) ? null : context.kBase.getRootClassLoader());
                }
                Object value = null;
                if (_logicalDependency.hasValueStrategyIndex()) {
                    strategy = context.usedStrategies.get(_logicalDependency.getValueStrategyIndex());
                    value = strategy.unmarshal(context.strategyContexts.get(strategy), context, _logicalDependency.getValue().toByteArray(), (context.kBase == null) ? null : context.kBase.getRootClassLoader());
                }
                ObjectTypeConf typeConf = context.wm.getObjectTypeConfigurationRegistry().getObjectTypeConf(((NamedEntryPoint) handle.getEntryPoint()).getEntryPoint(), handle.getObject());
                tms.readLogicalDependency(handle, object, value, activation, activation.getPropagationContext(), activation.getRule(), typeConf);
            }
        } else {
            handle.getEqualityKey().setBeliefSet(tms.getBeliefSystem().newBeliefSet(handle));
        }
    }
}
Also used : ObjectTypeConf(org.drools.core.reteoo.ObjectTypeConf) ObjectMarshallingStrategy(org.kie.api.marshalling.ObjectMarshallingStrategy) Activation(org.drools.core.spi.Activation) InternalFactHandle(org.drools.core.common.InternalFactHandle)

Example 4 with Activation

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

the class DefaultAgenda method clearAndCancelAgendaGroup.

/*
     * (non-Javadoc)
     *
     * @see org.kie.common.AgendaI#clearAgendaGroup(org.kie.common.AgendaGroupImpl)
     */
public void clearAndCancelAgendaGroup(InternalAgendaGroup agendaGroup) {
    // enforce materialization of all activations of this group before removing them
    for (Activation activation : agendaGroup.getActivations()) {
        ((RuleAgendaItem) activation).getRuleExecutor().reEvaluateNetwork(this);
    }
    final EventSupport eventsupport = this.workingMemory;
    agendaGroup.setClearedForRecency(this.workingMemory.getFactHandleFactory().getRecency());
    // this is thread safe for BinaryHeapQueue
    // Binary Heap locks while it returns the array and reset's it's own internal array. Lock is released afer getAndClear()
    List<RuleAgendaItem> lazyItems = new ArrayList<RuleAgendaItem>();
    for (Activation aQueueable : agendaGroup.getAndClear()) {
        final AgendaItem item = (AgendaItem) aQueueable;
        if (item.isRuleAgendaItem()) {
            lazyItems.add((RuleAgendaItem) item);
            ((RuleAgendaItem) item).getRuleExecutor().cancel(workingMemory, eventsupport);
            continue;
        }
        // this must be set false before removal from the activationGroup.
        // Otherwise the activationGroup will also try to cancel the Actvation
        // Also modify won't work properly
        item.setQueued(false);
        if (item.getActivationGroupNode() != null) {
            item.getActivationGroupNode().getActivationGroup().removeActivation(item);
        }
        eventsupport.getAgendaEventSupport().fireActivationCancelled(item, this.workingMemory, MatchCancelledCause.CLEAR);
    }
    // restore lazy items
    for (RuleAgendaItem lazyItem : lazyItems) {
        agendaGroup.add(lazyItem);
    }
}
Also used : RuleAgendaItem(org.drools.core.phreak.RuleAgendaItem) ArrayList(java.util.ArrayList) Activation(org.drools.core.spi.Activation) RuleAgendaItem(org.drools.core.phreak.RuleAgendaItem)

Example 5 with Activation

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

the class BinaryHeapQueue method percolateDownMaxHeap.

/**
 * Percolates element down heap from the position given by the index.
 * <p>
 * Assumes it is a maximum heap.
 *
 * @param index the index of the element
 */
protected void percolateDownMaxHeap(final int index) {
    final Activation element = elements[index];
    int hole = index;
    while ((hole * 2) <= size) {
        int child = hole * 2;
        // up then move onto other child
        if (child != size && compare(elements[child + 1], elements[child]) > 0) {
            child++;
        }
        // if we found resting place of bubble then terminate search
        if (compare(elements[child], element) <= 0) {
            break;
        }
        setElement(hole, elements[child]);
        hole = child;
    }
    setElement(hole, element);
}
Also used : Activation(org.drools.core.spi.Activation)

Aggregations

Activation (org.drools.core.spi.Activation)22 RuleAgendaItem (org.drools.core.phreak.RuleAgendaItem)6 Test (org.junit.Test)4 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Iterator (java.util.Iterator)2 AndDescr (org.drools.compiler.lang.descr.AndDescr)2 GlobalDescr (org.drools.compiler.lang.descr.GlobalDescr)2 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)2 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)2 DefaultKnowledgeHelper (org.drools.core.base.DefaultKnowledgeHelper)2 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)2 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)2 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)2 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)2 RuleExecutor (org.drools.core.phreak.RuleExecutor)2 CompositeObjectSinkAdapterTest (org.drools.core.reteoo.CompositeObjectSinkAdapterTest)2 LeftTupleImpl (org.drools.core.reteoo.LeftTupleImpl)2 ObjectTypeConf (org.drools.core.reteoo.ObjectTypeConf)2