-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
159 additions
and
36 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
43 changes: 43 additions & 0 deletions
43
kie-yard/kie-yard-core/src/main/java/org/kie/yard/core/MVELLiteralExpressionInterpreter.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,43 @@ | ||
package org.kie.yard.core; | ||
|
||
import org.drools.base.util.MVELExecutor; | ||
import org.mvel2.MVEL; | ||
|
||
import javax.script.Bindings; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class MVELLiteralExpressionInterpreter implements Firable { | ||
private final String name; | ||
private final QuotedExprParsed expr; | ||
|
||
public MVELLiteralExpressionInterpreter(final String name, | ||
final QuotedExprParsed expr) { | ||
this.name = name; | ||
this.expr = expr; | ||
} | ||
|
||
@Override | ||
public int fire(final Map<String, Object> context, | ||
final YaRDDefinitions units) { | ||
final Map<String, Object> internalContext = new HashMap<>(); | ||
internalContext.putAll(context); | ||
|
||
for (Map.Entry<String, StoreHandle<Object>> outKV : units.outputs().entrySet()) { | ||
if (!outKV.getValue().isValuePresent()) { | ||
continue; | ||
} | ||
internalContext.put(QuotedExprParsed.escapeIdentifier(outKV.getKey()), outKV.getValue().get()); | ||
} | ||
|
||
try { | ||
String rewrittenExpression = expr.getRewrittenExpression(); | ||
final Object result = MVEL.eval(rewrittenExpression, internalContext); | ||
units.outputs().get(name).set(result); | ||
return 1; | ||
} catch (Exception e) { | ||
throw new RuntimeException("interpretation failed at runtime", e); | ||
// TODO why throw and not return 0? | ||
} | ||
} | ||
} |
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
81 changes: 81 additions & 0 deletions
81
...d/kie-yard-core/src/test/java/org/kie/yard/core/MVELLiteralExpressionInterpreterTest.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,81 @@ | ||
package org.kie.yard.core; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
public class MVELLiteralExpressionInterpreterTest { | ||
|
||
|
||
private MVELLiteralExpressionInterpreter sum; | ||
private YaRDDefinitions yardDefinitions; | ||
|
||
void setUp(final String expression) { | ||
sum = new MVELLiteralExpressionInterpreter("sum", QuotedExprParsed.from(expression)); | ||
|
||
final Map<String, StoreHandle<Object>> outputs = new HashMap<>(); | ||
outputs.put("C", StoreHandle.of(5)); | ||
outputs.put("sum", StoreHandle.empty(Object.class)); | ||
yardDefinitions = new YaRDDefinitions(Collections.emptyMap(), Collections.EMPTY_LIST, outputs); | ||
|
||
} | ||
|
||
@Test | ||
public void testSum() { | ||
|
||
setUp("1+1"); | ||
|
||
final int fire = sum.fire(Collections.emptyMap(), yardDefinitions); | ||
|
||
assertEquals(1, fire); | ||
assertEquals(2, yardDefinitions.outputs().get("sum").get()); | ||
} | ||
|
||
@Test | ||
public void testOutputVar() { | ||
|
||
setUp("Math.max(C, 1)"); | ||
|
||
final int fire = sum.fire(Collections.emptyMap(), yardDefinitions); | ||
|
||
assertEquals(1, fire); | ||
assertEquals(5, yardDefinitions.outputs().get("sum").get()); | ||
} | ||
|
||
@Test | ||
public void testContext() { | ||
|
||
setUp("A+B"); | ||
|
||
final Map<String, Object> context = new HashMap<>(); | ||
context.put("A", 1); | ||
context.put("B", 2); | ||
final int fire = sum.fire(context, yardDefinitions); | ||
|
||
assertEquals(1, fire); | ||
assertEquals(3, yardDefinitions.outputs().get("sum").get()); | ||
} | ||
|
||
@Test | ||
public void testMaps() { | ||
|
||
setUp("person.address.street + ' ' + person.address.number"); | ||
|
||
final Map<String, Object> context = new HashMap<>(); | ||
final Map<String, Object> person = new HashMap<>(); | ||
final Map<String, Object> address = new HashMap<>(); | ||
address.put("street", "My Street"); | ||
address.put("number", 12); | ||
person.put("address", address); | ||
context.put("person", person); | ||
final int fire = sum.fire(context, yardDefinitions); | ||
|
||
assertEquals(1, fire); | ||
assertEquals("My Street 12", yardDefinitions.outputs().get("sum").get()); | ||
} | ||
} |
1 change: 0 additions & 1 deletion
1
kie-yard/kie-yard-core/src/test/resources/domestic-package-prices.yml
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
1 change: 0 additions & 1 deletion
1
kie-yard/kie-yard-core/src/test/resources/insurance-base-price.yml
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 |
---|---|---|
@@ -1,7 +1,6 @@ | ||
specVersion: alpha | ||
kind: YaRD | ||
name: 'BasePrice' | ||
expressionLang: jshell | ||
inputs: | ||
- name: 'Age' | ||
type: number | ||
|
18 changes: 0 additions & 18 deletions
18
kie-yard/kie-yard-core/src/test/resources/logback-test.xml
This file was deleted.
Oops, something went wrong.
1 change: 0 additions & 1 deletion
1
kie-yard/kie-yard-core/src/test/resources/ticket-score-cards.yml
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