Skip to content

Commit

Permalink
Merge pull request #17 from camunda-community-hub/feel-engine-1.17
Browse files Browse the repository at this point in the history
feat: Update the FEEL engine to 1.17.0
  • Loading branch information
saig0 authored Sep 20, 2023
2 parents e317dee + 4457819 commit 0764eeb
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 32 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<properties>
<!-- release parent settings -->
<version.java>17</version.java>
<feel.version>1.16.1</feel.version>
<feel.version>1.17.0</feel.version>
<spring.boot.version>3.1.3</spring.boot.version>
</properties>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package org.camunda.feel.playground.api;

import org.camunda.feel.api.EvaluationResult;
import org.camunda.feel.playground.dto.FeelEvaluationRequest;
import org.camunda.feel.playground.dto.FeelEvaluationResponse;
import org.camunda.feel.playground.dto.FeelEvaluationWarning;
import org.camunda.feel.playground.dto.FeelUnaryTestsEvaluationRequest;
import org.camunda.feel.playground.sevice.FeelEvaluationService;
import org.camunda.feel.playground.sevice.TrackingService;
Expand All @@ -15,6 +17,9 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api/v1")
@CrossOrigin()
Expand All @@ -39,8 +44,7 @@ public ResponseEntity<FeelEvaluationResponse> evaluate(

try {
final var result = evaluationService.evaluate(request.expression, request.context);

return new ResponseEntity<>(FeelEvaluationResponse.withResult(result), HttpStatus.OK);
return createEvaluationResponse(result);

} catch (Exception e) {
return new ResponseEntity<>(FeelEvaluationResponse.withError(e.getMessage()), HttpStatus.OK);
Expand All @@ -50,6 +54,38 @@ public ResponseEntity<FeelEvaluationResponse> evaluate(
}
}

private static ResponseEntity<FeelEvaluationResponse> createEvaluationResponse(
EvaluationResult result) {
final var warnings = collectEvaluationWarnings(result);

if (result.isSuccess()) {
final var response = FeelEvaluationResponse.withResult(result.result());
response.setWarnings(warnings);
return new ResponseEntity<>(response, HttpStatus.OK);

} else {
final var failureMessage = result.failure().message();
final var response = FeelEvaluationResponse.withError(failureMessage);
response.setWarnings(warnings);
return new ResponseEntity<>(response, HttpStatus.OK);
}
}

private static List<FeelEvaluationWarning> collectEvaluationWarnings(EvaluationResult result) {
final var warnings = new ArrayList<FeelEvaluationWarning>();
result
.suppressedFailures()
.foreach(
failure -> {
final var warning =
FeelEvaluationWarning.of(
failure.failureType().toString(), failure.failureMessage());
warnings.add(warning);
return null;
});
return warnings;
}

@PostMapping("/feel-unary-tests/evaluate")
public ResponseEntity<FeelEvaluationResponse> evaluateUnaryTests(
@RequestBody FeelUnaryTestsEvaluationRequest request) {
Expand All @@ -61,7 +97,7 @@ public ResponseEntity<FeelEvaluationResponse> evaluateUnaryTests(
evaluationService.evaluateUnaryTests(
request.expression, request.inputValue, request.context);

return new ResponseEntity<>(FeelEvaluationResponse.withResult(result), HttpStatus.OK);
return createEvaluationResponse(result);

} catch (Exception e) {
return new ResponseEntity<>(FeelEvaluationResponse.withError(e.getMessage()), HttpStatus.OK);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@
*/
package org.camunda.feel.playground.dto;

import java.util.List;

public final class FeelEvaluationResponse {

public Object result;
public String error;

public List<FeelEvaluationWarning> warnings;

public Object getResult() {
return result;
}
Expand All @@ -28,6 +32,14 @@ public void setError(final String error) {
this.error = error;
}

public List<FeelEvaluationWarning> getWarnings() {
return warnings;
}

public void setWarnings(List<FeelEvaluationWarning> warnings) {
this.warnings = warnings;
}

public static FeelEvaluationResponse withResult(Object result) {
final var response = new FeelEvaluationResponse();
response.setResult(result);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.camunda.feel.playground.dto;

public final class FeelEvaluationWarning {

public String type;
public String message;

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public static FeelEvaluationWarning of(String type, String message) {
final var warning = new FeelEvaluationWarning();
warning.type = type;
warning.message = message;
return warning;
}

@Override
public String toString() {
return "FeelEvaluationWarning{" + "type='" + type + '\'' + ", message='" + message + '\'' + '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,30 @@
*/
package org.camunda.feel.playground.sevice;

import java.util.HashMap;
import java.util.Map;
import org.camunda.feel.FeelEngine;
import org.camunda.feel.api.EvaluationResult;
import org.camunda.feel.api.FeelEngineApi;
import org.camunda.feel.impl.JavaValueMapper;
import org.springframework.stereotype.Component;

@Component
public final class FeelEvaluationService {

private final FeelEngine feelEngine =
new FeelEngine.Builder().customValueMapper(new JavaValueMapper()).build();
private final FeelEngineApi feelEngineApi = buildFeelEngine();

public Object evaluate(String expression, Map<String, Object> context) {
final var evaluationResult = feelEngine.evalExpression(expression, context);

if (evaluationResult.isRight()) {
return evaluationResult.right().get();
private FeelEngineApi buildFeelEngine() {
final var feelEngine =
new FeelEngine.Builder().customValueMapper(new JavaValueMapper()).build();
return new FeelEngineApi(feelEngine);
}

} else {
final var failure = evaluationResult.left().get();
throw new RuntimeException(failure.message());
}
public EvaluationResult evaluate(String expression, Map<String, Object> context) {
return feelEngineApi.evaluateExpression(expression, context);
}

public Object evaluateUnaryTests(
public EvaluationResult evaluateUnaryTests(
String expression, Object inputValue, Map<String, Object> context) {
final var contextWithInput = new HashMap<>(context);
contextWithInput.put("cellInput", inputValue); // FeelEngine.UnaryTests.defaultInputVariable()

final var evaluationResult = feelEngine.evalUnaryTests(expression, contextWithInput);

if (evaluationResult.isRight()) {
return evaluationResult.right().get();

} else {
final var failure = evaluationResult.left().get();
throw new RuntimeException(failure.message());
}
return feelEngineApi.evaluateUnaryTests(expression, inputValue, context);
}
}
19 changes: 16 additions & 3 deletions src/test/java/org/camunda/feel/playground/FeelApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,30 @@ void shouldReturnContext() throws Exception {
.andExpect(content().json("{'result': {'x': 1}}"));
}

@Test
@Test
void shouldReturnEvaluationWarnings() throws Exception {
mvc.perform(
post("/api/v1/feel/evaluate")
.contentType(MediaType.APPLICATION_JSON)
.content("{\"expression\": \"x\", \"context\": {}}"))
.andExpect(status().isOk())
.andExpect(content().json(
"{'result': null, 'warnings': [{'type': \"NO_VARIABLE_FOUND\", 'message': \"No variable found with name 'x'\"}]}"
));
}


@Test
void shouldReturnEvaluationFailure() throws Exception {
mvc.perform(
post("/api/v1/feel/evaluate")
.contentType(MediaType.APPLICATION_JSON)
.content("{\"expression\": \"x\", \"context\": {}}"))
.content("{\"expression\": \"assert(x, x != null)\", \"context\": {}}"))
.andExpect(status().isOk())
.andExpect(
content()
.json(
"{'error': \"failed to evaluate expression 'x': no variable found for name 'x'\"}"));
"{'error': \"Assertion failure on evaluate the expression 'assert(x, x != null)': The condition is not fulfilled\"}"));
}

@Test
Expand Down

0 comments on commit 0764eeb

Please sign in to comment.