Search in sources :

Example 41 with KieSession

use of org.kie.api.runtime.KieSession in project drools by kiegroup.

the class TruthMaintenanceTest method testLogicalInsertionsUpdateEqual.

@Test(timeout = 10000)
@Ignore("Currently cannot support updates")
public // @Ignore("in Java 8, the byte[] generated by serialization are not the same and requires investigation")
void testLogicalInsertionsUpdateEqual() throws Exception {
    // calling update on a justified FH, states it
    KieBase kbase = loadKnowledgeBase("test_LogicalInsertionsUpdateEqual.drl");
    KieSession ksession = kbase.newKieSession();
    final Person p = new Person("person");
    p.setAge(2);
    FactHandle h = ksession.insert(p);
    assertEquals(1, ksession.getObjects().size());
    ksession.fireAllRules();
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    assertEquals(2, ksession.getObjects().size());
    Collection l = ksession.getObjects(new ClassObjectFilter(CheeseEqual.class));
    assertEquals(1, l.size());
    assertEquals(3, ((CheeseEqual) l.iterator().next()).getPrice());
    h = getFactHandle(h, ksession);
    ksession.retract(h);
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    Collection list = ksession.getObjects();
    // CheeseEqual was updated, making it stated, so it wouldn't have been logically deleted
    assertEquals(1, list.size());
    assertEquals(new CheeseEqual("person", 3), list.iterator().next());
    FactHandle fh = ksession.getFactHandle(list.iterator().next());
    ksession.retract(fh);
    list = ksession.getObjects();
    assertEquals(0, list.size());
    TruthMaintenanceSystem tms = ((NamedEntryPoint) ksession.getEntryPoint(EntryPointId.DEFAULT.getEntryPointId())).getTruthMaintenanceSystem();
    final java.lang.reflect.Field field = tms.getClass().getDeclaredField("equalityKeyMap");
    field.setAccessible(true);
    final ObjectHashMap m = (ObjectHashMap) field.get(tms);
    field.setAccessible(false);
    assertEquals("assertMap should be empty", 0, m.size());
}
Also used : TruthMaintenanceSystem(org.drools.core.common.TruthMaintenanceSystem) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) ObjectHashMap(org.drools.core.util.ObjectHashMap) CheeseEqual(org.drools.compiler.CheeseEqual) ClassObjectFilter(org.drools.core.ClassObjectFilter) KieBase(org.kie.api.KieBase) Collection(java.util.Collection) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.compiler.Person) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 42 with KieSession

use of org.kie.api.runtime.KieSession in project drools by kiegroup.

the class TruthMaintenanceTest method testDeleteLogicalAssertionFromRule.

@Test
public void testDeleteLogicalAssertionFromRule() {
    // BZ-1317026
    String drl = "global java.util.List list;\n" + "rule R1 when\n" + "then\n" + "    insertLogical( \"test\" ); \n" + "end\n" + "rule R2 when\n" + "    $s : String()\n" + "then\n" + "    list.add( $s ); \n" + "    delete( $s ); \n" + "end\n";
    KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession();
    List<String> list = new ArrayList<String>();
    ksession.setGlobal("list", list);
    ksession.fireAllRules();
    assertEquals(1, list.size());
    assertEquals("test", list.get(0));
    Collection<FactHandle> fhs = ksession.getFactHandles(new ClassObjectFilter(String.class));
    assertEquals(0, fhs.size());
}
Also used : ClassObjectFilter(org.drools.core.ClassObjectFilter) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 43 with KieSession

use of org.kie.api.runtime.KieSession in project drools by kiegroup.

the class TruthMaintenanceTest method testLogicalInsertionsNoLoop.

