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