Search in sources :

Example 36 with KieBaseModel

use of org.kie.api.builder.model.KieBaseModel in project drools by kiegroup.

the class KieModuleMavenTest method testKieModuleFromMavenWithDependenciesProperties.

@Test
public void testKieModuleFromMavenWithDependenciesProperties() throws Exception {
    final KieServices ks = new KieServicesImpl() {

        @Override
        public KieRepository getRepository() {
            // override repository to not store the artifact on deploy to trigger load from maven repo
            return new KieRepositoryImpl();
        }
    };
    ReleaseId dependency = ks.newReleaseId("org.drools", "drools-core", "${version.org.drools}");
    ReleaseId releaseId = ks.newReleaseId("org.kie.test", "maven-test", "1.0-SNAPSHOT");
    InternalKieModule kJar1 = createKieJarWithProperties(ks, releaseId, true, "5.5.0.Final", new ReleaseId[] { dependency }, "rule1", "rule2");
    String pomText = generatePomXmlWithProperties(releaseId, "5.5.0.Final", dependency);
    File pomFile = new File(System.getProperty("java.io.tmpdir"), MavenRepository.toFileName(releaseId, null) + ".pom");
    try {
        FileOutputStream fos = new FileOutputStream(pomFile);
        fos.write(pomText.getBytes());
        fos.flush();
        fos.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    KieMavenRepository.getKieMavenRepository().installArtifact(releaseId, kJar1, pomFile);
    KieContainer kieContainer = ks.newKieContainer(releaseId);
    KieBaseModel kbaseModel = ((KieContainerImpl) kieContainer).getKieProject().getDefaultKieBaseModel();
    assertNotNull("Default kbase was not found", kbaseModel);
    String kbaseName = kbaseModel.getName();
    assertEquals("KBase1", kbaseName);
}
Also used : KieBaseModel(org.kie.api.builder.model.KieBaseModel) KieRepositoryImpl(org.drools.compiler.kie.builder.impl.KieRepositoryImpl) FileOutputStream(java.io.FileOutputStream) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) IOException(java.io.IOException) KieServicesImpl(org.drools.compiler.kie.builder.impl.KieServicesImpl) File(java.io.File) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 37 with KieBaseModel

use of org.kie.api.builder.model.KieBaseModel in project drools by kiegroup.

the class KieModuleMavenTest method testKieModuleFromMavenWithDependencies.

@Test
public void testKieModuleFromMavenWithDependencies() throws Exception {
    final KieServices ks = new KieServicesImpl() {

        @Override
        public KieRepository getRepository() {
            // override repository to not store the artifact on deploy to trigger load from maven repo
            return new KieRepositoryImpl();
        }
    };
    ReleaseId dependency = ks.newReleaseId("org.drools", "drools-core", "5.5.0.Final");
    ReleaseId releaseId = ks.newReleaseId("org.kie", "maven-test", "1.0-SNAPSHOT");
    InternalKieModule kJar1 = createKieJar(ks, releaseId, true, "rule1", "rule2");
    String pomText = getPom(releaseId, dependency);
    File pomFile = new File(System.getProperty("java.io.tmpdir"), MavenRepository.toFileName(releaseId, null) + ".pom");
    try {
        FileOutputStream fos = new FileOutputStream(pomFile);
        fos.write(pomText.getBytes());
        fos.flush();
        fos.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    KieMavenRepository.getKieMavenRepository().installArtifact(releaseId, kJar1, pomFile);
    KieContainer kieContainer = ks.newKieContainer(releaseId);
    KieBaseModel kbaseModel = ((KieContainerImpl) kieContainer).getKieProject().getDefaultKieBaseModel();
    assertNotNull("Default kbase was not found", kbaseModel);
    String kbaseName = kbaseModel.getName();
    assertEquals("KBase1", kbaseName);
}
Also used : KieBaseModel(org.kie.api.builder.model.KieBaseModel) KieRepositoryImpl(org.drools.compiler.kie.builder.impl.KieRepositoryImpl) FileOutputStream(java.io.FileOutputStream) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) IOException(java.io.IOException) KieServicesImpl(org.drools.compiler.kie.builder.impl.KieServicesImpl) File(java.io.File) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 38 with KieBaseModel

use of org.kie.api.builder.model.KieBaseModel in project drools-wb by kiegroup.

the class ScenarioRunnerServiceTest method initKieSession.

