Search in sources :

Example 26 with KieContainer

use of org.kie.api.runtime.KieContainer in project drools by kiegroup.

the class MavenDeployTest method testKScannerWithDeployUsingDistributionManagement.

@Test
public void testKScannerWithDeployUsingDistributionManagement() throws IOException {
    KieServices ks = KieServices.Factory.get();
    ReleaseId releaseId = ks.newReleaseId("org.kie", "scanner-test-deploy-dist", "1.0-SNAPSHOT");
    Path m2Folder = Files.createTempDirectory("temp-m2-dist");
    Path settingsXmlPath = generateSettingsXml(m2Folder);
    String oldSettingsXmlPath = System.getProperty(CUSTOM_SETTINGS_PROPERTY);
    try {
        System.setProperty(CUSTOM_SETTINGS_PROPERTY, settingsXmlPath.toString());
        MavenSettings.reinitSettings();
        InternalKieModule kJar1 = createKieJar(ks, releaseId, "rule1", "rule2");
        KieContainer kieContainer = ks.newKieContainer(releaseId);
        KieMavenRepository repository = getKieMavenRepository();
        repository.deployArtifact(releaseId, kJar1, createKPomWithDistributionManagement(m2Folder, releaseId).toFile());
        // create a ksesion and check it works as expected
        KieSession ksession = kieContainer.newKieSession("KSession1");
        checkKSession(ksession, "rule1", "rule2");
        // create a new kjar
        InternalKieModule kJar2 = createKieJar(ks, releaseId, "rule2", "rule3");
        // deploy it on maven
        repository.deployArtifact(releaseId, kJar2, createKPomWithDistributionManagement(m2Folder, releaseId).toFile());
        // since I am not calling start() on the scanner it means it won't have automatic scheduled scanning
        KieScanner scanner = ks.newKieScanner(kieContainer);
        // scan the maven repo to get the new kjar version and deploy it on the kcontainer
        scanner.scanNow();
        // create a ksesion and check it works as expected
        KieSession ksession2 = kieContainer.newKieSession("KSession1");
        checkKSession(ksession2, "rule2", "rule3");
        ks.getRepository().removeKieModule(releaseId);
    } finally {
        if (oldSettingsXmlPath == null) {
            System.clearProperty(CUSTOM_SETTINGS_PROPERTY);
        } else {
            System.setProperty(CUSTOM_SETTINGS_PROPERTY, oldSettingsXmlPath);
        }
        MavenSettings.reinitSettings();
    }
}
Also used : Path(java.nio.file.Path) KieMavenRepository.getKieMavenRepository(org.kie.scanner.KieMavenRepository.getKieMavenRepository) KieMavenRepository(org.kie.scanner.KieMavenRepository) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) KieScanner(org.kie.api.builder.KieScanner) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test) AbstractKieCiTest(org.kie.scanner.AbstractKieCiTest)

Example 27 with KieContainer

use of org.kie.api.runtime.KieContainer in project drools by kiegroup.

the class MavenDeployTest method testDeploy.

@Test
public void testDeploy() throws IOException {
    KieServices ks = KieServices.Factory.get();
    ReleaseId releaseId = ks.newReleaseId("org.kie", "scanner-test-deploy", "1.0-SNAPSHOT");
    Path m2Folder = Files.createTempDirectory("temp-m2");
    Path settingsXmlPath = generateSettingsXml(m2Folder);
    String oldSettingsXmlPath = System.getProperty(CUSTOM_SETTINGS_PROPERTY);
    try {
        System.setProperty(CUSTOM_SETTINGS_PROPERTY, settingsXmlPath.toString());
        MavenSettings.reinitSettings();
        InternalKieModule kJar1 = createKieJar(ks, releaseId, "rule1", "rule2");
        KieContainer kieContainer = ks.newKieContainer(releaseId);
        KieMavenRepository repository = getKieMavenRepository();
        RemoteRepository remote = createRemoteRepository(m2Folder);
        repository.deployArtifact(remote, releaseId, kJar1, createKPom(m2Folder, releaseId).toFile());
        // create a ksesion and check it works as expected
        KieSession ksession = kieContainer.newKieSession("KSession1");
        checkKSession(ksession, "rule1", "rule2");
        // create a new kjar
        InternalKieModule kJar2 = createKieJar(ks, releaseId, "rule2", "rule3");
        // deploy it on maven
        repository.deployArtifact(remote, releaseId, kJar2, createKPom(m2Folder, releaseId).toFile());
        // since I am not calling start() on the scanner it means it won't have automatic scheduled scanning
        KieScanner scanner = ks.newKieScanner(kieContainer);
        // scan the maven repo to get the new kjar version and deploy it on the kcontainer
        scanner.scanNow();
        // create a ksesion and check it works as expected
        KieSession ksession2 = kieContainer.newKieSession("KSession1");
        checkKSession(ksession2, "rule2", "rule3");
        ks.getRepository().removeKieModule(releaseId);
    } finally {
        if (oldSettingsXmlPath == null) {
            System.clearProperty(CUSTOM_SETTINGS_PROPERTY);
        } else {
            System.setProperty(CUSTOM_SETTINGS_PROPERTY, oldSettingsXmlPath);
        }
        MavenSettings.reinitSettings();
    }
}
Also used : Path(java.nio.file.Path) KieMavenRepository.getKieMavenRepository(org.kie.scanner.KieMavenRepository.getKieMavenRepository) KieMavenRepository(org.kie.scanner.KieMavenRepository) KieServices(org.kie.api.KieServices) RemoteRepository(org.eclipse.aether.repository.RemoteRepository) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) KieScanner(org.kie.api.builder.KieScanner) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test) AbstractKieCiTest(org.kie.scanner.AbstractKieCiTest)

