Search in sources :

Example 26 with KieSession

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");
}
Also used : FlowDSL.globalOf(org.drools.model.FlowDSL.globalOf) Man(org.drools.modelcompiler.domain.Man) Global(org.drools.model.Global) Toy(org.drools.modelcompiler.domain.Toy) FlowDSL.and(org.drools.model.FlowDSL.and) FlowDSL.accFunction(org.drools.model.FlowDSL.accFunction) FlowDSL.accumulate(org.drools.model.FlowDSL.accumulate) ArrayList(java.util.ArrayList) QueryDef(org.drools.model.QueryDef) Child(org.drools.modelcompiler.domain.Child) FlowDSL.from(org.drools.model.FlowDSL.from) QueryResults(org.kie.api.runtime.rule.QueryResults) FlowDSL.reactiveFrom(org.drools.model.FlowDSL.reactiveFrom) Assertions(org.assertj.core.api.Assertions) Woman(org.drools.modelcompiler.domain.Woman) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Model(org.drools.model.Model) FlowDSL.declarationOf(org.drools.model.FlowDSL.declarationOf) FlowDSL.rule(org.drools.model.FlowDSL.rule) FlowDSL.expr(org.drools.model.FlowDSL.expr) Variable(org.drools.model.Variable) FlowDSL.query(org.drools.model.FlowDSL.query) FlowDSL.on(org.drools.model.FlowDSL.on) Test(org.junit.Test) FlowDSL.input(org.drools.model.FlowDSL.input) InternationalAddress(org.drools.modelcompiler.domain.InternationalAddress) KieBaseBuilder(org.drools.modelcompiler.builder.KieBaseBuilder) List(java.util.List) Rule(org.drools.model.Rule) Assert.assertEquals(org.junit.Assert.assertEquals) ModelImpl(org.drools.model.impl.ModelImpl) ArrayList(java.util.ArrayList) KieBase(org.kie.api.KieBase) Model(org.drools.model.Model) ArrayList(java.util.ArrayList) List(java.util.List) KieSession(org.kie.api.runtime.KieSession) Man(org.drools.modelcompiler.domain.Man) Rule(org.drools.model.Rule) Woman(org.drools.modelcompiler.domain.Woman) ModelImpl(org.drools.model.impl.ModelImpl) Child(org.drools.modelcompiler.domain.Child) Test(org.junit.Test)

Example 27 with KieSession

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();
}
Also used : RunNotifier(org.junit.runner.notification.RunNotifier) Description(org.junit.runner.Description) HashMap(java.util.HashMap) KieSession(org.kie.api.runtime.KieSession) Failure(org.junit.runner.notification.Failure) Scenario(org.drools.workbench.models.testscenarios.shared.Scenario) RunListener(org.junit.runner.notification.RunListener) Test(org.junit.Test)

Example 28 with KieSession

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();
}
Also used : RunNotifier(org.junit.runner.notification.RunNotifier) Description(org.junit.runner.Description) HashMap(java.util.HashMap) KieSession(org.kie.api.runtime.KieSession) Failure(org.junit.runner.notification.Failure) Scenario(org.drools.workbench.models.testscenarios.shared.Scenario) RunListener(org.junit.runner.notification.RunListener) Test(org.junit.Test)

Example 29 with KieSession

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));
}
Also used : RunNotifier(org.junit.runner.notification.RunNotifier) KieSession(org.kie.api.runtime.KieSession) Failure(org.junit.runner.notification.Failure) Scenario(org.drools.workbench.models.testscenarios.shared.Scenario) RunListener(org.junit.runner.notification.RunListener) Test(org.junit.Test)

Example 30 with KieSession

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());
}
Also used : Import(org.kie.soup.project.datamodel.imports.Import) VerifyRuleFired(org.drools.workbench.models.testscenarios.shared.VerifyRuleFired) VerifyField(org.drools.workbench.models.testscenarios.shared.VerifyField) ArrayList(java.util.ArrayList) ExecutionTrace(org.drools.workbench.models.testscenarios.shared.ExecutionTrace) Scenario(org.drools.workbench.models.testscenarios.shared.Scenario) FieldData(org.drools.workbench.models.testscenarios.shared.FieldData) FactData(org.drools.workbench.models.testscenarios.shared.FactData) KieSession(org.kie.api.runtime.KieSession) Expectation(org.drools.workbench.models.testscenarios.shared.Expectation) VerifyFact(org.drools.workbench.models.testscenarios.shared.VerifyFact) Test(org.junit.Test)

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