Search in sources :

Example 46 with FactHandle

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());
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) Cheese(org.drools.compiler.Cheese) ObjectInsertedEvent(org.kie.api.event.rule.ObjectInsertedEvent) ObjectDeletedEvent(org.kie.api.event.rule.ObjectDeletedEvent) RuleRuntimeEventListener(org.kie.api.event.rule.RuleRuntimeEventListener) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 47 with FactHandle

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));
}
Also used : DefaultFactHandle(org.drools.core.common.DefaultFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 48 with FactHandle

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);
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) CheeseEqual(org.drools.compiler.CheeseEqual) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 49 with FactHandle

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));
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) KieBase(org.kie.api.KieBase) Setter(org.kie.api.command.Setter) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.compiler.Person) Test(org.junit.Test)

Example 50 with FactHandle

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();
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) KieBase(org.kie.api.KieBase) AFact(org.drools.compiler.integrationtests.facts.AFact) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Aggregations

FactHandle (org.kie.api.runtime.rule.FactHandle)650 Test (org.junit.Test)547 KieSession (org.kie.api.runtime.KieSession)506 KieBase (org.kie.api.KieBase)304 ArrayList (java.util.ArrayList)298 InternalFactHandle (org.drools.core.common.InternalFactHandle)192 List (java.util.List)165 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)119 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)66 KieHelper (org.kie.internal.utils.KieHelper)50 Person (org.drools.modelcompiler.domain.Person)46 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)43 HashMap (java.util.HashMap)41 Person (org.drools.compiler.Person)41 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)39 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)39 Person (org.drools.mvel.compiler.Person)38 Ignore (org.junit.Ignore)36 Match (org.kie.api.runtime.rule.Match)36 Collection (java.util.Collection)34