// (timeout=10000)
@Test
public void testLogicalInsertionsNoLoop() throws Exception {
    KieBase kbase = loadKnowledgeBase("test_LogicalInsertionsNoLoop.drl");
    KieSession ksession = kbase.newKieSession();
    final List l = new ArrayList();
    final Person a = new Person("a");
    ksession.setGlobal("a", a);
    ksession.setGlobal("l", l);
    ksession.fireAllRules();
    assertEquals("a still in WM", 0, ksession.getObjects(new ClassObjectFilter(a.getClass())).size());
    assertEquals("Rule should not loop", 1, l.size());
}
Also used : ClassObjectFilter(org.drools.core.ClassObjectFilter) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) Person(org.drools.compiler.Person) Test(org.junit.Test)

Example 44 with KieSession

use of org.kie.api.runtime.KieSession in project drools by kiegroup.

the class TruthMaintenanceTest method testRepetitiveUpdatesOnSameFacts.

@Test(timeout = 10000)
public void testRepetitiveUpdatesOnSameFacts() throws Exception {
    // JBRULES-3320
    // Using the concept of shift assignments covering interval requirements (staffing required for a given interval)
    // Interval requirements not covered by any shift assignments
    List notCovered = new ArrayList();
    // Interval requirements partially covered by shift assignments (staffing requirement partially met)
    List partiallyCovered = new ArrayList();
    // Interval requirements totally covered by shift assignments (staffing requirement met or exceeded)
    List totallyCovered = new ArrayList();
    // load up the knowledge base
    KieBase kbase = loadKnowledgeBase("test_RepetitiveUpdatesOnSameFacts.drl");
    KieSession ksession = kbase.newKieSession();
    ksession.setGlobal("totallyCovered", totallyCovered);
    ksession.setGlobal("partiallyCovered", partiallyCovered);
    ksession.setGlobal("notCovered", notCovered);
    KieRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, "target/test");
    // Using 4 IntervalRequirement objects that never change during the execution of the test
    // Staffing required at interval 100
    IntervalRequirement ir100 = new IntervalRequirement(100, 2);
    ksession.insert(ir100);
    // Staffing required at interval 101
    IntervalRequirement ir101 = new IntervalRequirement(101, 2);
    ksession.insert(ir101);
    // Staffing required at interval 102
    IntervalRequirement ir102 = new IntervalRequirement(102, 2);
    ksession.insert(ir102);
    // Staffing required at interval 103
    IntervalRequirement ir103 = new IntervalRequirement(103, 2);
    ksession.insert(ir103);
    // Using a single ShiftAssignment object that will get updated multiple times during the execution of the test
    ShiftAssignment sa = new ShiftAssignment();
    sa.setShiftStartTime(100);
    FactHandle saHandle = null;
    // Intersects 1 interval
    totallyCovered.clear();
    partiallyCovered.clear();
    notCovered.clear();
    sa.setShiftEndTime(101);
    System.out.println("ShiftAssignment set from " + sa.getShiftStartTime() + " to " + sa.getShiftEndTime());
    saHandle = ksession.insert(sa);
    ksession.fireAllRules();
    assertEquals("notCovered with " + sa, 3, notCovered.size());
    assertEquals("totallyCovered with " + sa, 0, totallyCovered.size());
    assertEquals("partiallyCovered with " + sa, 1, partiallyCovered.size());
    // Intersects 3 intervals
    totallyCovered.clear();
    partiallyCovered.clear();
    notCovered.clear();
    sa.setShiftEndTime(103);
    System.out.println("ShiftAssignment set from " + sa.getShiftStartTime() + " to " + sa.getShiftEndTime());
    ksession.update(saHandle, sa);
    ksession.fireAllRules();
    // this was fired in the previous scenario
    assertEquals("notCovered with " + sa, 0, notCovered.size());
    assertEquals("totallyCovered with " + sa, 0, totallyCovered.size());
    assertEquals("partiallyCovered with " + sa, 3, partiallyCovered.size());
    // Intersects 2 intervals
    totallyCovered.clear();
    partiallyCovered.clear();
    notCovered.clear();
    sa.setShiftEndTime(102);
    System.out.println("ShiftAssignment set from " + sa.getShiftStartTime() + " to " + sa.getShiftEndTime());
    ksession.update(saHandle, sa);
    ksession.fireAllRules();
    // new uncovered scenario
    assertEquals("notCovered with " + sa, 1, notCovered.size());
    assertEquals("totallyCovered with " + sa, 0, totallyCovered.size());
    assertEquals("partiallyCovered with " + sa, 2, partiallyCovered.size());
    // Intersects 4 intervals
    totallyCovered.clear();
    partiallyCovered.clear();
    notCovered.clear();
    sa.setShiftEndTime(104);
    System.out.println("ShiftAssignment set from " + sa.getShiftStartTime() + " to " + sa.getShiftEndTime());
    ksession.update(saHandle, sa);
    ksession.fireAllRules();
    assertEquals("notCovered with " + sa, 0, notCovered.size());
    assertEquals("totallyCovered with " + sa, 0, totallyCovered.size());
    assertEquals("partiallyCovered with " + sa, 4, partiallyCovered.size());
    // Intersects 1 interval
    totallyCovered.clear();
    partiallyCovered.clear();
    notCovered.clear();
    sa.setShiftEndTime(101);
    System.out.println("ShiftAssignment set from " + sa.getShiftStartTime() + " to " + sa.getShiftEndTime());
    ksession.update(saHandle, sa);
    ksession.fireAllRules();
    assertEquals("notCovered with " + sa, 3, notCovered.size());
    assertEquals("totallyCovered with " + sa, 0, totallyCovered.size());
    assertEquals("partiallyCovered with " + sa, 1, partiallyCovered.size());
    ksession.dispose();
    logger.close();
}
Also used : KieRuntimeLogger(org.kie.api.logger.KieRuntimeLogger) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 45 with KieSession