Example 28 with KieContainer

use of org.kie.api.runtime.KieContainer in project drools by kiegroup.

the class KieScannerMBeanTest method testKScannerMBean.

@Test
public void testKScannerMBean() throws Exception {
    KieServices ks = KieServices.Factory.get();
    ReleaseId releaseId = ks.newReleaseId("org.kie", "scanner-mbean-test", "1.0-SNAPSHOT");
    InternalKieModule kJar1 = createKieJar(ks, releaseId, "rule1", "rule2");
    KieContainer kieContainer = ks.newKieContainer(releaseId);
    KieMavenRepository repository = getKieMavenRepository();
    repository.installArtifact(releaseId, kJar1, createKPom(fileManager, releaseId));
    // create a ksesion and check it works as expected
    KieSession ksession = kieContainer.newKieSession("KSession1");
    checkKSession(ksession, "rule1", "rule2");
    KieRepositoryScannerImpl scanner = (KieRepositoryScannerImpl) ks.newKieScanner(kieContainer);
    KieScannerMBeanImpl mBean = (KieScannerMBeanImpl) scanner.getMBean();
    ObjectName mbeanName = mBean.getMBeanName();
    // we want to check that the mbean is register in the server and exposing the correct attribute values
    // so we fetch the attributes from the server
    Assert.assertEquals(releaseId.toExternalForm(), MBeanUtils.getAttribute(mbeanName, "ScannerReleaseId"));
    Assert.assertEquals(releaseId.toExternalForm(), MBeanUtils.getAttribute(mbeanName, "CurrentReleaseId"));
    Assert.assertEquals(InternalKieScanner.Status.STOPPED.toString(), MBeanUtils.getAttribute(mbeanName, "Status"));
    MBeanUtils.invoke(mbeanName, "start", new Object[] { Long.valueOf(10000) }, new String[] { "long" });
    Assert.assertEquals(InternalKieScanner.Status.RUNNING.toString(), MBeanUtils.getAttribute(mbeanName, "Status"));
    MBeanUtils.invoke(mbeanName, "stop", new Object[] {}, new String[] {});
    Assert.assertEquals(InternalKieScanner.Status.STOPPED.toString(), MBeanUtils.getAttribute(mbeanName, "Status"));
    // create a new kjar
    InternalKieModule kJar2 = createKieJar(ks, releaseId, "rule2", "rule3");
    // deploy it on maven
    repository.installArtifact(releaseId, kJar2, createKPom(fileManager, releaseId));
    MBeanUtils.invoke(mbeanName, "scanNow", new Object[] {}, new String[] {});
    // create a ksesion and check it works as expected
    KieSession ksession2 = kieContainer.newKieSession("KSession1");
    checkKSession(ksession2, "rule2", "rule3");
    MBeanUtils.invoke(mbeanName, "shutdown", new Object[] {}, new String[] {});
    Assert.assertEquals(InternalKieScanner.Status.SHUTDOWN.toString(), MBeanUtils.getAttribute(mbeanName, "Status"));
    ks.getRepository().removeKieModule(releaseId);
}
Also used : KieRepositoryScannerImpl(org.kie.scanner.KieRepositoryScannerImpl) KieMavenRepository.getKieMavenRepository(org.kie.scanner.KieMavenRepository.getKieMavenRepository) KieMavenRepository(org.kie.scanner.KieMavenRepository) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieContainer(org.kie.api.runtime.KieContainer) ObjectName(javax.management.ObjectName) Test(org.junit.Test) AbstractKieCiTest(org.kie.scanner.AbstractKieCiTest)

Example 29 with KieContainer

use of org.kie.api.runtime.KieContainer in project drools by kiegroup.

the class XSDResourceTest method testXSDResourceNotBreakingCompilation.

