use of org.drools.kiesession.rulebase.InternalKnowledgeBase in project drools by kiegroup.
the class KnowledgeBuilderTest method testSerializable.
@Test
public void testSerializable() throws Exception {
final KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
final PackageDescr packageDescr = new PackageDescr("p1");
final RuleDescr ruleDescr = new RuleDescr("rule-1");
packageDescr.addRule(ruleDescr);
final AndDescr lhs = new AndDescr();
ruleDescr.setLhs(lhs);
packageDescr.addGlobal(new GlobalDescr("map", "java.util.Map"));
ruleDescr.setConsequence("map.put(\"value\", new Integer(1) );");
// check that packageDescr is serializable
final PackageDescr back = (PackageDescr) SerializationHelper.serializeObject(packageDescr);
assertNotNull(back);
assertEquals("p1", back.getName());
builder.addPackage(packageDescr);
InternalKnowledgePackage pkg = builder.getPackage(packageDescr.getName());
final RuleImpl rule = pkg.getRule("rule-1");
assertLength(0, builder.getErrors().getErrors());
InternalKnowledgePackage newPkg = SerializationHelper.serializeObject(pkg);
final RuleImpl newRule = newPkg.getRule("rule-1");
InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
;
// It's been serialised so we have to simulate the re-wiring process
newPkg.getDialectRuntimeRegistry().onAdd(kBase.getRootClassLoader());
newPkg.getDialectRuntimeRegistry().onBeforeExecute();
kBase.getGlobals().put("map", Map.class);
final KieSession workingMemory = kBase.newKieSession();
final HashMap map = new HashMap();
workingMemory.setGlobal("map", map);
final LeftTupleImpl tuple = new MockTuple(new HashMap());
tuple.setLeftTupleSink(new RuleTerminalNode(1, new MockBetaNode(), newRule, newRule.getLhs(), 0, new BuildContext(kBase, Collections.emptyList())));
final Activation activation = new MockActivation(newRule, 0, newRule.getLhs(), tuple);
final DefaultKnowledgeHelper knowledgeHelper = new DefaultKnowledgeHelper(((StatefulKnowledgeSessionImpl) workingMemory));
knowledgeHelper.setActivation(activation);
newRule.getConsequence().evaluate(knowledgeHelper, ((StatefulKnowledgeSessionImpl) workingMemory));
assertEquals(new Integer(1), map.get("value"));
}
use of org.drools.kiesession.rulebase.InternalKnowledgeBase in project drools by kiegroup.
the class PatternBuilderForQueryTest method testRuleWithQuery.
@Test
public void testRuleWithQuery() throws Exception {
final KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
final PackageDescr packageDescr = new PackageDescr("p1");
QueryDescr queryDescr = new QueryDescr("query1");
queryDescr.addParameter("String", "$name");
queryDescr.addParameter("int", "$age");
queryDescr.addParameter("String", "$likes");
packageDescr.addRule(queryDescr);
AndDescr lhs = new AndDescr();
queryDescr.setLhs(lhs);
PatternDescr pattern = new PatternDescr(Person.class.getName());
lhs.addDescr(pattern);
pattern.addConstraint(new BindingDescr("$name", "name", true));
pattern.addConstraint(new BindingDescr("$age", "age", true));
pattern.addConstraint(new BindingDescr("$likes", "likes", true));
RuleDescr ruleDescr = new RuleDescr("rule-1");
packageDescr.addRule(ruleDescr);
lhs = new AndDescr();
ruleDescr.setLhs(lhs);
pattern = new PatternDescr(Cheese.class.getName());
lhs.addDescr(pattern);
pattern.addConstraint(new BindingDescr("$type", "type"));
pattern = new PatternDescr("query1");
pattern.setQuery(true);
lhs.addDescr(pattern);
ExprConstraintDescr expr = new ExprConstraintDescr("'bobba'");
expr.setPosition(0);
expr.setType(Type.POSITIONAL);
pattern.addConstraint(expr);
expr = new ExprConstraintDescr("$age");
expr.setPosition(1);
expr.setType(Type.POSITIONAL);
pattern.addConstraint(expr);
expr = new ExprConstraintDescr("$type");
expr.setPosition(2);
expr.setType(Type.POSITIONAL);
pattern.addConstraint(expr);
ruleDescr.setConsequence("System.out.println(\"age: \" + $age);");
builder.addPackage(packageDescr);
assertLength(0, builder.getErrors().getErrors());
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(Arrays.asList(new KiePackage[] { builder.getPackage(packageDescr.getName()) }));
final KieSession session = kbase.newKieSession();
session.insert(new Person("bobba", "stilton", 90));
session.insert(new Person("bobba", "brie", 80));
session.insert(new Person("bobba", "brie", 75));
session.insert(new Person("darth", "brie", 100));
session.insert(new Person("luke", "brie", 25));
session.insert(new Cheese("brie", 25));
session.fireAllRules();
}
use of org.drools.kiesession.rulebase.InternalKnowledgeBase in project drools by kiegroup.
the class DynamicRuleRemovalTest method testDynamicRuleRemoval.
@Test
public void testDynamicRuleRemoval() throws Exception {
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
;
addRule(kbase, "rule1");
addRule(kbase, "rule2");
addRule(kbase, "rule3");
final KieSession ksession = kbase.newKieSession();
List<String> rulesList = new ArrayList<String>();
ksession.setGlobal("list", rulesList);
ksession.insert("2");
ksession.fireAllRules();
assertEquals(3, rulesList.size());
assertTrue(rulesList.contains("rule1"));
assertTrue(rulesList.contains("rule2"));
assertTrue(rulesList.contains("rule3"));
removeRule(kbase, "rule1");
rulesList.clear();
ksession.insert("3");
ksession.fireAllRules();
assertEquals(2, rulesList.size());
assertFalse(rulesList.contains("rule1"));
assertTrue(rulesList.contains("rule2"));
assertTrue(rulesList.contains("rule3"));
}
use of org.drools.kiesession.rulebase.InternalKnowledgeBase in project drools by kiegroup.
the class DynamicRulesTest method testDynamicRuleRemovalsUnusedWorkingMemory.
@Test(timeout = 10000)
public void testDynamicRuleRemovalsUnusedWorkingMemory() throws Exception {
InternalKnowledgeBase kbase = (InternalKnowledgeBase) KieBaseUtil.getKieBaseFromClasspathResources("test", getClass(), kieBaseTestConfiguration, "test_Dynamic1.drl", "test_Dynamic2.drl", "test_Dynamic3.drl", "test_Dynamic4.drl");
KieSession workingMemory = kbase.newKieSession();
assertEquals(2, kbase.getKiePackages().size());
KiePackage knowledgePackage = null;
for (KiePackage pkg : kbase.getKiePackages()) {
if (pkg.getName().equals("org.drools.mvel.compiler.test")) {
knowledgePackage = pkg;
break;
}
}
assertEquals(5, knowledgePackage.getRules().size());
kbase.removeRule("org.drools.mvel.compiler.test", "Who likes Stilton");
assertEquals(4, knowledgePackage.getRules().size());
kbase.removeRule("org.drools.mvel.compiler.test", "like cheese");
assertEquals(3, knowledgePackage.getRules().size());
kbase.removeKiePackage("org.drools.mvel.compiler.test");
assertEquals(1, kbase.getKiePackages().size());
}
use of org.drools.kiesession.rulebase.InternalKnowledgeBase in project drools by kiegroup.
the class DynamicRulesTest method testDynamicRuleRemovals.
@Test(timeout = 10000)
public void testDynamicRuleRemovals() throws Exception {
InternalKnowledgeBase kbase = (InternalKnowledgeBase) KieBaseUtil.getKieBaseFromClasspathResources("test", getClass(), kieBaseTestConfiguration, "test_Dynamic1.drl", "test_Dynamic3.drl", "test_Dynamic4.drl");
Collection<KiePackage> kpkgs = KieBaseUtil.getKieBaseFromClasspathResources("tmp", getClass(), kieBaseTestConfiguration, "test_Dynamic2.drl").getKiePackages();
kbase.addPackages(kpkgs);
KieSession wm = kbase.newKieSession();
// AgendaEventListener ael = mock( AgendaEventListener.class );
// wm.addEventListener( ael );
final List<?> list = new ArrayList<Object>();
wm.setGlobal("list", list);
final PersonInterface bob = new Person("bob", "stilton");
bob.setStatus("Not evaluated");
FactHandle fh0 = wm.insert(bob);
final Cheese stilton1 = new Cheese("stilton", 5);
FactHandle fh1 = wm.insert(stilton1);
final Cheese stilton2 = new Cheese("stilton", 3);
FactHandle fh2 = wm.insert(stilton2);
final Cheese stilton3 = new Cheese("stilton", 1);
FactHandle fh3 = wm.insert(stilton3);
final Cheese cheddar = new Cheese("cheddar", 5);
FactHandle fh4 = wm.insert(cheddar);
wm.fireAllRules();
assertEquals(15, list.size());
list.clear();
kbase.removeRule("org.drools.mvel.compiler.test", "Who likes Stilton");
wm.update(fh0, bob);
wm.update(fh1, stilton1);
wm.update(fh2, stilton2);
wm.update(fh3, stilton3);
wm.update(fh4, cheddar);
wm.fireAllRules();
assertEquals(12, list.size());
list.clear();
kbase.removeRule("org.drools.mvel.compiler.test", "like cheese");
wm.update(fh0, bob);
wm.update(fh1, stilton1);
wm.update(fh2, stilton2);
wm.update(fh3, stilton3);
wm.update(fh4, cheddar);
wm.fireAllRules();
assertEquals(8, list.size());
list.clear();
final Cheese muzzarela = new Cheese("muzzarela", 5);
wm.insert(muzzarela);
wm.fireAllRules();
assertEquals(1, list.size());
list.clear();
}
Aggregations