use of org.drools.tms.beliefsystem.BeliefSet in project drools by kiegroup.
the class TruthMaintenanceSystemImpl method insertOnTms.
@Override
public InternalFactHandle insertOnTms(Object object, ObjectTypeConf typeConf, PropagationContext propagationContext, InternalFactHandle handle, BiFunction<Object, ObjectTypeConf, InternalFactHandle> fhFactory) {
EqualityKey key = get(object);
if (handle != null && key != null && key.getStatus() == EqualityKey.JUSTIFIED) {
// The justified set needs to be staged, before we can continue with the stated insert
BeliefSet bs = ((TruthMaintenanceSystemEqualityKey) handle.getEqualityKey()).getBeliefSet();
// staging will set it's status to stated
bs.getBeliefSystem().stage(propagationContext, bs);
}
// we know the handle is null
handle = fhFactory.apply(object, typeConf);
if (key == null) {
key = new TruthMaintenanceSystemEqualityKey(handle, EqualityKey.STATED);
put(key);
} else {
key.addFactHandle(handle);
}
handle.setEqualityKey(key);
return handle;
}
use of org.drools.tms.beliefsystem.BeliefSet in project drools by kiegroup.
the class TruthMaintenanceSystemKnowledgeHelper method bolster.
@Override
public InternalFactHandle bolster(final Object object, final Object value) {
if (object == null || !activation.isMatched()) {
return null;
}
InternalFactHandle handle = getFactHandleFromWM(object);
NamedEntryPoint ep = (NamedEntryPoint) reteEvaluator.getEntryPoint(EntryPointId.DEFAULT.getEntryPointId());
ObjectTypeConf otc = ep.getObjectTypeConfigurationRegistry().getOrCreateObjectTypeConf(ep.getEntryPoint(), object);
BeliefSet beliefSet = null;
if (handle == null) {
handle = RuntimeComponentFactory.get().getFactHandleFactoryService().newFactHandle(object, otc, reteEvaluator, ep);
}
if (handle.getEqualityKey() == null) {
handle.setEqualityKey(new TruthMaintenanceSystemEqualityKey(handle, EqualityKey.STATED));
} else {
beliefSet = ((TruthMaintenanceSystemEqualityKey) handle.getEqualityKey()).getBeliefSet();
}
BeliefSystem beliefSystem = value instanceof BeliefSystemMode ? ((BeliefSystemMode) value).getBeliefSystem() : ((TruthMaintenanceSystemImpl) TruthMaintenanceSystemFactory.get().getOrCreateTruthMaintenanceSystem(toStatefulKnowledgeSession())).getBeliefSystem();
if (beliefSet == null) {
beliefSet = beliefSystem.newBeliefSet(handle);
((TruthMaintenanceSystemEqualityKey) handle.getEqualityKey()).setBeliefSet(beliefSet);
}
return beliefSystem.insert(beliefSystem.asMode(value), activation.getRule(), (TruthMaintenanceSystemActivation) activation, object, beliefSet, activation.getPropagationContext(), otc).getFactHandle();
}
use of org.drools.tms.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(((TruthMaintenanceSystemEqualityKey) h.getEqualityKey()).getBeliefSet());
} else if ("John".equals(name)) {
BeliefSet bs = ((TruthMaintenanceSystemEqualityKey) h.getEqualityKey()).getBeliefSet();
assertTrue(bs.isPositive());
assertEquals(2, bs.size());
}
}
}
}
use of org.drools.tms.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 = ((TruthMaintenanceSystemEqualityKey) key).getBeliefSet();
assertTrue(set instanceof DefeasibleBeliefSet);
DefeasibleBeliefSet dfs = (DefeasibleBeliefSet) set;
assertEquals(support, dfs.size());
assertEquals(status, dfs.getStatus());
}
use of org.drools.tms.beliefsystem.BeliefSet in project drools by kiegroup.
the class ProtobufOutputMarshaller method writeBeliefSet.
private static void writeBeliefSet(MarshallerWriteContext context, BeliefSet beliefSet, ProtobufMessages.EqualityKey.Builder _key) throws IOException {
ProtobufMessages.BeliefSet.Builder _beliefSet = ProtobufMessages.BeliefSet.newBuilder();
_beliefSet.setHandleId(beliefSet.getFactHandle().getId());
ObjectMarshallingStrategyStore objectMarshallingStrategyStore = context.getObjectMarshallingStrategyStore();
// 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(writeTuple(context, activation, true)).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.getStrategyContext().get(strategy), (ObjectOutputStream) 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.getStrategyContext().get(strategy), (ObjectOutputStream) context, belief.getMode())));
}
_beliefSet.addLogicalDependency(_logicalDependency.build());
}
_key.setBeliefSet(_beliefSet);
}
Aggregations