use of org.kie.api.builder.Results in project drools by kiegroup.
the class ScoringStrategiesTest method executeAndFetchScore.
/* Internal functions */
private double executeAndFetchScore(String sheetName) throws Exception {
ScorecardCompiler scorecardCompiler = new ScorecardCompiler(INTERNAL_DECLARED_TYPES);
InputStream inputStream = PMMLDocumentTest.class.getResourceAsStream("/scoremodel_scoring_strategies.xls");
boolean compileResult = scorecardCompiler.compileFromExcel(inputStream, sheetName);
if (!compileResult) {
for (ScorecardError error : scorecardCompiler.getScorecardParseErrors()) {
System.err.println("Scorecard Compiler Error :" + error.getErrorLocation() + "->" + error.getErrorMessage());
}
return -999999;
}
String drl = scorecardCompiler.getDRL();
KieServices ks = KieServices.Factory.get();
KieFileSystem kfs = ks.newKieFileSystem();
kfs.write(ks.getResources().newByteArrayResource(drl.getBytes()).setSourcePath("scoremodel_scoring_strategies.drl").setResourceType(ResourceType.DRL));
KieBuilder kieBuilder = ks.newKieBuilder(kfs);
Results res = kieBuilder.buildAll().getResults();
if (res.hasMessages(Message.Level.ERROR)) {
System.out.println(res.getMessages());
}
assertEquals(0, res.getMessages(Message.Level.ERROR).size());
KieContainer kieContainer = ks.newKieContainer(kieBuilder.getKieModule().getReleaseId());
KieBase kbase = kieContainer.getKieBase();
StatelessKieSession session = kbase.newStatelessKieSession();
FactType scorecardType = kbase.getFactType("org.drools.scorecards.example", "SampleScore");
Object scorecard = scorecardType.newInstance();
scorecardType.set(scorecard, "age", 10);
session.execute(scorecard);
return (Double) scorecardType.get(scorecard, "scorecard__calculatedScore");
}
use of org.kie.api.builder.Results 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.builder.Results in project drools by kiegroup.
the class DMNExtensionRegisterTest method testUsingKModuleProperty_WrongClasses.
@Test
public void testUsingKModuleProperty_WrongClasses() {
final KieServices ks = KieServices.Factory.get();
final KieFileSystem kfs = ks.newKieFileSystem();
KieModuleModel kmm = ks.newKieModuleModel();
kmm.setConfigurationProperty("org.kie.dmn.profiles.FirstNameLastNameProfile", "foo");
kfs.writeKModuleXML(kmm.toXML());
kfs.write(ks.getResources().newClassPathResource("0001-input-data-string-with-extensions.dmn", this.getClass()));
KieBuilder kieBuilder = ks.newKieBuilder(kfs).buildAll();
Results results = kieBuilder.getResults();
LOG.info("buildAll() completed.");
results.getMessages(Level.WARNING).forEach(e -> LOG.warn("{}", e));
assertTrue(results.getMessages(Level.WARNING).size() > 0);
final KieContainer kieContainer = ks.newKieContainer(ks.getRepository().getDefaultReleaseId());
DMNRuntime runtime = kieContainer.newKieSession().getKieRuntime(DMNRuntime.class);
DMNModel dmnModel = runtime.getModel("https://github.com/kiegroup/kie-dmn", "0001-input-data-string");
assertThat(dmnModel, notNullValue());
assertThat(formatMessages(dmnModel.getMessages()), dmnModel.hasErrors(), is(false));
assertEquals(3, dmnModel.getDefinitions().getDrgElement().size());
InputData inputData1 = (InputData) dmnModel.getDefinitions().getDrgElement().get(1);
assertEquals("First Name", inputData1.getName());
DMNElement.ExtensionElements id1elements = inputData1.getExtensionElements();
assertTrue(id1elements != null);
assertEquals(0, id1elements.getAny().size());
InputData inputData2 = (InputData) dmnModel.getDefinitions().getDrgElement().get(2);
assertEquals("Last Name", inputData2.getName());
DMNElement.ExtensionElements id2elements = inputData2.getExtensionElements();
assertTrue(id2elements != null);
assertEquals(0, id2elements.getAny().size());
}
use of org.kie.api.builder.Results in project drools by kiegroup.
the class DMNExtensionRegisterTest method testUsingKModuleProperty.
@Test
public void testUsingKModuleProperty() {
final KieServices ks = KieServices.Factory.get();
final KieFileSystem kfs = ks.newKieFileSystem();
KieModuleModel kmm = ks.newKieModuleModel();
kmm.setConfigurationProperty("org.kie.dmn.profiles.FirstNameLastNameProfile", FirstNameLastNameProfile.class.getCanonicalName());
kfs.writeKModuleXML(kmm.toXML());
kfs.write(ks.getResources().newClassPathResource("0001-input-data-string-with-extensions.dmn", this.getClass()));
KieBuilder kieBuilder = ks.newKieBuilder(kfs).buildAll();
Results results = kieBuilder.getResults();
LOG.info("buildAll() completed.");
results.getMessages(Level.WARNING).forEach(e -> LOG.warn("{}", e));
assertTrue(results.getMessages(Level.WARNING).size() == 0);
final KieContainer kieContainer = ks.newKieContainer(ks.getRepository().getDefaultReleaseId());
DMNRuntime runtime = kieContainer.newKieSession().getKieRuntime(DMNRuntime.class);
DMNModel dmnModel = runtime.getModel("https://github.com/kiegroup/kie-dmn", "0001-input-data-string");
assertThat(dmnModel, notNullValue());
assertThat(formatMessages(dmnModel.getMessages()), dmnModel.hasErrors(), is(false));
assertEquals(3, dmnModel.getDefinitions().getDrgElement().size());
InputData inputData1 = (InputData) dmnModel.getDefinitions().getDrgElement().get(1);
assertEquals("First Name", inputData1.getName());
DMNElement.ExtensionElements id1elements = inputData1.getExtensionElements();
assertTrue(id1elements != null);
assertEquals(1, id1elements.getAny().size());
FirstNameDescription firstNameDescription = (FirstNameDescription) id1elements.getAny().get(0);
assertTrue(firstNameDescription.getContent().equals("First name in latin characters"));
InputData inputData2 = (InputData) dmnModel.getDefinitions().getDrgElement().get(2);
assertEquals("Last Name", inputData2.getName());
DMNElement.ExtensionElements id2elements = inputData2.getExtensionElements();
assertTrue(id2elements != null);
assertEquals(1, id2elements.getAny().size());
LastNameDescription lastNameDescription = (LastNameDescription) id2elements.getAny().get(0);
assertTrue(lastNameDescription.getContent().equals("Last name in latin characters"));
}
use of org.kie.api.builder.Results in project drools by kiegroup.
the class PMMLUsageDemoTest method invokePmmlWithTrait.
@Test
public void invokePmmlWithTrait() {
String extraDrl = "package org.kie.pmml.pmml_4_2.test;" + "" + "import org.drools.core.factmodel.traits.Entity;" + "" + "rule \"Init\" " + "when " + " $s : String( this == \"trigger\" ) " + "then " + " System.out.println( \"Trig\" ); " + " Entity o = new Entity(); " + " insert( o ); \n" + "" + // both soft and hard fields will be used to feed data into the model
"" + " MockColdTrait input = don( o, MockColdTrait.class ); " + " modify( input ) { " + " setTemp( 22.0 );" + " } " + "end " + "" + "" + "rule Log when $x : MockColdTrait() then System.out.println( \"IN \" + $x ); end " + "rule Log2 when $x : Cold() then System.out.println( \"OUT \" + $x ); end ";
KieServices ks = KieServices.Factory.get();
KieFileSystem kfs = ks.newKieFileSystem();
kfs.write(ResourceFactory.newClassPathResource(pmmlSource).setResourceType(ResourceType.PMML));
kfs.write(ResourceFactory.newByteArrayResource(extraDrl.getBytes()).setSourcePath("rules.drl").setResourceType(ResourceType.DRL));
Results res = ks.newKieBuilder(kfs).buildAll().getResults();
if (res.hasMessages(Message.Level.ERROR)) {
System.out.println(res.getMessages(Message.Level.ERROR));
}
assertEquals(0, res.getMessages(Message.Level.ERROR).size());
KieSession kSession = ks.newKieContainer(ks.getRepository().getDefaultReleaseId()).newKieSession();
kSession.insert("trigger");
kSession.fireAllRules();
System.out.println(reportWMObjects(kSession));
QueryResults qrs = kSession.getQueryResults("Cold", "MockCold", Variable.v);
assertTrue(qrs.iterator().hasNext());
Object val = qrs.iterator().next().get("$result");
Assert.assertEquals(0.56, val);
}
Aggregations