use of org.kie.api.definition.KiePackage in project drools by kiegroup.
the class KnowledgeBuilderTest method testAddPackageArray.
@Test
public void testAddPackageArray() throws Exception {
String rule = "package org.drools.compiler.test\n" + "import org.drools.compiler.StockTick\n" + "declare StockTick @role(event) end\n" + "rule R1 when\n" + " StockTick()\n" + "then\n" + "end\n";
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newByteArrayResource(rule.getBytes()), ResourceType.DRL);
assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
Collection<KiePackage> kpkgs = kbuilder.getKnowledgePackages();
assertEquals(2, kpkgs.size());
}
use of org.kie.api.definition.KiePackage in project drools by kiegroup.
the class DslTest method testDSLWithIndividualConstraintMappings.
@Test
public void testDSLWithIndividualConstraintMappings() throws Exception {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newClassPathResource("test_dslWithIndividualConstraints.dsl", getClass()), ResourceType.DSL);
kbuilder.add(ResourceFactory.newClassPathResource("test_dslWithIndividualConstraints.dslr", getClass()), ResourceType.DSLR);
if (kbuilder.hasErrors()) {
fail(kbuilder.getErrors().toString());
}
// Check errors
final String err = kbuilder.getErrors().toString();
assertEquals("", err);
assertEquals(0, kbuilder.getErrors().size());
// the compiled package
final Collection<KiePackage> pkgs = kbuilder.getKnowledgePackages();
assertEquals(1, pkgs.size());
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(pkgs);
kbase = SerializationHelper.serializeObject(kbase);
KieSession session = createKnowledgeSession(kbase);
List results = new ArrayList();
session.setGlobal("results", results);
Cheese cheese = new Cheese("stilton", 42);
session.insert(cheese);
// wm = SerializationHelper.serializeObject(wm);
session.fireAllRules();
// should have fired
assertEquals(1, results.size());
assertEquals(cheese, results.get(0));
}
use of org.kie.api.definition.KiePackage in project drools by kiegroup.
the class DslTest method testWithExpanderMore.
@Test
public void testWithExpanderMore() throws Exception {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newClassPathResource("test_expander.dsl", getClass()), ResourceType.DSL);
kbuilder.add(ResourceFactory.newClassPathResource("rule_with_expander_dsl_more.dslr", getClass()), ResourceType.DSLR);
assertFalse(kbuilder.hasErrors());
// Check errors
final String err = kbuilder.getErrors().toString();
assertEquals("", err);
assertEquals(0, kbuilder.getErrors().size());
// the compiled package
final Collection<KiePackage> pkgs = kbuilder.getKnowledgePackages();
assertEquals(2, pkgs.size());
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(pkgs);
kbase = SerializationHelper.serializeObject(kbase);
KieSession session = createKnowledgeSession(kbase);
session.insert(new Person("rage"));
session.insert(new Cheese("cheddar", 15));
final List messages = new ArrayList();
session.setGlobal("messages", messages);
// wm = SerializationHelper.serializeObject(wm);
session.fireAllRules();
// should have NONE, as both conditions should be false.
assertEquals(0, messages.size());
session.insert(new Person("fire"));
session.fireAllRules();
// still no firings
assertEquals(0, messages.size());
session.insert(new Cheese("brie", 15));
session.fireAllRules();
// YOUR FIRED
assertEquals(1, messages.size());
}
use of org.kie.api.definition.KiePackage in project drools by kiegroup.
the class DynamicRulesTest method testDynamicNotNode.
@Test(timeout = 10000)
public void testDynamicNotNode() throws Exception {
InternalKnowledgeBase kbase = (InternalKnowledgeBase) loadKnowledgeBase("test_CollectDynamicRules1.drl");
kbase = SerializationHelper.serializeObject(kbase);
Environment env = EnvironmentFactory.newEnvironment();
env.set(EnvironmentName.OBJECT_MARSHALLING_STRATEGIES, new ObjectMarshallingStrategy[] { new IdentityPlaceholderResolverStrategy(ClassObjectMarshallingStrategyAcceptor.DEFAULT) });
KieSession ksession = kbase.newKieSession(null, env);
List<?> results = new ArrayList<Object>();
ksession.setGlobal("results", results);
final Cheese a = new Cheese("stilton", 10);
final Cheese b = new Cheese("stilton", 15);
final Cheese c = new Cheese("stilton", 20);
ksession.insert(a);
ksession.insert(b);
ksession.insert(c);
Collection<KiePackage> kpkgs = SerializationHelper.serializeObject(loadKnowledgePackages("test_DynamicNotNode.drl"));
kbase.addPackages(kpkgs);
kbase = SerializationHelper.serializeObject(kbase);
ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, false);
results = (List) ksession.getGlobal("results");
ksession.fireAllRules();
assertEquals(0, results.size());
kbase.removeKiePackage("org.drools.compiler");
ksession.retract(ksession.getFactHandle(b));
kpkgs = SerializationHelper.serializeObject(loadKnowledgePackages("test_DynamicNotNode.drl"));
kbase.addPackages(kpkgs);
kbase = SerializationHelper.serializeObject(kbase);
ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, false);
results = (List<?>) ksession.getGlobal("results");
ksession.fireAllRules();
assertEquals(1, results.size());
}
use of org.kie.api.definition.KiePackage in project drools by kiegroup.
the class DynamicRulesTest method testDynamicRuleRemovalsUnusedWorkingMemory.
@Test(timeout = 10000)
public void testDynamicRuleRemovalsUnusedWorkingMemory() throws Exception {
InternalKnowledgeBase kbase = (InternalKnowledgeBase) SerializationHelper.serializeObject(loadKnowledgeBase("test_Dynamic1.drl", "test_Dynamic2.drl", "test_Dynamic3.drl", "test_Dynamic4.drl"));
KieSession workingMemory = createKnowledgeSession(kbase);
assertEquals(2, kbase.getKiePackages().size());
KiePackage knowledgePackage = null;
for (KiePackage pkg : kbase.getKiePackages()) {
if (pkg.getName().equals("org.drools.compiler.test")) {
knowledgePackage = pkg;
break;
}
}
assertEquals(5, knowledgePackage.getRules().size());
kbase.removeRule("org.drools.compiler.test", "Who likes Stilton");
assertEquals(4, knowledgePackage.getRules().size());
kbase.removeRule("org.drools.compiler.test", "like cheese");
assertEquals(3, knowledgePackage.getRules().size());
kbase.removeKiePackage("org.drools.compiler.test");
assertEquals(1, kbase.getKiePackages().size());
}
Aggregations