Search in sources :

Example 6 with ObjectTypeConf

use of org.drools.core.reteoo.ObjectTypeConf 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 7 with ObjectTypeConf

use of org.drools.core.reteoo.ObjectTypeConf in project drools by kiegroup.

the class NamedEntryPoint method deleteStated.

private void deleteStated(RuleImpl rule, TerminalNode terminalNode, InternalFactHandle handle, EqualityKey key) {
    if (key != null && key.getStatus() == EqualityKey.JUSTIFIED) {
        return;
    }
    if (handle.isTraitable()) {
        traitHelper.deleteWMAssertedTraitProxies(handle, rule, terminalNode);
    }
    final Object object = handle.getObject();
    final ObjectTypeConf typeConf = getObjectTypeConfigurationRegistry().getObjectTypeConf(this.entryPoint, object);
    if (typeConf.isDynamic()) {
        removePropertyChangeListener(handle, true);
    }
    PropagationContext propagationContext = delete(handle, object, typeConf, rule, null, terminalNode);
    deleteFromTMS(handle, key, typeConf, propagationContext);
    this.handleFactory.destroyFactHandle(handle);
}
Also used : ObjectTypeConf(org.drools.core.reteoo.ObjectTypeConf) PropagationContext(org.drools.core.spi.PropagationContext)

Example 8 with ObjectTypeConf

use of org.drools.core.reteoo.ObjectTypeConf in project drools by kiegroup.

the class NamedEntryPoint method removeFromObjectStore.

public void removeFromObjectStore(InternalFactHandle handle) {
    this.objectStore.removeHandle(handle);
    ObjectTypeConf typeConf = getObjectTypeConfigurationRegistry().getObjectTypeConf(this.entryPoint, handle.getObject());
    deleteFromTMS(handle, handle.getEqualityKey(), typeConf, null);
}
Also used : ObjectTypeConf(org.drools.core.reteoo.ObjectTypeConf)

Example 9 with ObjectTypeConf

use of org.drools.core.reteoo.ObjectTypeConf in project drools by kiegroup.

the class NamedEntryPoint method insertAsync.

public FactHandle insertAsync(Object object) {
    ObjectTypeConf typeConf = getObjectTypeConfigurationRegistry().getObjectTypeConf(this.entryPoint, object);
    PropagationContext pctx = this.pctxFactory.createPropagationContext(this.wm.getNextPropagationIdCounter(), PropagationContext.Type.INSERTION, null, null, null, entryPoint);
    InternalFactHandle handle = createHandle(object, typeConf);
    pctx.setFactHandle(handle);
    this.entryPointNode.assertObject(handle, pctx, typeConf, this.wm);
    this.wm.getRuleRuntimeEventSupport().fireObjectInserted(pctx, handle, object, this.wm);
    return handle;
}
Also used : ObjectTypeConf(org.drools.core.reteoo.ObjectTypeConf) PropagationContext(org.drools.core.spi.PropagationContext)

Example 10 with ObjectTypeConf

use of org.drools.core.reteoo.ObjectTypeConf in project drools by kiegroup.

the class TruthMaintenanceSystem method get.

public EqualityKey get(final Object object) {
    EqualityKey key = (EqualityKey) this.equalityKeyMap.get(object);
    if (key == null && assertBehaviour == AssertBehaviour.EQUALITY) {
        // Edge case: another object X, equivalent (equals+hashcode) to "object" Y
        // has been previously stated. However, if X is a subclass of Y, TMS
        // may have not been enabled yet, and key would be null.
        InternalFactHandle fh = ep.getObjectStore().getHandleForObject(object);
        if (fh != null) {
            key = fh.getEqualityKey();
            if (key == null) {
                // we use the FH's Object here, not the inserted object
                ObjectTypeConf typeC = this.typeConfReg.getObjectTypeConf(ep.getEntryPoint(), fh.getObject());
                enableTMS(fh.getObject(), typeC);
                key = fh.getEqualityKey();
            }
        }
    }
    return key;
}
Also used : ObjectTypeConf(org.drools.core.reteoo.ObjectTypeConf)

Aggregations

ObjectTypeConf (org.drools.core.reteoo.ObjectTypeConf)23 InternalFactHandle (org.drools.core.common.InternalFactHandle)9 PropagationContext (org.drools.core.spi.PropagationContext)6 NamedEntryPoint (org.drools.core.common.NamedEntryPoint)4 ObjectTypeConfigurationRegistry (org.drools.core.common.ObjectTypeConfigurationRegistry)4 WorkingMemoryEntryPoint (org.drools.core.WorkingMemoryEntryPoint)3 BeliefSet (org.drools.core.beliefsystem.BeliefSet)3 Test (org.junit.Test)3 ObjectMarshallingStrategy (org.kie.api.marshalling.ObjectMarshallingStrategy)3 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)2 EqualityKey (org.drools.core.common.EqualityKey)2 EventFactHandle (org.drools.core.common.EventFactHandle)2 QueryElementFactHandle (org.drools.core.common.QueryElementFactHandle)2 TraitableBean (org.drools.core.factmodel.traits.TraitableBean)2 ObjectTypeConfiguration (org.drools.core.marshalling.impl.ProtobufMessages.ObjectTypeConfiguration)2 EntryPointId (org.drools.core.rule.EntryPointId)2 Activation (org.drools.core.spi.Activation)2 MockActivation (org.drools.core.test.model.MockActivation)2 FactHandle (org.kie.api.runtime.rule.FactHandle)2 HashSet (java.util.HashSet)1