Search in sources :

Example 1 with BeliefSet

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

Example 2 with 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();
    }
}
Also used : ObjectTypeConf(org.drools.core.reteoo.ObjectTypeConf) PropagationContext(org.drools.core.spi.PropagationContext) BeliefSet(org.drools.core.beliefsystem.BeliefSet)

Example 3 with BeliefSet

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();
}
Also used : Mode(org.kie.api.internal.runtime.beliefs.Mode) SimpleMode(org.drools.core.beliefsystem.simple.SimpleMode) BeliefSystem(org.drools.core.beliefsystem.BeliefSystem) BeliefSet(org.drools.core.beliefsystem.BeliefSet) SimpleMode(org.drools.core.beliefsystem.simple.SimpleMode)

Example 4 with BeliefSet

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());
            }
        }
    }
}
Also used : BeliefSet(org.drools.core.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 5 with BeliefSet

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

Aggregations

BeliefSet (org.drools.core.beliefsystem.BeliefSet)8 ObjectTypeConf (org.drools.core.reteoo.ObjectTypeConf)3 BeliefSystem (org.drools.core.beliefsystem.BeliefSystem)2 SimpleMode (org.drools.core.beliefsystem.simple.SimpleMode)2 InternalFactHandle (org.drools.core.common.InternalFactHandle)2 LogicalDependency (org.drools.core.common.LogicalDependency)2 NamedEntryPoint (org.drools.core.common.NamedEntryPoint)2 Activation (org.drools.core.spi.Activation)2 Mode (org.kie.api.internal.runtime.beliefs.Mode)2 DefeasibleBeliefSet (org.drools.core.beliefsystem.defeasible.DefeasibleBeliefSet)1 EqualityKey (org.drools.core.common.EqualityKey)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 Test (org.junit.Test)1 FactType (org.kie.api.definition.type.FactType)1 ObjectMarshallingStrategy (org.kie.api.marshalling.ObjectMarshallingStrategy)1 ObjectMarshallingStrategyStore (org.kie.api.marshalling.ObjectMarshallingStrategyStore)1 KieSession (org.kie.api.runtime.KieSession)1