-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented asynchronous decision engine. Resolved #210
- Loading branch information
Showing
7 changed files
with
288 additions
and
2 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
dmn-engine/src/main/java/org/powerflows/dmn/engine/AsyncDecisionEngine.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright (c) 2018-present PowerFlows.org - all rights reserved. | ||
* | ||
* 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.powerflows.dmn.engine; | ||
|
||
|
||
import org.powerflows.dmn.engine.model.decision.Decision; | ||
import org.powerflows.dmn.engine.model.evaluation.result.DecisionResult; | ||
import org.powerflows.dmn.engine.model.evaluation.variable.DecisionVariables; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
|
||
/** | ||
* AsynchronousdDecision engine contract. | ||
*/ | ||
public interface AsyncDecisionEngine { | ||
|
||
/** | ||
* @param decision Definition of decision | ||
* @param decisionVariables Variables used in evaluation | ||
* @return a promise of evaluation result | ||
*/ | ||
CompletableFuture<DecisionResult> evaluate(Decision decision, DecisionVariables decisionVariables); | ||
|
||
} |
54 changes: 54 additions & 0 deletions
54
dmn-engine/src/main/java/org/powerflows/dmn/engine/DefaultAsyncDecisionEngine.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright (c) 2018-present PowerFlows.org - all rights reserved. | ||
* | ||
* 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.powerflows.dmn.engine; | ||
|
||
import org.powerflows.dmn.engine.model.decision.Decision; | ||
import org.powerflows.dmn.engine.model.evaluation.result.DecisionResult; | ||
import org.powerflows.dmn.engine.model.evaluation.variable.DecisionVariables; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ExecutorService; | ||
|
||
/** | ||
* Default asynchronous decision engine implementation. | ||
*/ | ||
public class DefaultAsyncDecisionEngine implements AsyncDecisionEngine { | ||
private static final Logger LOG = LoggerFactory.getLogger(DefaultAsyncDecisionEngine.class); | ||
private final DecisionEngine decisionEngine; | ||
private final ExecutorService executorService; | ||
|
||
public DefaultAsyncDecisionEngine(DecisionEngine decisionEngine, ExecutorService executorService) { | ||
this.decisionEngine = decisionEngine; | ||
this.executorService = executorService; | ||
} | ||
|
||
@Override | ||
public CompletableFuture<DecisionResult> evaluate(final Decision decision, final DecisionVariables decisionVariables) { | ||
final CompletableFuture<DecisionResult> result = new CompletableFuture<>(); | ||
executorService.submit(() -> { | ||
try { | ||
result.complete(decisionEngine.evaluate(decision, decisionVariables)); | ||
} catch (Exception e) { | ||
LOG.debug("Error completing decision evaluation", e); | ||
result.completeExceptionally(e); | ||
} | ||
}); | ||
|
||
return result; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
...c/main/java/org/powerflows/dmn/engine/configuration/AsyncDecisionEngineConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright (c) 2018-present PowerFlows.org - all rights reserved. | ||
* | ||
* 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.powerflows.dmn.engine.configuration; | ||
|
||
|
||
import org.powerflows.dmn.engine.AsyncDecisionEngine; | ||
|
||
/** | ||
* Interface for AsyncDecisionEngine configurers. | ||
* | ||
* @see DefaultAsyncDecisionEngineConfiguration | ||
*/ | ||
public interface AsyncDecisionEngineConfiguration { | ||
|
||
/** | ||
* Create ready to use decision engine. | ||
* | ||
* @return AsyncDecisionEngine instance | ||
*/ | ||
AsyncDecisionEngine configure(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
...java/org/powerflows/dmn/engine/configuration/DefaultAsyncDecisionEngineConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Copyright (c) 2018-present PowerFlows.org - all rights reserved. | ||
* | ||
* 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.powerflows.dmn.engine.configuration; | ||
|
||
import lombok.Setter; | ||
import lombok.experimental.Accessors; | ||
import org.powerflows.dmn.engine.AsyncDecisionEngine; | ||
import org.powerflows.dmn.engine.DefaultAsyncDecisionEngine; | ||
import org.powerflows.dmn.engine.evaluator.expression.provider.binding.MethodBinding; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
|
||
/** | ||
* Default DecisionEngine configurer. | ||
* Builds and configures {@link DefaultAsyncDecisionEngine} instances. | ||
* Allows for customisation of method binding configuration. | ||
* Uses single thread asynchronous executor service by default @see Executors.newSingleThreadExecutor | ||
* | ||
* @see MethodBinding | ||
*/ | ||
@Accessors(chain = true, fluent = true) | ||
public class DefaultAsyncDecisionEngineConfiguration implements AsyncDecisionEngineConfiguration { | ||
@Setter | ||
private DecisionEngineConfiguration engineConfiguration; | ||
@Setter | ||
private ExecutorService executorService; | ||
@Setter | ||
private List<MethodBinding> methodBindings = Collections.emptyList(); | ||
|
||
@Override | ||
public AsyncDecisionEngine configure() { | ||
initEngineConfiguration(); | ||
initMethodBindings(); | ||
initExecutorService(); | ||
|
||
return new DefaultAsyncDecisionEngine(engineConfiguration.configure(), executorService); | ||
} | ||
|
||
private void initMethodBindings() { | ||
engineConfiguration.methodBindings(methodBindings); | ||
} | ||
|
||
private void initEngineConfiguration() { | ||
if (engineConfiguration == null) { | ||
engineConfiguration = new DefaultDecisionEngineConfiguration(); | ||
} | ||
} | ||
|
||
private void initExecutorService() { | ||
if (executorService == null) { | ||
executorService = Executors.newSingleThreadExecutor(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
...rg/powerflows/dmn/engine/configuration/DefaultAsyncDecisionEngineConfigurationSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Copyright (c) 2018-present PowerFlows.org - all rights reserved. | ||
* | ||
* 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.powerflows.dmn.engine.configuration | ||
|
||
import org.powerflows.dmn.engine.AsyncDecisionEngine | ||
import org.powerflows.dmn.engine.DecisionEngine | ||
import org.powerflows.dmn.engine.model.decision.Decision | ||
import org.powerflows.dmn.engine.model.evaluation.result.DecisionResult | ||
import org.powerflows.dmn.engine.model.evaluation.variable.DecisionVariables | ||
import spock.lang.Shared | ||
import spock.lang.Specification | ||
|
||
import java.util.concurrent.CompletableFuture | ||
|
||
class DefaultAsyncDecisionEngineConfigurationSpec extends Specification { | ||
|
||
@Shared | ||
private AsyncDecisionEngineConfiguration asyncDecisionEngineConfiguration | ||
|
||
@Shared | ||
private DecisionEngineConfiguration decisionEngineConfiguration | ||
|
||
@Shared | ||
private DecisionEngine decisionEngine | ||
|
||
@Shared | ||
private DecisionResult decisionResult | ||
|
||
@Shared | ||
private Decision decision | ||
|
||
void setupSpec() { | ||
decisionResult = Mock() | ||
decisionEngine = Mock() { | ||
evaluate(_, _) >> decisionResult | ||
} | ||
decisionEngineConfiguration = Mock() { | ||
configure() >> | ||
decisionEngine | ||
|
||
} | ||
asyncDecisionEngineConfiguration = new DefaultAsyncDecisionEngineConfiguration( | ||
engineConfiguration: decisionEngineConfiguration | ||
) | ||
|
||
decision = Mock() | ||
} | ||
|
||
void 'should evaluate asynchronously'() { | ||
given: | ||
final DecisionVariables decisionVariables = new DecisionVariables([:]) | ||
|
||
when: | ||
final AsyncDecisionEngine asyncDecisionEngine = asyncDecisionEngineConfiguration.configure() | ||
|
||
then: | ||
asyncDecisionEngine != null | ||
|
||
when: | ||
final CompletableFuture<DecisionResult> future = asyncDecisionEngine.evaluate(decision, decisionVariables) | ||
|
||
then: | ||
future != null | ||
DecisionResult result = future.get() | ||
result != null | ||
|
||
0 * _ | ||
} | ||
} |