use of org.kie.api.builder.KieRepository in project drools by kiegroup.
the class KieBuilderTest method testKieModuleDependencies.
@Test
public void testKieModuleDependencies() throws ClassNotFoundException, InterruptedException, IOException {
KieServices ks = KieServices.Factory.get();
String namespace1 = "org.kie.test1";
ReleaseId releaseId1 = KieServices.Factory.get().newReleaseId(namespace1, "memory", "1.0-SNAPSHOT");
KieModuleModel kProj1 = createKieProject(namespace1);
KieFileSystem kfs1 = KieServices.Factory.get().newKieFileSystem();
generateAll(kfs1, namespace1, releaseId1, kProj1);
KieBuilder kb1 = createKieBuilder(kfs1);
kb1.buildAll();
if (kb1.getResults().hasMessages(Level.ERROR)) {
fail("Unable to build KieJar\n" + kb1.getResults().toString());
}
KieRepository kr = ks.getRepository();
KieModule kModule1 = kr.getKieModule(releaseId1);
assertNotNull(kModule1);
String namespace2 = "org.kie.test2";
ReleaseId releaseId2 = KieServices.Factory.get().newReleaseId(namespace2, "memory", "1.0-SNAPSHOT");
KieModuleModel kProj2 = createKieProject(namespace2);
KieBaseModelImpl kieBase2 = (KieBaseModelImpl) kProj2.getKieBaseModels().get(namespace2);
kieBase2.addInclude(namespace1);
KieFileSystem kfs2 = KieServices.Factory.get().newKieFileSystem();
generateAll(kfs2, namespace2, releaseId2, kProj2);
KieBuilder kb2 = createKieBuilder(kfs2);
kb2.setDependencies(kModule1);
kb2.buildAll();
if (kb2.getResults().hasMessages(Level.ERROR)) {
fail("Unable to build KieJar\n" + kb2.getResults().toString());
}
KieModule kModule2 = kr.getKieModule(releaseId2);
assertNotNull(kModule2);
KieContainer kContainer = ks.newKieContainer(releaseId2);
KieBase kBase = kContainer.getKieBase(namespace2);
KieSession kSession = kBase.newKieSession();
List list = new ArrayList();
kSession.setGlobal("list", list);
kSession.fireAllRules();
assertEquals(2, list.size());
if ("org.kie.test1.Message".equals(list.get(0).getClass().getName())) {
assertEquals("org.kie.test2.Message", list.get(1).getClass().getName());
} else {
assertEquals("org.kie.test2.Message", list.get(0).getClass().getName());
assertEquals("org.kie.test1.Message", list.get(1).getClass().getName());
}
}
use of org.kie.api.builder.KieRepository in project drools by kiegroup.
the class DroolsAbstractPMMLTest method readKnowledgeBase.
private static KieBase readKnowledgeBase(List<InputStream> theory) {
KieServices ks = KieServices.Factory.get();
KieRepository kr = ks.getRepository();
KieFileSystem kfs = ks.newKieFileSystem();
for (int j = 0; j < theory.size(); j++) {
Resource res = ks.getResources().newInputStreamResource(theory.get(j));
kfs.write(RESOURCE_PATH + "source_" + j + ".drl", res);
}
KieModuleModel model = ks.newKieModuleModel();
KieBaseModel kbModel = model.newKieBaseModel(DEFAULT_KIEBASE).setDefault(true).addPackage(BASE_PACK).setEventProcessingMode(EventProcessingOption.STREAM);
kfs.writeKModuleXML(model.toXML());
KieBuilder kb = ks.newKieBuilder(kfs);
kb.buildAll();
if (kb.getResults().hasMessages(Message.Level.ERROR)) {
throw new RuntimeException("Build Errors:\n" + kb.getResults().toString());
}
KieContainer kContainer = ks.newKieContainer(kr.getDefaultReleaseId());
return kContainer.getKieBase();
}
use of org.kie.api.builder.KieRepository in project drools by kiegroup.
the class DroolsAbstractPMMLTest method createExecutor.
protected RuleUnitExecutor createExecutor(String sourceName) {
KieServices ks = KieServices.Factory.get();
KieRepository kr = ks.getRepository();
ReleaseId releaseId = new ReleaseIdImpl("org.kie:pmmlTest:1.0-SNAPSHOT");
((KieRepositoryImpl) kr).setDefaultGAV(releaseId);
Resource res = ResourceFactory.newClassPathResource(sourceName);
kbase = new KieHelper().addResource(res, ResourceType.PMML).build();
assertNotNull(kbase);
RuleUnitExecutor executor = RuleUnitExecutor.create().bind(kbase);
KieContainer kc = ((KnowledgeBaseImpl) ((InternalRuleUnitExecutor) executor).getKieSession().getKieBase()).getKieContainer();
InternalKieModule ikm = (InternalKieModule) kr.getKieModule(releaseId);
try (FileOutputStream fos = new FileOutputStream("/tmp/outputModule.jar")) {
fos.write(ikm.getBytes());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
kc.getKieBaseNames().forEach(n -> {
System.out.println(n);
});
data = executor.newDataSource("request");
resultData = executor.newDataSource("results");
pmmlData = executor.newDataSource("pmmlData");
return executor;
}
use of org.kie.api.builder.KieRepository in project drools by kiegroup.
the class DroolsAbstractPMMLTest method getModelSession.
protected KieSession getModelSession(File fileName) {
KieServices ks = KieServices.Factory.get();
KieRepository kr = ks.getRepository();
KieFileSystem kfs = ks.newKieFileSystem();
KieModuleModel model = ks.newKieModuleModel();
model.setConfigurationProperty("drools.propertySpecific", "ALLOWED");
KieBaseModel kbModel = model.newKieBaseModel(DEFAULT_KIEBASE).addPackage(BASE_PACK).setEventProcessingMode(EventProcessingOption.CLOUD);
KieBuilder kb = ks.newKieBuilder(fileName);
kb.buildAll();
if (kb.getResults().hasMessages(Message.Level.ERROR)) {
throw new RuntimeException("Build Errors:\n" + kb.getResults().toString());
}
KieContainer kContainer = ks.newKieContainer(kr.getDefaultReleaseId());
KieBase kieBase = kContainer.getKieBase();
setKbase(kieBase);
return kieBase.newKieSession();
}
use of org.kie.api.builder.KieRepository in project drools by kiegroup.
the class BuildFromKJarTest method test.
@Test
public void test() {
KieServices ks = KieServices.get();
ReleaseId releaseId = ks.newReleaseId("org.kie", "kjar-test", "1.0");
KieRepository repo = ks.getRepository();
repo.removeKieModule(releaseId);
InternalKieModule kmodule = createKieModule(ks, releaseId);
repo.addKieModule(kmodule);
executeSession(ks, releaseId);
}
Aggregations