Search in sources :

Example 1 with RequiredConstraintFailureException

use of io.github.linuxforhealth.core.exception.RequiredConstraintFailureException 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 RequiredConstraintFailureException

use of io.github.linuxforhealth.core.exception.RequiredConstraintFailureException 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 RequiredConstraintFailureException

use of io.github.linuxforhealth.core.exception.RequiredConstraintFailureException 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 RequiredConstraintFailureException

use of io.github.linuxforhealth.core.exception.RequiredConstraintFailureException 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 RequiredConstraintFailureException

use of io.github.linuxforhealth.core.exception.RequiredConstraintFailureException in project hl7v2-fhir-converter by LinuxForHealth.

the class HL7MessageEngine method generateMultipleResources.

private static List<ResourceResult> generateMultipleResources(final HL7MessageData hl7DataInput, final ResourceModel rs, final Map<String, EvaluationResult> contextValues, final List<SegmentGroup> multipleSegments, boolean generateMultiple) {
    List<ResourceResult> resourceResults = new ArrayList<>();
    for (SegmentGroup currentGroup : multipleSegments) {
        Map<String, EvaluationResult> localContextValues = new HashMap<>(contextValues);
        localContextValues.put(Constants.GROUP_ID, EvaluationResultFactory.getEvaluationResult(currentGroup.getGroupId()));
        // Resource needs to be generated for each base value in the group
        List<EvaluationResult> baseValues = new ArrayList<>();
        currentGroup.getSegments().forEach(struct -> baseValues.add(EvaluationResultFactory.getEvaluationResult(struct)));
        localContextValues.putAll(getContextMap(currentGroup));
        for (EvaluationResult baseValue : baseValues) {
            try {
                ResourceResult result = rs.evaluate(hl7DataInput, ImmutableMap.copyOf(localContextValues), baseValue);
                if (result != null && result.getValue() != null) {
                    resourceResults.add(result);
                    if (!generateMultiple) {
                        // If only single resource needs to be generated then return.
                        return resourceResults;
                    }
                }
            } catch (RequiredConstraintFailureException | IllegalArgumentException | IllegalStateException e) {
                LOGGER.warn("generateMultipleResources - Exception encountered");
                LOGGER.debug("generateMultipleResources - Exception encountered", e);
            }
        }
    }
    return resourceResults;
}
Also used : RequiredConstraintFailureException(io.github.linuxforhealth.core.exception.RequiredConstraintFailureException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ResourceEvaluationResult(io.github.linuxforhealth.hl7.resource.ResourceEvaluationResult) SimpleEvaluationResult(io.github.linuxforhealth.core.expression.SimpleEvaluationResult) EvaluationResult(io.github.linuxforhealth.api.EvaluationResult) ResourceResult(io.github.linuxforhealth.core.resource.ResourceResult) SegmentGroup(io.github.linuxforhealth.hl7.message.util.SegmentGroup)

Aggregations

RequiredConstraintFailureException (io.github.linuxforhealth.core.exception.RequiredConstraintFailureException)5 EvaluationResult (io.github.linuxforhealth.api.EvaluationResult)4 DataExtractionException (io.github.linuxforhealth.core.exception.DataExtractionException)4 HashMap (java.util.HashMap)4 EmptyEvaluationResult (io.github.linuxforhealth.core.expression.EmptyEvaluationResult)3 ResourceEvaluationResult (io.github.linuxforhealth.hl7.resource.ResourceEvaluationResult)3 ArrayList (java.util.ArrayList)3 Expression (io.github.linuxforhealth.api.Expression)2 ResourceValue (io.github.linuxforhealth.api.ResourceValue)2 ResourceResult (io.github.linuxforhealth.core.resource.ResourceResult)2 Objects (java.util.Objects)2 SimpleEvaluationResult (io.github.linuxforhealth.core.expression.SimpleEvaluationResult)1 SimpleResourceValue (io.github.linuxforhealth.core.resource.SimpleResourceValue)1 SegmentGroup (io.github.linuxforhealth.hl7.message.util.SegmentGroup)1 PendingExpressionState (io.github.linuxforhealth.hl7.resource.PendingExpressionState)1