ksession.setGlobal("totallyCovered", totallyCovered);
ksession.setGlobal("partiallyCovered", partiallyCovered);
ksession.setGlobal("notCovered", notCovered);
KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, "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();
assertEquals("notCovered with " + sa, 0, notCovered.size()); // this was fired in the previous scenario
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();
assertEquals("notCovered with " + sa, 1, notCovered.size()); // new uncovered scenario
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();
}