Search in sources :

Example 6 with EvaluationResult

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

the class ResourceExpressionTest method test_organization_creation_with_mo_missing_value.

@Test
void test_organization_creation_with_mo_missing_value() throws IOException {
    String message = "MSH|^~\\&|MYEHR2.5|RI88140101|KIDSNET_IFL|RIHEALTH|20130531||VXU^V04^VXU_V04|20130531RI881401010105|P|2.5.1|||NE|AL||||||RI543763\r" + "PID|1||432155^^^^MR||Patient^Johnny^New^^^^L|Smith^Sally|20130414|M||2106-3^White^HL70005|123 Any St^^Somewhere^WI^54000^^M\r" + "NK1|1|Patient^Sally|MTH^mother^HL70063|123 Any St^^Somewhere^WI^54000^^M|^PRN^PH^^^608^5551212|||||||||||19820517||||eng^English^ISO639\r" + "ORC|RE||197027|||||||^Clerk^Myron||MD67895^Pediatric^MARY^^^^MD^^RIA|||||RI2050\r" + "RXA|0|1|20130531|20130531|48^HIB PRP-T^CVX|0.5|ML^^ISO+||00^new immunization record^NIP001|^Sticker^Nurse|^^^RI2050||||33k2a|20131210|PMC^sanofi^MVX|||CP|A\r" + "RXR|C28161^IM^NCIT^IM^INTRAMUSCULAR^HL70162|RT^right thigh^HL70163\r" + "OBX|1|CE|64994-7^vaccine fund pgm elig cat^LN|1|V02^VFC eligible Medicaid/MedicaidManaged Care^HL70064||||||F|||20130531|||VXC40^per imm^CDCPHINVS\r" + "OBX|2|CE|30956-7^Vaccine Type^LN|2|48^HIB PRP-T^CVX||||||F|||20130531\r" + "OBX|3|TS|29768-9^VIS Publication Date^LN|2|19981216||||||F|||20130531\r" + "OBX|4|TS|59785-6^VIS Presentation Date^LN|2|20130531||||||F|||20130531\r" + "OBX|5|ST|48767-8^Annotation^LN|2|Some text from doctor||||||F|||20130531\r";
    Message hl7message = getMessage(message);
    HL7DataExtractor hl7DTE = new HL7DataExtractor(hl7message);
    Structure s = hl7DTE.getAllStructures("ORDER", 0, "RXA").getValue();
    ExpressionAttributes attr = new ExpressionAttributes.Builder().withSpecs("RXA.17").withValueOf("resource/Organization").withGenerateList(true).build();
    ResourceExpression exp = new ResourceExpression(attr);
    assertThat(exp.getData()).isNotNull();
    Map<String, EvaluationResult> context = new HashMap<>();
    EvaluationResult value = exp.evaluate(new HL7MessageData(hl7DTE), ImmutableMap.copyOf(context), new SimpleEvaluationResult(s));
    List<Map<String, Object>> result = (List<Map<String, Object>>) value.getValue();
    assertThat(result.get(0)).containsEntry("name", "sanofi");
    List<Map<String, Object>> identifiers = (List<Map<String, Object>>) result.get(0).get("identifier");
    assertThat(identifiers.get(0)).containsEntry("value", "PMC");
    LOGGER.debug("result=" + result);
}
Also used : Message(ca.uhn.hl7v2.model.Message) HL7DataExtractor(io.github.linuxforhealth.hl7.parsing.HL7DataExtractor) HashMap(java.util.HashMap) EvaluationResult(io.github.linuxforhealth.api.EvaluationResult) SimpleEvaluationResult(io.github.linuxforhealth.core.expression.SimpleEvaluationResult) HL7MessageData(io.github.linuxforhealth.hl7.message.HL7MessageData) SimpleEvaluationResult(io.github.linuxforhealth.core.expression.SimpleEvaluationResult) List(java.util.List) Structure(ca.uhn.hl7v2.model.Structure) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 7 with EvaluationResult

use of io.github.linuxforhealth.api.EvaluationResult 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 8 with EvaluationResult

use of io.github.linuxforhealth.api.EvaluationResult 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 9 with EvaluationResult

use of io.github.linuxforhealth.api.EvaluationResult 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 10 with EvaluationResult

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

Aggregations

EvaluationResult (io.github.linuxforhealth.api.EvaluationResult)46 HashMap (java.util.HashMap)36 SimpleEvaluationResult (io.github.linuxforhealth.core.expression.SimpleEvaluationResult)30 Test (org.junit.jupiter.api.Test)26 Message (ca.uhn.hl7v2.model.Message)19 EmptyEvaluationResult (io.github.linuxforhealth.core.expression.EmptyEvaluationResult)19 HL7MessageData (io.github.linuxforhealth.hl7.message.HL7MessageData)19 HL7DataExtractor (io.github.linuxforhealth.hl7.parsing.HL7DataExtractor)19 Structure (ca.uhn.hl7v2.model.Structure)18 List (java.util.List)11 ImmutableMap (com.google.common.collect.ImmutableMap)10 ArrayList (java.util.ArrayList)10 Map (java.util.Map)10 ResourceValue (io.github.linuxforhealth.api.ResourceValue)6 RequiredConstraintFailureException (io.github.linuxforhealth.core.exception.RequiredConstraintFailureException)5 ResourceEvaluationResult (io.github.linuxforhealth.hl7.resource.ResourceEvaluationResult)5 ResourceResult (io.github.linuxforhealth.core.resource.ResourceResult)4 SimpleCode (io.github.linuxforhealth.core.terminology.SimpleCode)4 DataExtractionException (io.github.linuxforhealth.core.exception.DataExtractionException)3 Expression (io.github.linuxforhealth.api.Expression)2