use of org.kie.api.runtime.KieSession in project drools by kiegroup.

the class TruthMaintenanceTest method testLogicalInsertions2.

@Test(timeout = 10000)
public void testLogicalInsertions2() throws Exception {
    KieBase kbase = loadKnowledgeBase("test_LogicalInsertions2.drl");
    KieSession ksession = kbase.newKieSession();
    final List events = new ArrayList();
    ksession.setGlobal("events", events);
    final Sensor sensor = new Sensor(80, 80);
    FactHandle handle = ksession.insert(sensor);
    // everything should be normal
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, false);
    ksession.fireAllRules();
    Collection list = ksession.getObjects();
    assertEquals("Only sensor is there", 1, list.size());
    assertEquals("Only one event", 1, events.size());
    // problems should be detected
    sensor.setPressure(200);
    sensor.setTemperature(200);
    handle = getFactHandle(handle, ksession);
    ksession.update(handle, sensor);
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    ksession.fireAllRules();
    list = ksession.getObjects();
    assertEquals("Only sensor is there", 1, list.size());
    TruthMaintenanceSystem tms = ((NamedEntryPoint) ksession.getEntryPoint(EntryPointId.DEFAULT.getEntryPointId())).getTruthMaintenanceSystem();
    assertTrue(tms.getEqualityKeyMap().isEmpty());
}
Also used : TruthMaintenanceSystem(org.drools.core.common.TruthMaintenanceSystem) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) KieBase(org.kie.api.KieBase) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) ArrayList(java.util.ArrayList) Collection(java.util.Collection) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) Sensor(org.drools.compiler.Sensor) Test(org.junit.Test)

Aggregations

KieSession (org.kie.api.runtime.KieSession)5328 Test (org.junit.Test)4824 KieBase (org.kie.api.KieBase)2414 ArrayList (java.util.ArrayList)2317 List (java.util.List)1105 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)769 FactHandle (org.kie.api.runtime.rule.FactHandle)598 Person (org.drools.modelcompiler.domain.Person)519 HashMap (java.util.HashMap)416 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)415 KieServices (org.kie.api.KieServices)382 KieHelper (org.kie.internal.utils.KieHelper)355 KieContainer (org.kie.api.runtime.KieContainer)298 RuntimeEngine (org.kie.api.runtime.manager.RuntimeEngine)265 InternalFactHandle (org.drools.core.common.InternalFactHandle)259 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)234 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)234 ReleaseId (org.kie.api.builder.ReleaseId)232 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)229 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)207