use of io.github.linuxforhealth.core.exception.DataExtractionException in project hl7v2-fhir-converter by LinuxForHealth.
the class ExpressionUtility method evaluate.
/**
* Evaluates map of expression and generates ResourceEvaluationResult object.
*
* @param dataSource The data extractor to be used
* @param context The context in use
* @param baseValue The value to evaluate
* @param expressionMap Map of expressions
* @return {@link ResourceEvaluationResult}
*/
public static ResourceEvaluationResult evaluate(InputDataExtractor dataSource, Map<String, EvaluationResult> context, EvaluationResult baseValue, Map<String, Expression> expressionMap) {
try {
Map<String, Expression> expressionsToEvaluateLater = new HashMap<>();
Map<String, EvaluationResult> localContext = new HashMap<>(context);
localContext.put(Constants.NULL_VAR_NAME, new EmptyEvaluationResult());
// initialize the map and list to collect values
List<ResourceValue> additionalResolveValues = new ArrayList<>();
Map<String, Object> resolveValues = new HashMap<>();
for (Entry<String, Expression> entry : expressionMap.entrySet()) {
Expression exp = entry.getValue();
LOGGER.debug(EVALUATING, entry.getKey(), entry.getValue());
if (exp.isEvaluateLater()) {
expressionsToEvaluateLater.put(entry.getKey(), entry.getValue());
} else {
processExpression(dataSource, baseValue, localContext, additionalResolveValues, resolveValues, entry);
}
}
resolveValues.values().removeIf(Objects::isNull);
return new ResourceEvaluationResult(resolveValues, additionalResolveValues, new PendingExpressionState(expressionsToEvaluateLater, context));
} catch (RequiredConstraintFailureException e) {
LOGGER.warn("Resource Constraint condition not satisfied.");
LOGGER.debug("Resource Constraint condition not satisfied, exception", e);
return null;
} catch (IllegalArgumentException | IllegalStateException | DataExtractionException e) {
LOGGER.error("Exception during resource evaluation");
LOGGER.debug("Exception during resource evaluation reason ", e);
return null;
}
}
use of io.github.linuxforhealth.core.exception.DataExtractionException in project hl7v2-fhir-converter by LinuxForHealth.
the class HL7DataBasedResourceModel method evaluate.
@Override
public ResourceResult evaluate(InputDataExtractor dataSource, Map<String, EvaluationResult> context, EvaluationResult baseValue) {
ResourceResult resources = null;
try {
ResourceEvaluationResult result = ExpressionUtility.evaluate(dataSource, context, baseValue, this.expressions);
if (result != null && !result.getResolveValues().isEmpty()) {
String groupId = getGroupId(context);
resources = new ResourceResult(new SimpleResourceValue(result.getResolveValues(), this.name), result.getAdditionalResolveValues(), groupId, result.getPendingExpressions());
}
} catch (RequiredConstraintFailureException e) {
LOGGER.warn("Resource Constraint condition not satisfied for {}.", this.name);
LOGGER.debug("Resource Constraint condition not satisfied for {}, exception {}", this.name, e.toString());
return null;
} catch (IllegalArgumentException | IllegalStateException | DataExtractionException e) {
LOGGER.error("Exception during resource {} evaluation reason", this.name);
LOGGER.debug("Exception during resource {} evaluation reason {}", this.name, e.toString());
return null;
}
return resources;
}
use of io.github.linuxforhealth.core.exception.DataExtractionException in project hl7v2-fhir-converter by LinuxForHealth.
the class ExpressionUtility method evaluate.
public static ResourceEvaluationResult evaluate(HL7MessageData dataSource, Map<String, EvaluationResult> context, Map<String, Expression> expressionMap) {
try {
Map<String, EvaluationResult> localContext = new HashMap<>(context);
Map<String, Object> resolveValues = new HashMap<>();
List<ResourceValue> additionalResolveValues = new ArrayList<>();
for (Entry<String, Expression> entry : expressionMap.entrySet()) {
LOGGER.debug(EVALUATING, entry.getKey(), entry.getValue());
processExpression(dataSource, new EmptyEvaluationResult(), localContext, additionalResolveValues, resolveValues, entry);
}
resolveValues.values().removeIf(Objects::isNull);
return new ResourceEvaluationResult(resolveValues, additionalResolveValues);
} catch (RequiredConstraintFailureException e) {
LOGGER.warn("Resource Constraint condition not satisfied.");
LOGGER.debug("Resource Constraint condition not satisfied, exception", e);
return null;
} catch (IllegalArgumentException | IllegalStateException | DataExtractionException e) {
LOGGER.error("Exception during resource evaluation.");
LOGGER.debug("Exception during resource evaluation reason ", e);
return null;
}
}
use of io.github.linuxforhealth.core.exception.DataExtractionException in project hl7v2-fhir-converter by LinuxForHealth.
the class AbstractExpression method evaluate.
/**
* Evaluates the expression and generated single or multiple resources based on the expression
* values. If expression (reference and resource) ends with * then for that expression the Generic
* result includes list of values.
*
* @see io.github.linuxforhealth.api.Expression#evaluate(io.github.linuxforhealth.api.InputDataExtractor,
* java.util.Map, EvaluationResult)
*/
@Override
public EvaluationResult evaluate(InputDataExtractor dataSource, Map<String, EvaluationResult> contextValues, EvaluationResult baseValue) {
Preconditions.checkArgument(dataSource != null, "dataSource cannot be null");
Preconditions.checkArgument(contextValues != null, "contextValues cannot be null");
Preconditions.checkArgument(baseValue != null, "baseValue cannot be null");
EvaluationResult result;
try {
setLoggingContext();
LOGGER.debug("Started Evaluating with baseValue {} expression {} ", baseValue, this);
Map<String, EvaluationResult> localContextValues = new HashMap<>(contextValues);
if (!baseValue.isEmpty()) {
localContextValues.put(baseValue.getIdentifier(), baseValue);
localContextValues.put(Constants.BASE_VALUE_NAME, baseValue);
}
result = evaluateValueOfExpression(dataSource, localContextValues, baseValue);
LOGGER.debug("Completed Evaluating returned value {} ---- for expression {} ", result, this);
if (this.conditionSatisfiedState && this.isRequired() && (result == null || result.isEmpty())) {
String stringRep = this.toString();
throw new RequiredConstraintFailureException("Resource Constraint condition not satisfied for expression :" + stringRep);
} else {
return result;
}
} catch (DataExtractionException | IllegalArgumentException e) {
LOGGER.warn("Failure encountered during evaluation of expression {}", this.attr.getName());
return null;
} finally {
resetLoggingContext();
}
}
use of io.github.linuxforhealth.core.exception.DataExtractionException in project hl7v2-fhir-converter by LinuxForHealth.
the class JexlEngineUtil method evaluate.
public Object evaluate(String jexlExp, Map<String, Object> context) {
Preconditions.checkArgument(StringUtils.isNotBlank(jexlExp), "jexlExp cannot be blank");
Preconditions.checkArgument(context != null, "context cannot be null");
String trimedJexlExp = StringUtils.trim(jexlExp);
// ensure that expression
validateExpression(trimedJexlExp);
LOGGER.debug("Evaluating expression : {}", trimedJexlExp);
Map<String, Object> localContext = new HashMap<>(functions);
localContext.putAll(context);
JexlExpression exp = exprCache.get(trimedJexlExp);
if (exp == null) {
exp = jexl.createExpression(trimedJexlExp);
exprCache.put(trimedJexlExp, exp);
}
JexlContext jc = new MapContext();
localContext.entrySet().forEach(e -> jc.set(e.getKey(), e.getValue()));
// Now evaluate the expression, getting the result
try {
Object obj = exp.evaluate(jc);
LOGGER.debug("Evaluated expression : {}, returning object {}", trimedJexlExp, obj);
return obj;
} catch (JexlException e) {
throw new DataExtractionException("Exception encountered during JEXL expression evaluation", e);
}
}
Aggregations