use of org.kie.api.KieBase in project drools by kiegroup.
the class TruthMaintenanceTest method testStatedShadowLogicalWithSingleOccurance.
@Test(timeout = 10000)
public void testStatedShadowLogicalWithSingleOccurance() {
String droolsSource = "package org.drools.tms.test; \n" + "global java.util.List list; \n" + "rule Justify \n" + "when \n" + " String( this == 'go1' ) " + "then \n" + " insertLogical( 'f1' ); \n" + "end \n" + "rule StillHere \n" + "when \n" + " String( this == 'go2' ) " + " s : String( this == 'f1' ) " + "then \n" + " list.add( s ); \n" + "end \n" + "";
KieBaseConfiguration kieConf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kieConf.setOption(EqualityBehaviorOption.IDENTITY);
KieBase kbase = loadKnowledgeBaseFromString(kieConf, droolsSource);
KieSession session = kbase.newKieSession();
List list = new ArrayList();
session.setGlobal("list", list);
InternalFactHandle fh1 = (InternalFactHandle) session.insert("f1");
InternalFactHandle fh2 = (InternalFactHandle) session.insert("f2");
session.insert("go1");
session.fireAllRules();
session.insert("go2");
session.fireAllRules();
// Make sure f1 only occurs once
assertEquals(1, list.size());
assertEquals("f1", list.get(0));
}
use of org.kie.api.KieBase in project drools by kiegroup.
the class TruthMaintenanceTest method testLogicalInsertionsWithExists.
@Test(timeout = 10000)
public // @Ignore("in Java 8, the byte[] generated by serialization are not the same and requires investigation")
void testLogicalInsertionsWithExists() throws Exception {
KieBase kbase = loadKnowledgeBase("test_LogicalInsertionWithExists.drl");
KieSession ksession = kbase.newKieSession();
final Person p1 = new Person("p1", "stilton", 20);
p1.setStatus("europe");
FactHandle c1FactHandle = ksession.insert(p1);
final Person p2 = new Person("p2", "stilton", 30);
p2.setStatus("europe");
FactHandle c2FactHandle = ksession.insert(p2);
final Person p3 = new Person("p3", "stilton", 40);
p3.setStatus("europe");
FactHandle c3FactHandle = ksession.insert(p3);
ksession.fireAllRules();
ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
// all 3 in europe, so, 2 cheese
Collection cheeseList = ksession.getObjects(new ClassObjectFilter(Cheese.class));
assertEquals(2, cheeseList.size());
// europe=[ 1, 2 ], america=[ 3 ]
p3.setStatus("america");
c3FactHandle = getFactHandle(c3FactHandle, ksession);
ksession.update(c3FactHandle, p3);
ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
ksession.fireAllRules();
ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
cheeseList = ksession.getObjects(new ClassObjectFilter(Cheese.class));
assertEquals(1, cheeseList.size());
// europe=[ 1 ], america=[ 2, 3 ]
p2.setStatus("america");
c2FactHandle = getFactHandle(c2FactHandle, ksession);
ksession.update(c2FactHandle, p2);
ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
ksession.fireAllRules();
ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
cheeseList = ksession.getObjects(new ClassObjectFilter(Cheese.class));
assertEquals(1, cheeseList.size());
// europe=[ ], america=[ 1, 2, 3 ]
p1.setStatus("america");
c1FactHandle = getFactHandle(c1FactHandle, ksession);
ksession.update(c1FactHandle, p1);
ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
ksession.fireAllRules();
ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
cheeseList = ksession.getObjects(new ClassObjectFilter(Cheese.class));
assertEquals(2, cheeseList.size());
// europe=[ 2 ], america=[ 1, 3 ]
p2.setStatus("europe");
c2FactHandle = getFactHandle(c2FactHandle, ksession);
ksession.update(c2FactHandle, p2);
ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
ksession.fireAllRules();
ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
cheeseList = ksession.getObjects(new ClassObjectFilter(Cheese.class));
assertEquals(1, cheeseList.size());
// europe=[ 1, 2 ], america=[ 3 ]
p1.setStatus("europe");
c1FactHandle = getFactHandle(c1FactHandle, ksession);
ksession.update(c1FactHandle, p1);
ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
ksession.fireAllRules();
ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
cheeseList = ksession.getObjects(new ClassObjectFilter(Cheese.class));
assertEquals(1, cheeseList.size());
// europe=[ 1, 2, 3 ], america=[ ]
p3.setStatus("europe");
c3FactHandle = getFactHandle(c3FactHandle, ksession);
ksession.update(c3FactHandle, p3);
ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
ksession.fireAllRules();
ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
cheeseList = ksession.getObjects(new ClassObjectFilter(Cheese.class));
assertEquals(2, cheeseList.size());
}
use of org.kie.api.KieBase in project drools by kiegroup.
the class TruthMaintenanceTest method testLogicalInsertions3.
// (timeout=10000)
@Test
public void testLogicalInsertions3() throws Exception {
KieBase kbase = loadKnowledgeBase("test_logicalInsertions3.drl");
KieSession ksession = kbase.newKieSession();
final List list = new ArrayList();
ksession.setGlobal("events", list);
// asserting the sensor object
final Sensor sensor = new Sensor(150, 100);
FactHandle sensorHandle = ksession.insert(sensor);
ksession.fireAllRules();
ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
// alarm must sound
assertEquals(2, list.size());
assertEquals(2, ksession.getObjects().size());
// modifying sensor
sensor.setTemperature(125);
sensorHandle = getFactHandle(sensorHandle, ksession);
ksession.update(sensorHandle, sensor);
ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
ksession.fireAllRules();
ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
// alarm must continue to sound
assertEquals(3, list.size());
assertEquals(2, ksession.getObjects().size());
// modifying sensor
sensor.setTemperature(80);
sensorHandle = getFactHandle(sensorHandle, ksession);
ksession.update(sensorHandle, sensor);
ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
ksession.fireAllRules();
// no alarms anymore
assertEquals(3, list.size());
assertEquals(1, ksession.getObjects().size());
}
use of org.kie.api.KieBase in project drools by kiegroup.
the class XSDResourceTest method testXSDResourceNotBreakingCompilation.
@Test
public void testXSDResourceNotBreakingCompilation() {
final KieContainer kcontainer = KieServices.Factory.get().getKieClasspathContainer();
final KieBase kieBase = kcontainer.getKieBase("xsdKieBase");
Assertions.assertThat(kieBase).as("Created KieBase with XSD should not be null").isNotNull();
}
use of org.kie.api.KieBase in project drools by kiegroup.
the class AbstractConcurrentTest method getKieBase.
protected synchronized KieBase getKieBase(final String... drls) {
final KieHelper kieHelper = new KieHelper();
for (final String drl : drls) {
kieHelper.addContent(drl, ResourceType.DRL);
}
KieBase kieBase;
if (enforcedJitting) {
kieBase = kieHelper.build(ConstraintJittingThresholdOption.get(0));
} else {
kieBase = kieHelper.build();
}
if (serializeKieBase) {
kieBase = serializeAndDeserializeKieBase(kieBase);
}
return kieBase;
}
Aggregations