Search in sources :

Example 36 with KiePackage

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);
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KiePackage(org.kie.api.definition.KiePackage) StringReader(java.io.StringReader) KieSession(org.kie.api.runtime.KieSession) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 37 with KiePackage

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

Example 38 with KiePackage

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());
}
Also used : Order(org.drools.compiler.Order) KiePackage(org.kie.api.definition.KiePackage) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) OrderItem(org.drools.compiler.OrderItem) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 39 with KiePackage

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

Example 40 with KiePackage

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

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