use of io.github.linuxforhealth.hl7.resource.ResourceEvaluationResult 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.hl7.resource.ResourceEvaluationResult 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.hl7.resource.ResourceEvaluationResult in project hl7v2-fhir-converter by LinuxForHealth.
the class HL7MessageEngine method transform.
/**
* Converts a HL7 message to a FHIR bundle with the list of resources specified
*
* @see io.github.linuxforhealth.api.MessageEngine#transform(io.github.linuxforhealth.api.InputDataExtractor,
* java.lang.Iterable, java.util.Map)
*/
@Override
public Bundle transform(final InputDataExtractor dataInput, final Iterable<FHIRResourceTemplate> resources, final Map<String, EvaluationResult> contextValues) {
Preconditions.checkArgument(dataInput != null, "dataInput cannot be null");
Preconditions.checkArgument(contextValues != null, "contextValues cannot be null");
Preconditions.checkArgument(resources != null, "resources cannot be null");
HL7MessageData hl7DataInput = (HL7MessageData) dataInput;
Bundle bundle = initBundle();
Map<String, EvaluationResult> localContextValues = new HashMap<>(contextValues);
// Add run-time properties to localContextVariables
for (Map.Entry<String, String> entry : getFHIRContext().getProperties().entrySet()) {
localContextValues.put(entry.getKey(), new SimpleEvaluationResult<String>(entry.getValue()));
}
// Add the ZoneId runtime property to localContextVariables
// If it is not passed in (null), use "" (empty) to satisfy parser null checks in .yml parsing
String zoneIdText = getFHIRContext().getZoneIdText() != null ? getFHIRContext().getZoneIdText() : "";
localContextValues.put("ZONEID", new SimpleEvaluationResult<String>(zoneIdText));
List<ResourceResult> resourceResultsWithEvalLater = new ArrayList<>();
for (FHIRResourceTemplate genericTemplate : resources) {
HL7FHIRResourceTemplate hl7ResourceTemplate = (HL7FHIRResourceTemplate) genericTemplate;
ResourceModel rs = genericTemplate.getResource();
List<ResourceResult> resourceResults = new ArrayList<>();
try {
MDC.put(RESOURCE, rs.getName());
List<ResourceResult> results = generateResources(hl7DataInput, hl7ResourceTemplate, localContextValues);
if (results != null) {
resourceResults.addAll(results);
results.stream().filter(r -> (r.getPendingExpressions() != null && !r.getPendingExpressions().isEmpty())).forEach(re -> resourceResultsWithEvalLater.add(re));
List<ResourceResult> resultsToAddToBundle = results.stream().filter(r -> (r.getPendingExpressions() == null || r.getPendingExpressions().isEmpty())).collect(Collectors.toList());
addResourceToBundle(bundle, resultsToAddToBundle);
}
resourceResults.removeIf(isEmpty());
Map<String, EvaluationResult> newContextValues = getContextValuesFromResource(hl7ResourceTemplate, resourceResults);
localContextValues.putAll(newContextValues);
} catch (IllegalArgumentException | IllegalStateException e) {
LOGGER.error("Exception during resource {} generation", rs.getName());
LOGGER.debug("Exception during resource {} generation", rs.getName(), e);
} finally {
MDC.remove(RESOURCE);
}
}
for (ResourceResult r : resourceResultsWithEvalLater) {
MDC.put(RESOURCE, "PendingExpressions");
try {
Map<String, EvaluationResult> primaryContextValues = new HashMap<>(localContextValues);
r.getPendingExpressions().getContextValues().entrySet().forEach(e -> primaryContextValues.putIfAbsent(e.getKey(), e.getValue()));
ResourceEvaluationResult res = ExpressionUtility.evaluate(hl7DataInput, primaryContextValues, r.getPendingExpressions().getExpressions());
Map<String, Object> resolvedValues = new HashMap<>();
resolvedValues.putAll(r.getValue().getResource());
resolvedValues.putAll(res.getResolveValues());
List<ResourceValue> additionalResources = new ArrayList<>();
additionalResources.addAll(r.getAdditionalResources());
additionalResources.addAll(res.getAdditionalResolveValues());
ResourceResult updatedResourceResult = new ResourceResult(new SimpleResourceValue(resolvedValues, r.getValue().getFHIRResourceType()), additionalResources, r.getGroupId());
addResourceToBundle(bundle, Lists.newArrayList(updatedResourceResult));
} catch (IllegalArgumentException | IllegalStateException e) {
LOGGER.error("Exception during resource PendingExpressions generation");
LOGGER.debug("Exception during resource PendingExpressions generation", e);
} finally {
MDC.remove(RESOURCE);
}
}
LOGGER.info("Successfully converted message");
LOGGER.debug("Successfully converted Message: {} , Message Control Id: {} to FHIR bundle resource with id {}", dataInput.getName(), dataInput.getId(), bundle.getId());
return bundle;
}
use of io.github.linuxforhealth.hl7.resource.ResourceEvaluationResult in project hl7v2-fhir-converter by LinuxForHealth.
the class NestedExpression method evaluateExpression.
@Override
protected EvaluationResult evaluateExpression(InputDataExtractor dataSource, Map<String, EvaluationResult> contextValues, EvaluationResult baseValue) {
LOGGER.info("Evalutating expression");
LOGGER.debug("Evaluating child expressions {}", this.getExpressionAttr().getName());
ResourceEvaluationResult result = ExpressionUtility.evaluate(dataSource, contextValues, baseValue, this.childexpressions);
if (result == null || result.getResolveValues() == null || result.getResolveValues().isEmpty()) {
return EvaluationResultFactory.getEvaluationResult(null);
} else {
if (this.generateMap) {
return EvaluationResultFactory.getEvaluationResult(result.getResolveValues(), result.getAdditionalResolveValues());
} else {
List<Object> values = new ArrayList<>();
for (Object o : result.getResolveValues().values()) {
if (o instanceof Collection) {
values.addAll((Collection) o);
} else {
values.add(o);
}
}
return EvaluationResultFactory.getEvaluationResult(values, result.getAdditionalResolveValues());
}
}
}
Aggregations