use of org.drools.core.beliefsystem.BeliefSet 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);
}
use of org.drools.core.beliefsystem.BeliefSet in project drools by kiegroup.
the class NamedEntryPoint method insert.
public FactHandle insert(final Object object, final boolean dynamic, final RuleImpl rule, final TerminalNode terminalNode) {
if (object == null) {
// you cannot assert a null object
return null;
}
try {
this.wm.startOperation();
ObjectTypeConf typeConf = getObjectTypeConfigurationRegistry().getObjectTypeConf(this.entryPoint, object);
final PropagationContext propagationContext = this.pctxFactory.createPropagationContext(this.wm.getNextPropagationIdCounter(), PropagationContext.Type.INSERTION, rule, terminalNode, null, entryPoint);
if (this.wm.isSequential()) {
InternalFactHandle handle = createHandle(object, typeConf);
propagationContext.setFactHandle(handle);
insert(handle, object, rule, typeConf, propagationContext);
return handle;
}
InternalFactHandle handle;
try {
this.lock.lock();
// check if the object already exists in the WM
handle = this.objectStore.getHandleForObject(object);
if (!typeConf.isTMSEnabled()) {
// TMS not enabled for this object type
if (handle != null) {
return handle;
}
handle = createHandle(object, typeConf);
} else {
TruthMaintenanceSystem tms = getTruthMaintenanceSystem();
EqualityKey key;
if (handle != null && handle.getEqualityKey().getStatus() == EqualityKey.STATED) {
// it's already stated, so just return the handle
return handle;
} else {
key = tms.get(object);
}
if (key != null && key.getStatus() == EqualityKey.JUSTIFIED) {
// The justified set needs to be staged, before we can continue with the stated insert
BeliefSet bs = handle.getEqualityKey().getBeliefSet();
// staging will set it's status to stated
bs.getBeliefSystem().stage(propagationContext, bs);
}
handle = createHandle(object, // we know the handle is null
typeConf);
if (key == null) {
key = new EqualityKey(handle, EqualityKey.STATED);
tms.put(key);
} else {
key.addFactHandle(handle);
}
handle.setEqualityKey(key);
}
propagationContext.setFactHandle(handle);
// @propertyChangeSupport
if (dynamic || typeConf.isDynamic()) {
addPropertyChangeListener(handle, dynamic);
}
insert(handle, object, rule, typeConf, propagationContext);
} finally {
this.lock.unlock();
}
return handle;
} finally {
this.wm.endOperation();
}
}
use of org.drools.core.beliefsystem.BeliefSet in project drools by kiegroup.
the class TruthMaintenanceSystem method addLogicalDependency.
public InternalFactHandle addLogicalDependency(final InternalFactHandle handle, final Object object, final Object value, final Activation activation, final PropagationContext context, final RuleImpl rule, final ObjectTypeConf typeConf, final boolean read) {
BeliefSystem beliefSystem = defaultBeliefSystem;
if (value != null && value instanceof Mode & !(value instanceof SimpleMode)) {
Mode mode = (Mode) value;
beliefSystem = (BeliefSystem) mode.getBeliefSystem();
}
BeliefSet beliefSet = handle.getEqualityKey().getBeliefSet();
if (beliefSet == null) {
if (context.getType() == PropagationContext.Type.MODIFICATION) {
// if this was a update, chances are its trying to retract a logical assertion
}
beliefSet = beliefSystem.newBeliefSet(handle);
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, context, typeConf);
} else {
beliefSet = beliefSystem.insert(node, beliefSet, context, typeConf);
}
return beliefSet.getFactHandle();
}
use of org.drools.core.beliefsystem.BeliefSet in project drools by kiegroup.
the class AbductionTest method testCitizenshipExample.
@Test
public void testCitizenshipExample() {
// from wikipedia, abductive reasoning example
String droolsSource = "package org.drools.abductive.test; \n" + "" + "declare CitizenUS " + " name : String @key " + "end " + "declare Parent " + " parent : String @key " + " child : String @key " + "end " + "declare BornUS @Abducible name : String @key end " + "declare BornOutsideUS @Abducible name : String @key end " + "declare ResidentUS @Abducible name : String @key end " + "declare NaturalizedUS @Abducible name : String @key end " + "declare RegisteredUS @Abducible name : String @key end " + "query extractCitizen( CitizenUS $cit ) " + " $cit := CitizenUS() " + "end " + "query citizen( String $name ) " + " @Abductive( target=CitizenUS.class ) " + " bornUS( $name ; ) " + " or " + " ( bornOutsideUS( $name ; ) and residentUS( $name ; ) and naturalizedUS( $name ; ) ) " + " or " + " ( bornOutsideUS( $name ; ) and Parent( $parent, $name ; ) and CitizenUS( $parent ; ) and registeredUS( $name ; ) ) " + "end " + "query bornUS( String $name ) @Abductive( target=BornUS.class ) end " + "query bornOutsideUS( String $name ) @Abductive( target=BornOutsideUS.class ) end " + "query residentUS( String $name ) @Abductive( target=ResidentUS.class ) end " + "query naturalizedUS( String $name ) @Abductive( target=NaturalizedUS.class ) end " + "query registeredUS( String $name ) @Abductive( target=RegisteredUS.class ) end " + "rule Facts " + "when " + "then " + " insert( new CitizenUS( 'Mary' ) ); " + " insert( new Parent( 'Mary', 'John' ) ); " + " insertLogical( new ResidentUS( 'John' ), 'neg' ); " + "end " + "rule CheckCitizen " + "when " + " $cit : ?citizen( 'John' ; ) " + "then " + " System.out.println( 'John is a citizen ' + $cit ); " + "end " + "";
// ///////////////////////////////////
KieSession session = getSessionFromString(droolsSource);
session.fireAllRules();
FactType type = session.getKieBase().getFactType("org.drools.abductive.test", "CitizenUS");
for (Object o : session.getObjects()) {
System.out.println(">>> " + o);
if (o.getClass().equals(type.getFactClass())) {
InternalFactHandle h = (InternalFactHandle) session.getFactHandle(o);
String name = (String) type.get(o, "name");
if ("Mary".equals(name)) {
assertNull(h.getEqualityKey().getBeliefSet());
} else if ("John".equals(name)) {
BeliefSet bs = h.getEqualityKey().getBeliefSet();
assertTrue(bs.isPositive());
assertEquals(2, bs.size());
}
}
}
}
use of org.drools.core.beliefsystem.BeliefSet in project drools by kiegroup.
the class DefeasibilityTest method checkStatus.
private void checkStatus(EqualityKey key, int support, DefeasibilityStatus status) {
assertEquals(EqualityKey.JUSTIFIED, key.getStatus());
BeliefSet set = key.getBeliefSet();
assertTrue(set instanceof DefeasibleBeliefSet);
DefeasibleBeliefSet dfs = (DefeasibleBeliefSet) set;
assertEquals(support, dfs.size());
assertEquals(status, dfs.getStatus());
}
Aggregations