Search in sources :

Example 31 with KiePackage

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());
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KiePackage(org.kie.api.definition.KiePackage) Test(org.junit.Test)

Example 32 with KiePackage

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);
}
Also used : KiePackage(org.kie.api.definition.KiePackage) KieSession(org.kie.api.runtime.KieSession) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase)

Example 33 with KiePackage

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);
}
Also used : KiePackage(org.kie.api.definition.KiePackage) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.compiler.Cheese) Cheesery(org.drools.compiler.Cheesery) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 34 with KiePackage

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);
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) Address(org.drools.compiler.Address) KiePackage(org.kie.api.definition.KiePackage) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) PersonHolder(org.drools.compiler.PersonHolder) Person(org.drools.compiler.Person) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 35 with KiePackage

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());
}
Also used : KiePackage(org.kie.api.definition.KiePackage) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.compiler.Cheese) ArrayList(java.util.ArrayList) List(java.util.List) Person(org.drools.compiler.Person) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase)

Aggregations

KiePackage (org.kie.api.definition.KiePackage)124 Test (org.junit.Test)93 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)80 KieSession (org.kie.api.runtime.KieSession)70 ArrayList (java.util.ArrayList)44 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)39 List (java.util.List)25 Cheese (org.drools.compiler.Cheese)22 Person (org.drools.compiler.Person)18 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)17 InternalFactHandle (org.drools.core.common.InternalFactHandle)15 IteratorToList (org.drools.compiler.integrationtests.IteratorToList)11 EntryPoint (org.kie.api.runtime.rule.EntryPoint)10 Resource (org.kie.api.io.Resource)9 StringReader (java.io.StringReader)8 HashMap (java.util.HashMap)8 FactHandle (org.kie.api.runtime.rule.FactHandle)8 Map (java.util.Map)7 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)7 KieBase (org.kie.api.KieBase)7