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);
}
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());
}
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());
}
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());
}
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();
}
}
Aggregations