Search in sources :

Example 1 with ResourceEvaluationResult

use of io.github.linuxforhealth.hl7.resource.ResourceEvaluationResult 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 ResourceEvaluationResult

use of io.github.linuxforhealth.hl7.resource.ResourceEvaluationResult 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 3 with ResourceEvaluationResult

use of io.github.linuxforhealth.hl7.resource.ResourceEvaluationResult in project hl7v2-fhir-converter by LinuxForHealth.

the class HL7MessageEngine method transform.

/**
 * Converts a HL7 message to a FHIR bundle with the list of resources specified
 *
 * @see io.github.linuxforhealth.api.MessageEngine#transform(io.github.linuxforhealth.api.InputDataExtractor,
 *      java.lang.Iterable, java.util.Map)
 */
@Override
public Bundle transform(final InputDataExtractor dataInput, final Iterable<FHIRResourceTemplate> resources, final Map<String, EvaluationResult> contextValues) {
    Preconditions.checkArgument(dataInput != null, "dataInput cannot be null");
    Preconditions.checkArgument(contextValues != null, "contextValues cannot be null");
    Preconditions.checkArgument(resources != null, "resources cannot be null");
    HL7MessageData hl7DataInput = (HL7MessageData) dataInput;
    Bundle bundle = initBundle();
    Map<String, EvaluationResult> localContextValues = new HashMap<>(contextValues);
    // Add run-time properties to localContextVariables
    for (Map.Entry<String, String> entry : getFHIRContext().getProperties().entrySet()) {
        localContextValues.put(entry.getKey(), new SimpleEvaluationResult<String>(entry.getValue()));
    }
    // Add the ZoneId runtime property to localContextVariables
    // If it is not passed in (null), use "" (empty) to satisfy parser null checks in .yml parsing
    String zoneIdText = getFHIRContext().getZoneIdText() != null ? getFHIRContext().getZoneIdText() : "";
    localContextValues.put("ZONEID", new SimpleEvaluationResult<String>(zoneIdText));
    List<ResourceResult> resourceResultsWithEvalLater = new ArrayList<>();
    for (FHIRResourceTemplate genericTemplate : resources) {
        HL7FHIRResourceTemplate hl7ResourceTemplate = (HL7FHIRResourceTemplate) genericTemplate;
        ResourceModel rs = genericTemplate.getResource();
        List<ResourceResult> resourceResults = new ArrayList<>();
        try {
            MDC.put(RESOURCE, rs.getName());
            List<ResourceResult> results = generateResources(hl7DataInput, hl7ResourceTemplate, localContextValues);
            if (results != null) {
                resourceResults.addAll(results);
                results.stream().filter(r -> (r.getPendingExpressions() != null && !r.getPendingExpressions().isEmpty())).forEach(re -> resourceResultsWithEvalLater.add(re));
                List<ResourceResult> resultsToAddToBundle = results.stream().filter(r -> (r.getPendingExpressions() == null || r.getPendingExpressions().isEmpty())).collect(Collectors.toList());
                addResourceToBundle(bundle, resultsToAddToBundle);
            }
            resourceResults.removeIf(isEmpty());
            Map<String, EvaluationResult> newContextValues = getContextValuesFromResource(hl7ResourceTemplate, resourceResults);
            localContextValues.putAll(newContextValues);
        } catch (IllegalArgumentException | IllegalStateException e) {
            LOGGER.error("Exception during resource {} generation", rs.getName());
            LOGGER.debug("Exception during resource {} generation", rs.getName(), e);
        } finally {
            MDC.remove(RESOURCE);
        }
    }
    for (ResourceResult r : resourceResultsWithEvalLater) {
        MDC.put(RESOURCE, "PendingExpressions");
        try {
            Map<String, EvaluationResult> primaryContextValues = new HashMap<>(localContextValues);
            r.getPendingExpressions().getContextValues().entrySet().forEach(e -> primaryContextValues.putIfAbsent(e.getKey(), e.getValue()));
            ResourceEvaluationResult res = ExpressionUtility.evaluate(hl7DataInput, primaryContextValues, r.getPendingExpressions().getExpressions());
            Map<String, Object> resolvedValues = new HashMap<>();
            resolvedValues.putAll(r.getValue().getResource());
            resolvedValues.putAll(res.getResolveValues());
            List<ResourceValue> additionalResources = new ArrayList<>();
            additionalResources.addAll(r.getAdditionalResources());
            additionalResources.addAll(res.getAdditionalResolveValues());
            ResourceResult updatedResourceResult = new ResourceResult(new SimpleResourceValue(resolvedValues, r.getValue().getFHIRResourceType()), additionalResources, r.getGroupId());
            addResourceToBundle(bundle, Lists.newArrayList(updatedResourceResult));
        } catch (IllegalArgumentException | IllegalStateException e) {
            LOGGER.error("Exception during resource PendingExpressions generation");
            LOGGER.debug("Exception during resource PendingExpressions generation", e);
        } finally {
            MDC.remove(RESOURCE);
        }
    }
    LOGGER.info("Successfully converted message");
    LOGGER.debug("Successfully converted Message: {} , Message Control Id: {} to FHIR bundle resource with id {}", dataInput.getName(), dataInput.getId(), bundle.getId());
    return bundle;
}
Also used : SegmentGroup(io.github.linuxforhealth.hl7.message.util.SegmentGroup) ResourceEvaluationResult(io.github.linuxforhealth.hl7.resource.ResourceEvaluationResult) Constants(io.github.linuxforhealth.core.Constants) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) SimpleEvaluationResult(io.github.linuxforhealth.core.expression.SimpleEvaluationResult) FHIRResourceTemplate(io.github.linuxforhealth.api.FHIRResourceTemplate) ArrayList(java.util.ArrayList) Lists(com.google.common.collect.Lists) ExpressionUtility(io.github.linuxforhealth.hl7.util.ExpressionUtility) Map(java.util.Map) InputDataExtractor(io.github.linuxforhealth.api.InputDataExtractor) ResourceResult(io.github.linuxforhealth.core.resource.ResourceResult) Meta(org.hl7.fhir.r4.model.Meta) EvaluationResult(io.github.linuxforhealth.api.EvaluationResult) ObjectMapperUtil(io.github.linuxforhealth.core.ObjectMapperUtil) Logger(org.slf4j.Logger) ImmutableMap(com.google.common.collect.ImmutableMap) ResourceValue(io.github.linuxforhealth.api.ResourceValue) Predicate(java.util.function.Predicate) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Structure(ca.uhn.hl7v2.model.Structure) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) UUID(java.util.UUID) LocalDateTime(org.joda.time.LocalDateTime) Collectors(java.util.stream.Collectors) SimpleResourceValue(io.github.linuxforhealth.core.resource.SimpleResourceValue) SegmentExtractorUtil(io.github.linuxforhealth.hl7.message.util.SegmentExtractorUtil) List(java.util.List) MDC(org.slf4j.MDC) EvaluationResultFactory(io.github.linuxforhealth.core.expression.EvaluationResultFactory) Entry(java.util.Map.Entry) Bundle(org.hl7.fhir.r4.model.Bundle) Preconditions(com.google.common.base.Preconditions) ResourceModel(io.github.linuxforhealth.api.ResourceModel) BundleType(org.hl7.fhir.r4.model.Bundle.BundleType) MessageEngine(io.github.linuxforhealth.api.MessageEngine) RequiredConstraintFailureException(io.github.linuxforhealth.core.exception.RequiredConstraintFailureException) FHIRContext(io.github.linuxforhealth.fhir.FHIRContext) FHIRResourceMapper(io.github.linuxforhealth.fhir.FHIRResourceMapper) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SimpleResourceValue(io.github.linuxforhealth.core.resource.SimpleResourceValue) ResourceValue(io.github.linuxforhealth.api.ResourceValue) SimpleResourceValue(io.github.linuxforhealth.core.resource.SimpleResourceValue) ResourceModel(io.github.linuxforhealth.api.ResourceModel) FHIRResourceTemplate(io.github.linuxforhealth.api.FHIRResourceTemplate) Bundle(org.hl7.fhir.r4.model.Bundle) 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) ResourceEvaluationResult(io.github.linuxforhealth.hl7.resource.ResourceEvaluationResult) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 4 with ResourceEvaluationResult

