Search in sources :

Example 21 with StatelessKieSession

use of org.kie.api.runtime.StatelessKieSession in project drools by kiegroup.

the class PricingRuleDTExample method execute.

public static void execute(KieContainer kc) {
    StatelessKieSession ksession = kc.newStatelessKieSession("DecisionTableKS");
    // now create some test data
    Driver driver = new Driver();
    Policy policy = new Policy();
    ksession.execute(Arrays.asList(new Object[] { driver, policy }));
    System.out.println("BASE PRICE IS: " + policy.getBasePrice());
    System.out.println("DISCOUNT IS: " + policy.getDiscountPercent());
    policy.getBasePrice();
}
Also used : StatelessKieSession(org.kie.api.runtime.StatelessKieSession)

Example 22 with StatelessKieSession

use of org.kie.api.runtime.StatelessKieSession 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 23 with StatelessKieSession

use of org.kie.api.runtime.StatelessKieSession in project drools by kiegroup.

the class KieContainerDefaultsTest method testTwoStatelessKieSessionsOneDefault.

/**
 * This test checks if default StatelessKieSessions behave as expected.
 */
@Test
public void testTwoStatelessKieSessionsOneDefault() {
    final KieModuleModel kieModule = kieServices.newKieModuleModel();
    kieModule.newKieBaseModel("firstKBase").setDefault(true).newKieSessionModel("firstKSession").setType(KieSessionModel.KieSessionType.STATELESS).setDefault(true);
    kieModule.newKieBaseModel("secondKBase").setDefault(true).newKieSessionModel("secondKSession").setType(KieSessionModel.KieSessionType.STATELESS).setDefault(false);
    buildKieModule(kieModule);
    final KieContainer kieContainer = kieServices.newKieContainer(RELEASE_ID);
    final StatelessKieSession firstKSession = kieContainer.newStatelessKieSession("firstKSession");
    final StatelessKieSession secondKSession = kieContainer.newStatelessKieSession("secondKSession");
    Assertions.assertThat(firstKSession).isEqualTo(((InternalKieContainer) kieContainer).getStatelessKieSession());
    Assertions.assertThat(secondKSession).isNotEqualTo(((InternalKieContainer) kieContainer).getStatelessKieSession());
}
Also used : KieModuleModel(org.kie.api.builder.model.KieModuleModel) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) InternalKieContainer(org.drools.core.impl.InternalKieContainer) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 24 with StatelessKieSession

use of org.kie.api.runtime.StatelessKieSession in project drools by kiegroup.

the class SessionInsertMultiThreadingTest method testSingleCommonStatelessSession.

private void testSingleCommonStatelessSession() throws Exception {
    final List<Future<?>> futures = new ArrayList<Future<?>>();
    final StatelessKieSession statelessKieSession = kbase.newStatelessKieSession();
    runTestBySeveralThreads(statelessKieSession, futures);
    waitForCompletion(futures);
}
Also used : ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) StatelessKieSession(org.kie.api.runtime.StatelessKieSession)

Example 25 with StatelessKieSession

use of org.kie.api.runtime.StatelessKieSession in project drools by kiegroup.

the class DMNValidatorImpl method validateModel.

private List<DMNMessage> validateModel(Definitions dmnModel) {
    if (!kieContainer.isPresent()) {
        return failedInitMsg;
    }
    StatelessKieSession kieSession = kieContainer.get().newStatelessKieSession();
    MessageReporter reporter = new MessageReporter();
    kieSession.setGlobal("reporter", reporter);
    kieSession.execute(allChildren(dmnModel).collect(toList()));
    return reporter.getMessages().getMessages();
}
Also used : StatelessKieSession(org.kie.api.runtime.StatelessKieSession)

Aggregations

StatelessKieSession (org.kie.api.runtime.StatelessKieSession)55 Test (org.junit.Test)42 ArrayList (java.util.ArrayList)28 KieBase (org.kie.api.KieBase)20 List (java.util.List)14 Cheese (org.drools.compiler.Cheese)14 KieContainer (org.kie.api.runtime.KieContainer)13 Command (org.kie.api.command.Command)12 KieServices (org.kie.api.KieServices)10 DynamicRulesTest (org.drools.compiler.integrationtests.DynamicRulesTest)9 ExecutionResults (org.kie.api.runtime.ExecutionResults)9 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)7 KieSession (org.kie.api.runtime.KieSession)7 Message (org.drools.compiler.Message)6 Person (org.drools.compiler.Person)6 FireAllRulesCommand (org.drools.core.command.runtime.rule.FireAllRulesCommand)6 KieModuleModel (org.kie.api.builder.model.KieModuleModel)6 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)6 KieBuilder (org.kie.api.builder.KieBuilder)5 KieFileSystem (org.kie.api.builder.KieFileSystem)5