use of org.kie.api.definition.KiePackage in project drools by kiegroup.
the class DslTest method testEmptyDSL.
@Test
@Ignore("antlr cannot parse correctly if the file ends with a comment without a further line break")
public void testEmptyDSL() throws Exception {
// FIXME etirelli / mic_hat not sure what to do with this?
// gives antlr <EOF> error
final String DSL = "# This is an empty dsl file.";
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newClassPathResource("test_expander.dsl", getClass()), ResourceType.DSL);
kbuilder.add(ResourceFactory.newReaderResource(new StringReader(DSL)), ResourceType.DSLR);
// trying to expand Cheese() pattern
assertFalse(kbuilder.hasErrors());
// Check errors
final String err = kbuilder.getErrors().toString();
assertEquals("", err);
assertEquals(0, kbuilder.getErrors().size());
// the compiled package
Collection<KiePackage> pkgs = kbuilder.getKnowledgePackages();
assertEquals(0, pkgs.size());
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(pkgs);
kbase = SerializationHelper.serializeObject(kbase);
KieSession session = createKnowledgeSession(kbase);
pkgs = SerializationHelper.serializeObject(pkgs);
assertNull(pkgs);
}
use of org.kie.api.definition.KiePackage in project drools by kiegroup.
the class DynamicRulesTest method testDynamicRuleAdditions.
@Test(timeout = 10000)
public void testDynamicRuleAdditions() throws Exception {
InternalKnowledgeBase kbase = (InternalKnowledgeBase) SerializationHelper.serializeObject(loadKnowledgeBase("test_Dynamic1.drl"));
KieSession workingMemory = createKnowledgeSession(kbase);
workingMemory.setGlobal("total", new Integer(0));
final List<?> list = new ArrayList<Object>();
workingMemory.setGlobal("list", list);
// Adding person in advance. There is no Person() object
// type node in memory yet, but the rule engine is supposed
// to handle that correctly
final PersonInterface bob = new Person("bob", "stilton");
bob.setStatus("Not evaluated");
workingMemory.insert(bob);
final Cheese stilton = new Cheese("stilton", 5);
workingMemory.insert(stilton);
final Cheese cheddar = new Cheese("cheddar", 5);
workingMemory.insert(cheddar);
workingMemory.fireAllRules();
assertEquals(1, list.size());
assertEquals("stilton", list.get(0));
Collection<KiePackage> kpkgs = SerializationHelper.serializeObject(loadKnowledgePackages("test_Dynamic2.drl"));
kbase.addPackages(kpkgs);
workingMemory.fireAllRules();
assertEquals(5, list.size());
assertEquals("stilton", list.get(0));
assertTrue("cheddar".equals(list.get(1)) || "cheddar".equals(list.get(2)));
assertTrue("stilton".equals(list.get(1)) || "stilton".equals(list.get(2)));
list.clear();
kpkgs = SerializationHelper.serializeObject(loadKnowledgePackages("test_Dynamic3.drl"));
kbase.addPackages(kpkgs);
// Package 3 has a rule working on Person instances.
// As we added person instance in advance, rule should fire now
workingMemory.fireAllRules();
assertEquals("Rule from package 3 should have been fired", "match Person ok", bob.getStatus());
assertEquals(1, list.size());
assertEquals(bob, list.get(0));
kpkgs = SerializationHelper.serializeObject(loadKnowledgePackages("test_Dynamic4.drl"));
kbase.addPackages(kpkgs);
workingMemory.fireAllRules();
kbase = SerializationHelper.serializeObject(kbase);
assertEquals("Rule from package 4 should have been fired", "Who likes Stilton ok", bob.getStatus());
assertEquals(2, list.size());
assertEquals(bob, list.get(1));
}
use of org.kie.api.definition.KiePackage in project drools by kiegroup.
the class DynamicRulesTest method testDynamicRuleRemovalsSubNetwork.
@Test(timeout = 10000)
public void testDynamicRuleRemovalsSubNetwork() throws Exception {
Collection<KiePackage> kpkgs = SerializationHelper.serializeObject(loadKnowledgePackages("test_DynamicRulesWithSubnetwork1.drl", "test_DynamicRulesWithSubnetwork.drl"));
InternalKnowledgeBase kbase = (InternalKnowledgeBase) loadKnowledgeBase();
kbase.addPackages(kpkgs);
kpkgs = SerializationHelper.serializeObject(loadKnowledgePackages("test_DynamicRulesWithSubnetwork2.drl"));
kbase.addPackages(kpkgs);
KieSession session = createKnowledgeSession(kbase);
final List<?> list = new ArrayList<Object>();
session.setGlobal("results", list);
Order order = new Order();
OrderItem item1 = new OrderItem(order, 1, "Adventure Guide Brazil", OrderItem.TYPE_BOOK, 24);
order.addItem(item1);
FactHandle item1Fh = session.insert(item1);
OrderItem item2 = new OrderItem(order, 2, "Prehistoric Britain", OrderItem.TYPE_BOOK, 15);
order.addItem(item2);
FactHandle item2Fh = session.insert(item2);
OrderItem item3 = new OrderItem(order, 3, "Holiday Music", OrderItem.TYPE_CD, 9);
order.addItem(item3);
FactHandle item3Fh = session.insert(item3);
OrderItem item4 = new OrderItem(order, 4, "Very Best of Mick Jagger", OrderItem.TYPE_CD, 11);
order.addItem(item4);
FactHandle item4Fh = session.insert(item4);
session.insert(order);
session.fireAllRules();
assertEquals(11, list.size());
kbase.removeRule("org.drools.compiler", "Apply Discount on all books");
list.clear();
session.update(item1Fh, item1);
session.update(item2Fh, item2);
session.update(item3Fh, item3);
session.update(item4Fh, item4);
session.fireAllRules();
assertEquals(10, list.size());
kbase.removeRule("org.drools.compiler", "like book");
list.clear();
session.update(item1Fh, item1);
session.update(item2Fh, item2);
session.update(item3Fh, item3);
session.update(item4Fh, item4);
session.fireAllRules();
assertEquals(8, list.size());
final OrderItem item5 = new OrderItem(order, 5, "Sinatra : Vegas", OrderItem.TYPE_CD, 5);
FactHandle item5Fh = session.insert(item5);
session.fireAllRules();
assertEquals(10, list.size());
kbase.removeKiePackage("org.drools.compiler");
list.clear();
session.update(item1Fh, item1);
session.update(item2Fh, item2);
session.update(item3Fh, item3);
session.update(item4Fh, item4);
session.update(item5Fh, item5);
session.fireAllRules();
assertEquals(0, list.size());
}
use of org.kie.api.definition.KiePackage in project drools by kiegroup.
the class DynamicRulesTest method testRuleBaseAddRemoveSubNetworks.
@Test(timeout = 10000)
public void testRuleBaseAddRemoveSubNetworks() throws Exception {
try {
// add and remove
InternalKnowledgeBase kbase = (InternalKnowledgeBase) getKnowledgeBase();
Collection<KiePackage> kpkgs = loadKnowledgePackages("test_DynamicRulesWithSubnetwork.drl");
KiePackage kpkg = (KiePackage) kpkgs.toArray()[0];
kbase.addPackages(kpkgs);
kbase.removeKiePackage(kpkg.getName());
// add and remove again
kpkgs = loadKnowledgePackages("test_DynamicRulesWithSubnetwork.drl");
kpkg = (KiePackage) kpkgs.toArray()[0];
kbase.addPackages(kpkgs);
kbase.removeKiePackage(kpkg.getName());
} catch (Exception e) {
e.printStackTrace();
fail("Should not raise any exception");
}
}
use of org.kie.api.definition.KiePackage in project drools by kiegroup.
the class DynamicRulesTest method testDynamicRules2.
@Test(timeout = 10000)
public void testDynamicRules2() throws Exception {
InternalKnowledgeBase kbase = (InternalKnowledgeBase) loadKnowledgeBase();
KieSession session = createKnowledgeSession(kbase);
// Assert some simple facts
final FactA a = new FactA("hello", new Integer(1), new Float(3.14));
final FactB b = new FactB("hello", new Integer(2), new Float(6.28));
session.insert(a);
session.insert(b);
Collection<KiePackage> kpkgs = SerializationHelper.serializeObject(loadKnowledgePackages("test_DynamicRules2.drl"));
kbase.addPackages(kpkgs);
kbase = SerializationHelper.serializeObject(kbase);
session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, true);
session.fireAllRules();
}
Aggregations