use of org.kie.api.io.Resource in project drools by kiegroup.
the class DMNUpdateTest method testReplace.
@Test
public void testReplace() {
final KieServices ks = KieServices.Factory.get();
final KieContainer kieContainer = KieHelper.getKieContainer(ks.newReleaseId("org.kie", "dmn-test", "1.0.0"), ks.getResources().newClassPathResource("0001-input-data-string.dmn", this.getClass()));
DMNRuntime runtime = kieContainer.newKieSession().getKieRuntime(DMNRuntime.class);
Assert.assertNotNull(runtime);
assertThat(runtime.getModels(), hasSize(1));
DMNContext dmnContext = runtime.newContext();
dmnContext.set("Full Name", "John Doe");
DMNResult evaluateAll = runtime.evaluateAll(runtime.getModels().get(0), dmnContext);
assertThat(evaluateAll.getDecisionResultByName("Greeting Message").getResult(), is("Hello John Doe"));
ReleaseId v101 = ks.newReleaseId("org.kie", "dmn-test", "1.0.1");
Resource newClassPathResource = ks.getResources().newClassPathResource("0001-input-data-string-itIT.dmn", this.getClass());
newClassPathResource.setTargetPath("0001-input-data-string.dmn");
KieHelper.createAndDeployJar(ks, v101, newClassPathResource);
Results updateResults = kieContainer.updateToVersion(v101);
assertThat(updateResults.hasMessages(Level.ERROR), is(false));
runtime = kieContainer.newKieSession().getKieRuntime(DMNRuntime.class);
Assert.assertNotNull(runtime);
assertThat(runtime.getModels(), hasSize(1));
DMNContext dmnContext2 = runtime.newContext();
dmnContext2.set("Full Name", "John Doe");
DMNResult evaluateAll2 = runtime.evaluateAll(runtime.getModels().get(0), dmnContext2);
assertThat(evaluateAll2.getDecisionResultByName("Greeting Message").getResult(), is("Salve John Doe"));
}
use of org.kie.api.io.Resource in project drools by kiegroup.
the class DMNRuntimeUtil method createRuntimeWithAdditionalResources.
public static DMNRuntime createRuntimeWithAdditionalResources(final String resourceName, final Class testClass, final String... additionalResources) {
final KieServices ks = KieServices.Factory.get();
Resource mainResource = ks.getResources().newClassPathResource(resourceName, testClass);
List<Resource> totalResources = new ArrayList<>();
totalResources.add(mainResource);
for (String add : additionalResources) {
totalResources.add(ks.getResources().newClassPathResource(add, testClass));
}
final KieContainer kieContainer = KieHelper.getKieContainer(ks.newReleaseId("org.kie", "dmn-test-" + UUID.randomUUID(), "1.0"), totalResources.toArray(new Resource[] {}));
final DMNRuntime runtime = typeSafeGetKieRuntime(kieContainer);
Assert.assertNotNull(runtime);
return runtime;
}
use of org.kie.api.io.Resource in project drools by kiegroup.
the class DroolsAbstractPMMLTest method getModelSession.
protected KieSession getModelSession(String[] pmmlSources, boolean verbose) {
KieServices ks = KieServices.Factory.get();
KieRepository kr = ks.getRepository();
KieFileSystem kfs = ks.newKieFileSystem();
for (int j = 0; j < pmmlSources.length; j++) {
Resource res = ResourceFactory.newClassPathResource(pmmlSources[j]).setResourceType(ResourceType.PMML);
kfs.write(res);
}
KieModuleModel model = ks.newKieModuleModel();
model.setConfigurationProperty("drools.propertySpecific", "ALLOWED");
KieBaseModel kbModel = model.newKieBaseModel(DEFAULT_KIEBASE).addPackage(BASE_PACK).setEventProcessingMode(EventProcessingOption.CLOUD);
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());
// KieBase kieBase = kContainer.getKieBase("SampleMine_SampleMineSegmentation_SEGMENT_1");
KieBase kieBase = kContainer.getKieBase();
setKbase(kieBase);
return kieBase.newKieSession();
}
use of org.kie.api.io.Resource in project drools by kiegroup.
the class DecisionTreeTest method testMissingTreeWeighted2.
@Test
public void testMissingTreeWeighted2() throws Exception {
Resource res = ResourceFactory.newClassPathResource(source2);
KieBase kbase = new KieHelper().addResource(res, ResourceType.PMML).build();
RuleUnitExecutor executor = RuleUnitExecutor.create().bind(kbase);
KieRuntimeLogger console = ((InternalRuleUnitExecutor) executor).addConsoleLogger();
PMMLRequestData requestData = new PMMLRequestData("123", "Missing");
requestData.addRequestParam(new ParameterInfo<>("123", "fld1", Double.class, -1.0));
requestData.addRequestParam(new ParameterInfo<>("123", "fld2", Double.class, -1.0));
requestData.addRequestParam(new ParameterInfo<>("123", "fld3", String.class, "miss"));
PMML4Result resultHolder = new PMML4Result();
DataSource<PMMLRequestData> data = executor.newDataSource("request");
DataSource<PMML4Result> results = executor.newDataSource("results");
DataSource<PMML4Data> pmmlData = executor.newDataSource("pmmlData");
List<String> possiblePackages = calculatePossiblePackageNames("Missing");
Class<? extends RuleUnit> unitClass = getStartingRuleUnit("RuleUnitIndicator", (InternalKnowledgeBase) kbase, possiblePackages);
assertNotNull(unitClass);
int x = executor.run(unitClass);
data.insert(requestData);
results.insert(resultHolder);
executor.run(unitClass);
console.close();
System.out.println(resultHolder);
Collection<?> objects = ((InternalRuleUnitExecutor) executor).getSessionObjects();
objects.forEach(o -> {
System.out.println(o);
});
pmmlData.forEach(pd -> {
System.out.println(pd);
});
AbstractTreeToken token = (AbstractTreeToken) resultHolder.getResultValue("MissingTreeToken", null);
assertNotNull(token);
assertEquals(0.6, token.getConfidence(), 1e-6);
assertEquals("null", token.getCurrent());
assertEquals(100.0, token.getTotalCount(), 0.0);
Object fld9 = resultHolder.getResultValue("Fld9", null);
assertNotNull(fld9);
String value = (String) resultHolder.getResultValue("Fld9", "value");
assertNotNull(value);
assertEquals("tgtX", value);
}
use of org.kie.api.io.Resource in project drools by kiegroup.
the class KieHelper method deployJarIntoRepository.
private static KieModule deployJarIntoRepository(KieServices ks, byte[] jar) {
Resource jarRes = ks.getResources().newByteArrayResource(jar);
KieModule km = ks.getRepository().addKieModule(jarRes);
return km;
}
Aggregations