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());
}
}
;
}
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());
}
;
}
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());
}
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());
}
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);
}
Aggregations