Search in sources :

Example 1 with BeliefSet

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;
}
Also used : EqualityKey(org.drools.core.common.EqualityKey) BeliefSet(org.drools.tms.beliefsystem.BeliefSet)

Example 2 with BeliefSet

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();
}
Also used : TruthMaintenanceSystemActivation(org.drools.tms.agenda.TruthMaintenanceSystemActivation) ObjectTypeConf(org.drools.core.reteoo.ObjectTypeConf) BeliefSystemMode(org.drools.tms.beliefsystem.BeliefSystemMode) NamedEntryPoint(org.drools.kiesession.entrypoints.NamedEntryPoint) BeliefSystem(org.drools.tms.beliefsystem.BeliefSystem) BeliefSet(org.drools.tms.beliefsystem.BeliefSet) InternalFactHandle(org.drools.core.common.InternalFactHandle)

Example 3 with BeliefSet

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());
            }
        }
    }
}
Also used : BeliefSet(org.drools.tms.beliefsystem.BeliefSet) KieSession(org.kie.api.runtime.KieSession) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactType(org.kie.api.definition.type.FactType) Test(org.junit.Test)

Example 4 with BeliefSet

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());
}
Also used : TruthMaintenanceSystemEqualityKey(org.drools.tms.TruthMaintenanceSystemEqualityKey) DefeasibleBeliefSet(org.drools.tms.beliefsystem.defeasible.DefeasibleBeliefSet) DefeasibleBeliefSet(org.drools.tms.beliefsystem.defeasible.DefeasibleBeliefSet) BeliefSet(org.drools.tms.beliefsystem.BeliefSet)

Example 5 with BeliefSet

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);
}
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.tms.beliefsystem.BeliefSet) LogicalDependency(org.drools.tms.LogicalDependency) FastIterator(org.drools.core.util.FastIterator)

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