Search in sources :

Example 6 with BeliefSet

use of org.drools.tms.beliefsystem.BeliefSet in project drools by kiegroup.

the class BeliefSystemLogicalCallback method execute.

public void execute(ReteEvaluator reteEvaluator) {
    NamedEntryPoint nep = (NamedEntryPoint) handle.getEntryPoint(reteEvaluator);
    BeliefSet bs = ((TruthMaintenanceSystemEqualityKey) handle.getEqualityKey()).getBeliefSet();
    bs.setWorkingMemoryAction(null);
    if (update) {
        if (!bs.isEmpty()) {
            // We need the isEmpty check, in case the BeliefSet was made empty (due to retract) after this was scheduled
            nep.update(handle, handle.getObject(), allSetButTraitBitMask(), Object.class, null);
        }
    } else {
        if (fullyRetract) {
            nep.delete(this.handle, context.getRuleOrigin(), this.activation.getTuple().getTupleSink());
        } else {
            ObjectTypeConf typeConf = nep.getObjectTypeConfigurationRegistry().getOrCreateObjectTypeConf(nep.getEntryPoint(), handle.getObject());
            nep.getEntryPointNode().retractObject(handle, context, typeConf, reteEvaluator);
        }
    }
}
Also used : TruthMaintenanceSystemEqualityKey(org.drools.tms.TruthMaintenanceSystemEqualityKey) ObjectTypeConf(org.drools.core.reteoo.ObjectTypeConf) NamedEntryPoint(org.drools.kiesession.entrypoints.NamedEntryPoint) BeliefSet(org.drools.tms.beliefsystem.BeliefSet)

Example 7 with BeliefSet

use of org.drools.tms.beliefsystem.BeliefSet in project drools by kiegroup.

the class TruthMaintenanceSystemImpl method delete.

@Override
public void delete(FactHandle fh) {
    if (fh == null) {
        return;
    }
    InternalFactHandle ifh = (InternalFactHandle) fh;
    // This will clear out the logical entries for the FH. However the FH and EqualityKey remain, if it's stated
    // Update the equality key, which maintains a list of stated FactHandles
    final EqualityKey key = ifh.getEqualityKey();
    if (key.getLogicalFactHandle() != fh) {
        throw new IllegalArgumentException("The FactHandle did not originate from TMS : " + fh);
    }
    final PropagationContext propagationContext = ep.getPctxFactory().createPropagationContext(ep.getReteEvaluator().getNextPropagationIdCounter(), PropagationContext.Type.DELETION, null, null, ifh, ep.getEntryPoint());
    BeliefSet beliefSet = ((TruthMaintenanceSystemEqualityKey) key).getBeliefSet();
    if (beliefSet != null && !beliefSet.isEmpty()) {
        beliefSet.cancel(propagationContext);
    }
}
Also used : PropagationContext(org.drools.core.spi.PropagationContext) EqualityKey(org.drools.core.common.EqualityKey) BeliefSet(org.drools.tms.beliefsystem.BeliefSet) InternalFactHandle(org.drools.core.common.InternalFactHandle)

Example 8 with BeliefSet

use of org.drools.tms.beliefsystem.BeliefSet in project drools by kiegroup.

the class TruthMaintenanceSystemImpl method addLogicalDependency.

private InternalFactHandle addLogicalDependency(final InternalFactHandle handle, final Object object, final Object value, final TruthMaintenanceSystemActivation activation, final ObjectTypeConf typeConf, final boolean read) {
    BeliefSystem beliefSystem = defaultBeliefSystem;
    if (value instanceof Mode & !(value instanceof SimpleMode)) {
        BeliefSystemMode mode = (BeliefSystemMode) value;
        beliefSystem = mode.getBeliefSystem();
    }
    BeliefSet beliefSet = ((TruthMaintenanceSystemEqualityKey) handle.getEqualityKey()).getBeliefSet();
    if (beliefSet == null) {
        beliefSet = beliefSystem.newBeliefSet(handle);
        ((TruthMaintenanceSystemEqualityKey) handle.getEqualityKey()).setBeliefSet(beliefSet);
    }
    final LogicalDependency node = beliefSystem.newLogicalDependency(activation, beliefSet, object, value);
    activation.getRule().setHasLogicalDependency(true);
    activation.addLogicalDependency(node);
    if (read) {
        // used when deserialising
        beliefSystem.read(node, beliefSet, activation.getPropagationContext(), typeConf);
    } else {
        beliefSet = beliefSystem.insert(node, beliefSet, activation.getPropagationContext(), typeConf);
    }
    return beliefSet.getFactHandle();
}
Also used : BeliefSystemMode(org.drools.tms.beliefsystem.BeliefSystemMode) BeliefSystemMode(org.drools.tms.beliefsystem.BeliefSystemMode) Mode(org.drools.core.beliefsystem.Mode) BeliefSystem(org.drools.tms.beliefsystem.BeliefSystem) BeliefSet(org.drools.tms.beliefsystem.BeliefSet)

Example 9 with BeliefSet

use of org.drools.tms.beliefsystem.BeliefSet in project drools by kiegroup.

the class AbductiveQuery method processAbduction.

