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