Java Code Examples for org.kie.api.KieServices#newKieContainer()
The following examples show how to use
org.kie.api.KieServices#newKieContainer() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BatchRunFluentTest.java From kogito-runtimes with Apache License 2.0 | 6 votes |
@Test public void testSetKieContainerTest() { KieServices kieServices = KieServices.Factory.get(); KieContainer kieContainer = kieServices.newKieContainer(releaseId); ExecutableRunner<RequestContext> runner = ExecutableRunner.create(0L); ExecutableBuilder f = ExecutableBuilder.create(); f.newApplicationContext("app1") .setKieContainer(kieContainer) .newSession() .insert("h1") .fireAllRules() .getGlobal("outS").out("outS1") .dispose(); RequestContext requestContext = runner.execute(f.getExecutable()); assertEquals("h1", requestContext.getOutputs().get("outS1")); }
Example 2
Source File: DynamicRuleLoadTest.java From kogito-runtimes with Apache License 2.0 | 6 votes |
@Test public void testKJarUpgrade() throws Exception { // DROOLS-919 KieServices ks = KieServices.Factory.get(); // Create an in-memory jar for version 1.0.0 ReleaseId releaseId1 = ks.newReleaseId( "org.kie", "test-upgrade", "1.0.0" ); KieModule km = createAndDeployJar( ks, releaseId1, drl1, drl2_1 ); // Create a session and fire rules kieContainer = ks.newKieContainer( km.getReleaseId() ); ksession = kieContainer.newKieSession(); ksession.setGlobal( "test", this ); ksession.insert( new Message( "Hi Universe" ) ); ksession.fireAllRules(); assertTrue( done ); }
Example 3
Source File: KieContainerTest.java From kogito-runtimes with Apache License 2.0 | 6 votes |
@Test public void testGetDefaultKieSessionModelEmptyKmodule() { KieServices kieServices = KieServices.Factory.get(); String drl = "package org.drools.test\n" + "rule R1 when\n" + " $m : Object()\n" + "then\n" + "end\n"; Resource resource = kieServices.getResources().newReaderResource( new StringReader( drl), "UTF-8" ); resource.setTargetPath("org/drools/test/rules.drl"); String kmodule = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<kmodule xmlns=\"http://www.drools.org/xsd/kmodule\">\n" + "</kmodule>"; // Create an in-memory jar for version 1.0.0 ReleaseId releaseId = kieServices.newReleaseId("org.kie", "test-testGetDefaultKieSessionModelEmptyKmodule", "1.0.0"); createAndDeployJar(kieServices, kmodule, releaseId, resource); KieContainer kieContainer = kieServices.newKieContainer(releaseId); KieSessionModel sessionModel = kieContainer.getKieSessionModel(null); assertNotNull(sessionModel); }
Example 4
Source File: KieBuilderTest.java From kogito-runtimes with Apache License 2.0 | 6 votes |
public void createAndTestKieContainer(ReleaseId releaseId, KieBuilder kb, String kBaseName) throws IOException, ClassNotFoundException, InterruptedException { KieServices ks = KieServices.Factory.get(); kb.buildAll(); if ( kb.getResults().hasMessages(Level.ERROR) ) { fail("Unable to build KieModule\n" + kb.getResults( ).toString() ); } KieRepository kr = ks.getRepository(); KieModule kJar = kr.getKieModule(releaseId); assertNotNull( kJar ); KieContainer kContainer = ks.newKieContainer(releaseId); KieBase kBase = kBaseName != null ? kContainer.getKieBase( kBaseName ) : kContainer.getKieBase(); KieSession kSession = kBase.newKieSession(); List list = new ArrayList(); kSession.setGlobal( "list", list ); kSession.fireAllRules(); assertEquals( 1, list.size() ); assertEquals( "org.kie.test.Message", list.get(0).getClass().getName() ); }
Example 5
Source File: KieBuilderTest.java From kogito-runtimes with Apache License 2.0 | 6 votes |
@Test public void testValidXsdTargetNamespace() { final String drl1 = "package org.drools.compiler\n" + "rule R1 when\n" + " $m : Message()\n" + "then\n" + "end\n"; final String kmodule = "<kmodule xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \n" + " xmlns=\"http://www.drools.org/xsd/kmodule\">\n" + " <kbase name=\"kbase1\">\n" + " <ksession name=\"ksession1\" default=\"true\"/>\n" + " </kbase>\n" + "</kmodule>"; final KieServices ks = KieServices.Factory.get(); final ReleaseId releaseId1 = ks.newReleaseId( "org.kie", "test-kie-builder", "1.0.0" ); final Resource r1 = ResourceFactory.newByteArrayResource( drl1.getBytes() ).setResourceType( ResourceType.DRL ).setSourcePath( "kbase1/drl1.drl" ); final KieModule km = createAndDeployJar( ks, kmodule, releaseId1, r1); ks.newKieContainer( km.getReleaseId() ); }
Example 6
Source File: MBeansMonitoringTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
/** * Copied from KieRepositoryTest to test JMX monitoring */ @Test public void testLoadKjarFromClasspath() { // DROOLS-1335 ClassLoader cl = Thread.currentThread().getContextClassLoader(); URLClassLoader urlClassLoader = new URLClassLoader( new URL[]{this.getClass().getResource( "/kie-project-simple-1.0.0.jar" )} ); Thread.currentThread().setContextClassLoader( urlClassLoader ); MBeanServer mbserver = ManagementFactory.getPlatformMBeanServer(); try { KieServices ks = KieServices.Factory.get(); KieRepository kieRepository = ks.getRepository(); ReleaseId releaseId = ks.newReleaseId( "org.test", "kie-project-simple", "1.0.0" ); KieModule kieModule = kieRepository.getKieModule( releaseId ); assertNotNull( kieModule ); assertEquals( releaseId, kieModule.getReleaseId() ); ks.newKieContainer("myID", releaseId); KieContainerMonitorMXBean c1Monitor = JMX.newMXBeanProxy( mbserver, DroolsManagementAgent.createObjectNameBy("myID"), KieContainerMonitorMXBean.class); assertTrue(c1Monitor.getConfiguredReleaseId().sameGAVof(releaseId)); assertTrue(c1Monitor.getResolvedReleaseId().sameGAVof(releaseId)); } finally { Thread.currentThread().setContextClassLoader( cl ); } }
Example 7
Source File: KieContainerTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
@Test public void testMainKieModule() { KieServices ks = KieServices.Factory.get(); // Create an in-memory jar for version 1.0.0 ReleaseId releaseId = ks.newReleaseId("org.kie", "test-delete", "1.0.0"); createAndDeployJar( ks, releaseId, createDRL("ruleA") ); KieContainer kieContainer = ks.newKieContainer(releaseId); KieModule kmodule = ((InternalKieContainer) kieContainer).getMainKieModule(); assertEquals( releaseId, kmodule.getReleaseId() ); }
Example 8
Source File: KieFileSystemScannerTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
@Test public void testSnapshot() throws Exception { Path tempDir = Files.createTempDirectory(FileSystems.getDefault().getPath("./target"), null); File file = null; try { KieServices ks = KieServices.Factory.get(); ReleaseId releaseId = ks.newReleaseId( "org.kie", "scanner-test", "1.0-SNAPSHOT" ); createKieJar( ks, releaseId, "R1" ); KieContainer kieContainer = ks.newKieContainer( releaseId ); KieSession ksession = kieContainer.newKieSession(); checkKSession( ksession, "R1" ); KieScanner scanner = ks.newKieScanner( kieContainer, tempDir.toString() ); scanner.scanNow(); ksession = kieContainer.newKieSession(); checkKSession( ksession, "R1" ); file = write( createKieJar( ks, releaseId, "R2" ), tempDir, releaseId ); ksession = kieContainer.newKieSession(); checkKSession( ksession, "R1" ); scanner.scanNow(); ksession = kieContainer.newKieSession(); checkKSession( ksession, "R2" ); } finally { if (file != null) { file.delete(); } Files.delete(tempDir); } }
Example 9
Source File: SecurityPolicyTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
@Test public void testUntrustedMVELEnabled() throws Exception { String drl = "package org.foo.bar\n" + "import "+MaliciousExitHelper.class.getName().replace('$', '.')+" \n" + "rule R1 dialect \"mvel\" enabled( MaliciousExitHelper.isEnabled() ) \n" + "when\n" + "then\n" + "end\n"; try { KieServices ks = KieServices.Factory.get(); KieFileSystem kfs = ks.newKieFileSystem().write(ResourceFactory.newByteArrayResource(drl.getBytes()) .setSourcePath("org/foo/bar/r1.drl")); ks.newKieBuilder(kfs).buildAll(); ReleaseId releaseId = ks.getRepository().getDefaultReleaseId(); KieContainer kc = ks.newKieContainer(releaseId); KieSession ksession = kc.newKieSession(); ksession.fireAllRules(); fail("The security policy for the rule should have prevented this from executing..."); } catch (PropertyAccessException e) { // weak way of testing but couldn't find a better way if( e.toString().contains( "The security policy should have prevented" ) ) { fail("The security policy for the rule should have prevented this from executing..."); } else { // test succeeded } } }
Example 10
Source File: StartEventTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
@Test public void testSignalStartDynamic() throws Exception { KieBase kbase = createKnowledgeBase("BPMN2-SignalStart.bpmn2"); ksession = createKnowledgeSession(kbase); // create KieContainer after session was created to make sure no runtime data // will be used during serialization (deep clone) KieServices ks = KieServices.Factory.get(); KieRepository kr = ks.getRepository(); KieContainer kContainer = ks.newKieContainer(kr.getDefaultReleaseId()); kContainer.getKieBase(); final List<String> list = new ArrayList<>(); ksession.addEventListener(new DefaultProcessEventListener() { public void beforeProcessStarted(ProcessStartedEvent event) { logger.info("{}", event.getProcessInstance().getId()); list.add(event.getProcessInstance().getId()); } }); ksession.signalEvent("MySignal", "NewValue"); assertThat(getNumberOfProcessInstances("Minimal")).isEqualTo(1); // now remove the process from kbase to make sure runtime based listeners are removed from signal manager kbase.removeProcess("Minimal"); assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> { ksession.signalEvent("MySignal", "NewValue"); }) .withMessageContaining("Unknown process ID: Minimal"); // must be still one as the process was removed assertThat(getNumberOfProcessInstances("Minimal")).isEqualTo(1); }
Example 11
Source File: RuleEngine.java From support-rulesengine with Apache License 2.0 | 5 votes |
private void initKie() { KieServices ks = KieServices.Factory.get(); kfs = ks.newKieFileSystem(); uploadDroolFiles(); KieBuilder kbuilder = ks.newKieBuilder(kfs); kbuilder.buildAll(); if (kbuilder.getResults().hasMessages(Level.ERROR)) { throw new IllegalArgumentException(kbuilder.getResults().toString()); } KieContainer kcontainer = ks.newKieContainer(kbuilder.getKieModule().getReleaseId()); KieBaseConfiguration kbConfig = KieServices.Factory.get().newKieBaseConfiguration(); kbConfig.setOption(ConstraintJittingThresholdOption.get(-1)); kbase = kcontainer.newKieBase(kbConfig); }
Example 12
Source File: KieContainerTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
@Test public void testMemoryFileSystemFolderUniqueness() { KieServices kieServices = KieServices.Factory.get(); String drl = "package org.drools.test\n" + "rule R1 when\n" + " $m : Object()\n" + "then\n" + "end\n"; Resource resource = kieServices.getResources().newReaderResource( new StringReader( drl), "UTF-8" ); resource.setTargetPath("org/drools/test/rules.drl"); String kmodule = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<kmodule xmlns=\"http://www.drools.org/xsd/kmodule\">\n" + " <kbase name=\"testKbase\" packages=\"org.drools.test\">\n" + " <ksession name=\"testKsession\"/>\n" + " </kbase>\n" + "</kmodule>"; // Create an in-memory jar for version 1.0.0 ReleaseId releaseId = kieServices.newReleaseId("org.kie", "test-delete", "1.0.0"); createAndDeployJar(kieServices, kmodule, releaseId, resource); KieContainer kieContainer = kieServices.newKieContainer(releaseId); KieModule kieModule = ((InternalKieContainer) kieContainer).getMainKieModule(); MemoryFileSystem memoryFileSystem = ((MemoryKieModule) kieModule).getMemoryFileSystem(); Folder rootFolder = memoryFileSystem.getFolder(""); Object[] members = rootFolder.getMembers().toArray(); assertEquals(2, members.length); Folder firstFolder = (Folder) members[0]; Folder secondFolder = (Folder) members[1]; assertEquals(firstFolder.getParent(), secondFolder.getParent()); }
Example 13
Source File: GetKieContainerCommand.java From kogito-runtimes with Apache License 2.0 | 5 votes |
public KieContainer execute(Context context) { // use the new API to retrieve the session by ID KieServices kieServices = KieServices.Factory.get(); KieContainer kieContainer = kieServices.newKieContainer(releaseId); ((RegistryContext) context).register(KieContainer.class, kieContainer); return kieContainer; }
Example 14
Source File: I18nTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
@Test public void testKieModuleJar() { String str = "package org.drools.compiler.i18ntest;\n" + "import org.drools.compiler.I18nPerson;\n" + "\n" + "global java.util.List list;\n" + "rule \"名称 is 山田花子\"\n" + " when\n" + " p : I18nPerson( 名称 == \"山田花子\" )\n" + " then\n" + " list.add( \"名称は山田花子です\" );\n" + "end\n"; KieServices ks = KieServices.Factory.get(); ReleaseId releaseId = ks.newReleaseId("org.kie", "118ntest", "1.0.0"); byte[] jar = createKJar(ks, releaseId, null, str); KieModule km = deployJar(ks, jar); KieContainer kc = ks.newKieContainer(km.getReleaseId()); KieSession ksession = kc.newKieSession(); List list = new ArrayList(); ksession.setGlobal( "list", list ); I18nPerson i18nPerson = new I18nPerson(); i18nPerson.set名称("山田花子"); ksession.insert(i18nPerson); ksession.fireAllRules(); assertTrue(list.contains("名称は山田花子です")); ksession.dispose(); }
Example 15
Source File: WireListenerTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
@Test public void testWireListener() throws Exception { KieServices ks = KieServices.Factory.get(); ReleaseId releaseId = ks.newReleaseId("org.kie", "listener-test", "1.0"); build(ks, releaseId); KieContainer kieContainer = ks.newKieContainer(releaseId); KieSession ksession = kieContainer.newKieSession(); ksession.fireAllRules(); assertEquals(1, insertEvents.size()); assertEquals(1, updateEvents.size()); assertEquals(1, retractEvents.size()); }
Example 16
Source File: KieContainerTest.java From kogito-runtimes with Apache License 2.0 | 4 votes |
@Test public void testClassLoaderGetResources() throws IOException, URISyntaxException { KieServices kieServices = KieServices.Factory.get(); String drl1 = "package org.drools.testdrl;\n" + "rule R1 when\n" + " $m : Object()\n" + "then\n" + "end\n"; Resource resource1 = kieServices.getResources().newReaderResource(new StringReader(drl1), "UTF-8"); resource1.setTargetPath("org/drools/testdrl/rules1.drl"); String drl2 = "package org.drools.testdrl;\n" + "rule R2 when\n" + " $m : Object()\n" + "then\n" + "end\n"; Resource resource2 = kieServices.getResources().newReaderResource(new StringReader(drl2), "UTF-8"); resource2.setTargetPath("org/drools/testdrl/rules2.drl"); String java3 = "package org.drools.testjava;\n" + "public class Message {}"; Resource resource3 = kieServices.getResources().newReaderResource(new StringReader(java3), "UTF-8"); resource3.setTargetPath("org/drools/testjava/Message.java"); resource3.setResourceType(ResourceType.JAVA); String kmodule = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<kmodule xmlns=\"http://www.drools.org/xsd/kmodule\">\n" + " <kbase name=\"testKbase\" packages=\"org.drools.testdrl\">\n" + " <ksession name=\"testKsession\"/>\n" + " </kbase>\n" + "</kmodule>"; // Create an in-memory jar for version 1.0.0 ReleaseId releaseId = kieServices.newReleaseId("org.kie", "test-delete", "1.0.0"); createAndDeployJar(kieServices, kmodule, releaseId, resource1, resource2, resource3); KieContainer kieContainer = kieServices.newKieContainer(releaseId); ClassLoader classLoader = kieContainer.getClassLoader(); assertEnumerationSize(1, classLoader.getResources("org/drools/testjava")); // no trailing "/" assertEnumerationSize(1, classLoader.getResources("org/drools/testdrl/")); // trailing "/" to test both variants // make sure the package resource correctly lists all its child resources (files in this case) URL url = classLoader.getResources("org/drools/testdrl").nextElement(); List<String> lines = read(url.openStream()); assertThat(lines).contains("rules1.drl", "rules1.drl.properties", "rules2.drl", "rules2.drl.properties"); assertUrlEnumerationContainsMatch("^mfs\\:/$", classLoader.getResources("")); }
Example 17
Source File: KieBuilderTest.java From kogito-runtimes with Apache License 2.0 | 4 votes |
@Test public void testJavaSourceFileAndDrlDeployWithClassFilter() { final String allowedJava = "package org.drools.compiler;\n" + "public class JavaSourceMessage { }\n"; final String filteredJava = "package org.drools.compiler;\n" + "public class ClassCausingClassNotFoundException { non.existing.Type foo() { return null; } }\n"; final String drl = "package org.drools.compiler;\n" + "import org.drools.compiler.JavaSourceMessage;" + "rule R1 when\n" + " $m : JavaSourceMessage()\n" + "then\n" + "end\n"; final String kmodule = "<kmodule xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \n" + " xmlns=\"http://www.drools.org/xsd/kmodule\">\n" + " <kbase name=\"kbase1\">\n" + " <ksession name=\"ksession1\" default=\"true\"/>\n" + " </kbase>\n" + "</kmodule>"; final KieServices ks = KieServices.Factory.get(); final ReleaseId releaseId1 = ks.newReleaseId( "org.kie", "test-kie-builder", "1.0.0" ); final Resource allowedJavaResource = ResourceFactory.newByteArrayResource( allowedJava.getBytes() ).setResourceType( ResourceType.JAVA ) .setSourcePath( "org/drools/compiler/JavaSourceMessage.java" ); final Resource filteredJavaResource = ResourceFactory.newByteArrayResource( filteredJava.getBytes() ).setResourceType( ResourceType.JAVA ) .setSourcePath( "org/drools/compiler/ClassCausingClassNotFoundException.java" ); final Resource drlResource = ResourceFactory.newByteArrayResource( drl.getBytes() ).setResourceType( ResourceType.DRL ) .setSourcePath( "kbase1/drl1.drl" ); final Predicate<String> filter = fileName -> !fileName.endsWith( "org/drools/compiler/ClassCausingClassNotFoundException.java" ); KieModule km = null; try { km = createAndDeployJar( ks, kmodule, filter, releaseId1, allowedJavaResource, filteredJavaResource, drlResource); } catch ( final IllegalStateException ise ) { if ( ise.getMessage().contains( "org/drools/compiler/ClassCausingClassNotFoundException.java" ) ) { fail( "Build failed because source file was not filtered out." ); } else { throw ise; } } final KieContainer kieContainer = ks.newKieContainer(km.getReleaseId()); try { final Class<?> messageClass = kieContainer.getClassLoader().loadClass("org.drools.compiler.JavaSourceMessage"); assertNotNull(messageClass); } catch (final ClassNotFoundException e) { throw new IllegalStateException("Loading the java class failed.", e); } }
Example 18
Source File: KieContainerTest.java From kogito-runtimes with Apache License 2.0 | 4 votes |
@Test public void testIncrementalCompilationSynchronization() throws Exception { final KieServices kieServices = KieServices.Factory.get(); ReleaseId releaseId = kieServices.newReleaseId("org.kie.test", "sync-scanner-test", "1.0.0"); createAndDeployJar( kieServices, releaseId, createDRL("rule0") ); final KieContainer kieContainer = kieServices.newKieContainer(releaseId); KieSession kieSession = kieContainer.newKieSession(); List<String> list = new ArrayList<String>(); kieSession.setGlobal("list", list); kieSession.fireAllRules(); kieSession.dispose(); assertEquals(1, list.size()); Thread t = new Thread(() -> { for (int i = 1; i < 10; i++) { ReleaseId releaseId1 = kieServices.newReleaseId("org.kie.test", "sync-scanner-test", "1.0." + i); createAndDeployJar(kieServices, releaseId1, createDRL("rule" + i) ); kieContainer.updateToVersion(releaseId1); } }); t.setDaemon(true); t.start(); assertTimeoutPreemptively(Duration.ofSeconds(10), () -> { while (true) { KieSession kieSession2 = kieContainer.newKieSession(); ArrayList<String> list2 = new ArrayList<>(); kieSession2.setGlobal("list", list2); kieSession2.fireAllRules(); kieSession2.dispose(); // There can be multiple items in the list if an updateToVersion is triggered during a fireAllRules // (updateToVersion can be called multiple times during fireAllRules, especially on slower machines) // in that case it may fire with the old rule and multiple new ones assertThat(list2).isNotEmpty(); if (list2.get(0).equals("rule9")) { break; } } }); }
Example 19
Source File: KieAPITestUtils.java From hacep with Apache License 2.0 | 3 votes |
private static KieContainer setupKieContainer(ReleaseId releaseId, Resource pomResource, Resource kmodule, String... resources) { KieServices kieServices = KieServices.Factory.get(); buildRelease(releaseId, pomResource, kmodule, resources); KieContainer kieContainer = kieServices.newKieContainer(releaseId); kieContainers.add(kieContainer); return kieContainer; }
Example 20
Source File: DroolsUtil.java From qzr with Apache License 2.0 | 2 votes |
/** * Creates a new {@link KieContainer} using a collection of resources. It * does this by creating a new virtual file system and adding the resources * to it. * * @param resources * An array of {@link DroolsResource} indicating where the * various resources should be loaded from. These could be * classpath, file or URL resources. * @return A new {@link KieContainer}. * @throws KieBuildException */ public static KieContainer createKieContainer(DroolsResource[] resources) throws KieBuildException { KieServices kieServices = createAndBuildKieServices(resources); KieContainer kieContainer = kieServices.newKieContainer(kieServices.getRepository().getDefaultReleaseId()); return kieContainer; }