Search in sources :

Example 36 with KieSession

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

the class TestingEventListenerTest method testInclusive.

@Test
public void testInclusive() throws Exception {
    HashSet<String> set = new HashSet<String>();
    set.add("rule1");
    set.add("rule2");
    KieSession session = getKieSession("test_rules.drl");
    TestingEventListener ls = new TestingEventListener();
    // TestingEventListener.stubOutRules(set, session.getRuleBase(), true);
    session.addEventListener(ls);
    session.insert(new Cheese());
    session.fireAllRules(ls.getAgendaFilter(set, true));
    assertEquals(new Integer(1), (Integer) ls.firingCounts.get("rule1"));
    assertEquals(new Integer(1), (Integer) ls.firingCounts.get("rule2"));
    // assertEquals(new Integer(1), (Integer) ls.firingCounts.get("rule3"));
    assertFalse(ls.firingCounts.containsKey("rule3"));
    assertFalse(ls.firingCounts.containsKey("rule4"));
    session.insert(new Cheese());
    session.fireAllRules(ls.getAgendaFilter(set, true));
    assertEquals(new Integer(2), (Integer) ls.firingCounts.get("rule1"));
    assertEquals(new Integer(2), (Integer) ls.firingCounts.get("rule2"));
    assertFalse(ls.firingCounts.containsKey("rule3"));
    assertEquals(4, ls.totalFires);
}
Also used : KieSession(org.kie.api.runtime.KieSession) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 37 with KieSession

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

the class GlobalFactPopulatorTest method testWithGlobals.

@Test
public void testWithGlobals() throws Exception {
    FactData global = new FactData("Cheese", "c", Arrays.<Field>asList(new FieldData("type", "cheddar")), false);
    TypeResolver resolver = new ClassTypeResolver(new HashSet<String>(), Thread.currentThread().getContextClassLoader());
    resolver.addImport("org.drools.workbench.models.testscenarios.backend.Cheese");
    KieSession ksession = mock(KieSession.class);
    Map<String, Object> populatedData = new HashMap<String, Object>();
    Map<String, Object> globalData = new HashMap<String, Object>();
    GlobalFactPopulator globalFactPopulator = new GlobalFactPopulator(populatedData, resolver, global, globalData);
    globalFactPopulator.populate(ksession, new HashMap<String, FactHandle>());
    verify(ksession).setGlobal(eq(global.getName()), any(Object.class));
    assertEquals(1, globalData.size());
    assertEquals(0, populatedData.size());
}
Also used : HashMap(java.util.HashMap) FactHandle(org.kie.api.runtime.rule.FactHandle) ClassTypeResolver(org.kie.soup.project.datamodel.commons.types.ClassTypeResolver) TypeResolver(org.kie.soup.project.datamodel.commons.types.TypeResolver) FieldData(org.drools.workbench.models.testscenarios.shared.FieldData) FactData(org.drools.workbench.models.testscenarios.shared.FactData) KieSession(org.kie.api.runtime.KieSession) ClassTypeResolver(org.kie.soup.project.datamodel.commons.types.ClassTypeResolver) Test(org.junit.Test)

Example 38 with KieSession

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

the class TemplateIntegrationTest method test.

@Test
public void test() {
    String drl = "global java.util.List list\n" + "rule \"String detector\"\n" + "    when\n" + "        $s : String( )\n" + "    then\n" + "        list.add($s);\n" + "end";
    final KieSession ksession = new KieHelper().addContent(template, ResourceType.TEMPLATE).build().newKieSession();
    Company myCompany = new Company(123, "myCompany");
    Company yourCompany = new Company(456, "yourCompany");
    ksession.insert(myCompany);
    ksession.insert(yourCompany);
    ksession.fireAllRules();
    assertTrue(myCompany.isFound());
    assertFalse(yourCompany.isFound());
}
Also used : KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 39 with KieSession

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

the class TemplateWithDSLIntegrationTest method testCompileTemplateWithDSL.

