use of org.kie.api.definition.KiePackage in project drools by kiegroup.
the class KnowledgeBuilderTest method testAddPackageSingle.
@Test
public void testAddPackageSingle() throws Exception {
String rule = "package org.drools.compiler.test\n" + "import org.drools.compiler.StockTick\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());
KiePackage kpkg = kpkgs.iterator().next();
assertEquals(1, kpkg.getRules().size());
}
use of org.kie.api.definition.KiePackage in project drools by kiegroup.
the class FailureOnRemovalTest method runTest.
private void runTest(boolean shareBetaNodes) throws Exception {
InternalKnowledgeBase kbase = (InternalKnowledgeBase) createKnowledgeBase(shareBetaNodes);
Collection<KiePackage> rule1 = compileRule(RULE_1);
kbase.addPackages(rule1);
// we need to add at least two rules. Test will not fail with only one rule.
Collection<KiePackage> rule2 = compileRule(RULE_2);
kbase.addPackages(rule2);
kbase.removeRule(PACKAGE, RULE_1);
KieSession ksession = createKnowledgeSession(kbase);
int fired = ksession.fireAllRules();
ksession.dispose();
assertEquals(1, fired);
Collection<KiePackage> rule3 = compileRule(RULE_3);
kbase.addPackages(rule3);
}
use of org.kie.api.definition.KiePackage in project drools by kiegroup.
the class FirstOrderLogicTest method testCollectAfterOrCE.
@Test
public void testCollectAfterOrCE() throws Exception {
Collection<KiePackage> pkgs = loadKnowledgePackages("test_OrCEFollowedByCollect.drl");
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(pkgs);
KieSession session = createKnowledgeSession(kbase);
// Set up facts
final Cheesery bonFromage = new Cheesery();
bonFromage.addCheese(new Cheese("cheddar"));
bonFromage.addCheese(new Cheese("cheddar"));
session.insert(bonFromage);
int rules = session.fireAllRules();
assertEquals(2, rules);
// Serialize and test again
pkgs = SerializationHelper.serializeObject(pkgs);
kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(pkgs);
session = createKnowledgeSession(kbase);
session.insert(bonFromage);
rules = session.fireAllRules();
assertEquals(2, rules);
}
use of org.kie.api.definition.KiePackage in project drools by kiegroup.
the class Misc2Test method testMergeMVELDialect.
@Test
public void testMergeMVELDialect() {
// DROOLS-1751
String drl1 = "package com.sample\n" + "import org.drools.compiler.*;\n" + "rule rule1 \n" + " when\n" + " (PersonHolder($addresses : person.addresses))\n" + " &&\n" + " (Address (street == \"AAA\") from $addresses)\n" + " then\n" + "end";
KnowledgeBuilder kbuilder1 = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder1.add(ResourceFactory.newByteArrayResource(drl1.getBytes()), ResourceType.DRL);
Collection<KiePackage> knowledgePackages1 = kbuilder1.getKnowledgePackages();
String drl2 = "package com.sample\n" + "import org.drools.compiler.*;\n" + "rule rule2 \n" + " when\n" + " PersonHolder()\n" + " then\n" + "end";
KnowledgeBuilder kbuilder2 = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder2.add(ResourceFactory.newByteArrayResource(drl2.getBytes()), ResourceType.DRL);
Collection<KiePackage> knowledgePackages2 = kbuilder2.getKnowledgePackages();
InternalKnowledgeBase kbase1 = KnowledgeBaseFactory.newKnowledgeBase();
Collection<KiePackage> combinedPackages1 = new ArrayList<KiePackage>();
combinedPackages1.addAll(knowledgePackages1);
combinedPackages1.addAll(knowledgePackages2);
// Add once to make inUse=true
kbase1.addPackages(combinedPackages1);
InternalKnowledgeBase kbase2 = KnowledgeBaseFactory.newKnowledgeBase();
Collection<KiePackage> combinedPackages2 = new ArrayList<KiePackage>();
combinedPackages2.addAll(knowledgePackages1);
combinedPackages2.addAll(knowledgePackages2);
// this will cause package deepClone
kbase2.addPackages(combinedPackages2);
KieSession ksession = kbase2.newKieSession();
PersonHolder personHolder = new PersonHolder();
Person person = new Person("John");
Address address = new Address("AAA", "BBB", "111");
person.addAddress(address);
personHolder.setPerson(person);
ksession.insert(personHolder);
int fired = ksession.fireAllRules();
assertEquals(2, fired);
}
use of org.kie.api.definition.KiePackage in project drools by kiegroup.
the class DslTest method checkDSLExpanderTest.
private void checkDSLExpanderTest(KnowledgeBuilder kbuilder) throws IOException, ClassNotFoundException {
assertFalse(kbuilder.hasErrors());
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("Bob", "http://foo.bar"));
session.insert(new Cheese("stilton", 42));
final List messages = new ArrayList();
session.setGlobal("messages", messages);
session.fireAllRules();
assertEquals(1, messages.size());
}
Aggregations