-
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.
Merge pull request #142 from powerflows/Add-support-for-providing-cus…
…tom-functions-in-Expressions Added support for providing custom functions in Expressions.
- Loading branch information
Showing
33 changed files
with
910 additions
and
207 deletions.
There are no files selected for viewing
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
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
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
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
66 changes: 66 additions & 0 deletions
66
.../dmn/engine/evaluator/expression/provider/DefaultExpressionEvaluationProviderFactory.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,66 @@ | ||
/* | ||
* 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.evaluator.expression.provider; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.powerflows.dmn.engine.model.decision.expression.ExpressionType; | ||
|
||
import java.util.EnumMap; | ||
import java.util.Optional; | ||
import java.util.ServiceLoader; | ||
|
||
@Slf4j | ||
public class DefaultExpressionEvaluationProviderFactory { | ||
|
||
private static final ServiceLoader<ExpressionEvaluationProviderFactory> serviceLoader = ServiceLoader.load(ExpressionEvaluationProviderFactory.class); | ||
|
||
private final EnumMap<ExpressionType, ExpressionEvaluationProviderFactory> factories = new EnumMap<>(ExpressionType.class); | ||
|
||
private final EnumMap<ExpressionType, ExpressionEvaluationProvider> providers = new EnumMap<>(ExpressionType.class); | ||
private final ExpressionEvaluationConfiguration configuration; | ||
|
||
|
||
public DefaultExpressionEvaluationProviderFactory() { | ||
this(ExpressionEvaluationConfiguration.simpleConfiguration()); | ||
} | ||
|
||
public DefaultExpressionEvaluationProviderFactory(final ExpressionEvaluationConfiguration configuration) { | ||
this.configuration = configuration; | ||
serviceLoader.forEach(provider -> | ||
provider.supportedExpressionTypes() | ||
.forEach(type -> { | ||
log.debug("Found ExpressionEvaluationProvider for type {} - {}", type, provider); | ||
factories.put(type, provider); | ||
} | ||
) | ||
); | ||
} | ||
|
||
public ExpressionEvaluationProvider getInstance(final ExpressionType expressionType) { | ||
final ExpressionEvaluationProvider expressionEvaluationProvider = providers.computeIfAbsent(expressionType, key -> Optional | ||
.ofNullable(factories.get(key)) | ||
.map(factory -> factory.createProvider(configuration)) | ||
.orElse(null) | ||
); | ||
|
||
if (expressionEvaluationProvider == null) { | ||
throw new IllegalArgumentException("Unknown expression type " + expressionType); | ||
} | ||
|
||
return expressionEvaluationProvider; | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
...owerflows/dmn/engine/evaluator/expression/provider/ExpressionEvaluationConfiguration.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,41 @@ | ||
/* | ||
* 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.evaluator.expression.provider; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
import org.powerflows.dmn.engine.evaluator.expression.provider.binding.MethodBinding; | ||
|
||
import javax.script.ScriptEngineManager; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
@Getter | ||
@Builder | ||
public class ExpressionEvaluationConfiguration { | ||
|
||
private final List<MethodBinding> methodBinding; | ||
private final ScriptEngineManager scriptEngineManager; | ||
|
||
private ExpressionEvaluationConfiguration(final List<MethodBinding> methodBinding, final ScriptEngineManager scriptEngineManager) { | ||
this.methodBinding = methodBinding == null ? Collections.emptyList() : methodBinding; | ||
this.scriptEngineManager = scriptEngineManager == null ? new ScriptEngineManager() : scriptEngineManager; | ||
} | ||
|
||
public static ExpressionEvaluationConfiguration simpleConfiguration() { | ||
return ExpressionEvaluationConfiguration.builder().build(); | ||
} | ||
} |
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
35 changes: 35 additions & 0 deletions
35
...ows/dmn/engine/evaluator/expression/provider/FeelExpressionEvaluationProviderFactory.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,35 @@ | ||
/* | ||
* 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.evaluator.expression.provider; | ||
|
||
import org.powerflows.dmn.engine.model.decision.expression.ExpressionType; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public class FeelExpressionEvaluationProviderFactory implements ExpressionEvaluationProviderFactory { | ||
private static final List<ExpressionType> SUPPORTED = Collections.singletonList(ExpressionType.FEEL); | ||
|
||
@Override | ||
public ExpressionEvaluationProvider createProvider(final ExpressionEvaluationConfiguration configuration) { | ||
return new FeelExpressionEvaluationProvider(); | ||
} | ||
|
||
@Override | ||
public List<ExpressionType> supportedExpressionTypes() { | ||
return SUPPORTED; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
...werflows/dmn/engine/evaluator/expression/provider/GroovyExpressionEvaluationProvider.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,39 @@ | ||
/* | ||
* 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.evaluator.expression.provider; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.codehaus.groovy.runtime.MethodClosure; | ||
import org.powerflows.dmn.engine.evaluator.expression.provider.binding.MethodBinding; | ||
|
||
@Slf4j | ||
class GroovyExpressionEvaluationProvider extends ScriptEngineExpressionEvaluationProvider { | ||
|
||
public GroovyExpressionEvaluationProvider(final ExpressionEvaluationConfiguration configuration) { | ||
super(configuration); | ||
} | ||
|
||
@Override | ||
protected Object createMethodBinding(final MethodBinding methodBinding) { | ||
return new MethodClosure(methodBinding, "execute"); | ||
} | ||
|
||
@Override | ||
protected String getEngineName() { | ||
return "groovy"; | ||
} | ||
} |
Oops, something went wrong.