use of org.kie.api.builder.KieRepository in project jbpm by kiegroup.
the class JbpmBpmn2TestCase method createKnowledgeBaseFromDisc.
protected KieBase createKnowledgeBaseFromDisc(String process) throws Exception {
KieServices ks = KieServices.Factory.get();
KieRepository kr = ks.getRepository();
KieFileSystem kfs = ks.newKieFileSystem();
Resource res = ResourceFactory.newClassPathResource(process);
kfs.write(res);
KieBuilder kb = ks.newKieBuilder(kfs);
// kieModule is automatically deployed to KieRepository
kb.buildAll();
if (kb.getResults().hasMessages(Level.ERROR)) {
throw new RuntimeException("Build Errors:\n" + kb.getResults().toString());
}
KieContainer kContainer = ks.newKieContainer(kr.getDefaultReleaseId());
KieBase kbase = kContainer.getKieBase();
File packageFile = null;
for (KiePackage pkg : kbase.getKiePackages()) {
packageFile = new File(System.getProperty("java.io.tmpdir") + File.separator + pkg.getName() + ".pkg");
packageFile.deleteOnExit();
FileOutputStream out = new FileOutputStream(packageFile);
try {
DroolsStreamUtils.streamOut(out, pkg);
} finally {
out.close();
}
// store first package only
break;
}
kfs.delete(res.getSourcePath());
kfs.write(ResourceFactory.newFileResource(packageFile));
kb = ks.newKieBuilder(kfs);
// kieModule is automatically deployed to KieRepository
kb.buildAll();
if (kb.getResults().hasMessages(Level.ERROR)) {
throw new RuntimeException("Build Errors:\n" + kb.getResults().toString());
}
kContainer = ks.newKieContainer(kr.getDefaultReleaseId());
kbase = kContainer.getKieBase();
return kbase;
}
use of org.kie.api.builder.KieRepository in project raml-module-builder by folio-org.
the class Rules method buildSession.
/**
* Load all .drl files
*
* if a rule does not compile an exception will be thrown indicating the problem
*
* @param path
* @return - session containing all loaded rules
* @throws Exception
*/
public KieSession buildSession() throws Exception {
kieServices = KieServices.Factory.get();
KieRepository kr = kieServices.getRepository();
kfs = kieServices.newKieFileSystem();
if (rules != null) {
loadFromList();
} else {
loadFromFiles();
}
kieBuilder = kieServices.newKieBuilder(kfs).buildAll();
Results results = kieBuilder.getResults();
if (results.hasMessages(Message.Level.ERROR)) {
System.out.println(results.getMessages());
throw new IllegalStateException(results.getMessages() + "");
}
kieContainer = kieServices.newKieContainer(kr.getDefaultReleaseId());
ksession = kieContainer.newKieSession();
ksession.addEventListener(new RuleTracker());
return ksession;
}
use of org.kie.api.builder.KieRepository in project drools by kiegroup.
the class MBeansMonitoringTest method testLoadKjarFromClasspath.
/**
* 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);
}
}
use of org.kie.api.builder.KieRepository in project drools by kiegroup.
the class KieRepositoryTest method testKieModuleAddition.
private void testKieModuleAddition(final ReleaseId releaseId) {
final KieRepository kieRepository = kieServices.getRepository();
this.createKieModule(releaseId);
Assertions.assertThat(kieRepository.getKieModule(releaseId)).as("KieModule should be in KieRepository").isNotNull();
}
use of org.kie.api.builder.KieRepository in project drools by kiegroup.
the class KieBuilderTest method createAndTestKieContainer.
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());
}
Aggregations