Skip to content

Commit

Permalink
[DROOLS-1353] define ExecutableRunner interface (apache#1022)
Browse files Browse the repository at this point in the history
  • Loading branch information
mariofusco committed Dec 6, 2016
1 parent 5ed5a5b commit 9a976d5
Show file tree
Hide file tree
Showing 155 changed files with 1,013 additions and 791 deletions.
@@ -1,16 +1,32 @@
/*
* Copyright 2016 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.drools.compiler.simulation;

import org.drools.compiler.CommonTestMethodBase;
import org.drools.compiler.Message;
import org.drools.core.command.RequestContextImpl;
import org.drools.core.fluent.impl.FluentBuilderImpl;
import org.drools.core.fluent.impl.PseudoClockRunner;
import org.junit.Test;
import org.kie.api.KieServices;
import org.kie.api.builder.KieModule;
import org.kie.api.builder.ReleaseId;
import org.kie.api.io.ResourceType;
import org.kie.internal.fluent.RequestContext;
import org.kie.api.runtime.ExecutableRunner;
import org.kie.api.runtime.RequestContext;
import org.kie.internal.fluent.Scope;
import org.kie.internal.fluent.runtime.FluentBuilder;
import org.kie.internal.fluent.runtime.KieSessionFluent;
Expand All @@ -32,9 +48,7 @@ public class BatchRunFluentTest extends CommonTestMethodBase {

@Test
public void testOutName() {
PseudoClockRunner runner = new PseudoClockRunner();

FluentBuilder f = new FluentBuilderImpl();
FluentBuilder f = FluentBuilder.create();

f.newApplicationContext("app1")
.getKieContainer(releaseId).newSession()
Expand All @@ -43,17 +57,15 @@ public void testOutName() {
.getGlobal("outS").out("outS")
.dispose();

RequestContext requestContext = (RequestContext) runner.execute( f.getExecutable() );
RequestContext requestContext = ExecutableRunner.create().execute( f.getExecutable() );

assertEquals("h1", requestContext.getOut().get("outS"));
assertEquals("h1", requestContext.get("outS"));
}


@Test
public void testOutWithPriorSetAndNoName() {
PseudoClockRunner runner = new PseudoClockRunner();

FluentBuilder f = new FluentBuilderImpl();
FluentBuilder f = FluentBuilder.create();

f.newApplicationContext("app1")
.getKieContainer(releaseId).newSession()
Expand All @@ -62,17 +74,15 @@ public void testOutWithPriorSetAndNoName() {
.getGlobal("outS").set("outS").out()
.dispose();

RequestContextImpl requestContext = (RequestContextImpl) runner.execute(f.getExecutable());
RequestContext requestContext = ExecutableRunner.create().execute(f.getExecutable());

assertEquals("h1", requestContext.getOut().get("outS"));
assertEquals("h1", requestContext.get("outS"));
assertEquals("h1", requestContext.get("outS"));
}

@Test
public void testOutWithoutPriorSetAndNoName() {
PseudoClockRunner runner = new PseudoClockRunner();

FluentBuilder f = new FluentBuilderImpl();
FluentBuilder f = FluentBuilder.create();

f.newApplicationContext("app1")
.getKieContainer(releaseId).newSession()
Expand All @@ -82,9 +92,9 @@ public void testOutWithoutPriorSetAndNoName() {
.dispose();

try {
RequestContextImpl requestContext = (RequestContextImpl) runner.execute(f.getExecutable());
RequestContext requestContext = ExecutableRunner.create().execute(f.getExecutable());

assertEquals("h1", requestContext.getOut().get("out1"));
assertEquals("h1", requestContext.get("out1"));
fail("Must throw Exception, as no prior set was called and no name given to out");
} catch ( Exception e ) {

Expand All @@ -93,9 +103,7 @@ public void testOutWithoutPriorSetAndNoName() {

@Test
public void testSetAndGetWithCommandRegisterWithEnds() {
PseudoClockRunner runner = new PseudoClockRunner();

FluentBuilder f = new FluentBuilderImpl();
FluentBuilder f = FluentBuilder.create();

f.newApplicationContext("app1")
// create two sessions, and assign names
Expand All @@ -116,18 +124,16 @@ public void testSetAndGetWithCommandRegisterWithEnds() {
.get("s2", KieSessionFluent.class)
.getGlobal("outS").out("outS2").dispose();

RequestContextImpl requestContext = (RequestContextImpl) runner.execute(f.getExecutable());
RequestContext requestContext = ExecutableRunner.create().execute(f.getExecutable());

// Check that nothing went to the 'out'
assertEquals("h1", requestContext.getOut().get("outS1"));
assertEquals("h2", requestContext.getOut().get("outS2"));
assertEquals("h1", requestContext.get("outS1"));
assertEquals("h2", requestContext.get("outS2"));
}

@Test
public void testSetAndGetWithCommandRegisterWithoutEnds() {
PseudoClockRunner runner = new PseudoClockRunner();

FluentBuilder f = new FluentBuilderImpl();
FluentBuilder f = FluentBuilder.create();

f.newApplicationContext("app1")
// create two sessions, and assign names
Expand All @@ -148,42 +154,39 @@ public void testSetAndGetWithCommandRegisterWithoutEnds() {
.get("s2", KieSessionFluent.class)
.getGlobal("outS").out("outS2").dispose();

RequestContextImpl requestContext = (RequestContextImpl) runner.execute(f.getExecutable());
RequestContext requestContext = ExecutableRunner.create().execute(f.getExecutable());

// Check that nothing went to the 'out'
assertEquals("h1", requestContext.getOut().get("outS1"));
assertEquals("h2", requestContext.getOut().get("outS2"));
assertEquals("h1", requestContext.get("outS1"));
assertEquals("h2", requestContext.get("outS2"));
}


@Test
public void testConversationIdIncreases() {
PseudoClockRunner runner = new PseudoClockRunner();
public void testDifferentConversationIds() {
ExecutableRunner<RequestContext> runner = ExecutableRunner.create();
RequestContext requestContext = runner.createContext();

FluentBuilder f = new FluentBuilderImpl();
FluentBuilder f = FluentBuilder.create();

f.newApplicationContext("app1").startConversation()
.getKieContainer(releaseId).newSession()
.insert("h1")
.fireAllRules()
.dispose();

RequestContextImpl requestContext = (RequestContextImpl) runner.execute(f.getExecutable());
runner.execute(f.getExecutable(), requestContext);

long conversationId = requestContext.getConversationContext().getConversationId();
assertEquals(0, conversationId);
String conversationId = requestContext.getConversationContext().getName();

requestContext = (RequestContextImpl) runner.execute(f.getExecutable());
runner.execute(f.getExecutable(), requestContext);

conversationId = requestContext.getConversationContext().getConversationId();
assertEquals(1, conversationId);
assertNotEquals(conversationId, requestContext.getConversationContext().getName());
}

@Test
public void testRequestScope() {
PseudoClockRunner runner = new PseudoClockRunner();

FluentBuilder f = new FluentBuilderImpl();
FluentBuilder f = FluentBuilder.create();

f.newApplicationContext("app1")
.getKieContainer(releaseId).newSession()
Expand All @@ -192,20 +195,20 @@ public void testRequestScope() {
.getGlobal("outS").set("outS1") // Request is default
.dispose();

RequestContextImpl requestContext = (RequestContextImpl) runner.execute(f.getExecutable());
RequestContext requestContext = ExecutableRunner.create().execute(f.getExecutable());

// Check that nothing went to the 'out'
assertNull(requestContext.getOut().get("outS"));
assertNull(requestContext.get("outS"));
assertNull(requestContext.getApplicationContext().get("outS1") );
assertNull(requestContext.getConversationContext() );
assertEquals("h1", requestContext.get("outS1") );
}

@Test
public void testApplicationScope() {
PseudoClockRunner runner = new PseudoClockRunner();
ExecutableRunner<RequestContext> runner = ExecutableRunner.create();

FluentBuilder f = new FluentBuilderImpl();
FluentBuilder f = FluentBuilder.create();

f.newApplicationContext("app1")
.getKieContainer(releaseId).newSession()
Expand All @@ -214,10 +217,10 @@ public void testApplicationScope() {
.getGlobal("outS").set("outS1", Scope.APPLICATION)
.dispose();

RequestContextImpl requestContext = (RequestContextImpl) runner.execute(f.getExecutable());
RequestContext requestContext = runner.execute(f.getExecutable());

// Check that nothing went to the 'out'
assertEquals(null, requestContext.getOut().get("outS"));
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.
Expand All @@ -238,9 +241,9 @@ public void testApplicationScope() {

@Test
public void testConversationScope() {
PseudoClockRunner runner = new PseudoClockRunner();
ExecutableRunner<RequestContext> runner = ExecutableRunner.create();

FluentBuilder f = new FluentBuilderImpl();
FluentBuilder f = FluentBuilder.create();

f.newApplicationContext("app1").startConversation()
.getKieContainer(releaseId).newSession()
Expand All @@ -252,9 +255,9 @@ public void testConversationScope() {
RequestContextImpl requestContext = (RequestContextImpl) runner.execute(f.getExecutable());

// check that nothing went to the 'out'
assertEquals(null, requestContext.getOut().get("outS"));
assertEquals(null, requestContext.get("outS"));

long conversationId = requestContext.getConversationContext().getConversationId();
String conversationId = requestContext.getConversationContext().getName();

assertEquals("h1", requestContext.getConversationContext().get("outS1") );

Expand Down Expand Up @@ -283,9 +286,9 @@ public void testConversationScope() {

@Test
public void testContextScopeSearching() {
PseudoClockRunner runner = new PseudoClockRunner();
ExecutableRunner<RequestContext> runner = ExecutableRunner.create();

FluentBuilder f = new FluentBuilderImpl();
FluentBuilder f = FluentBuilder.create();

// Check that get() will search up to Application, when no request or conversation values
f.newApplicationContext("app1")
Expand All @@ -295,9 +298,9 @@ public void testContextScopeSearching() {
.getGlobal("outS").set("outS1", Scope.APPLICATION)
.get("outS1").out()
.dispose();
RequestContextImpl requestContext = (RequestContextImpl) runner.execute(f.getExecutable());
RequestContext requestContext = runner.execute(f.getExecutable());

assertEquals("h1", requestContext.getOut().get("outS1"));
assertEquals("h1", requestContext.get("outS1"));
assertEquals("h1", requestContext.getApplicationContext().get("outS1") );
assertEquals("h1", requestContext.get("outS1") );

Expand All @@ -311,9 +314,9 @@ public void testContextScopeSearching() {
.getGlobal("outS").set("outS1", Scope.CONVERSATION)
.get("outS1").out()
.dispose();
requestContext = (RequestContextImpl) runner.execute(f.getExecutable());
requestContext = runner.execute(f.getExecutable());

assertEquals("h2", requestContext.getOut().get("outS1"));
assertEquals("h2", requestContext.get("outS1"));
assertEquals("h1", requestContext.getApplicationContext().get("outS1") );
assertEquals("h2", requestContext.getConversationContext().get("outS1") );
assertEquals("h2", requestContext.get("outS1") );
Expand All @@ -322,16 +325,16 @@ public void testContextScopeSearching() {
// Check that get() will search directly to Request, thus over-riding Application and Conversation scoped values
f = new FluentBuilderImpl();

f.getApplicationContext("app1").joinConversation(requestContext.getConversationContext().getConversationId())
f.getApplicationContext("app1").joinConversation(requestContext.getConversationContext().getName())
.getKieContainer(releaseId).newSession()
.insert("h3")
.fireAllRules()
.getGlobal("outS").set("outS1", Scope.REQUEST)
.get("outS1").out()
.dispose();
requestContext = (RequestContextImpl) runner.execute(f.getExecutable());
requestContext = runner.execute(f.getExecutable());

assertEquals("h3", requestContext.getOut().get("outS1"));
assertEquals("h3", requestContext.get("outS1"));
assertEquals("h1", requestContext.getApplicationContext().get("outS1") );
assertEquals("h2", requestContext.getConversationContext().get("outS1") );
assertEquals("h3", requestContext.get("outS1") );
Expand All @@ -341,9 +344,9 @@ public void testContextScopeSearching() {

@Test
public void testAfter() {
PseudoClockRunner runner = new PseudoClockRunner( 0);
ExecutableRunner<RequestContext> runner = ExecutableRunner.create(0L);

FluentBuilder f = new FluentBuilderImpl();
FluentBuilder f = FluentBuilder.create();

// Check that get() will search up to Application, when no request or conversation values
f.after(1000).newApplicationContext("app1")
Expand All @@ -361,10 +364,10 @@ public void testAfter() {
.getGlobal("timeNow").out("timeNow2")
.dispose();

RequestContextImpl requestContext = (RequestContextImpl) runner.execute(f.getExecutable());
RequestContext requestContext = runner.execute(f.getExecutable());

assertEquals(1000l, requestContext.getOut().get("timeNow1"));
assertEquals(2000l, requestContext.getOut().get("timeNow2"));
assertEquals(1000l, requestContext.get("timeNow1"));
assertEquals(2000l, requestContext.get("timeNow2"));
}

public static KieModule createAndDeployJar( KieServices ks,
Expand Down
Expand Up @@ -16,12 +16,12 @@

package org.drools.core;

import org.drools.core.command.CommandService;
import org.drools.core.process.instance.WorkItemManagerFactory;
import org.drools.core.time.TimerService;
import org.drools.core.time.impl.TimerJobFactoryManager;
import org.drools.core.util.StringUtils;
import org.kie.api.runtime.Environment;
import org.kie.api.runtime.ExecutableRunner;
import org.kie.api.runtime.KieSessionConfiguration;
import org.kie.api.runtime.conf.BeliefSystemTypeOption;
import org.kie.api.runtime.conf.ClockTypeOption;
Expand Down Expand Up @@ -101,7 +101,7 @@ public final TimerJobFactoryManager getTimerJobFactoryManager() {

public abstract String getSignalManagerFactory();

public abstract CommandService getCommandService(KnowledgeBase kbase, Environment environment);
public abstract ExecutableRunner getRunner( KnowledgeBase kbase, Environment environment );

public abstract TimerService newTimerService();

Expand Down
Expand Up @@ -16,10 +16,10 @@

package org.drools.core;

import org.drools.core.command.CommandService;
import org.drools.core.process.instance.WorkItemManagerFactory;
import org.drools.core.time.TimerService;
import org.kie.api.runtime.Environment;
import org.kie.api.runtime.ExecutableRunner;
import org.kie.api.runtime.conf.QueryListenerOption;
import org.kie.api.runtime.conf.TimedRuleExecutionFilter;
import org.kie.api.runtime.process.WorkItemHandler;
Expand Down Expand Up @@ -204,8 +204,8 @@ public String getSignalManagerFactory() {
}

@Override
public CommandService getCommandService( KnowledgeBase kbase, Environment environment ) {
return delegate.getCommandService( kbase, environment );
public ExecutableRunner getRunner( KnowledgeBase kbase, Environment environment ) {
return delegate.getRunner( kbase, environment );
}

@Override
Expand Down

0 comments on commit 9a976d5

Please sign in to comment.