Search in sources :

Example 81 with Expression

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

the class DefaultAtlasPreviewContext method processPreview.

/**
 * Process single mapping entry in preview mode. Since modules don't participate
 * in preview mode, any document format specific function won't be applied.
 *
 * @param mapping A @link{Mapping} entry to process
 */
@Override
public Audits processPreview(Mapping mapping) throws AtlasException {
    DefaultAtlasSession session = new DefaultAtlasSession(this);
    this.originalMapping = mapping;
    Mapping cloned;
    try {
        byte[] serialized = jsonMapper.writeValueAsBytes(mapping);
        cloned = jsonMapper.readValue(serialized, Mapping.class);
    } catch (Exception e) {
        throw new AtlasException(e);
    }
    session.head().setMapping(cloned);
    MappingType mappingType = cloned.getMappingType();
    String expression = cloned.getExpression();
    FieldGroup sourceFieldGroup = cloned.getInputFieldGroup();
    List<Field> sourceFields = cloned.getInputField();
    List<Field> targetFields = cloned.getOutputField();
    targetFields.forEach(tf -> tf.setValue(null));
    if ((sourceFieldGroup == null && sourceFields.isEmpty()) || targetFields.isEmpty()) {
        return session.getAudits();
    }
    if (sourceFieldGroup != null) {
        sourceFields = sourceFieldGroup.getField();
    }
    for (Field sf : sourceFields) {
        if (sf.getFieldType() == null || sf.getValue() == null) {
            continue;
        }
        if (sf.getValue() instanceof String && ((String) sf.getValue()).isEmpty()) {
            continue;
        }
        if (!restoreSourceFieldType(session, sf)) {
            return session.getAudits();
        }
    }
    processSourceFieldMapping(session);
    if (session.hasErrors()) {
        return session.getAudits();
    }
    Field sourceField = session.head().getSourceField();
    Field targetField;
    if (mappingType == null || mappingType == MappingType.MAP) {
        sourceFieldGroup = sourceField instanceof FieldGroup ? (FieldGroup) sourceField : null;
        for (int i = 0; i < targetFields.size(); i++) {
            targetField = targetFields.get(i);
            session.head().setTargetField(targetField);
            if (sourceFieldGroup != null) {
                if (sourceFieldGroup.getField().size() == 0) {
                    AtlasUtil.addAudit(session, targetField, String.format("Skipping empty source group field '%s:%s'", sourceField.getDocId(), sourceField.getPath()), AuditStatus.INFO, null);
                    continue;
                }
                Integer index = targetField.getIndex();
                AtlasPath targetPath = new AtlasPath(targetField.getPath());
                if (targetPath.hasCollection() && !targetPath.isIndexedCollection()) {
                    if (targetFields.size() > 1) {
                        AtlasUtil.addAudit(session, targetField, "It's not yet supported to have a collection field as a part of multiple target fields in a same mapping", AuditStatus.ERROR, null);
                        session.getAudits().getAudit().addAll(session.head().getAudits());
                        return session.getAudits();
                    }
                    if (index != null) {
                        LOG.warn("Field index '{}' is detected on target field '{}:{}' while there's only one target field, ignoring", index, targetField.getDocId(), targetField.getPath());
                        targetField.setIndex(null);
                    }
                    FieldGroup targetFieldGroup = targetField instanceof FieldGroup ? (FieldGroup) targetField : AtlasModelFactory.createFieldGroupFrom(targetField, true);
                    targetFields.set(i, targetFieldGroup);
                    Field previousTargetField = null;
                    for (Field subSourceField : sourceFieldGroup.getField()) {
                        Field subTargetField = AtlasModelFactory.cloneFieldToSimpleField(targetFieldGroup);
                        targetFieldGroup.getField().add(subTargetField);
                        collectionHelper.copyCollectionIndexes(sourceFieldGroup, subSourceField, subTargetField, previousTargetField);
                        previousTargetField = subTargetField;
                        if (!convertSourceToTarget(session, subSourceField, subTargetField)) {
                            session.getAudits().getAudit().addAll(session.head().getAudits());
                            return session.getAudits();
                        }
                        ;
                        Field processed = subTargetField;
                        if (expression == null || expression.isEmpty()) {
                            processed = applyFieldActions(session, subTargetField);
                        }
                        subTargetField.setValue(processed.getValue());
                    }
                    continue;
                } else if (index == null) {
                    session.head().setSourceField(sourceFieldGroup.getField().get(sourceFieldGroup.getField().size() - 1));
                } else {
                    if (sourceFieldGroup.getField().size() > index) {
                        session.head().setSourceField(sourceFieldGroup.getField().get(index));
                    } else {
                        AtlasUtil.addAudit(session, targetField, String.format("The number of source fields '%s' is fewer than expected via target field index '%s'", sourceFieldGroup.getField().size(), targetField.getIndex()), AuditStatus.WARN, null);
                        continue;
                    }
                }
            }
            if (session.hasErrors()) {
                session.getAudits().getAudit().addAll(session.head().getAudits());
                return session.getAudits();
            }
            if (!convertSourceToTarget(session, session.head().getSourceField(), targetField)) {
                session.getAudits().getAudit().addAll(session.head().getAudits());
                return session.getAudits();
            }
            Field processed = targetField;
            if (expression == null || expression.isEmpty()) {
                processed = applyFieldActions(session, targetField);
            }
            targetField.setValue(processed.getValue());
        }
    } else if (mappingType == MappingType.COMBINE) {
        targetField = targetFields.get(0);
        Field combined = processCombineField(session, cloned, sourceFields, targetField);
        if (!convertSourceToTarget(session, combined, targetField)) {
            session.getAudits().getAudit().addAll(session.head().getAudits());
            return session.getAudits();
        }
        applyFieldActions(session, targetField);
    } else if (mappingType == MappingType.SEPARATE) {
        List<Field> separatedFields;
        try {
            separatedFields = processSeparateField(session, cloned, sourceField);
        } catch (AtlasException e) {
            AtlasUtil.addAudit(session, sourceField, String.format("Failed to separate field: %s", AtlasUtil.getChainedMessage(e)), AuditStatus.ERROR, null);
            if (LOG.isDebugEnabled()) {
                LOG.error("", e);
            }
            session.getAudits().getAudit().addAll(session.head().getAudits());
            return session.getAudits();
        }
        if (separatedFields == null) {
            session.getAudits().getAudit().addAll(session.head().getAudits());
            return session.getAudits();
        }
        for (Field f : targetFields) {
            targetField = f;
            if (targetField.getIndex() == null || targetField.getIndex() < 0) {
                AtlasUtil.addAudit(session, targetField, String.format("Separate requires zero or positive Index value to be set on targetField targetField.path=%s", targetField.getPath()), AuditStatus.WARN, null);
                continue;
            }
            if (separatedFields.size() <= targetField.getIndex()) {
                String errorMessage = String.format("Separate returned fewer segments count=%s when targetField.path=%s requested index=%s", separatedFields.size(), targetField.getPath(), targetField.getIndex());
                AtlasUtil.addAudit(session, targetField, errorMessage, AuditStatus.WARN, null);
                break;
            }
            if (!convertSourceToTarget(session, separatedFields.get(targetField.getIndex()), targetField)) {
                break;
            }
            applyFieldActions(session, targetField);
        }
    } else {
        AtlasUtil.addAudit(session, (String) null, String.format("Unsupported mappingType=%s detected", cloned.getMappingType()), AuditStatus.ERROR, null);
    }
    mapping.getOutputField().clear();
    mapping.getOutputField().addAll(cloned.getOutputField());
    session.getAudits().getAudit().addAll(session.head().getAudits());
    return session.getAudits();
}
Also used : MappingType(io.atlasmap.v2.MappingType) FieldGroup(io.atlasmap.v2.FieldGroup) Mapping(io.atlasmap.v2.Mapping) AtlasMapping(io.atlasmap.v2.AtlasMapping) AtlasException(io.atlasmap.api.AtlasException) AtlasException(io.atlasmap.api.AtlasException) AtlasConversionException(io.atlasmap.api.AtlasConversionException) SimpleField(io.atlasmap.v2.SimpleField) Field(io.atlasmap.v2.Field)

