Search in sources :

Example 81 with KiePackage

use of org.kie.api.definition.KiePackage in project drools by kiegroup.

the class DynamicRulesTest method testDynamicFunction.

@Test(timeout = 10000)
public void testDynamicFunction() throws Exception {
    // JBRULES-1258 serialising a package breaks function removal -- left the serialisation commented out for now
    Collection<KiePackage> kpkgs = SerializationHelper.serializeObject(loadKnowledgePackages("test_DynamicFunction1.drl"));
    InternalKnowledgeBase kbase = (InternalKnowledgeBase) loadKnowledgeBase();
    kbase.addPackages(kpkgs);
    kbase = SerializationHelper.serializeObject(kbase);
    final KieSession workingMemory = createKnowledgeSession(kbase);
    final List<?> list = new ArrayList<Object>();
    workingMemory.setGlobal("list", list);
    final Cheese stilton = new Cheese("stilton", 5);
    workingMemory.insert(stilton);
    workingMemory.fireAllRules();
    assertEquals(new Integer(5), list.get(0));
    // Check a function can be removed from a package.
    // Once removed any efforts to use it should throw an Exception
    kbase.removeFunction("org.drools.compiler.test", "addFive");
    final Cheese cheddar = new Cheese("cheddar", 5);
    workingMemory.insert(cheddar);
    try {
        workingMemory.fireAllRules();
        fail("Function should have been removed and NoClassDefFoundError thrown from the Consequence");
    } catch (final Throwable e) {
    }
    // Check a new function can be added to replace an old function
    Collection<KiePackage> kpkgs2 = SerializationHelper.serializeObject(loadKnowledgePackages("test_DynamicFunction2.drl"));
    kbase.addPackages(kpkgs2);
    final Cheese brie = new Cheese("brie", 5);
    workingMemory.insert(brie);
    workingMemory.fireAllRules();
    assertEquals(new Integer(6), list.get(1));
    Collection<KiePackage> kpkgs3 = SerializationHelper.serializeObject(loadKnowledgePackages("test_DynamicFunction3.drl"));
    kbase.addPackages(kpkgs3);
    final Cheese feta = new Cheese("feta", 5);
    workingMemory.insert(feta);
    workingMemory.fireAllRules();
    assertEquals(new Integer(5), list.get(2));
}
Also used : KiePackage(org.kie.api.definition.KiePackage) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.compiler.Cheese) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 82 with KiePackage

use of org.kie.api.definition.KiePackage in project drools by kiegroup.

the class TimerAndCalendarTest method buildKnowledgePackage.

private Collection<KiePackage> buildKnowledgePackage(Resource resource, ResourceType resourceType) {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(resource, resourceType);
    KnowledgeBuilderErrors errors = kbuilder.getErrors();
    if (errors != null && errors.size() > 0) {
        for (KnowledgeBuilderError error : errors) {
            System.err.println("Error: " + error.getMessage());
        }
        Assert.fail("KnowledgeBase did not build");
    }
    Collection<KiePackage> packages = kbuilder.getKnowledgePackages();
    return packages;
}
Also used : KnowledgeBuilderError(org.kie.internal.builder.KnowledgeBuilderError) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KnowledgeBuilderErrors(org.kie.internal.builder.KnowledgeBuilderErrors) KiePackage(org.kie.api.definition.KiePackage)

Example 83 with KiePackage

use of org.kie.api.definition.KiePackage in project drools by kiegroup.

the class TimerAndCalendarTest method testEventExpires.

@Test
public void testEventExpires() throws Exception {
    String timerRule = "package org.drools.test\n" + "declare TestEvent \n" + "    @role( event )\n" + "    @expires( 10s )\n" + "end\n" + "" + "rule TimerRule \n" + "    when \n" + "        TestEvent( ) from entry-point \"Test\"\n" + "    then \n" + "end";
    KieBaseConfiguration kbconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kbconf.setOption(EventProcessingOption.STREAM);
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(kbconf);
    Resource resource = ResourceFactory.newByteArrayResource(timerRule.getBytes());
    Collection<KiePackage> kpackages = buildKnowledgePackage(resource, ResourceType.DRL);
    kbase.addPackages(kpackages);
    KieSession ksession = createSession(kbase);
    FactType type = kbase.getFactType("org.drools.test", "TestEvent");
    Assert.assertNotNull("could not get type", type);
    ksession = disposeAndReloadSession(ksession, kbase);
    ksession.getEntryPoint("Test").insert(type.newInstance());
    ksession.fireAllRules();
    ksession = disposeAndReloadSession(ksession, kbase);
    ksession = disposeAndReloadSession(ksession, kbase);
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) KiePackage(org.kie.api.definition.KiePackage) Resource(org.kie.api.io.Resource) KieSession(org.kie.api.runtime.KieSession) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) FactType(org.kie.api.definition.type.FactType) Test(org.junit.Test)

