Skip to content

Instantly share code, notes, and snippets.

@danielezonca
Created October 14, 2019 12:13
Show Gist options
  • Save danielezonca/f58bcb4e81c734ecca9c30a98278e558 to your computer and use it in GitHub Desktop.
Save danielezonca/f58bcb4e81c734ecca9c30a98278e558 to your computer and use it in GitHub Desktop.
/*
* Copyright 2018 Red Hat, Inc. and/or its affiliates.
*
* 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.drools.scenariosimulation;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;
import org.junit.Test;
import org.kie.dmn.api.feel.runtime.events.FEELEvent;
import org.kie.dmn.api.feel.runtime.events.FEELEvent.Severity;
import org.kie.dmn.core.compiler.profiles.ExtendedDMNProfile;
import org.kie.dmn.feel.FEEL;
import org.kie.dmn.feel.lang.EvaluationContext;
import org.kie.dmn.feel.lang.Type;
import org.kie.dmn.feel.lang.impl.EvaluationContextImpl;
import org.kie.dmn.feel.lang.impl.FEELEventListenersManager;
import org.kie.dmn.feel.lang.types.BuiltInType;
import org.kie.dmn.feel.runtime.UnaryTest;
import org.kie.dmn.feel.runtime.events.FEELEventBase;
import org.kie.dmn.feel.runtime.events.SyntaxErrorEvent;
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.kie.dmn.api.feel.runtime.events.FEELEvent.Severity.ERROR;
public class SampleTest {
ClassLoader classLoader = SampleTest.class.getClassLoader();
@Test
public void evaluateUnaryExpression() {
assertTrue(unaryExpression("2", BigDecimal.valueOf(2)));
assertEquals(BigDecimal.valueOf(2), literalExpression("2", BigDecimal.valueOf(2)));
assertEquals(true, literalExpression("? == 2", BigDecimal.valueOf(2))); // KO
assertTrue(unaryExpression("? == 2", BigDecimal.valueOf(2))); // KO
}
private boolean unaryExpression(String rawExpression, Object resultValue) {
FEEL feel = newFeel(rawExpression);
EvaluationContext evaluationContext = newEvaluationContext(resultValue);
Map<String, Type> variables = new HashMap<>();
variables.put("?", BuiltInType.UNKNOWN);
List<UnaryTest> unaryTests = feel.evaluateUnaryTests(rawExpression, variables);
return unaryTests.stream()
.allMatch(unaryTest -> Optional
.ofNullable(unaryTest.apply(evaluationContext, resultValue))
.orElse(false));
}
private Object literalExpression(String rawExpression, Object resultValue) {
FEEL feel = newFeel(rawExpression);
EvaluationContext evaluationContext = newEvaluationContext(resultValue);
return feel.evaluate(rawExpression, evaluationContext);
}
private FEEL newFeel(String rawExpression) {
FEEL feel = FEEL.newInstance(singletonList(new ExtendedDMNProfile()));
feel.addListener(event -> {
System.out.println(rawExpression + " " + event);
});
return feel;
}
private EvaluationContext newEvaluationContext(Object resultValue) {
FEELEventListenersManager eventsManager = new FEELEventListenersManager();
EvaluationContext evaluationContext = new EvaluationContextImpl(classLoader, eventsManager);
evaluationContext.setValue("?", resultValue);
return evaluationContext;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment