Search in sources :

Example 1 with DataExtractionException

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;
    }
}
Also used : RequiredConstraintFailureException(io.github.linuxforhealth.core.exception.RequiredConstraintFailureException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) PendingExpressionState(io.github.linuxforhealth.hl7.resource.PendingExpressionState) DataExtractionException(io.github.linuxforhealth.core.exception.DataExtractionException) EvaluationResult(io.github.linuxforhealth.api.EvaluationResult) ResourceEvaluationResult(io.github.linuxforhealth.hl7.resource.ResourceEvaluationResult) EmptyEvaluationResult(io.github.linuxforhealth.core.expression.EmptyEvaluationResult) EmptyEvaluationResult(io.github.linuxforhealth.core.expression.EmptyEvaluationResult) ResourceEvaluationResult(io.github.linuxforhealth.hl7.resource.ResourceEvaluationResult) Expression(io.github.linuxforhealth.api.Expression) ResourceValue(io.github.linuxforhealth.api.ResourceValue) Objects(java.util.Objects)

Example 2 with DataExtractionException

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;
}
Also used : RequiredConstraintFailureException(io.github.linuxforhealth.core.exception.RequiredConstraintFailureException) ResourceResult(io.github.linuxforhealth.core.resource.ResourceResult) SimpleResourceValue(io.github.linuxforhealth.core.resource.SimpleResourceValue) DataExtractionException(io.github.linuxforhealth.core.exception.DataExtractionException)

Example 3 with DataExtractionException

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;
    }
}
Also used : RequiredConstraintFailureException(io.github.linuxforhealth.core.exception.RequiredConstraintFailureException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) DataExtractionException(io.github.linuxforhealth.core.exception.DataExtractionException) EvaluationResult(io.github.linuxforhealth.api.EvaluationResult) ResourceEvaluationResult(io.github.linuxforhealth.hl7.resource.ResourceEvaluationResult) EmptyEvaluationResult(io.github.linuxforhealth.core.expression.EmptyEvaluationResult) EmptyEvaluationResult(io.github.linuxforhealth.core.expression.EmptyEvaluationResult) ResourceEvaluationResult(io.github.linuxforhealth.hl7.resource.ResourceEvaluationResult) Expression(io.github.linuxforhealth.api.Expression) ResourceValue(io.github.linuxforhealth.api.ResourceValue) Objects(java.util.Objects)

Example 4 with DataExtractionException

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();
    }
}
Also used : RequiredConstraintFailureException(io.github.linuxforhealth.core.exception.RequiredConstraintFailureException) HashMap(java.util.HashMap) DataExtractionException(io.github.linuxforhealth.core.exception.DataExtractionException) EmptyEvaluationResult(io.github.linuxforhealth.core.expression.EmptyEvaluationResult) EvaluationResult(io.github.linuxforhealth.api.EvaluationResult)

Example 5 with DataExtractionException

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);
    }
}
Also used : HashMap(java.util.HashMap) JexlException(org.apache.commons.jexl3.JexlException) JexlContext(org.apache.commons.jexl3.JexlContext) JexlExpression(org.apache.commons.jexl3.JexlExpression) MapContext(org.apache.commons.jexl3.MapContext) DataExtractionException(io.github.linuxforhealth.core.exception.DataExtractionException)

Aggregations

DataExtractionException (io.github.linuxforhealth.core.exception.DataExtractionException)5 RequiredConstraintFailureException (io.github.linuxforhealth.core.exception.RequiredConstraintFailureException)4 HashMap (java.util.HashMap)4 EvaluationResult (io.github.linuxforhealth.api.EvaluationResult)3 EmptyEvaluationResult (io.github.linuxforhealth.core.expression.EmptyEvaluationResult)3 Expression (io.github.linuxforhealth.api.Expression)2 ResourceValue (io.github.linuxforhealth.api.ResourceValue)2 ResourceEvaluationResult (io.github.linuxforhealth.hl7.resource.ResourceEvaluationResult)2 ArrayList (java.util.ArrayList)2 Objects (java.util.Objects)2 ResourceResult (io.github.linuxforhealth.core.resource.ResourceResult)1 SimpleResourceValue (io.github.linuxforhealth.core.resource.SimpleResourceValue)1 PendingExpressionState (io.github.linuxforhealth.hl7.resource.PendingExpressionState)1 JexlContext (org.apache.commons.jexl3.JexlContext)1 JexlException (org.apache.commons.jexl3.JexlException)1 JexlExpression (org.apache.commons.jexl3.JexlExpression)1 MapContext (org.apache.commons.jexl3.MapContext)1