Search in sources :

Example 51 with KiePackage

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

the class ChangesetUndoTest method testCompilationUndoAfterGoodResults.

@Test
public void testCompilationUndoAfterGoodResults() {
    KnowledgeBuilder knowledgeBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    knowledgeBuilder.add(new FileSystemResource(resources[1]), ResourceType.DRL);
    knowledgeBuilder.add(new FileSystemResource(resources[4]), ResourceType.CHANGE_SET);
    assertTrue(knowledgeBuilder.hasErrors());
    knowledgeBuilder.undo();
    assertFalse(knowledgeBuilder.hasErrors());
    for (KiePackage kp : knowledgeBuilder.getKnowledgePackages()) {
        if ("org.drools.test1".equals(kp.getName())) {
            assertEquals(1, kp.getRules().size());
            assertEquals(1, kp.getFactTypes().size());
        } else {
            assertTrue(kp.getRules().isEmpty());
            assertTrue(kp.getFactTypes().isEmpty());
        }
    }
    ;
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KiePackage(org.kie.api.definition.KiePackage) FileSystemResource(org.drools.core.io.impl.FileSystemResource) Test(org.junit.Test)

Example 52 with KiePackage

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

the class ChangesetUndoTest method testCompilationUndo.

@Test
public void testCompilationUndo() {
    KnowledgeBuilder knowledgeBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    knowledgeBuilder.add(new FileSystemResource(resources[0]), ResourceType.CHANGE_SET);
    assertTrue(knowledgeBuilder.hasErrors());
    knowledgeBuilder.undo();
    assertFalse(knowledgeBuilder.hasErrors());
    for (KiePackage kp : knowledgeBuilder.getKnowledgePackages()) {
        assertTrue(kp.getRules().isEmpty());
        assertTrue(kp.getFactTypes().isEmpty());
    }
    ;
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KiePackage(org.kie.api.definition.KiePackage) FileSystemResource(org.drools.core.io.impl.FileSystemResource) Test(org.junit.Test)

Example 53 with KiePackage

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

the class TimerAndCalendarTest method testTimerRuleAfterCronReloadSession.

@Test
@Ignore("beta4 phreak")
public void testTimerRuleAfterCronReloadSession() 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 300ms
    String timerRule = "package org.drools.test\n" + "global java.util.List list \n" + "rule TimerRule \n" + "   timer (cron: * * * * * ?) \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);
    List<Integer> list = Collections.synchronizedList(new ArrayList<Integer>());
    ksession.setGlobal("list", list);
    ksession.setGlobal("list", list);
    clock.advanceTime(10, TimeUnit.MILLISECONDS);
    ksession.fireAllRules();
    clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
    clock.advanceTime(10, TimeUnit.MILLISECONDS);
    ksession = disposeAndReloadSession(ksession, kbase);
    ksession.setGlobal("list", list);
    clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
    clock.advanceTime(10, TimeUnit.MILLISECONDS);
    Assert.assertEquals(1, list.size());
    clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
    clock.advanceTime(3, TimeUnit.SECONDS);
    Assert.assertEquals(4, list.size());
    ksession = disposeAndReloadSession(ksession, kbase);
    ksession.setGlobal("list", list);
    clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
    clock.advanceTime(2, TimeUnit.SECONDS);
    // if the rule is fired, the list size will greater than one.
    Assert.assertEquals(6, 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 54 with KiePackage

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

the class TimerAndCalendarTest method testTimerWithRemovingRule.

@Test
public void testTimerWithRemovingRule() throws Exception {
    // DROOLS-576
    // Only reproducible with RETEOO
    InternalKnowledgeBase kbase1 = KnowledgeBaseFactory.newKnowledgeBase();
    String str1 = "package org.test; " + "import java.util.*; " + "global java.util.List list; " + "rule R1\n" + "    timer ( int: 5s )\n" + "when\n" + "    $s : String( )\n" + "then\n" + "    list.add( $s );\n" + "end\n";
    Resource resource1 = ResourceFactory.newByteArrayResource(str1.getBytes());
    Collection<KiePackage> kpackages1 = buildKnowledgePackage(resource1, ResourceType.DRL);
    kbase1.addPackages(kpackages1);
    StatefulKnowledgeSession ksession1 = JPAKnowledgeService.newStatefulKnowledgeSession(kbase1, null, createEnvironment(context));
    long ksessionId = ksession1.getIdentifier();
    ArrayList<String> list = new ArrayList<String>();
    ksession1.setGlobal("list", list);
    ksession1.insert("hello");
    ksession1.fireAllRules();
    // dispose before firing
    ksession1.dispose();
    Assert.assertEquals(0, list.size());
    Thread.sleep(5000);
    // A new kbase without the timer's activated rule
    InternalKnowledgeBase kbase2 = KnowledgeBaseFactory.newKnowledgeBase();
    String str2 = "package org.test; " + "import java.util.*; " + "global java.util.List list; " + "rule R2\n" + "when\n" + "    $s : Integer( )\n" + "then\n" + "    list.add( $s );\n" + "end\n";
    Resource resource2 = ResourceFactory.newByteArrayResource(str2.getBytes());
    Collection<KiePackage> kpackages2 = buildKnowledgePackage(resource2, ResourceType.DRL);
    kbase2.addPackages(kpackages2);
    StatefulKnowledgeSession ksession2 = JPAKnowledgeService.loadStatefulKnowledgeSession(ksessionId, kbase2, null, createEnvironment(context));
    ksession2.setGlobal("list", list);
    ksession2.fireAllRules();
    ksession2.dispose();
    Assert.assertEquals(0, list.size());
}
Also used : KiePackage(org.kie.api.definition.KiePackage) StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) Resource(org.kie.api.io.Resource) ArrayList(java.util.ArrayList) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 55 with KiePackage

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

the class ResourcesTest method verifyPackageWithImports.

private void verifyPackageWithImports(final KieBase kbase, final String packageName) {
    final KiePackage pack = kbase.getKiePackage(packageName);
    Assertions.assertThat(pack).as("KiePackage with given name not found in KieBase").isNotNull();
    Assertions.assertThat(pack.getName()).as("Unexpected package name").isEqualTo(packageName);
    Assertions.assertThat((long) pack.getRules().size()).as("Package with import should contain no rules").isEqualTo((long) 0);
}
Also used : KiePackage(org.kie.api.definition.KiePackage)

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