@Test public void testOutName() { ExecutableBuilder f = ExecutableBuilder.create(); f.newApplicationContext("app1") .getKieContainer(releaseId).newSession() .insert("h1") .fireAllRules() .getGlobal("outS").out("outS") .dispose(); RequestContext requestContext = ExecutableRunner.create().execute(f.getExecutable()); assertEquals("h1", requestContext.getOutputs().get("outS")); }
@Test public void testKieSessionCustomizationTest() { ExecutableRunner<RequestContext> runner = ExecutableRunner.create(0L); ExecutableBuilder f = ExecutableBuilder.create(); f.newApplicationContext("app1") .getKieContainer(releaseId) .newSessionCustomized(null, ((sessionName, kieContainer) -> { KieSessionModel kieSessionModel = kieContainer.getKieSessionModel(sessionName); kieSessionModel.setConsoleLogger("newConsoleLoggerName"); return kieContainer; })) .insert("h1") .fireAllRules() .getGlobal("outS").out("outS1") .dispose(); RequestContext requestContext = runner.execute(f.getExecutable()); assertEquals("h1", requestContext.getOutputs().get("outS1")); }
@Test public void testUnitLambdaInitializer() { ExecutableBuilder f = ExecutableBuilder.create(); DataSource<Person> people = DataSource.create( new Person("Mario", 10), new Person("Daniele", 30), new Person("Mark", 90)); f.newApplicationContext("app1") .getKieContainer(releaseIdUnit) .newRuleUnitExecutor() .bindVariable("people", people) .run((() -> new AdultUnitDifferentDataSourceName(people))) .out("firedRules") .dispose(); RequestContext requestContext = ExecutableRunner.create().execute(f.getExecutable()); assertEquals(1, requestContext.getOutputs().get("firedRules")); }
private DMNScenarioExecutableBuilder(KieContainer kieContainer, String applicationName) { executableBuilder = ExecutableBuilder.create(); dmnRuntimeFluent = executableBuilder.newApplicationContext(applicationName) .setKieContainer(kieContainer) .newDMNRuntime(); }
.newDMNRuntime() .setActiveModel("http://www.trisotech.com/definitions/_99ccd4df-41ac-43c3-a563-d58f43149829", "typecheck in DT") .setInput("a number", 0)
@Test public void testOutWithPriorSetAndNoName() { ExecutableBuilder f = ExecutableBuilder.create(); f.newApplicationContext("app1") .getKieContainer(releaseId).newSession() .insert("h1") .fireAllRules() .getGlobal("outS").set("outS").out() .dispose(); RequestContext requestContext = ExecutableRunner.create().execute(f.getExecutable()); assertEquals("h1", requestContext.getOutputs().get("outS")); assertEquals("h1", requestContext.get("outS")); }
private RuleScenarioExecutableBuilder(KieContainer kieContainer, String name) { executableBuilder = ExecutableBuilder.create(); kieSessionFluent = executableBuilder.newApplicationContext(name) .setKieContainer(kieContainer) .newSessionCustomized(null, forcePseudoClock); }
@Test public void testUnitNoBinding() { ExecutableBuilder f = ExecutableBuilder.create(); f.newApplicationContext("app1") .getKieContainer(releaseIdUnit) .newRuleUnitExecutor() .createDataSource(Person.class) .insert(new Person("Mario", 10)) .insert(new Person("Daniele", 30)) .insert(new Person("Mark", 40)) .buildDataSource() .run(AdultUnit.class) .out("firedRulesNoBinding") .dispose(); RequestContext requestContext = ExecutableRunner.create().execute(f.getExecutable()); assertEquals(0, requestContext.getOutputs().get("firedRulesNoBinding")); }
@Test public void testSetKieContainerTest() { KieServices kieServices = KieServices.Factory.get(); KieContainer kieContainer = kieServices.newKieContainer(releaseId); ExecutableRunner<RequestContext> runner = ExecutableRunner.create(0L); ExecutableBuilder f = ExecutableBuilder.create(); f.newApplicationContext("app1") .setKieContainer(kieContainer) .newSession() .insert("h1") .fireAllRules() .getGlobal("outS").out("outS1") .dispose(); RequestContext requestContext = runner.execute(f.getExecutable()); assertEquals("h1", requestContext.getOutputs().get("outS1")); }
@Test public void testUnitMultipleBinding() { ExecutableBuilder f = ExecutableBuilder.create(); f.newApplicationContext("app1") .getKieContainer(releaseIdUnit) .newRuleUnitExecutor() .createDataSource(Person.class) .addBinding("persons").addBinding("people") .insert(new Person("Mario", 40)) .insert(new Person("Daniele", 30)) .insert(new Person("Mark", 90)) .buildDataSource() .run(AdultUnit.class) .out("firedRules1") .run(AdultUnitDifferentDataSourceName.class) .out("firedRules2") .dispose(); RequestContext requestContext = ExecutableRunner.create().execute(f.getExecutable()); assertEquals(3, requestContext.getOutputs().get("firedRules1")); assertEquals(1, requestContext.getOutputs().get("firedRules2")); }
@Test public void testAfter() { ExecutableRunner<RequestContext> runner = ExecutableRunner.create(0L); ExecutableBuilder f = ExecutableBuilder.create(); // Check that get() will search up to Application, when no request or conversation values f.after(1000).newApplicationContext("app1") .getKieContainer(releaseId).newSession() .insert("h1") .fireAllRules() .getGlobal("outS").out("outS1") .getGlobal("timeNow").out("timeNow1") .dispose() .after(2000).newApplicationContext("app1") .getKieContainer(releaseId).newSession() .insert("h1") .fireAllRules() .getGlobal("outS").out("outS2") .getGlobal("timeNow").out("timeNow2") .dispose(); RequestContext requestContext = runner.execute(f.getExecutable()); assertEquals(1000l, requestContext.getOutputs().get("timeNow1")); assertEquals(2000l, requestContext.getOutputs().get("timeNow2")); }
@Test public void testUnit() { ExecutableBuilder f = ExecutableBuilder.create(); f.newApplicationContext("app1") .getKieContainer(releaseIdUnit) .newRuleUnitExecutor() .createDataSource(Person.class) .addBinding("persons") .insert(new Person("Mario", 10)) .insert(new Person("Daniele", 30)) .insert(new Person("Mark", 40)) .buildDataSource() .run(AdultUnit.class) .out("firedRules") .dispose(); RequestContext requestContext = ExecutableRunner.create().execute(f.getExecutable()); assertEquals(2, requestContext.getOutputs().get("firedRules")); }
@Test public void testSetAndGetWithCommandRegisterWithoutEnds() { ExecutableBuilder f = ExecutableBuilder.create(); f.newApplicationContext("app1") // create two sessions, and assign names .getKieContainer(releaseId).newSession().set("s1").end() // this end is needed, it's the get(String, Class) we are checking to see if it auto ends .getKieContainer(releaseId).newSession().set("s2") // initialise s1 with data .get("s1", KieSessionFluent.class) .insert("h1").fireAllRules() // initialise s2 with data .get("s2", KieSessionFluent.class) .insert("h2").fireAllRules() // assign s1 to out .get("s1", KieSessionFluent.class) .getGlobal("outS").out("outS1").dispose() .get("s2", KieSessionFluent.class) .getGlobal("outS").out("outS2").dispose(); RequestContext requestContext = ExecutableRunner.create().execute(f.getExecutable()); // Check that nothing went to the 'out' assertEquals("h1", requestContext.getOutputs().get("outS1")); assertEquals("h2", requestContext.getOutputs().get("outS2")); }
@Test public void testUnitLazyVarBinding() { ExecutableBuilder f = ExecutableBuilder.create(); DataSource<Person> people = DataSource.create( new Person("Mario", 10), new Person("Daniele", 30), new Person("Mark", 90)); f.newApplicationContext("app1") .getKieContainer(releaseIdUnit) .newRuleUnitExecutor() .run((() -> new AdultUnitDifferentDataSourceName(people))) .getGlobal("oldName") .set("test") .bindVariableByExpression("lazyVariable", context -> context.get("test")) .get("lazyVariable") .out("firedRules") .dispose(); RequestContext requestContext = ExecutableRunner.create().execute(f.getExecutable()); assertEquals("Mark", requestContext.getOutputs().get("firedRules")); }
@Test public void testSetAndOutBehaviour() { ExecutableBuilder f = ExecutableBuilder.create(); f.newApplicationContext("app1") .getKieContainer(releaseId).newSession() .insert("h1") .fireAllRules() .getGlobal("outS").set("outS") .getGlobal("outS").set("outS1").out() .dispose(); RequestContext requestContext = ExecutableRunner.create().execute(f.getExecutable()); assertNull(requestContext.getOutputs().get("outS")); assertEquals("h1", requestContext.get("outS")); assertNotNull(requestContext.getOutputs().get("outS1")); assertEquals(requestContext.get("outS1"), requestContext.getOutputs().get("outS1")); assertEquals(requestContext.get("outS"), requestContext.get("outS1")); }
@Test public void testSetAndGetWithCommandRegisterWithEnds() { ExecutableBuilder f = ExecutableBuilder.create(); f.newApplicationContext("app1") // create two sessions, and assign names .getKieContainer(releaseId).newSession().set("s1").end() .getKieContainer(releaseId).newSession().set("s2").end() // initialise s1 with data .get("s1", KieSessionFluent.class) .insert("h1").fireAllRules().end() // initialise s2 with data .get("s2", KieSessionFluent.class) .insert("h2").fireAllRules().end() // assign s1 to out .get("s1", KieSessionFluent.class) .getGlobal("outS").out("outS1").dispose() .get("s2", KieSessionFluent.class) .getGlobal("outS").out("outS2").dispose(); RequestContext requestContext = ExecutableRunner.create().execute(f.getExecutable()); // Check that nothing went to the 'out' assertEquals("h1", requestContext.getOutputs().get("outS1")); assertEquals("h2", requestContext.getOutputs().get("outS2")); }
@Test public void testOutWithoutPriorSetAndNoName() { ExecutableBuilder f = ExecutableBuilder.create(); f.newApplicationContext("app1") .getKieContainer(releaseId).newSession() .insert("h1") .fireAllRules() .getGlobal("outS").out() .dispose(); try { RequestContext requestContext = ExecutableRunner.create().execute(f.getExecutable()); assertEquals("h1", requestContext.get("out1")); fail("Must throw Exception, as no prior set was called and no name given to out"); } catch (Exception e) { } }
@Test public void testRequestScope() { ExecutableBuilder f = ExecutableBuilder.create(); f.newApplicationContext("app1") .getKieContainer(releaseId).newSession() .insert("h1") .fireAllRules() .getGlobal("outS").set("outS1") // Request is default .dispose(); RequestContext requestContext = ExecutableRunner.create().execute(f.getExecutable()); // Check that nothing went to the 'out' assertNull(requestContext.get("outS")); assertNull(requestContext.getOutputs().get("outS1")); assertNull(requestContext.getApplicationContext().get("outS1")); assertNull(requestContext.getConversationContext()); assertEquals("h1", requestContext.get("outS1")); }
@Test public void testDifferentConversationIds() { ExecutableRunner<RequestContext> runner = ExecutableRunner.create(); RequestContext requestContext = runner.createContext(); ExecutableBuilder f = ExecutableBuilder.create(); f.newApplicationContext("app1").startConversation() .getKieContainer(releaseId).newSession() .insert("h1") .fireAllRules() .dispose(); runner.execute(f.getExecutable(), requestContext); String conversationId = requestContext.getConversationContext().getName(); runner.execute(f.getExecutable(), requestContext); assertNotEquals(conversationId, requestContext.getConversationContext().getName()); }
@Test public void testApplicationScope() { ExecutableRunner<RequestContext> runner = ExecutableRunner.create(); ExecutableBuilder f = ExecutableBuilder.create(); f.newApplicationContext("app1") .getKieContainer(releaseId).newSession() .insert("h1") .fireAllRules() .getGlobal("outS").set("outS1", Scope.APPLICATION) .dispose(); RequestContext requestContext = runner.execute(f.getExecutable()); // Check that nothing went to the 'out' assertEquals(null, requestContext.get("outS")); assertEquals("h1", requestContext.getApplicationContext().get("outS1")); // Make another request, add to application context, assert old and new values are there. f = new ExecutableBuilderImpl(); f.getApplicationContext("app1") .getKieContainer(releaseId).newSession() .insert("h2") .fireAllRules() .getGlobal("outS").set("outS2", Scope.APPLICATION) .dispose(); requestContext = (RequestContext) runner.execute(f.getExecutable()); assertEquals("h1", requestContext.getApplicationContext().get("outS1")); assertEquals("h2", requestContext.getApplicationContext().get("outS2")); }