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;
}
}
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);
}
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;
}
}
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;
}
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;
}
Aggregations