Search in sources :

Example 51 with Results

use of org.kie.api.builder.Results in project drools by kiegroup.

the class AgendaRuleFlowGroupsTest method testConflictingAgendaAndRuleflowGroups.

@Test
public void testConflictingAgendaAndRuleflowGroups() throws Exception {
    String drl = "package org.drools.test; " + "" + "rule Test " + "  agenda-group 'ag' " + "  ruleflow-group 'rf' " + "when " + "then " + "end ";
    KieHelper helper = new KieHelper();
    helper.addContent(drl, ResourceType.DRL);
    Results res = helper.verify();
    System.err.println(res.getMessages());
    assertEquals(1, res.getMessages(Message.Level.WARNING).size());
    assertEquals(0, res.getMessages(Message.Level.ERROR).size());
}
Also used : Results(org.kie.api.builder.Results) KieHelper(org.kie.internal.utils.KieHelper) Test(org.junit.Test)

Example 52 with Results

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.drools.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);
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) Results(org.kie.api.builder.Results) QueryResults(org.kie.api.runtime.rule.QueryResults) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) QueryResults(org.kie.api.runtime.rule.QueryResults) Test(org.junit.Test)

Example 53 with Results

use of org.kie.api.builder.Results in project drools by kiegroup.

the class DrlFromPMMLTest method testDRLExecution.

@Test
public void testDRLExecution() throws Exception {
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem();
    kfs.write(ks.getResources().newByteArrayResource(drl.getBytes()).setSourcePath("test_scorecard_rules.drl").setResourceType(ResourceType.DRL));
    KieBuilder kieBuilder = ks.newKieBuilder(kfs);
    Results res = kieBuilder.buildAll().getResults();
    KieContainer kieContainer = ks.newKieContainer(kieBuilder.getKieModule().getReleaseId());
    KieBase kbase = kieContainer.getKieBase();
    KieSession session = kbase.newKieSession();
    FactType scorecardType = kbase.getFactType("org.drools.scorecards.example", "SampleScore");
    Object scorecard = scorecardType.newInstance();
    scorecardType.set(scorecard, "age", 10);
    session.insert(scorecard);
    session.fireAllRules();
    session.dispose();
    // occupation = 5, age = 25, validLicence -1
    assertEquals(29.0, scorecardType.get(scorecard, "scorecard__calculatedScore"));
    session = kbase.newKieSession();
    scorecard = scorecardType.newInstance();
    scorecardType.set(scorecard, "occupation", "SKYDIVER");
    scorecardType.set(scorecard, "age", 0);
    session.insert(scorecard);
    session.fireAllRules();
    session.dispose();
    // occupation = -10, age = +10, validLicense = -1;
    assertEquals(-1.0, scorecardType.get(scorecard, "scorecard__calculatedScore"));
    session = kbase.newKieSession();
    scorecard = scorecardType.newInstance();
    scorecardType.set(scorecard, "residenceState", "AP");
    scorecardType.set(scorecard, "occupation", "TEACHER");
    scorecardType.set(scorecard, "age", 20);
    scorecardType.set(scorecard, "validLicense", true);
    session.insert(scorecard);
    session.fireAllRules();
    session.dispose();
    // occupation = +10, age = +40, state = -10, validLicense = 1
    assertEquals(41.0, scorecardType.get(scorecard, "scorecard__calculatedScore"));
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) Results(org.kie.api.builder.Results) KieBase(org.kie.api.KieBase) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) KieBuilder(org.kie.api.builder.KieBuilder) KieContainer(org.kie.api.runtime.KieContainer) FactType(org.kie.api.definition.type.FactType) Test(org.junit.Test)

Example 54 with Results

use of org.kie.api.builder.Results in project drools by kiegroup.

the class ExternalObjectModelTest method testDRLExecution.