Example 82 with Expression

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

the class StringComplexFieldActions method replaceAll.

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

Example 83 with Expression

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

the class JexlTest method testJavaNull.

@Test
public void testJavaNull() {
    JexlEngine jexl = new JexlEngine();
    String jexlExp = "is_bad_new != null";
    String jexlExpEqual = "is_bad_new == null";
    Expression e = jexl.createExpression(jexlExp);
    Expression exp = jexl.createExpression(jexlExpEqual);
    JexlContext jc = new MapContext();
    jc.set("is_bad_new", null);
    Assert.assertEquals(Boolean.FALSE, e.evaluate(jc));
    Assert.assertEquals(Boolean.TRUE, exp.evaluate(jc));
    jc.set("is_bad_new", new Object());
    Assert.assertEquals(Boolean.TRUE, e.evaluate(jc));
    Assert.assertEquals(Boolean.FALSE, exp.evaluate(jc));
}
Also used : JexlEngine(org.apache.commons.jexl2.JexlEngine) Expression(org.apache.commons.jexl2.Expression) JexlContext(org.apache.commons.jexl2.JexlContext) MapContext(org.apache.commons.jexl2.MapContext) Test(org.testng.annotations.Test)

Example 84 with Expression

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

the class JexlTest method testMathMethod.

@Test
public void testMathMethod() {
    JexlEngine jexl = new JexlEngine();
    String jexlExp = "NumberUtils.max(a, b, c)";
    Expression e = jexl.createExpression(jexlExp);
    JexlContext jc = new MapContext();
    jc.set("NumberUtils", new NumberUtils());
    jc.set("a", 7);
    jc.set("b", 5);
    jc.set("c", 9);
    Assert.assertEquals(9, e.evaluate(jc));
}
Also used : JexlEngine(org.apache.commons.jexl2.JexlEngine) Expression(org.apache.commons.jexl2.Expression) JexlContext(org.apache.commons.jexl2.JexlContext) MapContext(org.apache.commons.jexl2.MapContext) NumberUtils(org.apache.commons.lang.math.NumberUtils) Test(org.testng.annotations.Test)

Example 85 with Expression

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

the class JexlTest method testJavaExpressionString.

@Test
public void testJavaExpressionString() {
    JexlEngine jexl = new JexlEngine();
    String jexlExp = "name == \"user_a\"";
    Expression e = jexl.createExpression(jexlExp);
    JexlContext jc = new MapContext();
    jc.set("name", "user_a");
    // Now evaluate the expression, getting the result
    Boolean isEqual = (Boolean) e.evaluate(jc);
    Assert.assertTrue(isEqual);
    jc.set("name", "user_b");
    isEqual = (Boolean) e.evaluate(jc);
    Assert.assertFalse(isEqual);
}
Also used : JexlEngine(org.apache.commons.jexl2.JexlEngine) Expression(org.apache.commons.jexl2.Expression) JexlContext(org.apache.commons.jexl2.JexlContext) MapContext(org.apache.commons.jexl2.MapContext) Test(org.testng.annotations.Test)

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