use of org.drools.core.util.ObjectHashMap in project drools by kiegroup.
the class DefeasibilityTest method testMultipleDefeats.
@Test(timeout = 10000)
public void testMultipleDefeats() {
KieSession kSession = getSession("org/drools/compiler/beliefsystem/defeasible/multiDefeat.drl");
kSession.fireAllRules();
TruthMaintenanceSystem tms = ((NamedEntryPoint) kSession.getEntryPoint("DEFAULT")).getTruthMaintenanceSystem();
FactType Xtype = kSession.getKieBase().getFactType("org.drools.defeasible", "X");
ObjectHashMap keys = tms.getEqualityKeyMap();
Iterator iter = keys.iterator();
ObjectHashMap.ObjectEntry entry;
while ((entry = (ObjectHashMap.ObjectEntry) iter.next()) != null) {
EqualityKey key = (EqualityKey) entry.getValue();
Class factClass = key.getFactHandle().getObject().getClass();
if (factClass == Xtype.getFactClass()) {
checkStatus(key, 2, DefeasibilityStatus.DEFEATEDLY);
} else {
fail("Unrecognized object has been logically justified : " + factClass);
}
}
for (Object o : kSession.getObjects()) {
System.out.println(o);
}
assertEquals(2, kSession.getObjects().size());
kSession.fireAllRules();
}
use of org.drools.core.util.ObjectHashMap in project drools by kiegroup.
the class ProtobufOutputMarshaller method writeTruthMaintenanceSystem.
public static void writeTruthMaintenanceSystem(MarshallerWriteContext context, EntryPoint wmep, ProtobufMessages.EntryPoint.Builder _epb) throws IOException {
TruthMaintenanceSystem tms = ((NamedEntryPoint) wmep).getTruthMaintenanceSystem();
ObjectHashMap justifiedMap = tms.getEqualityKeyMap();
if (!justifiedMap.isEmpty()) {
EqualityKey[] keys = new EqualityKey[justifiedMap.size()];
org.drools.core.util.Iterator it = justifiedMap.iterator();
int i = 0;
for (org.drools.core.util.ObjectHashMap.ObjectEntry entry = (org.drools.core.util.ObjectHashMap.ObjectEntry) it.next(); entry != null; entry = (org.drools.core.util.ObjectHashMap.ObjectEntry) it.next()) {
EqualityKey key = (EqualityKey) entry.getKey();
keys[i++] = key;
}
Arrays.sort(keys, EqualityKeySorter.instance);
ProtobufMessages.TruthMaintenanceSystem.Builder _tms = ProtobufMessages.TruthMaintenanceSystem.newBuilder();
// write the assert map of Equality keys
for (EqualityKey key : keys) {
ProtobufMessages.EqualityKey.Builder _key = ProtobufMessages.EqualityKey.newBuilder();
_key.setStatus(key.getStatus());
_key.setHandleId(key.getFactHandle().getId());
if (key.size() > 1) {
// add all the other key's if they exist
FastIterator keyIter = key.fastIterator();
for (DefaultFactHandle handle = key.getFirst().getNext(); handle != null; handle = (DefaultFactHandle) keyIter.next(handle)) {
_key.addOtherHandle(handle.getId());
}
}
if (key.getBeliefSet() != null) {
writeBeliefSet(context, key.getBeliefSet(), _key);
}
_tms.addKey(_key.build());
}
_epb.setTms(_tms.build());
}
}
use of org.drools.core.util.ObjectHashMap in project drools by kiegroup.
the class ObjectTypeNodeParser method getFirstAlphaNode.
/**
* Returns the first {@link org.kie.reteoo.AlphaNode} from the specified {@link ObjectHashMap}.
*
* @param hashedAlphaNodes map of hashed AlphaNodes
* @return first alpha from the specified map
* @throws IllegalArgumentException thrown if the map doesn't contain any alpha nodes
*/
private AlphaNode getFirstAlphaNode(final ObjectHashMap hashedAlphaNodes) throws IllegalArgumentException {
AlphaNode firstAlphaNode;
final Iterator iter = hashedAlphaNodes.iterator();
final ObjectHashMap.ObjectEntry entry = (ObjectHashMap.ObjectEntry) iter.next();
if (entry != null) {
firstAlphaNode = (AlphaNode) entry.getValue();
} else {
throw new IllegalArgumentException("ObjectHashMap does not contain any hashed AlphaNodes!");
}
return firstAlphaNode;
}
use of org.drools.core.util.ObjectHashMap in project drools by kiegroup.
the class CompositeObjectSinkAdapter method hashSinks.
void hashSinks(final FieldIndex fieldIndex) {
if (this.hashedSinkMap == null) {
this.hashedSinkMap = new ObjectHashMap();
}
final int index = fieldIndex.getIndex();
final InternalReadAccessor fieldReader = fieldIndex.getFieldExtractor();
ObjectSinkNode currentSink = this.hashableSinks.getFirst();
while (currentSink != null) {
final AlphaNode alphaNode = (AlphaNode) currentSink;
final AlphaNodeFieldConstraint fieldConstraint = alphaNode.getConstraint();
final IndexableConstraint indexableConstraint = (IndexableConstraint) fieldConstraint;
// position to the next sink now because alphaNode may be removed if the index is equal. If we were to do this
// afterwards, currentSink.nextNode would be null
currentSink = currentSink.getNextObjectSinkNode();
// the right field index
if (index == indexableConstraint.getFieldExtractor().getIndex()) {
final FieldValue value = indexableConstraint.getField();
this.hashedSinkMap.put(new HashKey(index, value, fieldReader), alphaNode);
// remove the alpha from the possible candidates of hashable sinks since it is now hashed
hashableSinks.remove(alphaNode);
}
}
if (this.hashableSinks.isEmpty()) {
this.hashableSinks = null;
}
fieldIndex.setHashed(true);
}
use of org.drools.core.util.ObjectHashMap in project drools by kiegroup.
the class CompositePartitionAwareObjectSinkAdapter method hashSink.
private boolean hashSink(ObjectSink sink) {
InternalReadAccessor readAccessor = getHashableAccessor(sink);
if (readAccessor != null) {
int index = readAccessor.getIndex();
if (fieldIndex == null) {
this.fieldIndex = new CompositeObjectSinkAdapter.FieldIndex(index, readAccessor);
this.hashedSinkMap = new ObjectHashMap();
}
if (fieldIndex.getIndex() == index) {
AlphaNode alpha = (AlphaNode) sink;
this.hashedSinkMap.put(new CompositeObjectSinkAdapter.HashKey(index, ((IndexableConstraint) alpha.getConstraint()).getField(), fieldIndex.getFieldExtractor()), alpha, false);
return true;
}
}
this.fieldIndex = null;
this.hashedSinkMap = null;
return false;
}
Aggregations