use of io.github.linuxforhealth.hl7.resource.ResourceEvaluationResult in project hl7v2-fhir-converter by LinuxForHealth.

the class NestedExpression method evaluateExpression.

@Override
protected EvaluationResult evaluateExpression(InputDataExtractor dataSource, Map<String, EvaluationResult> contextValues, EvaluationResult baseValue) {
    LOGGER.info("Evalutating expression");
    LOGGER.debug("Evaluating child expressions {}", this.getExpressionAttr().getName());
    ResourceEvaluationResult result = ExpressionUtility.evaluate(dataSource, contextValues, baseValue, this.childexpressions);
    if (result == null || result.getResolveValues() == null || result.getResolveValues().isEmpty()) {
        return EvaluationResultFactory.getEvaluationResult(null);
    } else {
        if (this.generateMap) {
            return EvaluationResultFactory.getEvaluationResult(result.getResolveValues(), result.getAdditionalResolveValues());
        } else {
            List<Object> values = new ArrayList<>();
            for (Object o : result.getResolveValues().values()) {
                if (o instanceof Collection) {
                    values.addAll((Collection) o);
                } else {
                    values.add(o);
                }
            }
            return EvaluationResultFactory.getEvaluationResult(values, result.getAdditionalResolveValues());
        }
    }
}
Also used : ResourceEvaluationResult(io.github.linuxforhealth.hl7.resource.ResourceEvaluationResult) ArrayList(java.util.ArrayList) Collection(java.util.Collection)

Aggregations

ResourceEvaluationResult (io.github.linuxforhealth.hl7.resource.ResourceEvaluationResult)4 ArrayList (java.util.ArrayList)4 EvaluationResult (io.github.linuxforhealth.api.EvaluationResult)3 ResourceValue (io.github.linuxforhealth.api.ResourceValue)3 RequiredConstraintFailureException (io.github.linuxforhealth.core.exception.RequiredConstraintFailureException)3 HashMap (java.util.HashMap)3 Expression (io.github.linuxforhealth.api.Expression)2 DataExtractionException (io.github.linuxforhealth.core.exception.DataExtractionException)2 EmptyEvaluationResult (io.github.linuxforhealth.core.expression.EmptyEvaluationResult)2 Objects (java.util.Objects)2 Structure (ca.uhn.hl7v2.model.Structure)1 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Preconditions (com.google.common.base.Preconditions)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Lists (com.google.common.collect.Lists)1 FHIRResourceTemplate (io.github.linuxforhealth.api.FHIRResourceTemplate)1 InputDataExtractor (io.github.linuxforhealth.api.InputDataExtractor)1 MessageEngine (io.github.linuxforhealth.api.MessageEngine)1 ResourceModel (io.github.linuxforhealth.api.ResourceModel)1