@Override
public boolean processAbduction(Activation resultLeftTuple, DroolsQuery dquery, Object[] objects, ReteEvaluator reteEvaluator) {
    boolean pass = true;
    InternalWorkingMemory workingMemory = (InternalWorkingMemory) reteEvaluator;
    int numArgs = abducibleArgs.length;
    Object[] constructorArgs = new Object[abducibleArgs.length];
    for (int j = 0; j < numArgs; j++) {
        int k = arg2param[j];
        if (objects[k] != null) {
            constructorArgs[j] = objects[k];
        } else if (dquery.getElements()[k] != null) {
            constructorArgs[j] = dquery.getElements()[k];
        }
    }
    Object abduced = abduce(constructorArgs);
    if (abduced != null) {
        boolean firstAssertion = true;
        ObjectStore store = workingMemory.getObjectStore();
        InternalFactHandle handle = store.getHandleForObject(abduced);
        if (handle != null) {
            abduced = handle.getObject();
            firstAssertion = false;
        } else {
            handle = TruthMaintenanceSystemFactory.get().getOrCreateTruthMaintenanceSystem(workingMemory).insertPositive(abduced, resultLeftTuple);
        }
        BeliefSet bs = handle.getEqualityKey() != null ? ((TruthMaintenanceSystemEqualityKey) handle.getEqualityKey()).getBeliefSet() : null;
        if (bs == null) {
            abduced = handle.getObject();
        } else {
            if (!bs.isPositive()) {
                pass = false;
            } else {
                if (!firstAssertion) {
                    TruthMaintenanceSystemFactory.get().getOrCreateTruthMaintenanceSystem(workingMemory).insertPositive(abduced, resultLeftTuple);
                }
            }
        }
    }
    objects[objects.length - 1] = abduced;
    return pass;
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) ObjectStore(org.drools.core.common.ObjectStore) BeliefSet(org.drools.tms.beliefsystem.BeliefSet) InternalFactHandle(org.drools.core.common.InternalFactHandle)

Example 10 with BeliefSet

use of org.drools.tms.beliefsystem.BeliefSet in project drools by kiegroup.

the class ProtobufOutputMarshaller method writeActivation.

public static <M extends ModedAssertion<M>> ProtobufMessages.Activation writeActivation(MarshallerWriteContext context, AgendaItem agendaItem, boolean isDormient) {
    ProtobufMessages.Activation.Builder _activation = ProtobufMessages.Activation.newBuilder();
    RuleImpl rule = agendaItem.getRule();
    _activation.setPackageName(rule.getPackage());
    _activation.setRuleName(rule.getName());
    _activation.setTuple(writeTuple(context, agendaItem, isDormient));
    _activation.setSalience(agendaItem.getSalience());
    _activation.setIsActivated(agendaItem.isQueued());
    _activation.setEvaluated(agendaItem.isRuleAgendaItem());
    if (agendaItem.getActivationGroupNode() != null) {
        _activation.setActivationGroup(agendaItem.getActivationGroupNode().getActivationGroup().getName());
    }
    if (agendaItem.getActivationFactHandle() != null) {
        _activation.setHandleId(agendaItem.getActivationFactHandle().getId());
    }
    if (agendaItem instanceof TruthMaintenanceSystemAgendaItem) {
        org.drools.core.util.LinkedList<LogicalDependency<M>> list = ((TruthMaintenanceSystemAgendaItem) agendaItem).getLogicalDependencies();
        if (list != null && !list.isEmpty()) {
            for (LogicalDependency<?> node = list.getFirst(); node != null; node = node.getNext()) {
                _activation.addLogicalDependency(((BeliefSet) node.getJustified()).getFactHandle().getId());
            }
        }
    }
    return _activation.build();
}
Also used : TruthMaintenanceSystemAgendaItem(org.drools.tms.agenda.TruthMaintenanceSystemAgendaItem) LogicalDependency(org.drools.tms.LogicalDependency) BeliefSet(org.drools.tms.beliefsystem.BeliefSet) Activation(org.drools.core.spi.Activation) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl)

Aggregations

BeliefSet (org.drools.tms.beliefsystem.BeliefSet)10 InternalFactHandle (org.drools.core.common.InternalFactHandle)4 EqualityKey (org.drools.core.common.EqualityKey)2 ObjectTypeConf (org.drools.core.reteoo.ObjectTypeConf)2 Activation (org.drools.core.spi.Activation)2 NamedEntryPoint (org.drools.kiesession.entrypoints.NamedEntryPoint)2 LogicalDependency (org.drools.tms.LogicalDependency)2 TruthMaintenanceSystemEqualityKey (org.drools.tms.TruthMaintenanceSystemEqualityKey)2 BeliefSystem (org.drools.tms.beliefsystem.BeliefSystem)2 BeliefSystemMode (org.drools.tms.beliefsystem.BeliefSystemMode)2 Mode (org.drools.core.beliefsystem.Mode)1 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)1 ObjectStore (org.drools.core.common.ObjectStore)1 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)1 PropagationContext (org.drools.core.spi.PropagationContext)1 FastIterator (org.drools.core.util.FastIterator)1 LinkedListEntry (org.drools.core.util.LinkedListEntry)1 TruthMaintenanceSystemActivation (org.drools.tms.agenda.TruthMaintenanceSystemActivation)1 TruthMaintenanceSystemAgendaItem (org.drools.tms.agenda.TruthMaintenanceSystemAgendaItem)1 DefeasibleBeliefSet (org.drools.tms.beliefsystem.defeasible.DefeasibleBeliefSet)1