Search in sources :

Example 1 with EmptyEvaluationResult

use of io.github.linuxforhealth.core.expression.EmptyEvaluationResult in project hl7v2-fhir-converter by LinuxForHealth.

the class SimpleExpressionTest method test_variable_no_context.

@Test
void test_variable_no_context() {
    ExpressionAttributes attr = new ExpressionAttributes.Builder().withValueOf("$var1").build();
    SimpleExpression exp = new SimpleExpression(attr);
    Map<String, EvaluationResult> context = new HashMap<>();
    EvaluationResult value = exp.evaluate(data, ImmutableMap.copyOf(context), new EmptyEvaluationResult());
    assertThat(value).isNull();
}
Also used : HashMap(java.util.HashMap) EvaluationResult(io.github.linuxforhealth.api.EvaluationResult) SimpleEvaluationResult(io.github.linuxforhealth.core.expression.SimpleEvaluationResult) EmptyEvaluationResult(io.github.linuxforhealth.core.expression.EmptyEvaluationResult) EmptyEvaluationResult(io.github.linuxforhealth.core.expression.EmptyEvaluationResult) Test(org.junit.jupiter.api.Test)

Example 2 with EmptyEvaluationResult

use of io.github.linuxforhealth.core.expression.EmptyEvaluationResult in project hl7v2-fhir-converter by LinuxForHealth.

the class SimpleExpressionTest method test_constant.

@Test
void test_constant() {
    ExpressionAttributes attr = new ExpressionAttributes.Builder().withValue(SOME_VALUE).build();
    SimpleExpression exp = new SimpleExpression(attr);
    Map<String, EvaluationResult> context = new HashMap<>();
    EvaluationResult value = exp.evaluate(data, ImmutableMap.copyOf(context), new EmptyEvaluationResult());
    assertThat((String) value.getValue()).isEqualTo(SOME_VALUE);
}
Also used : HashMap(java.util.HashMap) EvaluationResult(io.github.linuxforhealth.api.EvaluationResult) SimpleEvaluationResult(io.github.linuxforhealth.core.expression.SimpleEvaluationResult) EmptyEvaluationResult(io.github.linuxforhealth.core.expression.EmptyEvaluationResult) EmptyEvaluationResult(io.github.linuxforhealth.core.expression.EmptyEvaluationResult) Test(org.junit.jupiter.api.Test)

Example 3 with EmptyEvaluationResult

use of io.github.linuxforhealth.core.expression.EmptyEvaluationResult 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 4 with EmptyEvaluationResult

use of io.github.linuxforhealth.core.expression.EmptyEvaluationResult 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 5 with EmptyEvaluationResult

use of io.github.linuxforhealth.core.expression.EmptyEvaluationResult in project hl7v2-fhir-converter by LinuxForHealth.

the class ExpressionVariable method extractVariableValue.

// resolve variable value
@Override
public EvaluationResult extractVariableValue(Map<String, EvaluationResult> contextValues, InputDataExtractor dataSource) {
    EvaluationResult result = null;
    if (!this.getSpec().isEmpty()) {
        result = getValueFromSpecs(contextValues, dataSource);
    }
    if (result == null) {
        result = new EmptyEvaluationResult();
    }
    if (this.expression != null) {
        // resolve expression
        Map<String, EvaluationResult> localContextValues = new HashMap<>(contextValues);
        localContextValues.put(this.getName(), result);
        result = dataSource.evaluateJexlExpression(expression, localContextValues);
    }
    return result;
}
Also used : HashMap(java.util.HashMap) EvaluationResult(io.github.linuxforhealth.api.EvaluationResult) EmptyEvaluationResult(io.github.linuxforhealth.core.expression.EmptyEvaluationResult) EmptyEvaluationResult(io.github.linuxforhealth.core.expression.EmptyEvaluationResult)

Aggregations

EmptyEvaluationResult (io.github.linuxforhealth.core.expression.EmptyEvaluationResult)15 EvaluationResult (io.github.linuxforhealth.api.EvaluationResult)14 HashMap (java.util.HashMap)14 SimpleEvaluationResult (io.github.linuxforhealth.core.expression.SimpleEvaluationResult)10 Test (org.junit.jupiter.api.Test)9 ResourceValue (io.github.linuxforhealth.api.ResourceValue)3 Message (ca.uhn.hl7v2.model.Message)2 Expression (io.github.linuxforhealth.api.Expression)2 DataExtractionException (io.github.linuxforhealth.core.exception.DataExtractionException)2 RequiredConstraintFailureException (io.github.linuxforhealth.core.exception.RequiredConstraintFailureException)2 HL7MessageData (io.github.linuxforhealth.hl7.message.HL7MessageData)2 HL7DataExtractor (io.github.linuxforhealth.hl7.parsing.HL7DataExtractor)2 ResourceEvaluationResult (io.github.linuxforhealth.hl7.resource.ResourceEvaluationResult)2 ArrayList (java.util.ArrayList)2 Objects (java.util.Objects)2 CX (ca.uhn.hl7v2.model.v26.datatype.CX)1 Variable (io.github.linuxforhealth.api.Variable)1 PendingExpressionState (io.github.linuxforhealth.hl7.resource.PendingExpressionState)1 Map (java.util.Map)1