Skip to content

Commit

Permalink
MVEL support
Browse files Browse the repository at this point in the history
  • Loading branch information
Rikkola committed May 7, 2024
1 parent 2424aab commit d0bb0e3
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,14 @@ private OnExecute getExecutionAction(String hitPolicy) {
if (hitPolicy == null || Objects.equals("ANY", hitPolicy)) {
return (ruleDefinition, storeHandle) -> {
final RuleCell rc = parseGenericRuleThen(ruleDefinition);
storeHandle.set(rc.value);
Object value = resolveValue(rc.value);
storeHandle.set(value);
};
} else if (Objects.equals("FIRST", hitPolicy)) {
return (ruleDefinition, storeHandle) -> {
if (!storeHandle.isValuePresent()) {
final RuleCell rc = parseGenericRuleThen(ruleDefinition);
storeHandle.set(rc.value);
storeHandle.set(resolveValue(rc.value));
}
};
} else if (Objects.equals("COLLECT", hitPolicy)) {
Expand All @@ -102,22 +103,23 @@ private OnExecute getExecutionAction(String hitPolicy) {
final RuleCell rc = parseGenericRuleThen(ruleDefinition);

if (storeHandle.get() instanceof List list) {
list.add(resolveValue(rc));
list.add(resolveValue(rc.value));
}
String remove = "";
};
} else {
throw new UnsupportedOperationException("Not implemented ");
}
}

private Object resolveValue(final RuleCell rc) {
private Object resolveValue(Object value) {
try {
if (rc.value instanceof String text) {
if (value instanceof String text) {
return jsonMapper.readValue(text, Map.class);
}
} catch (JsonProcessingException ignored) {
}
return rc.value;
return value;
}

private RuleCell parseGenericRuleThen(Rule rule) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package org.kie.yard.core;

import org.drools.base.util.MVELExecutor;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.json.JsonMapper;
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;

private final JsonMapper jsonMapper = JsonMapper.builder().build();

public MVELLiteralExpressionInterpreter(final String name,
final QuotedExprParsed expr) {
this.name = name;
Expand All @@ -33,11 +35,21 @@ public int fire(final Map<String, Object> context,
try {
String rewrittenExpression = expr.getRewrittenExpression();
final Object result = MVEL.eval(rewrittenExpression, internalContext);
units.outputs().get(name).set(result);
units.outputs().get(name).set(resolveValue(result));
return 1;
} catch (Exception e) {
throw new RuntimeException("interpretation failed at runtime", e);
// TODO why throw and not return 0?
}
}

private Object resolveValue(Object value) {
try {
if (value instanceof String text) {
return jsonMapper.readValue(text, Map.class);
}
} catch (JsonProcessingException ignored) {
}
return value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class DomesticPackagePricesTest
extends TestBase {
Expand All @@ -41,7 +42,8 @@ public void testMPackage() throws Exception {
}
""";
Map<String, Object> outputJSONasMap = evaluate(CTX, FILE_NAME);
assertThat(outputJSONasMap).hasFieldOrPropertyWithValue("Package", "{ \"Size\": \"M\", \"Cost\": 6.90 }");
assertEquals(6.9, ((Map) outputJSONasMap.get("Package")).get("Cost"));
assertEquals("M", ((Map) outputJSONasMap.get("Package")).get("Size"));
}

@Test
Expand All @@ -55,6 +57,7 @@ public void testLPackage() throws Exception {
}
""";
Map<String, Object> outputJSONasMap = evaluate(CTX, FILE_NAME);
assertThat(outputJSONasMap).hasFieldOrPropertyWithValue("Package", "{ \"Size\": \"L\", \"Cost\": 8.90 }");
assertEquals(8.9, ((Map) outputJSONasMap.get("Package")).get("Cost"));
assertEquals("L", ((Map) outputJSONasMap.get("Package")).get("Size"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class InsuranceBasePriceTest
extends TestBase {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.kie.yard.core;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class MVELJSONTest
extends TestBase {

private static final String FILE_NAME = "/mvel-json-dot-access.yml";

@Test
public void testMVELManagesJSONMaps() throws Exception {
final String CTX = """
{
"Work Address":true
}
""";
final Map<String, Object> output = evaluate(CTX, FILE_NAME);

final Map mailingAddress = (Map) output.get("Mailing Address");
assertEquals("Work Street", mailingAddress.get("Street"));
assertEquals(23, mailingAddress.get("Number"));

final List deliveryItemNames = (List) output.get("Delivery Item Names");
assertEquals(3, deliveryItemNames.size());
assertTrue(deliveryItemNames.contains("Work Shoes"));
assertTrue(deliveryItemNames.contains("Work Hat"));
assertTrue(deliveryItemNames.contains("Work Shirt"));

final Map JSONTest= (Map) output.get("JSON Test");
assertEquals("Best Company LTD", JSONTest.get("Company"));

final Map mapTest = (Map) output.get("Map Test");
assertEquals("Hello", mapTest.get("Map"));
}
// TODO Validate two elements can not have the same name
}
57 changes: 57 additions & 0 deletions kie-yard/kie-yard-core/src/test/resources/mvel-json-dot-access.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
specVersion: alpha
kind: YaRD
name: "Testing MVEL"
inputs:
- name: Work Address
type: boolean
elements:
- name: JSON Test
type: Decision
logic:
type: LiteralExpression
expression: |
'{ "Company": "Best Company LTD" }'
- name: Map Test
type: Decision
logic:
type: LiteralExpression
expression: |
[ "Map": "Hello" ]
- name: Delivery Items
type: Decision
logic:
type: DecisionTable
hitPolicy: COLLECT
inputs: [ 'Work Address']
rules:
- when: [ true ]
then: '{ "Item": { "Name": "Work Shoes" } }'
- when: [ true ]
then: '{ "Item": { "Name": "Work Hat" } }'
- when: [ true ]
then: '{ "Item": { "Name": "Work Shirt" } }'
- when: [ false ]
then: '{ "Item": { "Name": "Holiday Hat" } }'
- name: Customer
type: Decision
logic:
type: DecisionTable
hitPolicy: ANY
inputs: [ 'Work Address']
rules:
- when: [ true ]
then: '{ "Address": { "Street": "Work Street", "Number": 23 } }'
- when: [ false ]
then: '{ "Address": { "Street": "Free Time Path", "Number": 123 } }'
- name: Mailing Address
type: Decision
logic:
type: LiteralExpression
expression: |
Customer.Address
- name: Delivery Item Names
type: Decision
logic:
type: LiteralExpression
expression: |
( Item.Name in `Delivery Items` )

0 comments on commit d0bb0e3

Please sign in to comment.