@Test
public void testDRLExecution() throws Exception {
    PMML pmmlDocument = null;
    String drl = null;
    if (scorecardCompiler.compileFromExcel(PMMLDocumentTest.class.getResourceAsStream("/scoremodel_externalmodel.xls"))) {
        pmmlDocument = scorecardCompiler.getPMMLDocument();
        assertNotNull(pmmlDocument);
        PMML4Compiler.dumpModel(pmmlDocument, System.out);
        drl = scorecardCompiler.getDRL();
        assertTrue(drl != null && !drl.isEmpty());
    // System.out.println(drl);
    } else {
        fail("failed to parse scoremodel Excel.");
    }
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem();
    kfs.write(ks.getResources().newByteArrayResource(drl.getBytes()).setSourcePath("test_scorecard_rules.drl").setResourceType(ResourceType.DRL));
    KieBuilder kieBuilder = ks.newKieBuilder(kfs);
    Results res = kieBuilder.buildAll().getResults();
    System.err.print(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.fireAllRules();
    session.dispose();
    // occupation = 0, age = 30, validLicence -1
    assertEquals(29.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;
    assertEquals(-1.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
    assertEquals(41.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 55 with Results

use of org.kie.api.builder.Results in project drools by kiegroup.

the class ExternalObjectModelTest method testWithReasonCodes.

@Test
public void testWithReasonCodes() 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_reasoncode")) {
        pmmlDocument2 = scorecardCompiler2.getPMMLDocument();
        PMML4Compiler.dumpModel(pmmlDocument2, System.out);
        assertNotNull(pmmlDocument2);
        drl2 = scorecardCompiler2.getDRL();
    // System.out.println(drl2);
    } else {
        for (ScorecardError error : scorecardCompiler2.getScorecardParseErrors()) {
            System.out.println(error.getErrorLocation() + ":" + error.getErrorMessage());
        }
        fail("failed to parse scoremodel Excel (scorecards_reasoncode).");
    }
    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();
    KieContainer kieContainer = ks.newKieContainer(kieBuilder.getKieModule().getReleaseId());
    KieBase kbase = kieContainer.getKieBase();
    KieSession session = kbase.newKieSession();
    FactType scorecardInternalsType = kbase.getFactType(PMML4Helper.pmmlDefaultPackageName(), "ScoreCard");
    Applicant applicant = new Applicant();
    applicant.setAge(10);
    session.insert(applicant);
    // session.addEventListener(new DebugWorkingMemoryEventListener());
    session.fireAllRules();
    // occupation = 0, age = 30, validLicence -1, initialScore=100
    assertEquals(129.0, applicant.getTotalScore(), 0.0);
    assertEquals("VL0099", applicant.getReasonCodes());
    Object scorecardInternals = session.getObjects(new ClassObjectFilter(scorecardInternalsType.getFactClass())).iterator().next();
    Assert.assertEquals(129.0, scorecardInternalsType.get(scorecardInternals, "score"));
    Map reasonCodesMap = (Map) scorecardInternalsType.get(scorecardInternals, "ranking");
    Assert.assertNotNull(reasonCodesMap);
    Assert.assertEquals(Arrays.asList("VL0099", "AGE02"), new ArrayList(reasonCodesMap.keySet()));
    session.dispose();
    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) ArrayList(java.util.ArrayList) KieServices(org.kie.api.KieServices) FactType(org.kie.api.definition.type.FactType) ClassObjectFilter(org.kie.api.runtime.ClassObjectFilter) Results(org.kie.api.builder.Results) KieBase(org.kie.api.KieBase) PMML(org.dmg.pmml.pmml_4_2.descr.PMML) KieSession(org.kie.api.runtime.KieSession) KieBuilder(org.kie.api.builder.KieBuilder) Applicant(org.drools.scorecards.example.Applicant) Map(java.util.Map) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Aggregations

Results (org.kie.api.builder.Results)92 Test (org.junit.Test)81 KieServices (org.kie.api.KieServices)72 KieFileSystem (org.kie.api.builder.KieFileSystem)60 KieContainer (org.kie.api.runtime.KieContainer)36 KieBuilder (org.kie.api.builder.KieBuilder)32 KieSession (org.kie.api.runtime.KieSession)25 IncrementalResults (org.kie.internal.builder.IncrementalResults)20 ReleaseId (org.kie.api.builder.ReleaseId)19 QueryResults (org.kie.api.runtime.rule.QueryResults)17 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)15 KieBase (org.kie.api.KieBase)14 InternalKieBuilder (org.kie.internal.builder.InternalKieBuilder)13 DMNRuntime (org.kie.dmn.api.core.DMNRuntime)11 KieHelper (org.kie.internal.utils.KieHelper)11 KieModuleModel (org.kie.api.builder.model.KieModuleModel)10 FactType (org.kie.api.definition.type.FactType)10 ArrayList (java.util.ArrayList)9 FlatQueryResults (org.drools.core.runtime.rule.impl.FlatQueryResults)9 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)7