Search in sources :

Example 46 with Expression

use of io.atlasmap.v2.Expression in project atlasmap by atlasmap.

the class ExpressionFieldActionTest method testLT.

@Test
public void testLT() throws Exception {
    Expression action = new Expression();
    action.setExpression("IF(LT(${0}, ${1}), ${0}, ${1})");
    assertEquals(10, ExpressionFieldAction.process(action, Arrays.asList(10, 100)));
    assertEquals(100, ExpressionFieldAction.process(action, Arrays.asList(1000, 100)));
    assertEquals(-10, ExpressionFieldAction.process(action, Arrays.asList(10, -10)));
    action.setExpression("IF(${0} < ${1}, ${0}, ${1})");
    assertEquals(10, ExpressionFieldAction.process(action, Arrays.asList(10, 100)));
    assertEquals(100, ExpressionFieldAction.process(action, Arrays.asList(1000, 100)));
    assertEquals(-10, ExpressionFieldAction.process(action, Arrays.asList(10, -10)));
}
Also used : Expression(io.atlasmap.v2.Expression) Test(org.junit.jupiter.api.Test)

Example 47 with Expression

use of io.atlasmap.v2.Expression in project atlasmap by atlasmap.

the class ArithmeticExpression method evaluate.

/**
 * Evaluates the expression.
 * @param message expression context
 * @return result {@link Field}
 */
public Field evaluate(ExpressionContext message) throws ExpressionException {
    Field lfield = left.evaluate(message);
    if (lfield == null || lfield.getValue() == null) {
        return wrapWithField(null);
    }
    Field rfield = right.evaluate(message);
    if (rfield == null || rfield.getValue() == null) {
        return null;
    }
    return evaluate(lfield, rfield);
}
Also used : Field(io.atlasmap.v2.Field) AtlasModelFactory.wrapWithField(io.atlasmap.v2.AtlasModelFactory.wrapWithField)

Example 48 with Expression

use of io.atlasmap.v2.Expression in project atlasmap by atlasmap.

the class StringComplexFieldActions method replaceFirst.

/**
 * Replaces first hit with the regular expression specified as a parameter.
 * @param replaceFirst action model
 * @param input source
 * @return processed
 */
@AtlasActionProcessor
public static String replaceFirst(ReplaceFirst replaceFirst, String input) {
    if (replaceFirst == null || replaceFirst.getMatch() == null || replaceFirst.getMatch().isEmpty()) {
        throw new IllegalArgumentException("ReplaceFirst action must be specified with a non-empty old string");
    }
    String match = replaceFirst.getMatch();
    String newString = replaceFirst.getNewString();
    return input == null ? null : input.replaceFirst(match, newString == null ? "" : newString);
}
Also used : SubString(io.atlasmap.v2.SubString) AtlasActionProcessor(io.atlasmap.spi.AtlasActionProcessor)

Example 49 with Expression

use of io.atlasmap.v2.Expression in project atlasmap by atlasmap.

the class ExpressionFieldAction method process.

/**
 * Processes expression field action.
 * @param action action model
 * @param args expression arguments
 * @return processed
 * @throws ExpressionException expression processing error
 */
@Deprecated
@AtlasActionProcessor
public static Object process(io.atlasmap.v2.Expression action, List<Object> args) throws ExpressionException {
    if (action.getExpression() == null || action.getExpression().trim().isEmpty()) {
        return null;
    }
    Expression parsedExpression = Expression.parse(action.getExpression(), DefaultAtlasFunctionResolver.getInstance());
    Field answer = parsedExpression.evaluate((index) -> {
        try {
            return wrapWithField(args.get(Integer.parseInt(index)));
        } catch (Throwable e) {
            throw new ExpressionException("Invalid variable: " + index);
        }
    });
    return unwrapField(answer);
}
Also used : Field(io.atlasmap.v2.Field) AtlasModelFactory.unwrapField(io.atlasmap.v2.AtlasModelFactory.unwrapField) AtlasModelFactory.wrapWithField(io.atlasmap.v2.AtlasModelFactory.wrapWithField) Expression(io.atlasmap.expression.Expression) ExpressionException(io.atlasmap.expression.ExpressionException) AtlasActionProcessor(io.atlasmap.spi.AtlasActionProcessor)

Example 50 with Expression

use of io.atlasmap.v2.Expression in project vorto by eclipse.

the class DataMapperJxpath method matchesCondition.

private boolean matchesCondition(FunctionblockModel fbModel, JXPathContext context) {
    Optional<Stereotype> conditionStereotype = fbModel.getStereotype("condition");
    if (conditionStereotype.isPresent() && conditionStereotype.get().hasAttribute("value")) {
        Expression e = jexlEngine.createExpression(normalizeCondition(conditionStereotype.get().getAttributes().get("value")));
        JexlContext jc = new ObjectContext<Object>(jexlEngine, context.getContextBean());
        jc.set("this", context.getContextBean());
        jc.set("obj", context.getContextBean());
        return (boolean) e.evaluate(jc);
    } else {
        return true;
    }
}
Also used : Expression(org.apache.commons.jexl2.Expression) JexlContext(org.apache.commons.jexl2.JexlContext) Stereotype(org.eclipse.vorto.model.Stereotype) ObjectContext(org.apache.commons.jexl2.ObjectContext)

Aggregations

Test (org.junit.jupiter.api.Test)34 Expression (org.apache.commons.jexl2.Expression)28 Expression (io.atlasmap.v2.Expression)26 JexlContext (org.apache.commons.jexl2.JexlContext)25 JexlEngine (org.apache.commons.jexl2.JexlEngine)22 Field (io.atlasmap.v2.Field)21 FieldGroup (io.atlasmap.v2.FieldGroup)20 MapContext (org.apache.commons.jexl2.MapContext)20 SimpleField (io.atlasmap.v2.SimpleField)16 Test (org.testng.annotations.Test)13 PropertyField (io.atlasmap.v2.PropertyField)9 ENotificationImpl (org.eclipse.emf.ecore.impl.ENotificationImpl)7 Expression (org.eclipse.xtext.resource.bug385636.Expression)7 Expression (org.kie.workbench.common.dmn.api.definition.v1_1.Expression)7 Expression (io.atlasmap.expression.Expression)6 ArrayList (java.util.ArrayList)6 List (java.util.List)5 ParseException (io.atlasmap.expression.parser.ParseException)4 Action (io.atlasmap.v2.Action)4 InformationItem (org.kie.workbench.common.dmn.api.definition.v1_1.InformationItem)4