use of org.drools.kiesession.rulebase.InternalKnowledgeBase in project drools by kiegroup.
the class MarshallingTest method testSerializeAdd2.
/*
* Here I am inserting data points which are not used by any rule (e.g Person).
* Later adding rule (e.g. Rule: 'match Person') for those data points.
*
* Result: Pkg/Rule addition is failing with ClassCastException
*/
@Test
public void testSerializeAdd2() throws Exception {
// Create a rulebase, a session, and test it
Collection<KiePackage> kpkgs = loadKnowledgePackages("test_Dynamic1_0.drl");
kpkgs = SerializationHelper.serializeObject(kpkgs);
InternalKnowledgeBase kBase = (InternalKnowledgeBase) getKnowledgeBase();
kBase.addPackages(kpkgs);
List results = new ArrayList();
KieSession session = kBase.newKieSession();
session.setGlobal("results", results);
InternalFactHandle stilton1 = (InternalFactHandle) session.insert(new Cheese("stilton", 10));
session.insert(new Cheese("brie", 10));
InternalFactHandle bob = (InternalFactHandle) session.insert(new Person("bob", 10));
// fire rules
session.fireAllRules();
// check the results are correct
assertEquals(1, results.size());
assertEquals(stilton1.getObject(), results.get(0));
// serialize session and rulebase
session = getSerialisedStatefulKnowledgeSession(session, kBase, true);
// dynamically add a new package
kpkgs = loadKnowledgePackages("test_Dynamic3_0.drl");
kBase.addPackages(SerializationHelper.serializeObject(kpkgs));
session = getSerialisedStatefulKnowledgeSession(session, kBase, true);
InternalFactHandle stilton2 = (InternalFactHandle) session.insert(new Cheese("stilton", 20));
session.insert(new Cheese("brie", 20));
InternalFactHandle mark = (InternalFactHandle) session.insert(new Person("mark", 20));
session.fireAllRules();
results = (List) session.getGlobal("results");
assertEquals(4, results.size());
assertEquals(stilton2.getObject(), results.get(1));
assertEquals(bob.getObject(), results.get(2));
assertEquals(mark.getObject(), results.get(3));
session = getSerialisedStatefulKnowledgeSession(session, kBase, true);
// dispose session
session.dispose();
}
use of org.drools.kiesession.rulebase.InternalKnowledgeBase in project drools by kiegroup.
the class TruthMaintenanceTest method testLogicalInsertionsModifySameRuleGivesDifferentLogicalInsertion.
@Test(timeout = 10000)
public void testLogicalInsertionsModifySameRuleGivesDifferentLogicalInsertion() throws Exception {
// TODO JBRULES-1804
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newClassPathResource("test_LogicalInsertionsModifySameRuleGivesDifferentLogicalInsertion.drl", getClass()), ResourceType.DRL);
Collection<KiePackage> pkgs = kbuilder.getKnowledgePackages();
InternalKnowledgeBase kbase = (InternalKnowledgeBase) getKnowledgeBase();
kbase.addPackages(pkgs);
KieSession session = createKnowledgeSession(kbase);
try {
Sensor sensor1 = new Sensor(100, 0);
FactHandle sensor1Handle = session.insert(sensor1);
Sensor sensor2 = new Sensor(200, 0);
FactHandle sensor2Handle = session.insert(sensor2);
Sensor sensor3 = new Sensor(200, 0);
FactHandle sensor3Handle = session.insert(sensor3);
session.fireAllRules();
session = getSerialisedStatefulKnowledgeSession(session, true);
List temperatureList = new ArrayList(session.getObjects(new ClassObjectFilter(Integer.class)));
assertTrue(temperatureList.contains(Integer.valueOf(100)));
assertTrue(temperatureList.contains(Integer.valueOf(200)));
assertEquals(2, temperatureList.size());
sensor1.setTemperature(150);
sensor1Handle = getFactHandle(sensor1Handle, session);
session.update(sensor1Handle, sensor1);
session = getSerialisedStatefulKnowledgeSession(session, true);
session.fireAllRules();
temperatureList = new ArrayList(session.getObjects(new ClassObjectFilter(Integer.class)));
assertFalse(temperatureList.contains(Integer.valueOf(100)));
assertTrue(temperatureList.contains(Integer.valueOf(150)));
assertTrue(temperatureList.contains(Integer.valueOf(200)));
assertEquals(2, temperatureList.size());
} finally {
session.dispose();
}
}
use of org.drools.kiesession.rulebase.InternalKnowledgeBase in project drools by kiegroup.
the class TruthMaintenanceTest method testLogicalInsertionsBacking.
@Test(timeout = 10000)
public void testLogicalInsertionsBacking() throws Exception {
final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newClassPathResource("test_LogicalInsertionsBacking.drl", getClass()), ResourceType.DRL);
Collection<KiePackage> kpkgs = kbuilder.getKnowledgePackages();
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kpkgs);
KieSession session = createKnowledgeSession(kbase);
try {
final Cheese cheese1 = new Cheese("c", 1);
final Cheese cheese2 = new Cheese(cheese1.getType(), 1);
FactHandle h1 = session.insert(cheese1);
session.fireAllRules();
session = getSerialisedStatefulKnowledgeSession(session, true);
Collection<?> list = session.getObjects(new ClassObjectFilter(cheese1.getType().getClass()));
assertEquals(1, list.size());
// probably dangerous, as contains works with equals, not identity
assertEquals(cheese1.getType(), list.iterator().next());
FactHandle h2 = session.insert(cheese2);
session.fireAllRules();
session = getSerialisedStatefulKnowledgeSession(session, true);
list = session.getObjects(new ClassObjectFilter(cheese1.getType().getClass()));
assertEquals(1, list.size());
assertEquals(cheese1.getType(), list.iterator().next());
assertEquals(3, session.getObjects().size());
h1 = getFactHandle(h1, session);
session.delete(h1);
session = getSerialisedStatefulKnowledgeSession(session, true);
session.fireAllRules();
session = getSerialisedStatefulKnowledgeSession(session, true);
list = session.getObjects(new ClassObjectFilter(cheese1.getType().getClass()));
assertEquals("cheese-type " + cheese1.getType() + " was deleted, but should not. Backed by cheese2 => type.", 1, list.size());
assertEquals("cheese-type " + cheese1.getType() + " was deleted, but should not. Backed by cheese2 => type.", cheese1.getType(), list.iterator().next());
h2 = getFactHandle(h2, session);
session.delete(h2);
session = getSerialisedStatefulKnowledgeSession(session, true);
session.fireAllRules();
session = getSerialisedStatefulKnowledgeSession(session, true);
list = session.getObjects(new ClassObjectFilter(cheese1.getType().getClass()));
assertEquals("cheese-type " + cheese1.getType() + " was not deleted, but should have. Neither cheese1 => type nor cheese2 => type is true.", 0, list.size());
} finally {
session.dispose();
}
}
use of org.drools.kiesession.rulebase.InternalKnowledgeBase in project drools by kiegroup.
the class UnmarshallingTest method initializeKnowledgeBase.
private KieBase initializeKnowledgeBase(String rule) {
// Setup knowledge base
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newReaderResource(new StringReader(rule)), ResourceType.DRL);
if (kbuilder.hasErrors()) {
throw new RuntimeException(kbuilder.getErrors().toString());
}
KieBaseConfiguration config = RuleBaseFactory.newKnowledgeBaseConfiguration();
config.setOption(EventProcessingOption.STREAM);
InternalKnowledgeBase knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase(RuleBaseFactory.newRuleBase(config));
knowledgeBase.addPackages(kbuilder.getKnowledgePackages());
return knowledgeBase;
}
use of org.drools.kiesession.rulebase.InternalKnowledgeBase in project drools by kiegroup.
the class StreamsTest method testEventExpirationValue.
@Test(timeout = 10000)
public void testEventExpirationValue() {
final String drl1 = "package org.drools.pkg1\n" + "import " + StockTick.class.getCanonicalName() + ";\n" + "declare StockTick\n" + " @role(event)\n" + "end\n" + "rule X\n" + "when\n" + " StockTick()\n" + "then\n" + "end\n";
final String drl2 = "package org.drools.pkg2\n" + "import " + StockTick.class.getCanonicalName() + ";\n" + "declare StockTick\n" + " @role(event)\n" + "end\n" + "rule X\n" + "when\n" + " StockTick()\n" + "then\n" + "end\n";
final InternalKnowledgeBase kbase = (InternalKnowledgeBase) KieBaseUtil.getKieBaseFromKieModuleFromDrl("stream-test", kieBaseTestConfiguration, drl1, drl2);
final List<ObjectTypeNode> otns = kbase.getRete().getObjectTypeNodes();
final ObjectType stot = new ClassObjectType(StockTick.class);
for (final ObjectTypeNode otn : otns) {
if (otn.getObjectType().isAssignableFrom(stot)) {
assertEquals(NEVER_EXPIRES, otn.getExpirationOffset());
}
}
}
Aggregations