@Test
public void testXSDResourceNotBreakingCompilation() {
    final KieContainer kcontainer = KieServices.Factory.get().getKieClasspathContainer();
    final KieBase kieBase = kcontainer.getKieBase("xsdKieBase");
    Assertions.assertThat(kieBase).as("Created KieBase with XSD should not be null").isNotNull();
}
Also used : KieBase(org.kie.api.KieBase) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 30 with KieContainer

use of org.kie.api.runtime.KieContainer in project drools by kiegroup.

the class IncrementalCompilationTest method testEventDeclarationInSeparatedDRL.

@Test
public void testEventDeclarationInSeparatedDRL() throws Exception {
    // DROOLS-1241
    String drl1 = "import " + SimpleEvent.class.getCanonicalName() + ";\n" + "declare SimpleEvent\n" + "    @role( event )\n" + "    @timestamp( timestamp )\n" + "    @expires( 2d )\n" + "end\n";
    String drl2 = "import " + SimpleEvent.class.getCanonicalName() + ";\n" + "global java.util.List list;\n" + "rule R1 when\n" + "    $s:SimpleEvent(code==\"MY_CODE\") over window:time( 1s )\n" + "then\n" + "    list.add(\"MY_CODE\");\n" + "end\n";
    String drl3 = "import " + SimpleEvent.class.getCanonicalName() + ";\n" + "global java.util.List list;\n" + "rule R2 when\n" + "    $s:SimpleEvent(code==\"YOUR_CODE\") over window:time( 1s )\n" + "then\n" + "    list.add(\"YOUR_CODE\");\n" + "end\n";
    KieServices ks = KieServices.Factory.get();
    KieModuleModel kproj = ks.newKieModuleModel();
    KieBaseModel kieBaseModel1 = kproj.newKieBaseModel("KBase1").setDefault(true).setEventProcessingMode(EventProcessingOption.STREAM);
    KieSessionModel ksession1 = kieBaseModel1.newKieSessionModel("KSession1").setDefault(true).setType(KieSessionModel.KieSessionType.STATEFUL).setClockType(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-cep-upgrade", "1.1.1");
    KieModule km = deployJar(ks, createKJar(ks, kproj, releaseId1, null, drl1, drl2));
    KieContainer kc = ks.newKieContainer(km.getReleaseId());
    KieSession ksession = kc.newKieSession();
    List<String> list = new ArrayList<String>();
    ksession.setGlobal("list", list);
    ksession.insert(new SimpleEvent("1", "MY_CODE", 0));
    ksession.insert(new SimpleEvent("2", "YOUR_CODE", 0));
    ksession.fireAllRules();
    assertEquals(1, list.size());
    assertEquals("MY_CODE", list.get(0));
    list.clear();
    ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-cep-upgrade", "1.1.2");
    // the null drl placeholder is used to have the same drl with a different file name
    // this causes the removal and readdition of both rules
    km = deployJar(ks, createKJar(ks, kproj, releaseId2, null, drl1, drl2, drl3));
    Results results = kc.updateToVersion(releaseId2);
    assertEquals(0, results.getMessages().size());
    ksession.fireAllRules();
    assertEquals(1, list.size());
    assertEquals("YOUR_CODE", list.get(0));
}
Also used : ArrayList(java.util.ArrayList) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) KieSessionModel(org.kie.api.builder.model.KieSessionModel) KieBaseModel(org.kie.api.builder.model.KieBaseModel) IncrementalResults(org.kie.internal.builder.IncrementalResults) Results(org.kie.api.builder.Results) KieModuleModel(org.kie.api.builder.model.KieModuleModel) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieModule(org.kie.api.builder.KieModule) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Aggregations

KieContainer (org.kie.api.runtime.KieContainer)357 KieServices (org.kie.api.KieServices)271 Test (org.junit.Test)246 KieSession (org.kie.api.runtime.KieSession)191 ReleaseId (org.kie.api.builder.ReleaseId)185 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)108 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)90 KieFileSystem (org.kie.api.builder.KieFileSystem)81 KieModule (org.kie.api.builder.KieModule)75 KieBuilder (org.kie.api.builder.KieBuilder)56 KieModuleModel (org.kie.api.builder.model.KieModuleModel)54 KieBase (org.kie.api.KieBase)49 ArrayList (java.util.ArrayList)44 Results (org.kie.api.builder.Results)40 Resource (org.kie.api.io.Resource)29 KieMavenRepository.getKieMavenRepository (org.kie.scanner.KieMavenRepository.getKieMavenRepository)29 DMNRuntime (org.kie.dmn.api.core.DMNRuntime)27 Message (org.drools.compiler.Message)26 KieBaseModel (org.kie.api.builder.model.KieBaseModel)26 InternalKieContainer (org.drools.core.impl.InternalKieContainer)25