Search in sources :

Example 61 with KieBuilder

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();
}
Also used : KieBuilderImpl(org.drools.compiler.kie.builder.impl.KieBuilderImpl) KieFileSystem(org.kie.api.builder.KieFileSystem) Message(org.kie.api.builder.Message) Resource(org.kie.api.io.Resource) KieBuilder(org.kie.api.builder.KieBuilder) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule)

Example 62 with KieBuilder

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();
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) ClassObjectFilter(org.kie.api.runtime.ClassObjectFilter) ModelMarker(org.drools.pmml.pmml_4_2.ModelMarker) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) KieBuilder(org.kie.api.builder.KieBuilder) InternalKieBuilder(org.kie.internal.builder.InternalKieBuilder) DroolsAbstractPMMLTest(org.drools.pmml.pmml_4_2.DroolsAbstractPMMLTest) Test(org.junit.Test)

Example 63 with KieBuilder

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);
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) Results(org.kie.api.builder.Results) KieBase(org.kie.api.KieBase) PMML(org.dmg.pmml.pmml_4_2.descr.PMML) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) KieBuilder(org.kie.api.builder.KieBuilder) Applicant(org.drools.scorecards.example.Applicant) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 64 with KieBuilder

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");
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) InputStream(java.io.InputStream) KieServices(org.kie.api.KieServices) FactType(org.kie.api.definition.type.FactType) Results(org.kie.api.builder.Results) KieBase(org.kie.api.KieBase) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieBuilder(org.kie.api.builder.KieBuilder) KieContainer(org.kie.api.runtime.KieContainer)

Example 65 with KieBuilder

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();
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) KieBuilder(org.kie.api.builder.KieBuilder)

Aggregations

KieBuilder (org.kie.api.builder.KieBuilder)211 KieFileSystem (org.kie.api.builder.KieFileSystem)179 KieServices (org.kie.api.KieServices)145 Test (org.junit.Test)134 KieSession (org.kie.api.runtime.KieSession)59 KieModuleModel (org.kie.api.builder.model.KieModuleModel)57 InternalKieBuilder (org.kie.internal.builder.InternalKieBuilder)54 KieContainer (org.kie.api.runtime.KieContainer)48 IncrementalResults (org.kie.internal.builder.IncrementalResults)35 ReleaseId (org.kie.api.builder.ReleaseId)34 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)33 Results (org.kie.api.builder.Results)32 Message (org.kie.api.builder.Message)30 KieBase (org.kie.api.KieBase)29 ArrayList (java.util.ArrayList)22 KieBaseModel (org.kie.api.builder.model.KieBaseModel)22 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)21 Resource (org.kie.api.io.Resource)19 FactType (org.kie.api.definition.type.FactType)16 KieModule (org.kie.api.builder.KieModule)12