private void initKieSession(String... resourceNames) {
    final KieServices kieServices = KieServices.Factory.get();
    final KieModuleModel kieModuleModel = kieServices.newKieModuleModel();
    final KieBaseModel kieBaseModel = kieModuleModel.newKieBaseModel("defaultKieBase").setDefault(true).setEqualsBehavior(EqualityBehaviorOption.EQUALITY).setEventProcessingMode(EventProcessingOption.STREAM);
    kieBaseModel.newKieSessionModel("defaultKieSession").setDefault(true).setType(KieSessionModel.KieSessionType.STATEFUL).setClockType(ClockTypeOption.get("pseudo")).newWorkItemHandlerModel("Rest", "org.drools.workbench.screens.testscenario.backend.server.GithubContributorsWIH");
    final KieFileSystem kfs = kieServices.newKieFileSystem();
    kfs.writeKModuleXML(kieModuleModel.toXML());
    for (String resource : resourceNames) {
        kfs.write(ResourceFactory.newUrlResource(this.getClass().getResource(resource).toString()));
    }
    kieServices.newKieBuilder(kfs).buildAll();
    kieContainer = kieServices.newKieContainer(kieServices.getRepository().getDefaultReleaseId());
    kieSession = kieContainer.newKieSession();
    doReturn(kieSession).when(sessionService).newDefaultKieSessionWithPseudoClock(any(KieModule.class));
}
Also used : KieBaseModel(org.kie.api.builder.model.KieBaseModel) KieFileSystem(org.kie.api.builder.KieFileSystem) KieModuleModel(org.kie.api.builder.model.KieModuleModel) KieServices(org.kie.api.KieServices) KieModule(org.kie.workbench.common.services.shared.project.KieModule)

Example 39 with KieBaseModel

use of org.kie.api.builder.model.KieBaseModel in project drools by kiegroup.

the class KieHelloWorldTest method testHelloWorldWithKBaseInclude.

@Test
public void testHelloWorldWithKBaseInclude() throws Exception {
    String drl = "package org.drools.compiler.integrationtests\n" + "declare CancelFact\n" + " cancel : boolean = true\n" + "end\n" + "rule R1 when\n" + " $m : CancelFact( cancel == true )\n" + "then\n" + "end\n";
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem().write("src/main/resources/r1.drl", drl);
    KieModuleModel module = ks.newKieModuleModel();
    // define first kbase
    final String defaultBaseName = "defaultKBase";
    KieBaseModel defaultBase = module.newKieBaseModel(defaultBaseName);
    defaultBase.setDefault(true);
    defaultBase.addPackage("*");
    defaultBase.newKieSessionModel("defaultKSession").setDefault(true);
    // define second kbase including resources of the first one
    final String includingBaseName = "includingKBase";
    KieBaseModel includingBase = module.newKieBaseModel(includingBaseName);
    includingBase.setDefault(false);
    includingBase.addInclude(defaultBaseName);
    includingBase.newKieSessionModel("includingKSession").setDefault(false);
    kfs.writeKModuleXML(module.toXML());
    KieBuilder kb = ks.newKieBuilder(kfs).buildAll();
    assertEquals(0, kb.getResults().getMessages().size());
    KieSession ksession = ks.newKieContainer(ks.getRepository().getDefaultReleaseId()).newKieSession();
    FactType factType = ksession.getKieBase().getFactType("org.drools.compiler.integrationtests", "CancelFact");
    assertNotNull(factType);
    ksession.insert(factType.newInstance());
    int count = ksession.fireAllRules();
    assertEquals(1, count);
}
Also used : KieBaseModel(org.kie.api.builder.model.KieBaseModel) KieFileSystem(org.kie.api.builder.KieFileSystem) KieModuleModel(org.kie.api.builder.model.KieModuleModel) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) KieBuilder(org.kie.api.builder.KieBuilder) FactType(org.kie.api.definition.type.FactType) Test(org.junit.Test)

Example 40 with KieBaseModel

use of org.kie.api.builder.model.KieBaseModel in project drools by kiegroup.

the class MBeansMonitoringTest method testContainerMBeans.

