use of org.drools.core.common.DefaultFactHandle in project drools by kiegroup.
the class ReteTest method testIsShadowed.
@Test
public void testIsShadowed() {
StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
// Create a Rete network with ObjectTypeNodes for List, Collection and ArrayList
final Rete rete = kBase.getRete();
final ObjectTypeNode objectTypeNode = new ObjectTypeNode(1, this.entryPoint, new ClassObjectType(Cheese.class), buildContext);
objectTypeNode.attach(buildContext);
final MockObjectSink sink1 = new MockObjectSink();
objectTypeNode.addObjectSink(sink1);
// There are no String ObjectTypeNodes, make sure its not propagated
final Cheese cheese = new Cheese("brie", 15);
final DefaultFactHandle h1 = new DefaultFactHandle(1, cheese);
rete.assertObject(h1, pctxFactory.createPropagationContext(0, PropagationContext.Type.INSERTION, null, null, null), ksession);
ksession.fireAllRules();
final Object[] results = (Object[]) sink1.getAsserted().get(0);
}
use of org.drools.core.common.DefaultFactHandle in project drools by kiegroup.
the class RuleUnlinkingWithSegmentMemoryTest method testRuleSegmentLinking.
@Test
public void testRuleSegmentLinking() {
setUp(JOIN_NODE);
InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
StatefulKnowledgeSessionImpl wm = new StatefulKnowledgeSessionImpl(1L, kBase);
BetaMemory bm = null;
List<PathMemory> list;
PathMemory rtn1Rs = wm.getNodeMemory(rtn1);
PathMemory rtn2Rs = wm.getNodeMemory(rtn2);
PathMemory rtn3Rs = wm.getNodeMemory(rtn3);
DefaultFactHandle f1 = (DefaultFactHandle) wm.insert("test1");
lian.assertObject(f1, context, wm);
n1.assertObject(f1, context, wm);
n3.assertObject(f1, context, wm);
n4.assertObject(f1, context, wm);
n8.assertObject(f1, context, wm);
assertFalse(rtn1Rs.isRuleLinked());
assertFalse(rtn2Rs.isRuleLinked());
assertFalse(rtn3Rs.isRuleLinked());
// Link in Rule1
bm = (BetaMemory) wm.getNodeMemory(n2);
assertFalse(bm.getSegmentMemory().isSegmentLinked());
DefaultFactHandle f2 = (DefaultFactHandle) wm.insert("test2");
n2.assertObject(f2, context, wm);
assertTrue(bm.getSegmentMemory().isSegmentLinked());
assertTrue(rtn1Rs.isRuleLinked());
assertFalse(rtn2Rs.isRuleLinked());
assertFalse(rtn3Rs.isRuleLinked());
// Link in Rule2
bm = (BetaMemory) wm.getNodeMemory(n5);
assertFalse(bm.getSegmentMemory().isSegmentLinked());
n5.assertObject(f1, context, wm);
assertTrue(bm.getSegmentMemory().isSegmentLinked());
assertTrue(rtn1Rs.isRuleLinked());
assertTrue(rtn2Rs.isRuleLinked());
assertFalse(rtn3Rs.isRuleLinked());
// Link in Rule3
n6.assertObject(f1, context, wm);
n7.assertObject(f1, context, wm);
assertTrue(bm.getSegmentMemory().isSegmentLinked());
assertTrue(rtn1Rs.isRuleLinked());
assertTrue(rtn2Rs.isRuleLinked());
assertTrue(rtn3Rs.isRuleLinked());
// retract n2, should unlink all rules
n2.retractRightTuple(f2.getFirstRightTuple(), context, wm);
assertFalse(rtn1Rs.isRuleLinked());
assertFalse(rtn2Rs.isRuleLinked());
assertFalse(rtn3Rs.isRuleLinked());
}
use of org.drools.core.common.DefaultFactHandle in project drools by kiegroup.
the class Misc2Test method testDynamicAddRule.
@Test
public void testDynamicAddRule() {
// DROOLS-17
String str = "import " + Misc2Test.A.class.getCanonicalName() + "\n" + "rule r1 when\n" + " $a : A( f1 == 1 )\n" + "then\n" + "end\n" + "\n" + "rule r2 when\n" + " $a : A( f2 == 1 )\n" + "then\n" + "end\n" + "\n" + "rule r3 when\n" + " $a : A( f3 == 1 )" + "then\n" + "end";
String str2 = "import " + Misc2Test.A.class.getCanonicalName() + "\n" + "rule r4 when\n" + " $a : A( f2 == 1, f4 == 1 )" + "then\n" + "end";
InternalKnowledgeBase kbase = (InternalKnowledgeBase) KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, str);
KieSession ksession = kbase.newKieSession();
FactHandle fh = ksession.insert(new A(1, 1, 1, 1));
ksession.fireAllRules();
Collection<KiePackage> kpkgs = KieBaseUtil.getKieBaseFromKieModuleFromDrl("tmp", kieBaseTestConfiguration, str2).getKiePackages();
kbase.addPackages(kpkgs);
ksession.fireAllRules();
// this second insert forces the regeneration of the otnIds
ksession.insert(new A(2, 2, 2, 2));
LeftTuple leftTuple = ((DefaultFactHandle) fh).getFirstLeftTuple();
ObjectTypeNode.Id letTupleOtnId = leftTuple.getInputOtnId();
leftTuple = leftTuple.getHandleNext();
while (leftTuple != null) {
assertTrue(letTupleOtnId.before(leftTuple.getInputOtnId()));
letTupleOtnId = leftTuple.getInputOtnId();
leftTuple = leftTuple.getHandleNext();
}
}
use of org.drools.core.common.DefaultFactHandle in project drools by kiegroup.
the class EventFactHandleDeserializationTest method testDisconnectedEventFactHandle.
@Test
public void testDisconnectedEventFactHandle() {
// DROOLS-924
final String drl = "declare String \n" + " @role(event)\n" + "end\n";
final KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession();
final DefaultFactHandle helloHandle = (DefaultFactHandle) ksession.insert("hello");
final DefaultFactHandle goodbyeHandle = (DefaultFactHandle) ksession.insert("goodbye");
final SoftAssertions softly = new SoftAssertions();
FactHandle key = DefaultFactHandle.createFromExternalFormat(helloHandle.toExternalForm());
softly.assertThat(key).isInstanceOf(EventFactHandle.class);
softly.assertThat(ksession.getObject(key)).isEqualTo("hello");
key = DefaultFactHandle.createFromExternalFormat(goodbyeHandle.toExternalForm());
softly.assertThat(key).isInstanceOf(EventFactHandle.class);
softly.assertThat(ksession.getObject(key)).isEqualTo("goodbye");
softly.assertAll();
}
use of org.drools.core.common.DefaultFactHandle in project drools by kiegroup.
the class InsertObjectCommand method execute.
public FactHandle execute(Context context) {
KieSession ksession = ((RegistryContext) context).lookup(KieSession.class);
FactHandle factHandle;
if (StringUtils.isEmpty(this.entryPoint)) {
factHandle = ksession.insert(object);
} else {
factHandle = ksession.getEntryPoint(this.entryPoint).insert(object);
}
if (outIdentifier != null) {
if (this.returnObject) {
((RegistryContext) context).lookup(ExecutionResultImpl.class).setResult(this.outIdentifier, object);
}
((RegistryContext) context).lookup(ExecutionResultImpl.class).getFactHandles().put(this.outIdentifier, factHandle);
}
if (disconnected) {
DefaultFactHandle disconnectedHandle = ((DefaultFactHandle) factHandle).clone();
disconnectedHandle.disconnect();
return disconnectedHandle;
}
return factHandle;
}
Aggregations