@Test
public void testCompileTemplateWithDSL() {
    String template = "<rule>\n" + "  <name>guided-template-with-dsl</name>\n" + "  <modelVersion>1.0</modelVersion>\n" + "  <attributes/>\n" + "  <metadataList/>\n" + "  <lhs>\n" + "    <dslSentence>\n" + "      <drl>applicant:" + Applicant.class.getCanonicalName() + "(approved=={bool})</drl>\n" + "      <definition>When the applicant approval is {bool:BOOLEAN:checked}</definition>\n" + "      <values>\n" + "        <org.drools.workbench.models.datamodel.rule.DSLVariableValue>\n" + "          <value>false</value>\n" + "        </org.drools.workbench.models.datamodel.rule.DSLVariableValue>\n" + "        <org.drools.workbench.models.datamodel.rule.DSLComplexVariableValue>\n" + "          <value>bool</value>\n" + "          <id>BOOLEAN:checked</id>\n" + "        </org.drools.workbench.models.datamodel.rule.DSLComplexVariableValue>\n" + "      </values>\n" + "    </dslSentence>\n" + "  </lhs>\n" + "  <rhs>\n" + "    <dslSentence>\n" + "      <drl>applicant.setApproved(true)</drl>\n" + "      <definition>Approve the loan</definition>\n" + "      <values/>\n" + "    </dslSentence>\n" + "  </rhs>\n" + "  <imports>\n" + "    <imports/>\n" + "  </imports>\n" + "  <packageName>org.mortgages</packageName>\n" + "  <isNegated>false</isNegated>\n" + "  <table>\n" + "    <entry>\n" + "      <string>__ID_KOL_NAME__</string>\n" + "      <list>\n" + "        <string>1</string>\n" + "      </list>\n" + "    </entry>\n" + "  </table>\n" + "  <idCol>1</idCol>\n" + "  <rowsCount>1</rowsCount>\n" + "</rule>";
    String dsl = "[when]When the applicant approval is {bool:BOOLEAN:checked} = applicant:" + Applicant.class.getCanonicalName() + "(approved=={bool})\n" + "[then]Approve the loan = applicant.setApproved(true)";
    KieHelper kieHelper = new KieHelper();
    KieSession kieSession = kieHelper.addContent(template, ResourceType.TEMPLATE).addContent(dsl, ResourceType.DSL).build().newKieSession();
    Applicant applicant = new Applicant();
    applicant.setApproved(false);
    kieSession.insert(applicant);
    int rulesFired = kieSession.fireAllRules();
    Assert.assertEquals("Incorrect number of rules fired!", 1, rulesFired);
    Assert.assertEquals("Rule RHS wasn't triggered!", true, applicant.isApproved());
}
Also used : KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 40 with KieSession

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

the class ScenarioRunner4JUnit method runScenario.

private void runScenario(RunNotifier notifier, Scenario scenario) {
    Description childDescription = Description.createTestDescription(getClass(), scenario.getName());
    descr.addChild(childDescription);
    EachTestNotifier eachNotifier = new EachTestNotifier(notifier, childDescription);
    try {
        eachNotifier.fireTestStarted();
        // If a KieSession is not available, fail fast
        if (ksessions == null || ksessions.values().isEmpty()) {
            eachNotifier.addFailure(new NullKieSessionException("Unable to get a Session to run tests. Check the project for build errors."));
        } else {
            KieSession ksession = getKSession(scenario.getKSessions());
            if (ksession == null) {
                String ksessionName = getKSessionName(scenario.getKSessions());
                if (ksessionName == null) {
                    eachNotifier.addFailure(new NullPointerException("Test scenario runner could not find the default knowledge session."));
                } else {
                    eachNotifier.addFailure(new NullPointerException("Test Scenarios require Stateful KieSession to run."));
                }
            } else {
                final ScenarioRunner runner = new ScenarioRunner(ksession, maxRuleFirings);
                runner.run(scenario);
                if (!scenario.wasSuccessful()) {
                    StringBuilder builder = new StringBuilder();
                    for (String message : scenario.getFailureMessages()) {
                        builder.append(message).append("\n");
                    }
                    eachNotifier.addFailedAssumption(new AssumptionViolatedException(builder.toString()));
                }
                // FLUSSSSSH!
                for (FactHandle factHandle : ksession.getFactHandles()) {
                    ksession.delete(factHandle);
                }
                resetKieSession(ksession);
            }
        }
    } catch (Throwable t) {
        eachNotifier.addFailure(t);
    } finally {
        // has to always be called as per junit docs
        eachNotifier.fireTestFinished();
    }
}
Also used : Description(org.junit.runner.Description) AssumptionViolatedException(org.junit.internal.AssumptionViolatedException) FactHandle(org.kie.api.runtime.rule.FactHandle) EachTestNotifier(org.junit.internal.runners.model.EachTestNotifier) KieSession(org.kie.api.runtime.KieSession)

Aggregations

KieSession (org.kie.api.runtime.KieSession)5328 Test (org.junit.Test)4824 KieBase (org.kie.api.KieBase)2414 ArrayList (java.util.ArrayList)2317 List (java.util.List)1105 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)769 FactHandle (org.kie.api.runtime.rule.FactHandle)598 Person (org.drools.modelcompiler.domain.Person)519 HashMap (java.util.HashMap)416 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)415 KieServices (org.kie.api.KieServices)382 KieHelper (org.kie.internal.utils.KieHelper)355 KieContainer (org.kie.api.runtime.KieContainer)298 RuntimeEngine (org.kie.api.runtime.manager.RuntimeEngine)265 InternalFactHandle (org.drools.core.common.InternalFactHandle)259 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)234 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)234 ReleaseId (org.kie.api.builder.ReleaseId)232 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)229 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)207