@Test
public void testContainerMBeans() throws Exception {
    String drl = "package org.drools.compiler.test\n" + "import org.drools.compiler.StockTick\n" + "declare StockTick\n" + "    @role(event)\n" + "    @expires(10s)\n" + "end\n" + "rule X\n" + "when\n" + "    StockTick()\n" + "then\n" + "end";
    KieServices ks = KieServices.Factory.get();
    KieModuleModel kproj = ks.newKieModuleModel();
    KieBaseModel kieBaseModel1 = kproj.newKieBaseModel(KBASE1).setDefault(true).setEventProcessingMode(EventProcessingOption.STREAM);
    KieSessionModel ksessionModel1 = kieBaseModel1.newKieSessionModel(KSESSION1).setDefault(true).setType(KieSessionModel.KieSessionType.STATEFUL).setClockType(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    ReleaseId releaseId1 = ks.newReleaseId("org.kie.test", "mbeans", "1.0.0");
    createKJar(ks, kproj, releaseId1, null, drl);
    KieContainer kc = ks.newKieContainer(releaseId1);
    KieBase kiebase = kc.getKieBase(KBASE1);
    kc.newKieSession(KSESSION1);
    kiebase.newKieSession();
    String kc1ID = ((InternalKieContainer) kc).getContainerId();
    MBeanServer mbserver = ManagementFactory.getPlatformMBeanServer();
    LOG.debug("{}", mbserver.queryNames(new ObjectName("org.kie:kcontainerId=" + ObjectName.quote(kc1ID) + ",*"), null));
    ReleaseId verRelease = ks.newReleaseId("org.kie.test", "mbeans", "RELEASE");
    KieContainer kc2 = ks.newKieContainer("Matteo", verRelease);
    kc2.newKieSession(KSESSION1);
    KieContainerMonitorMXBean c1Monitor = JMX.newMXBeanProxy(mbserver, DroolsManagementAgent.createObjectNameBy(kc1ID), KieContainerMonitorMXBean.class);
    assertEquals(releaseId1.toExternalForm(), c1Monitor.getConfiguredReleaseIdStr());
    assertEquals(releaseId1.toExternalForm(), c1Monitor.getResolvedReleaseIdStr());
    assertTrue(c1Monitor.getConfiguredReleaseId().sameGAVof(releaseId1));
    assertTrue(c1Monitor.getResolvedReleaseId().sameGAVof(releaseId1));
    assertEquals(releaseId1.getVersion(), c1Monitor.getConfiguredReleaseId().getVersion());
    assertEquals(releaseId1.getVersion(), c1Monitor.getResolvedReleaseId().getVersion());
    KieContainerMonitorMXBean c2Monitor = JMX.newMXBeanProxy(mbserver, DroolsManagementAgent.createObjectNameBy("Matteo"), KieContainerMonitorMXBean.class);
    assertEquals(verRelease.toExternalForm(), c2Monitor.getConfiguredReleaseIdStr());
    assertEquals(releaseId1.toExternalForm(), c2Monitor.getResolvedReleaseIdStr());
    assertTrue(c2Monitor.getConfiguredReleaseId().sameGAVof(verRelease));
    assertTrue(c2Monitor.getResolvedReleaseId().sameGAVof(releaseId1));
    assertEquals(verRelease.getVersion(), c2Monitor.getConfiguredReleaseId().getVersion());
    assertEquals(releaseId1.getVersion(), c2Monitor.getResolvedReleaseId().getVersion());
    // MBean are supported only via KieContainer public API.
    assertEquals(3, mbserver.queryNames(new ObjectName("org.kie:kcontainerId=" + ObjectName.quote(kc1ID) + ",*"), null).size());
    kc.dispose();
    assertEquals(0, mbserver.queryNames(new ObjectName("org.kie:kcontainerId=" + ObjectName.quote(kc1ID) + ",*"), null).size());
    assertEquals(3, mbserver.queryNames(new ObjectName("org.kie:kcontainerId=" + ObjectName.quote("Matteo") + ",*"), null).size());
}
Also used : KieBaseModel(org.kie.api.builder.model.KieBaseModel) KieModuleModel(org.kie.api.builder.model.KieModuleModel) KieBase(org.kie.api.KieBase) KieContainerMonitorMXBean(org.kie.api.management.KieContainerMonitorMXBean) KieServices(org.kie.api.KieServices) InternalKieContainer(org.drools.core.impl.InternalKieContainer) ReleaseId(org.kie.api.builder.ReleaseId) KieSessionModel(org.kie.api.builder.model.KieSessionModel) InternalKieContainer(org.drools.core.impl.InternalKieContainer) KieContainer(org.kie.api.runtime.KieContainer) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName) Test(org.junit.Test)

Aggregations

KieBaseModel (org.kie.api.builder.model.KieBaseModel)74 KieModuleModel (org.kie.api.builder.model.KieModuleModel)55 KieServices (org.kie.api.KieServices)45 KieFileSystem (org.kie.api.builder.KieFileSystem)31 Test (org.junit.Test)26 KieSessionModel (org.kie.api.builder.model.KieSessionModel)24 KieBuilder (org.kie.api.builder.KieBuilder)22 KieContainer (org.kie.api.runtime.KieContainer)22 ReleaseId (org.kie.api.builder.ReleaseId)20 KieSession (org.kie.api.runtime.KieSession)13 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)10 ArrayList (java.util.ArrayList)7 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)7 KieBase (org.kie.api.KieBase)7 KieModule (org.kie.api.builder.KieModule)7 KieRepository (org.kie.api.builder.KieRepository)6 Before (org.junit.Before)5 File (java.io.File)4 FileOutputStream (java.io.FileOutputStream)4 IOException (java.io.IOException)4