use of org.kie.api.runtime.rule.FactHandle in project drools by kiegroup.
the class RuleRuntimeEventTest method testEventModel.
@Test
public void testEventModel() throws Exception {
final KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBase("test_EventModel.drl"));
final KieSession wm = createKnowledgeSession(kbase);
final RuleRuntimeEventListener wmel = mock(RuleRuntimeEventListener.class);
wm.addEventListener(wmel);
final Cheese stilton = new Cheese("stilton", 15);
final FactHandle stiltonHandle = wm.insert(stilton);
final ArgumentCaptor<ObjectInsertedEvent> oic = ArgumentCaptor.forClass(org.kie.api.event.rule.ObjectInsertedEvent.class);
verify(wmel).objectInserted(oic.capture());
assertSame(stiltonHandle, oic.getValue().getFactHandle());
wm.update(stiltonHandle, stilton);
final ArgumentCaptor<org.kie.api.event.rule.ObjectUpdatedEvent> ouc = ArgumentCaptor.forClass(org.kie.api.event.rule.ObjectUpdatedEvent.class);
verify(wmel).objectUpdated(ouc.capture());
assertSame(stiltonHandle, ouc.getValue().getFactHandle());
wm.delete(stiltonHandle);
final ArgumentCaptor<ObjectDeletedEvent> orc = ArgumentCaptor.forClass(ObjectDeletedEvent.class);
verify(wmel).objectDeleted(orc.capture());
assertSame(stiltonHandle, orc.getValue().getFactHandle());
}
use of org.kie.api.runtime.rule.FactHandle in project drools by kiegroup.
the class StatefulSessionTest method testDisconnectedFactHandle.
@Test
public void testDisconnectedFactHandle() {
final KieBase kbase = getKnowledgeBase();
final KieSession ksession = createKnowledgeSession(kbase);
final DefaultFactHandle helloHandle = (DefaultFactHandle) ksession.insert("hello");
final DefaultFactHandle goodbyeHandle = (DefaultFactHandle) ksession.insert("goodbye");
FactHandle key = DefaultFactHandle.createFromExternalFormat(helloHandle.toExternalForm());
assertEquals("hello", ksession.getObject(key));
key = DefaultFactHandle.createFromExternalFormat(goodbyeHandle.toExternalForm());
assertEquals("goodbye", ksession.getObject(key));
}
use of org.kie.api.runtime.rule.FactHandle in project drools by kiegroup.
the class StatefulSessionTest method testGetFactHandleEqualityBehavior.
@Test
public void testGetFactHandleEqualityBehavior() throws Exception {
final KieBaseConfiguration kbc = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kbc.setOption(EqualityBehaviorOption.EQUALITY);
final KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBase(kbc));
final KieSession ksession = createKnowledgeSession(kbase);
final CheeseEqual cheese = new CheeseEqual("stilton", 10);
ksession.insert(cheese);
final FactHandle fh = ksession.getFactHandle(new CheeseEqual("stilton", 10));
assertNotNull(fh);
}
use of org.kie.api.runtime.rule.FactHandle in project drools by kiegroup.
the class UpdateTest method testModifyCommand.
@Test
public void testModifyCommand() {
final String str = "rule \"sample rule\"\n" + " when\n" + " then\n" + " System.out.println(\"\\\"Hello world!\\\"\");\n" + "end";
final KieBase kbase = loadKnowledgeBaseFromString(str);
final KieSession ksession = kbase.newKieSession();
final Person p1 = new Person("John", "nobody", 25);
ksession.execute(CommandFactory.newInsert(p1));
final FactHandle fh = ksession.getFactHandle(p1);
final Person p = new Person("Frank", "nobody", 30);
final List<Setter> setterList = new ArrayList<Setter>();
setterList.add(CommandFactory.newSetter("age", String.valueOf(p.getAge())));
setterList.add(CommandFactory.newSetter("name", p.getName()));
setterList.add(CommandFactory.newSetter("likes", p.getLikes()));
ksession.execute(CommandFactory.newModify(fh, setterList));
}
use of org.kie.api.runtime.rule.FactHandle in project drools by kiegroup.
the class UpdateTest method testNotIterativeModifyBug.
@Test
public void testNotIterativeModifyBug() {
// JBRULES-2809
// This bug occurs when a tuple is modified, the remove/add puts it onto the memory end
// However before this was done it would attempt to find the next tuple, starting from itself
// This meant it would just re-add itself as the blocker, but then be moved to end of the memory
// If this tuple was then removed or changed, the blocked was unable to check previous tuples.
String str = "";
str += "package org.simple \n";
str += "import " + AFact.class.getCanonicalName() + "\n";
str += "global java.util.List list \n";
str += "rule xxx \n";
str += "when \n";
str += " $f1 : AFact() \n";
str += " not AFact(this != $f1, eval(field2 == $f1.getField2())) \n";
str += " eval( !$f1.getField1().equals(\"1\") ) \n";
str += "then \n";
str += " list.add($f1); \n";
str += "end \n";
final KieBase kbase = loadKnowledgeBaseFromString(str);
final KieSession ksession = createKnowledgeSession(kbase);
final List list = new ArrayList();
ksession.setGlobal("list", list);
final AFact a1 = new AFact("2", "2");
final AFact a2 = new AFact("1", "2");
final AFact a3 = new AFact("1", "2");
final FactHandle fa1 = ksession.insert(a1);
final FactHandle fa2 = ksession.insert(a2);
final FactHandle fa3 = ksession.insert(a3);
ksession.fireAllRules();
// a1 is blocked by a2
assertEquals(0, list.size());
// modify a2, so that a1 is now blocked by a3
// Do
a2.setField2("1");
ksession.update(fa2, a2);
// Undo
a2.setField2("2");
ksession.update(fa2, a2);
// modify a3 to cycle, so that it goes on the memory end, but in a previous bug still blocked a1
ksession.update(fa3, a3);
// Do
a3.setField2("1");
ksession.update(fa3, a3);
ksession.fireAllRules();
// this should still now blocked by a2, but bug from previous update hanging onto blocked
assertEquals(0, list.size());
ksession.dispose();
}
Aggregations