use of io.atlasmap.expression.ExpressionException in project atlasmap by atlasmap.
the class DefaultAtlasExpressionProcessor method processExpression.
/**
* Processes the expression.
* @param session session
* @param expression expression
*/
public static void processExpression(DefaultAtlasSession session, String expression) {
if (expression == null || expression.trim().isEmpty()) {
return;
}
try {
Map<String, Field> sourceFieldMap = new HashMap<>();
Field parent = session.head().getSourceField();
if (parent != null && !AtlasUtil.isEmpty(parent.getDocId()) && !AtlasUtil.isEmpty(parent.getPath())) {
sourceFieldMap.put(parent.getDocId() + ":" + parent.getPath(), parent);
}
// Anonymous FieldGroup is just a wrapping, peel it off
if (parent instanceof FieldGroup && AtlasUtil.isEmpty(parent.getPath())) {
FieldGroup parentGroup = FieldGroup.class.cast(parent);
for (Field child : parentGroup.getField()) {
if (!(AtlasUtil.isEmpty(child.getDocId()) && AtlasUtil.isEmpty(child.getPath()))) {
sourceFieldMap.put(child.getDocId() + ":" + child.getPath(), child);
}
}
}
Expression parsedExpression = Expression.parse(expression, DefaultAtlasFunctionResolver.getInstance());
Object answer = parsedExpression.evaluate((path) -> {
if (path == null || path.isEmpty()) {
return null;
}
try {
Field f = sourceFieldMap.get(path);
if (f == null) {
return null;
}
AtlasModule sourceModule;
Map<String, AtlasModule> sourceModules = session.getAtlasContext().getSourceModules();
if (f instanceof ConstantField) {
sourceModule = sourceModules.get(AtlasConstants.CONSTANTS_DOCUMENT_ID);
} else if (f instanceof PropertyField) {
sourceModule = sourceModules.get(AtlasConstants.PROPERTIES_SOURCE_DOCUMENT_ID);
} else {
String[] splitted = path.split(":", 2);
sourceModule = sourceModules.get(splitted[0]);
}
if (sourceModule == null) {
throw new ExpressionException(String.format("Module for the path '%s' is not found", path));
}
session.head().setSourceField(f);
sourceModule.readSourceValue(session);
return session.head().getSourceField();
} catch (Exception e) {
throw new ExpressionException(e);
}
});
if (answer instanceof Field) {
session.head().setSourceField((Field) answer);
} else {
Field from = session.head().getSourceField();
SimpleField to = new SimpleField();
AtlasModelFactory.copyField(from, to, false);
to.setValue(answer);
session.head().setSourceField(to);
}
} catch (Exception e) {
AtlasUtil.addAudit(session, expression, String.format("Expression processing error [%s]: %s", expression, e.getMessage()), AuditStatus.ERROR, null);
if (LOG.isDebugEnabled()) {
LOG.debug("", e);
}
}
}
use of io.atlasmap.expression.ExpressionException 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);
}
Aggregations