use of org.kie.api.builder.KieBuilder in project drools by kiegroup.
the class BuildFromKJarTest method createKieModule.
private InternalKieModule createKieModule(KieServices ks, ReleaseId releaseId) {
KieFileSystem kfs = ks.newKieFileSystem();
kfs.writeKModuleXML(getDefaultKieModuleModel(ks).toXML());
kfs.writePomXML(KJARUtils.getPom(releaseId));
String javaSrc = Person.class.getCanonicalName().replace('.', '/') + ".java";
Resource javaResource = ks.getResources().newFileSystemResource("src/test/java/" + javaSrc);
kfs.write("src/main/java/" + javaSrc, javaResource);
kfs.write("src/main/resources/rule.drl", getRule());
KieBuilder kieBuilder = ks.newKieBuilder(kfs);
List<Message> messages = ((KieBuilderImpl) kieBuilder).buildAll(ExecutableModelFlowProject.class).getResults().getMessages();
if (!messages.isEmpty()) {
fail(messages.toString());
}
return (InternalKieModule) kieBuilder.getKieModule();
}
use of org.kie.api.builder.KieBuilder in project drools by kiegroup.
the class MultipleModelTest method testCompositeBuilding.
@Test
public void testCompositeBuilding() throws Exception {
KieServices ks = KieServices.Factory.get();
KieFileSystem kfs = ks.newKieFileSystem();
kfs.write(ResourceFactory.newClassPathResource(source1).setResourceType(ResourceType.PMML));
kfs.write(ResourceFactory.newClassPathResource(source2).setResourceType(ResourceType.PMML));
KieBuilder kb = ks.newKieBuilder(kfs);
kb.buildAll();
KieSession kSession = ks.newKieContainer(ks.getRepository().getDefaultReleaseId()).newKieSession();
setKbase(kSession.getKieBase());
kSession.fireAllRules();
assertEquals(2, kSession.getObjects(new ClassObjectFilter(ModelMarker.class)).size());
assertEquals(11, kSession.getObjects(new ClassObjectFilter(kSession.getKieBase().getFactType(packageName, "Synapse").getFactClass())).size());
kSession.dispose();
checkGeneratedRules();
}
use of org.kie.api.builder.KieBuilder in project drools by kiegroup.
the class ExternalObjectModelTest method testWithInitialScore.
@Test
public void testWithInitialScore() throws Exception {
ScorecardCompiler scorecardCompiler2 = new ScorecardCompiler(EXTERNAL_OBJECT_MODEL);
PMML pmmlDocument2 = null;
String drl2 = null;
if (scorecardCompiler2.compileFromExcel(PMMLDocumentTest.class.getResourceAsStream("/scoremodel_externalmodel.xls"), "scorecards_initialscore")) {
pmmlDocument2 = scorecardCompiler2.getPMMLDocument();
assertNotNull(pmmlDocument2);
drl2 = scorecardCompiler2.getDRL();
PMML4Compiler.dumpModel(pmmlDocument2, System.out);
} else {
fail("failed to parse scoremodel Excel.");
}
assertNotNull(pmmlDocument2);
assertTrue(drl2 != null && !drl2.isEmpty());
KieServices ks = KieServices.Factory.get();
KieFileSystem kfs = ks.newKieFileSystem();
kfs.write(ks.getResources().newByteArrayResource(drl2.getBytes()).setSourcePath("test_scorecard_rules.drl").setResourceType(ResourceType.DRL));
KieBuilder kieBuilder = ks.newKieBuilder(kfs);
Results res = kieBuilder.buildAll().getResults();
System.err.println(res.getMessages());
KieContainer kieContainer = ks.newKieContainer(kieBuilder.getKieModule().getReleaseId());
KieBase kbase = kieContainer.getKieBase();
KieSession session = kbase.newKieSession();
Applicant applicant = new Applicant();
applicant.setAge(10);
session.insert(applicant);
// session.addEventListener(new DebugWorkingMemoryEventListener());
session.fireAllRules();
session.dispose();
// occupation = 0, age = 30, validLicence -1, initialScore=100
assertEquals(129.0, applicant.getTotalScore(), 0.0);
session = kbase.newKieSession();
applicant = new Applicant();
applicant.setOccupation("SKYDIVER");
applicant.setAge(0);
session.insert(applicant);
session.fireAllRules();
session.dispose();
// occupation = -10, age = +10, validLicense = -1, initialScore=100;
assertEquals(99.0, applicant.getTotalScore(), 0.0);
session = kbase.newKieSession();
applicant = new Applicant();
applicant.setResidenceState("AP");
applicant.setOccupation("TEACHER");
applicant.setAge(20);
applicant.setValidLicense(true);
session.insert(applicant);
session.fireAllRules();
session.dispose();
// occupation = +10, age = +40, state = -10, validLicense = 1, initialScore=100
assertEquals(141.0, applicant.getTotalScore(), 0.0);
}
use of org.kie.api.builder.KieBuilder 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.KieBuilder in project drools by kiegroup.
the class KieContainerDefaultsTest method buildKieModule.
/**
* This is a helper method that prevents code copying.
*
* @param kieModule KieModuleModel used in the particular test.
*/
private void buildKieModule(KieModuleModel kieModule) {
final KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
kieFileSystem.generateAndWritePomXML(RELEASE_ID);
kieFileSystem.writeKModuleXML(kieModule.toXML());
kieFileSystem.write("src/main/resources/defaultKBase/test.drl", kieServices.getResources().newByteArrayResource(DRL.getBytes()));
final KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem);
kieBuilder.buildAll();
}
Aggregations