use of org.kie.api.builder.KieFileSystem in project drools by kiegroup.
the class CepJavaTypeTest method testJavaTypeAnnotatedWithRole_WindowLength.
@Test
public void testJavaTypeAnnotatedWithRole_WindowLength() {
String drl = "package org.drools.compiler.integrationtests\n" + "\n" + "import org.drools.compiler.integrationtests.CepJavaTypeTest.Event;\n" + "\n" + "rule \"CEP Window Length\"\n" + "when\n" + " Event() over window:length (10)\n" + "then\n" + "end\n";
KieServices ks = KieServices.Factory.get();
KieModuleModel module = ks.newKieModuleModel();
KieBaseModel defaultBase = module.newKieBaseModel("defaultKBase").setDefault(true).addPackage("*");
defaultBase.newKieSessionModel("defaultKSession").setDefault(true);
KieFileSystem kfs = ks.newKieFileSystem().write("src/main/resources/r1.drl", drl);
kfs.writeKModuleXML(module.toXML());
KieBuilder builder = ks.newKieBuilder(kfs).buildAll();
assertTrue(builder.getResults().getMessages().isEmpty());
}
use of org.kie.api.builder.KieFileSystem in project drools by kiegroup.
the class CepQueryTest method prepare.
@Before
public void prepare() {
final String drl = "package org.drools.compiler.integrationtests\n" + "import org.drools.compiler.integrationtests.CepQueryTest.TestEvent;\n" + "declare TestEvent\n" + " @role( event )\n" + "end\n" + "query EventsAfterZeroToNineSeconds\n" + " $event : TestEvent() from entry-point FirstStream\n" + " $result : TestEvent( this after [0s, 9s] $event) from entry-point SecondStream\n" + "end\n";
final KieServices ks = KieServices.Factory.get();
KieFileSystem kfs = ks.newKieFileSystem();
KieModuleModel module = ks.newKieModuleModel();
KieBaseModel baseModel = module.newKieBaseModel("defaultKBase").setDefault(true).setEventProcessingMode(EventProcessingOption.STREAM);
baseModel.newKieSessionModel("defaultKSession").setDefault(true).setClockType(ClockTypeOption.get("pseudo"));
kfs.writeKModuleXML(module.toXML());
kfs.write(ResourceFactory.newByteArrayResource(drl.getBytes()).setTargetPath("defaultPkg/query.drl"));
assertTrue(ks.newKieBuilder(kfs).buildAll().getResults().getMessages().isEmpty());
ksession = ks.newKieContainer(ks.getRepository().getDefaultReleaseId()).newKieSession();
clock = ksession.getSessionClock();
firstEntryPoint = ksession.getEntryPoint("FirstStream");
secondEntryPoint = ksession.getEntryPoint("SecondStream");
}
use of org.kie.api.builder.KieFileSystem in project drools by kiegroup.
the class ClassLoaderTest method testClassLoaderFromPojo.
@Test
public void testClassLoaderFromPojo() throws IOException {
// DROOLS-1108
final String source = "package org.drools.testdrl;\n" + "public class MyPojo {\n" + " public String getUrlPath() {" + " return getClass().getClassLoader().getResource(\"META-INF/foo.xml\").getPath();\n" + " }\n" + "}\n";
final String drl1 = "package org.drools.testdrl;\n" + "import org.drools.testdrl.MyPojo;\n" + "global java.util.List list;\n" + "rule R1 when\n" + "then\n" + " insert(new MyPojo());\n" + "end\n" + "rule R2 when\n" + " $m : MyPojo()\n" + "then\n" + " list.add($m.getUrlPath());\n" + "end\n";
final String foo = "<xyz/>\n";
final KieServices ks = KieServices.Factory.get();
final ReleaseId releaseId = ks.newReleaseId("org.kie", "test-cl", "1.0.0");
final KieFileSystem kfs = ks.newKieFileSystem();
final KieModuleModel kproj = ks.newKieModuleModel();
final KieBaseModel kieBaseModel1 = kproj.newKieBaseModel("testKbase").setDefault(true).addPackage("org.drools.testdrl");
final KieSessionModel ksession1 = kieBaseModel1.newKieSessionModel("testKsession").setDefault(true);
kfs.writeKModuleXML(kproj.toXML());
kfs.write("src/main/resources/org/drools/testdrl/rules1.drl", drl1);
kfs.write("src/main/java/org/drools/testdrl/MyPojo.java", source);
kfs.write("src/main/resources/META-INF/foo.xml", foo);
kfs.generateAndWritePomXML(releaseId);
final KieBuilder kieBuilder = ks.newKieBuilder(kfs);
assertTrue(kieBuilder.buildAll().getResults().getMessages().isEmpty());
final KieContainer kieContainer = ks.newKieContainer(releaseId);
final ClassLoader classLoader = kieContainer.getClassLoader();
final URL url = classLoader.getResource("META-INF/foo.xml");
assertNotNull(url);
final KieSession ksession = kieContainer.newKieSession("testKsession");
final List<URL> list = new ArrayList<>();
ksession.setGlobal("list", list);
ksession.fireAllRules();
assertEquals(1, list.size());
assertEquals(url.getPath(), list.get(0));
ksession.dispose();
}
use of org.kie.api.builder.KieFileSystem in project drools by kiegroup.
the class JpaPersistentStatefulSessionTest method testSetFocus.
@Test
public void testSetFocus() {
String str = "";
str += "package org.kie.test\n";
str += "global java.util.List list\n";
str += "rule rule1\n";
str += "agenda-group \"badfocus\"";
str += "when\n";
str += " Integer(intValue > 0)\n";
str += "then\n";
str += " list.add( 1 );\n";
str += "end\n";
str += "\n";
KieServices ks = KieServices.Factory.get();
KieFileSystem kfs = ks.newKieFileSystem().write("src/main/resources/r1.drl", str);
ks.newKieBuilder(kfs).buildAll();
KieBase kbase = ks.newKieContainer(ks.getRepository().getDefaultReleaseId()).getKieBase();
KieSession ksession = ks.getStoreServices().newKieSession(kbase, null, env);
List<?> list = new ArrayList<Object>();
ksession.setGlobal("list", list);
ksession.insert(1);
ksession.insert(2);
ksession.insert(3);
ksession.getAgenda().getAgendaGroup("badfocus").setFocus();
ksession.fireAllRules();
assertEquals(3, list.size());
}
use of org.kie.api.builder.KieFileSystem in project drools by kiegroup.
the class JpaPersistentStatefulSessionTest method testFactHandleSerialization.
@Test
public void testFactHandleSerialization() {
String str = "";
str += "package org.kie.test\n";
str += "import java.util.concurrent.atomic.AtomicInteger\n";
str += "global java.util.List list\n";
str += "rule rule1\n";
str += "when\n";
str += " $i: AtomicInteger(intValue > 0)\n";
str += "then\n";
str += " list.add( $i );\n";
str += "end\n";
str += "\n";
KieServices ks = KieServices.Factory.get();
KieFileSystem kfs = ks.newKieFileSystem().write("src/main/resources/r1.drl", str);
ks.newKieBuilder(kfs).buildAll();
KieBase kbase = ks.newKieContainer(ks.getRepository().getDefaultReleaseId()).getKieBase();
KieSession ksession = ks.getStoreServices().newKieSession(kbase, null, env);
List<?> list = new ArrayList<Object>();
ksession.setGlobal("list", list);
AtomicInteger value = new AtomicInteger(4);
FactHandle atomicFH = ksession.insert(value);
ksession.fireAllRules();
assertEquals(1, list.size());
value.addAndGet(1);
ksession.update(atomicFH, value);
ksession.fireAllRules();
assertEquals(2, list.size());
String externalForm = atomicFH.toExternalForm();
ksession = ks.getStoreServices().loadKieSession(ksession.getIdentifier(), kbase, null, env);
atomicFH = ksession.execute(CommandFactory.fromExternalFactHandleCommand(externalForm));
value.addAndGet(1);
ksession.update(atomicFH, value);
ksession.fireAllRules();
list = (List<?>) ksession.getGlobal("list");
assertEquals(3, list.size());
}
Aggregations