use of org.kie.api.runtime.KieSession in project drools by kiegroup.
the class OOPathFlowTest method testOOPath.
@Test
public void testOOPath() {
Global<List> listG = globalOf(List.class, "defaultpkg", "list");
Variable<Man> manV = declarationOf(Man.class);
Variable<Woman> wifeV = declarationOf(Woman.class, reactiveFrom(manV, Man::getWife));
Variable<Child> childV = declarationOf(Child.class, reactiveFrom(wifeV, Woman::getChildren));
Rule rule = rule("oopath").build(expr("exprA", childV, c -> c.getAge() > 10), on(manV, listG).execute((t, l) -> l.add(t.getName())));
Model model = new ModelImpl().addGlobal(listG).addRule(rule);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession ksession = kieBase.newKieSession();
final List<String> list = new ArrayList<>();
ksession.setGlobal("list", list);
final Woman alice = new Woman("Alice", 38);
final Man bob = new Man("Bob", 40);
final Man carl = new Man("Bob", 42);
bob.setWife(alice);
final Child charlie = new Child("Charles", 12);
final Child debbie = new Child("Debbie", 10);
alice.addChild(charlie);
alice.addChild(debbie);
ksession.insert(bob);
ksession.insert(carl);
ksession.fireAllRules();
Assertions.assertThat(list).containsExactlyInAnyOrder("Bob");
}
use of org.kie.api.runtime.KieSession in project drools by kiegroup.
the class ScenarioRunner4JUnitTest method testBasic.
@Test
public void testBasic() throws Exception {
HashMap<String, KieSession> ksessions = new HashMap<String, KieSession>();
ksessions.put("someId", ksession);
Scenario scenario = new Scenario();
scenario.getKSessions().add("someId");
ScenarioRunner4JUnit runner4JUnit = new ScenarioRunner4JUnit(scenario, ksessions);
RunNotifier notifier = new RunNotifier();
RunListener runListener = spy(new RunListener());
notifier.addListener(runListener);
runner4JUnit.run(notifier);
verify(runListener, never()).testFailure(any(Failure.class));
verify(runListener).testFinished(any(Description.class));
verify(ksession).reset();
}
use of org.kie.api.runtime.KieSession in project drools by kiegroup.
the class ScenarioRunner4JUnitTest method testIDNotSet.
@Test
public void testIDNotSet() throws Exception {
HashMap<String, KieSession> ksessions = new HashMap<String, KieSession>();
ksessions.put(null, ksession);
ScenarioRunner4JUnit runner4JUnit = new ScenarioRunner4JUnit(new Scenario(), ksessions);
RunNotifier notifier = new RunNotifier();
RunListener runListener = spy(new RunListener());
notifier.addListener(runListener);
runner4JUnit.run(notifier);
verify(runListener, never()).testFailure(any(Failure.class));
verify(runListener).testFinished(any(Description.class));
verify(ksession).reset();
}
use of org.kie.api.runtime.KieSession in project drools by kiegroup.
the class ScenarioRunner4JUnitTest method testNoKieSession.
@Test
public void testNoKieSession() throws Exception {
ScenarioRunner4JUnit runner4JUnit = new ScenarioRunner4JUnit(new Scenario(), new HashMap<String, KieSession>());
RunNotifier notifier = new RunNotifier();
RunListener runListener = spy(new RunListener());
notifier.addListener(runListener);
runner4JUnit.run(notifier);
verify(runListener).testFailure(any(Failure.class));
}
use of org.kie.api.runtime.KieSession in project drools by kiegroup.
the class ScenarioRunnerTest method testIntegrationInfiniteLoop.
@Test
public void testIntegrationInfiniteLoop() throws Exception {
Scenario sc = new Scenario();
sc.getImports().addImport(new Import("org.drools.workbench.models.testscenarios.backend.Cheese"));
sc.getImports().addImport(new Import("org.drools.workbench.models.testscenarios.backend.Person"));
FactData[] facts = new FactData[] { new FactData("Cheese", "c1", Arrays.<Field>asList(new FieldData("type", "cheddar"), new FieldData("price", "42")), false) };
sc.getGlobals().add(new FactData("Person", "p", new ArrayList(), false));
sc.getFixtures().addAll(Arrays.asList(facts));
ExecutionTrace executionTrace = new ExecutionTrace();
sc.getRules().add("rule1");
sc.getRules().add("rule2");
sc.setInclusive(true);
sc.getFixtures().add(executionTrace);
Expectation[] assertions = new Expectation[5];
assertions[0] = new VerifyFact("c1", ls(new VerifyField("type", "cheddar", "==")));
assertions[1] = new VerifyFact("p", ls(new VerifyField("name", "rule1", "=="), new VerifyField("status", "rule2", "==")));
assertions[2] = new VerifyRuleFired("rule1", 1, null);
assertions[3] = new VerifyRuleFired("rule2", 1, null);
assertions[4] = new VerifyRuleFired("rule3", 0, null);
sc.getFixtures().addAll(Arrays.asList(assertions));
KieSession ksession = getKieSession("test_rules_infinite_loop.drl");
ScenarioRunner run = new ScenarioRunner(ksession);
run.run(sc);
assertEquals(sc.getMaxRuleFirings(), executionTrace.getNumberOfRulesFired().intValue());
}
Aggregations