Search in sources :

Example 1 with ResourceValue

use of io.github.linuxforhealth.api.ResourceValue 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 ResourceValue

use of io.github.linuxforhealth.api.ResourceValue 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 ResourceValue

use of io.github.linuxforhealth.api.ResourceValue in project hl7v2-fhir-converter by LinuxForHealth.

the class AbstractExpression method evaluateValueOfExpression.

private EvaluationResult evaluateValueOfExpression(InputDataExtractor dataSource, Map<String, EvaluationResult> localContextValues, EvaluationResult baseinputValue) {
    /**
     * Steps:
     * <ul>
     * <li>Add all constants to the context map</li>
     * <li>Evaluate the specs</li>
     * <li>Evaluate the variables</li>
     * <li>Apply the condition</li>
     * <li>If condition is satisfies then evaluate the valueOf/value attribute.</li>
     * </ul>
     * Note: If generateMultiple is set that all the spec values are used for generating list value
     * from the expression.
     */
    // Add constants to the context map
    this.attr.getConstants().entrySet().forEach(e -> localContextValues.put(e.getKey(), EvaluationResultFactory.getEvaluationResult(e.getValue())));
    List<Object> result = new ArrayList<>();
    List<ResourceValue> additionalresourcesresult = new ArrayList<>();
    List<Object> baseSpecvalues = getSpecValues(dataSource, localContextValues, baseinputValue, this.getspecs());
    LOGGER.debug("Base values evaluated {} -----  values {} ", this, baseSpecvalues);
    if (!baseSpecvalues.isEmpty()) {
        for (Object o : baseSpecvalues) {
            Map<String, EvaluationResult> localContextValuesSpec = new HashMap<>(localContextValues);
            localContextValuesSpec.put(Constants.BASE_VALUE_NAME, EvaluationResultFactory.getEvaluationResult(o));
            EvaluationResult gen = generateValue(dataSource, localContextValuesSpec, EvaluationResultFactory.getEvaluationResult(o));
            if (gen != null && gen.getValue() != null && !gen.isEmpty()) {
                if (gen.getValue() instanceof List) {
                    result.addAll(gen.getValue());
                } else {
                    result.add(gen.getValue());
                }
                additionalresourcesresult.addAll(gen.getAdditionalResources());
            }
            if (!this.attr.isGenerateMultiple() && !result.isEmpty()) {
                break;
            }
        }
    } else {
        EvaluationResult gen = generateValue(dataSource, localContextValues, baseinputValue);
        if (gen != null && gen.getValue() != null && !gen.isEmpty()) {
            if (gen.getValue() instanceof List) {
                result.addAll(gen.getValue());
            } else {
                result.add(gen.getValue());
            }
            additionalresourcesresult.addAll(gen.getAdditionalResources());
        }
    }
    return getResult(result, additionalresourcesresult);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ResourceValue(io.github.linuxforhealth.api.ResourceValue) ArrayList(java.util.ArrayList) List(java.util.List) EmptyEvaluationResult(io.github.linuxforhealth.core.expression.EmptyEvaluationResult) EvaluationResult(io.github.linuxforhealth.api.EvaluationResult)

Example 4 with ResourceValue

use of io.github.linuxforhealth.api.ResourceValue in project hl7v2-fhir-converter by LinuxForHealth.

the class ReferenceExpression method evaluateExpression.

@Override
public EvaluationResult evaluateExpression(InputDataExtractor dataSource, Map<String, EvaluationResult> contextValues, EvaluationResult baseValue) {
    Preconditions.checkArgument(dataSource != null, "dataSource cannot be null");
    Preconditions.checkArgument(contextValues != null, "contextValues cannot be null");
    LOGGER.debug("Evaluating expression {}", this.reference);
    EvaluationResult resourceReferenceResult = null;
    // Evaluate the resource first and add it to the list of additional resources generated
    ResourceResult primaryResourceResult = evaluateResource(dataSource, contextValues, baseValue);
    // If the primary resource is generated then create the reference
    if (primaryResourceResult != null && primaryResourceResult.getValue() != null) {
        List<ResourceValue> additionalResources = new ArrayList<>();
        additionalResources.addAll(primaryResourceResult.getAdditionalResources());
        additionalResources.add(primaryResourceResult.getValue());
        EvaluationResult genBaseValue = EvaluationResultFactory.getEvaluationResult(primaryResourceResult.getValue().getResource());
        Map<String, EvaluationResult> localContextValues = new HashMap<>(contextValues);
        ResourceResult result = this.referenceModel.evaluate(dataSource, ImmutableMap.copyOf(localContextValues), genBaseValue);
        if (result != null && result.getValue() != null) {
            ResourceValue resolvedvalues = result.getValue();
            LOGGER.debug("Evaluated expression {}, returning {} ", this.reference, resolvedvalues);
            if (resolvedvalues != null) {
                resourceReferenceResult = EvaluationResultFactory.getEvaluationResult(resolvedvalues.getResource(), additionalResources);
            }
        }
    }
    return resourceReferenceResult;
}
Also used : ResourceResult(io.github.linuxforhealth.core.resource.ResourceResult) HashMap(java.util.HashMap) ResourceValue(io.github.linuxforhealth.api.ResourceValue) ArrayList(java.util.ArrayList) EvaluationResult(io.github.linuxforhealth.api.EvaluationResult)

Example 5 with ResourceValue

use of io.github.linuxforhealth.api.ResourceValue in project hl7v2-fhir-converter by LinuxForHealth.

the class ResourceExpression method evaluateExpression.

@Override
public EvaluationResult evaluateExpression(InputDataExtractor dataSource, Map<String, EvaluationResult> contextValues, EvaluationResult baseValue) {
    Preconditions.checkArgument(dataSource != null, "dataSource cannot be null");
    Preconditions.checkArgument(contextValues != null, "contextValues cannot be null");
    LOGGER.debug("Evaluating expression {}", this.resourceToGenerate);
    EvaluationResult evaluationResult = null;
    ResourceResult result = this.data.evaluate(dataSource, ImmutableMap.copyOf(contextValues), baseValue);
    if (result != null && result.getValue() != null) {
        ResourceValue resolvedvalues = result.getValue();
        LOGGER.debug("Evaluated expression {}, returning {} ", this.resourceToGenerate, resolvedvalues);
        if (resolvedvalues != null) {
            evaluationResult = EvaluationResultFactory.getEvaluationResult(resolvedvalues.getResource(), result.getAdditionalResources());
        }
    }
    return evaluationResult;
}
Also used : ResourceResult(io.github.linuxforhealth.core.resource.ResourceResult) ResourceValue(io.github.linuxforhealth.api.ResourceValue) EvaluationResult(io.github.linuxforhealth.api.EvaluationResult)

Aggregations

ResourceValue (io.github.linuxforhealth.api.ResourceValue)8 EvaluationResult (io.github.linuxforhealth.api.EvaluationResult)6 HashMap (java.util.HashMap)6 ArrayList (java.util.ArrayList)5 EmptyEvaluationResult (io.github.linuxforhealth.core.expression.EmptyEvaluationResult)4 RequiredConstraintFailureException (io.github.linuxforhealth.core.exception.RequiredConstraintFailureException)3 ResourceResult (io.github.linuxforhealth.core.resource.ResourceResult)3 ResourceEvaluationResult (io.github.linuxforhealth.hl7.resource.ResourceEvaluationResult)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 Expression (io.github.linuxforhealth.api.Expression)2 DataExtractionException (io.github.linuxforhealth.core.exception.DataExtractionException)2 List (java.util.List)2 Map (java.util.Map)2 Objects (java.util.Objects)2 Structure (ca.uhn.hl7v2.model.Structure)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