use of org.drools.core.common.DefaultFactHandle 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.common.DefaultFactHandle in project drools by kiegroup.
the class ObjectMarshallingStrategyStoreTest method testMultipleObjectMarshallingStrategiesOfTheSameClassWithDifferentNames.
@Test
public void testMultipleObjectMarshallingStrategiesOfTheSameClassWithDifferentNames() throws IOException, ClassNotFoundException {
Environment env = EnvironmentFactory.newEnvironment();
final Thing entityOne = new Thing(1, "Object 1");
final Thing entityTwo = new Thing(2, "Object 2");
Collection srcItems = new ArrayList();
srcItems.add(entityOne);
srcItems.add(entityTwo);
ObjectMarshallingStrategy[] strats = new ObjectMarshallingStrategy[] { new IdentityPlaceholderResolverStrategy("entityOne", new ObjectMarshallingStrategyAcceptor() {
@Override
public boolean accept(Object object) {
return entityOne.equals(object);
}
}, Collections.singletonMap(entityOne.id, (Object) entityOne)), new IdentityPlaceholderResolverStrategy("entityTwo", new ObjectMarshallingStrategyAcceptor() {
@Override
public boolean accept(Object object) {
return entityTwo.equals(object);
}
}, Collections.singletonMap(entityTwo.id, (Object) entityTwo)) };
env.set(EnvironmentName.OBJECT_MARSHALLING_STRATEGIES, strats);
KieSessionConfiguration ksc = SessionConfiguration.newInstance();
final KieBaseConfiguration kbconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kbconf.setOption(EventProcessingOption.STREAM);
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(kbconf);
KieSession ks = kbase.newKieSession(ksc, env);
ks.insert(entityOne);
ks.insert(entityTwo);
ProtobufMarshaller marshaller = (ProtobufMarshaller) MarshallerFactory.newMarshaller(kbase, strats);
// Serialize object
final byte[] b1;
{
ByteArrayOutputStream bos = new ByteArrayOutputStream();
marshaller.marshall(bos, ks, System.currentTimeMillis());
b1 = bos.toByteArray();
bos.close();
}
// Deserialize object
StatefulKnowledgeSession ksession2;
{
ByteArrayInputStream bais = new ByteArrayInputStream(b1);
try {
ksession2 = marshaller.unmarshall(bais, ks.getSessionConfiguration(), ks.getEnvironment());
Collection items = ksession2.getFactHandles();
Assert.assertTrue(items.size() == 2);
for (Object item : items) {
FactHandle factHandle = (FactHandle) item;
Assert.assertTrue(srcItems.contains(((DefaultFactHandle) factHandle).getObject()));
}
} catch (RuntimeException npe) {
// Here ocurrs the bug that shows that NamedObjectMarshallingStrategies are required.
Assert.fail("This error only happens if identity ObjectMarshallingStrategy use old name");
} finally {
bais.close();
}
}
}
use of org.drools.core.common.DefaultFactHandle in project drools by kiegroup.
the class AlphaNodeTest method testLiteralConstraintAssertObjectWithoutMemory.
@Test
public void testLiteralConstraintAssertObjectWithoutMemory() throws Exception {
InternalKnowledgeBase kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase();
BuildContext buildContext = new BuildContext(kBase);
buildContext.setRule(new RuleImpl("test"));
StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
final RuleImpl rule = new RuleImpl("test-rule");
PropagationContextFactory pctxFactory = kBase.getConfiguration().getComponentFactory().getPropagationContextFactory();
final PropagationContext context = pctxFactory.createPropagationContext(0, PropagationContext.Type.INSERTION, null, null, null);
final MockObjectSource source = new MockObjectSource(buildContext.getNextId());
final ClassFieldReader extractor = store.getReader(Cheese.class, "type");
final FieldValue field = FieldFactory.getInstance().getFieldValue("cheddar");
final MvelConstraint constraint = new MvelConstraintTestUtil("type == \"cheddar\"", field, extractor);
// With Memory
final AlphaNode alphaNode = new AlphaNode(buildContext.getNextId(), constraint, source, // no memory
buildContext);
final MockObjectSink sink = new MockObjectSink();
alphaNode.addObjectSink(sink);
final Cheese cheddar = new Cheese("cheddar", 5);
final DefaultFactHandle f0 = (DefaultFactHandle) ksession.insert(cheddar);
// check sink is empty
assertLength(0, sink.getAsserted());
// object should assert as it passes text
alphaNode.assertObject(f0, context, ksession);
assertEquals(1, sink.getAsserted().size());
Object[] list = (Object[]) sink.getAsserted().get(0);
assertSame(cheddar, ksession.getObject((DefaultFactHandle) list[0]));
final Cheese stilton = new Cheese("stilton", 6);
final DefaultFactHandle f1 = new DefaultFactHandle(1, stilton);
// object should NOT assert as it does not pass test
alphaNode.assertObject(f1, context, ksession);
assertLength(1, sink.getAsserted());
list = (Object[]) sink.getAsserted().get(0);
assertSame(cheddar, ksession.getObject((DefaultFactHandle) list[0]));
}
use of org.drools.core.common.DefaultFactHandle in project drools by kiegroup.
the class NodeSegmentUnlinkingTest method testAllLinkedInWithJoinNodesOnly.
@Test
public void testAllLinkedInWithJoinNodesOnly() {
setUp(JOIN_NODE);
// make sure it created JoinNodes
assertEquals(JoinNode.class, n3.getClass());
KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
InternalKnowledgeBase kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase(kconf);
StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
DefaultFactHandle f1 = (DefaultFactHandle) ksession.insert("test1");
n3.assertObject(f1, context, ksession);
BetaMemory bm = (BetaMemory) ksession.getNodeMemory(n3);
assertFalse(bm.getSegmentMemory().isSegmentLinked());
n4.assertObject(f1, context, ksession);
assertFalse(bm.getSegmentMemory().isSegmentLinked());
n5.assertObject(f1, context, ksession);
assertFalse(bm.getSegmentMemory().isSegmentLinked());
n6.assertObject(f1, context, ksession);
// only after all 4 nodes are populated, is the segment linked in
assertTrue(bm.getSegmentMemory().isSegmentLinked());
}
use of org.drools.core.common.DefaultFactHandle in project drools by kiegroup.
the class NodeSegmentUnlinkingTest method testAllLinkedInWithExistsNodesOnly.
@Test
public void testAllLinkedInWithExistsNodesOnly() {
setUp(EXISTS_NODE);
// make sure it created ExistsNodes
assertEquals(ExistsNode.class, n3.getClass());
KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
InternalKnowledgeBase kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase(kconf);
StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
DefaultFactHandle f1 = (DefaultFactHandle) ksession.insert("test1");
n3.assertObject(f1, context, ksession);
BetaMemory bm = (BetaMemory) ksession.getNodeMemory(n3);
assertFalse(bm.getSegmentMemory().isSegmentLinked());
n4.assertObject(f1, context, ksession);
assertFalse(bm.getSegmentMemory().isSegmentLinked());
n5.assertObject(f1, context, ksession);
assertFalse(bm.getSegmentMemory().isSegmentLinked());
n6.assertObject(f1, context, ksession);
// only after all 4 nodes are populated, is the segment linked in
assertTrue(bm.getSegmentMemory().isSegmentLinked());
}
Aggregations