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());
}
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());
}
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());
}
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();
}
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());
}
Aggregations