Example 84 with KiePackage

use of org.kie.api.definition.KiePackage in project drools by kiegroup.

the class TimerAndCalendarTest method testTimerRuleAfterIntReloadSession.

@Test
@Ignore("beta4 phreak")
public void testTimerRuleAfterIntReloadSession() throws Exception {
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    KieSession ksession = createSession(kbase);
    // must advance time or it won't save.
    SessionPseudoClock clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
    clock.advanceTime(300, TimeUnit.MILLISECONDS);
    // if we do not call 'ksession.fireAllRules()', this test will run successfully.
    ksession.fireAllRules();
    clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
    clock.advanceTime(300, TimeUnit.MILLISECONDS);
    ksession = disposeAndReloadSession(ksession, kbase);
    clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
    clock.advanceTime(300, TimeUnit.MILLISECONDS);
    // build timer rule, if the rule is fired, the list size will increase every 500ms
    String timerRule = "package org.drools.test\n" + "global java.util.List list \n" + "rule TimerRule \n" + "   timer (int:1000 500) \n" + "when \n" + "then \n" + "        list.add(list.size()); \n" + " end";
    Resource resource = ResourceFactory.newByteArrayResource(timerRule.getBytes());
    Collection<KiePackage> kpackages = buildKnowledgePackage(resource, ResourceType.DRL);
    kbase.addPackages(kpackages);
    clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
    clock.advanceTime(10, TimeUnit.MILLISECONDS);
    ksession.fireAllRules();
    clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
    clock.advanceTime(10, TimeUnit.MILLISECONDS);
    ksession = disposeAndReloadSession(ksession, kbase);
    clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
    clock.advanceTime(10, TimeUnit.MILLISECONDS);
    List<Integer> list = Collections.synchronizedList(new ArrayList<Integer>());
    ksession.setGlobal("list", list);
    Assert.assertEquals(0, list.size());
    clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
    clock.advanceTime(1700, TimeUnit.MILLISECONDS);
    Assert.assertEquals(2, list.size());
    ksession = disposeAndReloadSession(ksession, kbase);
    ksession.setGlobal("list", list);
    clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
    clock.advanceTime(1000, TimeUnit.MILLISECONDS);
    // if the rule is fired, the list size will greater than one.
    Assert.assertEquals(4, list.size());
}
Also used : SessionPseudoClock(org.kie.api.time.SessionPseudoClock) KiePackage(org.kie.api.definition.KiePackage) Resource(org.kie.api.io.Resource) KieSession(org.kie.api.runtime.KieSession) SessionClock(org.kie.api.time.SessionClock) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 85 with KiePackage

use of org.kie.api.definition.KiePackage in project drools by kiegroup.

the class HeaderTest method testPMMLHeader.

@Test
public void testPMMLHeader() {
    String source = PMML4Helper.pmmlDefaultPackageName().replace(".", File.separator) + File.separator + "test_header.xml";
    boolean header = false;
    boolean timestamp = false;
    boolean appl = false;
    boolean descr = false;
    boolean copyright = false;
    boolean annotation = false;
    PMML4Compiler compiler = new PMML4Compiler();
    compiler.getHelper().setPack("org.drools.pmml.pmml_4_2.test");
    String theory = compiler.compile(source, null);
    BufferedReader reader = new BufferedReader(new StringReader(theory));
    try {
        String line = "";
        while ((line = reader.readLine()) != null) {
            line = line.trim();
            if (line.startsWith("// Imported PMML Model Theory"))
                header = true;
            else if (line.startsWith("// Creation timestamp :"))
                timestamp = line.contains("now");
            else if (line.startsWith("// Description :"))
                descr = line.contains("test");
            else if (line.startsWith("// Copyright :"))
                copyright = line.contains("opensource");
            else if (line.startsWith("// Annotation :"))
                annotation = line.contains("notes here");
            else if (line.startsWith("// Trained with :"))
                appl = line.contains("handmade");
        }
    } catch (IOException ioe) {
        ioe.printStackTrace();
        fail();
    }
    assertTrue(header);
    assertTrue(timestamp);
    assertTrue(descr);
    assertTrue(copyright);
    assertTrue(annotation);
    assertTrue(appl);
    KieSession ksession = getSession(theory);
    KiePackage pack = ksession.getKieBase().getKiePackage("org.drools.pmml.pmml_4_2.test");
    assertNotNull(pack);
    ksession.dispose();
}
Also used : KiePackage(org.kie.api.definition.KiePackage) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) KieSession(org.kie.api.runtime.KieSession) PMML4Compiler(org.drools.pmml.pmml_4_2.PMML4Compiler) IOException(java.io.IOException) DroolsAbstractPMMLTest(org.drools.pmml.pmml_4_2.DroolsAbstractPMMLTest) 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