use of org.kie.api.runtime.rule.FactHandle 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.rule.FactHandle 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.rule.FactHandle 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());
}
use of org.kie.api.runtime.rule.FactHandle in project drools by kiegroup.
the class TruthMaintenanceTest method testLogicalInsertions.
@Test(timeout = 10000)
public void testLogicalInsertions() throws Exception {
final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newClassPathResource("test_LogicalInsertions.drl", getClass()), ResourceType.DRL);
Collection<KiePackage> kpkgs = kbuilder.getKnowledgePackages();
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kpkgs);
kbase = SerializationHelper.serializeObject(kbase);
KieSession session = createKnowledgeSession(kbase);
final List list = new ArrayList();
session.setGlobal("list", list);
final Cheese brie = new Cheese("brie", 12);
FactHandle brieHandle = session.insert(brie);
final Cheese provolone = new Cheese("provolone", 12);
FactHandle provoloneHandle = session.insert(provolone);
session.fireAllRules();
session = getSerialisedStatefulKnowledgeSession(session, true);
System.out.println(list);
assertEquals(3, list.size());
assertEquals(3, session.getObjects().size());
brieHandle = getFactHandle(brieHandle, session);
session.retract(brieHandle);
session = getSerialisedStatefulKnowledgeSession(session, true);
assertEquals(2, session.getObjects().size());
provoloneHandle = getFactHandle(provoloneHandle, session);
session.retract(provoloneHandle);
session.fireAllRules();
assertEquals(0, session.getObjects().size());
}
use of org.kie.api.runtime.rule.FactHandle in project drools by kiegroup.
the class TruthMaintenanceTest method testLogicalInsertionsNot.
@Test(timeout = 10000)
public // @Ignore("in Java 8, the byte[] generated by serialization are not the same and requires investigation")
void testLogicalInsertionsNot() throws Exception {
KieBase kbase = loadKnowledgeBase("test_LogicalInsertionsNot.drl");
KieSession ksession = kbase.newKieSession();
final Person a = new Person("a");
final Cheese cheese = new Cheese("brie", 1);
ksession.setGlobal("cheese", cheese);
ksession.fireAllRules();
ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
Collection list = ksession.getObjects();
assertEquals("i was not asserted by not a => i.", 1, list.size());
assertEquals("i was not asserted by not a => i.", cheese, list.iterator().next());
FactHandle h = ksession.insert(a);
ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
// no need to fire rules, assertion alone removes justification for i,
// so it should be deleted.
// workingMemory.fireAllRules();
ksession.fireAllRules();
list = ksession.getObjects();
assertEquals("a was not asserted or i not deleted.", 1, list.size());
assertEquals("a was asserted.", a, list.iterator().next());
assertFalse("i was not rectracted.", list.contains(cheese));
// no rules should fire, but nevertheless...
// workingMemory.fireAllRules();
assertEquals("agenda should be empty.", 0, ((InternalAgenda) ((StatefulKnowledgeSessionImpl) ksession).getAgenda()).agendaSize());
h = getFactHandle(h, ksession);
ksession.retract(h);
ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
ksession.fireAllRules();
ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
list = ksession.getObjects();
assertEquals("i was not asserted by not a => i.", 1, list.size());
assertEquals("i was not asserted by not a => i.", cheese, list.iterator().next());
}
Aggregations