Search in sources :

Example 1 with Expression

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

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

the class HL7DataBasedResourceDeserializer method deserialize.

@Override
public HL7DataBasedResourceModel deserialize(JsonParser jsonParser, DeserializationContext ctxt) throws IOException {
    ObjectNode node = jsonParser.getCodec().readTree(jsonParser);
    JsonNode hl7PrefixNode = node.get(SPEC);
    String hl7Prefix = null;
    if (hl7PrefixNode != null) {
        hl7Prefix = hl7PrefixNode.toString();
    }
    // generate expressions from the resource YAML
    Map<String, Expression> expressions = generateExpressions(node);
    JsonNode namenode = node.get(RESOURCE_TYPE_FIELD_NAME);
    String name = String.valueOf(ctxt.findInjectableValue("resourceName", null, null));
    if (namenode != null) {
        name = namenode.textValue();
    }
    // And is base FHIR resource like Patient and Organization. Not a datatype/? or reference/? etc.
    if (!expressions.isEmpty() && name.indexOf('/') == -1) {
        LOGGER.debug("Adding common expressions to the list of expressions for {}", name);
        expressions.putAll(getCommonExpressions());
    }
    return new HL7DataBasedResourceModel(name, expressions, hl7Prefix);
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Expression(io.github.linuxforhealth.api.Expression) JsonNode(com.fasterxml.jackson.databind.JsonNode) HL7DataBasedResourceModel(io.github.linuxforhealth.hl7.resource.HL7DataBasedResourceModel)

Example 3 with Expression

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

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

the class HL7DataBasedResourceDeserializer method generateExpressions.

private static Map<String, Expression> generateExpressions(JsonNode node) {
    Map<String, Expression> expressions = new HashMap<>();
    Iterator<Entry<String, JsonNode>> iter = node.fields();
    while (iter.hasNext()) {
        Entry<String, JsonNode> entry = iter.next();
        Expression e;
        LOGGER.debug("deserealizing {}", entry);
        ExpressionAttributes expAttr = MAPPER.convertValue(entry.getValue(), ExpressionAttributes.class);
        if (expAttr != null && expAttr.getExpressionType() != null) {
            expAttr.setName(entry.getKey());
            try {
                e = generateExpression(expAttr);
            } catch (IllegalStateException e1) {
                LOGGER.error("deserialization failure expression type {}", expAttr.getExpressionType());
                LOGGER.debug("deserialization failure expression type {}", expAttr.getExpressionType(), e1);
                e = null;
            }
            if (e != null) {
                expressions.put(entry.getKey(), e);
            }
            LOGGER.debug("deserialized {} expression type {}", entry, e);
        }
    }
    return expressions;
}
Also used : Entry(java.util.Map.Entry) ExpressionAttributes(io.github.linuxforhealth.hl7.expression.ExpressionAttributes) Expression(io.github.linuxforhealth.api.Expression) HashMap(java.util.HashMap) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 5 with Expression

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

the class HL7DataBasedResourceDeserializer method getCommonExpressions.

// Reads the resource/Common.yml and generates expressions from it.
private static synchronized Map<String, Expression> getCommonExpressions() throws JsonProcessingException {
    if (commonExpressions == null) {
        // generate the common expressions from the Common YAML file.
        commonExpressions = new HashMap<>();
        String path = ResourceReader.getInstance().getResource(Constants.HL7_BASE_PATH + Constants.COMMON_RESOURCE_PATH);
        JsonNode node = ObjectMapperUtil.getYAMLInstance().readTree(path);
        Map<String, Expression> expressions = generateExpressions(node);
        commonExpressions.putAll(expressions);
    }
    return commonExpressions;
}
Also used : Expression(io.github.linuxforhealth.api.Expression) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Aggregations

Expression (io.github.linuxforhealth.api.Expression)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 HashMap (java.util.HashMap)3 EvaluationResult (io.github.linuxforhealth.api.EvaluationResult)2 ResourceValue (io.github.linuxforhealth.api.ResourceValue)2 DataExtractionException (io.github.linuxforhealth.core.exception.DataExtractionException)2 RequiredConstraintFailureException (io.github.linuxforhealth.core.exception.RequiredConstraintFailureException)2 EmptyEvaluationResult (io.github.linuxforhealth.core.expression.EmptyEvaluationResult)2 ResourceEvaluationResult (io.github.linuxforhealth.hl7.resource.ResourceEvaluationResult)2 ArrayList (java.util.ArrayList)2 Objects (java.util.Objects)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 ExpressionAttributes (io.github.linuxforhealth.hl7.expression.ExpressionAttributes)1 HL7DataBasedResourceModel (io.github.linuxforhealth.hl7.resource.HL7DataBasedResourceModel)1 PendingExpressionState (io.github.linuxforhealth.hl7.resource.PendingExpressionState)1 Entry (java.util.Map.Entry)1