use of org.drools.core.common.InternalFactHandle in project drools by kiegroup.
the class TraitHelper method asTrait.
private <T, K> T asTrait(K core, TraitableBean inner, Class<T> trait, boolean needsProxy, boolean hasTrait, boolean needsUpdate, TraitFactory builder, boolean logical, Activation activation) throws LogicalTypeInconsistencyException {
T thing;
if (needsProxy) {
thing = (T) inner;
inner.addTrait(trait.getName(), (Thing<K>) core);
} else if (hasTrait) {
thing = (T) inner.getTrait(trait.getName());
} else {
thing = (T) builder.getProxy(inner, trait, logical);
}
if (needsUpdate) {
InternalFactHandle h = (InternalFactHandle) lookupFactHandle(core);
if (h == null) {
h = lookupHandleForWrapper(core);
}
if (h == null) {
h = (InternalFactHandle) this.workingMemory.insert(core, false, activation.getRule(), activation.getTuple().getTupleSink());
}
if (!h.isTraitOrTraitable()) {
throw new IllegalStateException("A traited working memory element is being used with a default fact handle. " + "Please verify that its class was declared as @Traitable : " + core.getClass().getName());
}
this.update(h, inner, activation);
}
return thing;
}
use of org.drools.core.common.InternalFactHandle in project drools by kiegroup.
the class TraitHelper method update.
public void update(final FactHandle handle, final Object newObject, final Activation activation) {
InternalFactHandle h = (InternalFactHandle) handle;
h.getEntryPoint().update(h, newObject, onlyTraitBitSetMask(), newObject.getClass(), activation);
}
use of org.drools.core.common.InternalFactHandle in project drools by kiegroup.
the class TraitHelper method updateCore.
private <T> void updateCore(TraitableBean inner, Object core, Class<T> trait, boolean logical, Activation activation) {
FactHandle handle = lookupFactHandle(inner);
InternalFactHandle h = (InternalFactHandle) handle;
if (handle != null) {
TraitFieldTMS fieldTMS = inner._getFieldTMS();
BitMask mask = fieldTMS == null ? onlyTraitBitSetMask() : fieldTMS.getModificationMask();
Object o = h.getObject();
NamedEntryPoint nep = (NamedEntryPoint) h.getEntryPoint();
PropagationContext propagationContext = nep.getPctxFactory().createPropagationContext(nep.getInternalWorkingMemory().getNextPropagationIdCounter(), PropagationContext.Type.MODIFICATION, activation.getRule(), activation.getTuple().getTupleSink(), h, nep.getEntryPoint(), mask, core.getClass(), null);
nep.update(h, o, o, nep.getObjectTypeConfigurationRegistry().getObjectTypeConf(nep.getEntryPoint(), o), propagationContext);
} else {
handle = this.workingMemory.insert(inner, false, activation.getRule(), activation.getTuple().getTupleSink());
}
}
use of org.drools.core.common.InternalFactHandle in project drools by kiegroup.
the class TraitHelper method shed.
public <T, K, X extends TraitableBean> Thing<K> shed(TraitableBean<K, X> core, Class<T> trait, Activation activation) {
if (trait.isAssignableFrom(core.getClass())) {
Collection<Thing<K>> removedTypes = core.removeTrait(trait.getName());
if (!removedTypes.isEmpty()) {
reassignNodes(core, removedTypes);
update(getFactHandle(core), onlyTraitBitSetMask(), core.getClass(), activation);
// updateTraits( core, Long.MIN_VALUE, null, core.getClass(), null, ((TraitableBean) core).getMostSpecificTraits() );
}
if (core instanceof Thing) {
return (Thing<K>) core;
} else {
return null;
}
} else {
Collection<Thing<K>> removedTypes;
Thing<K> thing = core.getTrait(Thing.class.getName());
if (trait == Thing.class) {
removedTypes = new ArrayList<Thing<K>>(core._getTraitMap().values());
for (Thing t : removedTypes) {
if (!((TraitType) t)._isVirtual()) {
delete(getFactHandle(t), activation);
}
}
core._getTraitMap().clear();
core._setTraitMap(null);
return thing;
} else if (core.hasTrait(trait.getName())) {
removedTypes = core.removeTrait(trait.getName());
} else {
HierarchyEncoder hier = this.workingMemory.getKnowledgeBase().getConfiguration().getComponentFactory().getTraitRegistry().getHierarchy();
BitSet code = hier.getCode(trait.getName());
removedTypes = core.removeTrait(code);
}
removedTypes = new ArrayList<Thing<K>>(removedTypes);
reassignNodes(core, removedTypes);
for (Thing t : removedTypes) {
if (!((TraitType) t)._isVirtual()) {
InternalFactHandle handle = (InternalFactHandle) getFactHandle(t);
if (handle.getEqualityKey() != null && handle.getEqualityKey().getLogicalFactHandle() == handle) {
entryPoint.getTruthMaintenanceSystem().delete(handle);
} else {
delete(getFactHandle(t), activation);
}
}
}
if (!core.hasTraits()) {
don(activation, core, Thing.class, false);
} else if (!removedTypes.isEmpty()) {
update(getFactHandle(core), onlyTraitBitSetMask(), core.getClass(), activation);
// updateTraits( core, Long.MIN_VALUE, null, core.getClass(), null, ((TraitableBean) core).getMostSpecificTraits() );
}
return thing;
}
}
use of org.drools.core.common.InternalFactHandle in project drools by kiegroup.
the class TraitHelper method deleteWMAssertedTraitProxies.
public void deleteWMAssertedTraitProxies(InternalFactHandle handle, RuleImpl rule, TerminalNode terminalNode) {
TraitableBean traitableBean = (TraitableBean) handle.getObject();
if (traitableBean.hasTraits()) {
PriorityQueue<TraitProxy> removedTypes = new PriorityQueue<TraitProxy>(traitableBean._getTraitMap().values().size());
removedTypes.addAll(traitableBean._getTraitMap().values());
while (!removedTypes.isEmpty()) {
TraitProxy proxy = removedTypes.poll();
if (!proxy._isVirtual()) {
InternalFactHandle proxyHandle = (InternalFactHandle) getFactHandle(proxy);
if (proxyHandle.getEqualityKey() == null || proxyHandle.getEqualityKey().getLogicalFactHandle() != proxyHandle) {
entryPoint.delete(proxyHandle, rule, terminalNode);
}
}
}
}
}
Aggregations