use of io.github.linuxforhealth.api.ResourceValue 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.ResourceValue 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.ResourceValue 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);
}
use of io.github.linuxforhealth.api.ResourceValue in project hl7v2-fhir-converter by LinuxForHealth.
the class ReferenceExpression method evaluateExpression.
@Override
public EvaluationResult evaluateExpression(InputDataExtractor dataSource, Map<String, EvaluationResult> contextValues, EvaluationResult baseValue) {
Preconditions.checkArgument(dataSource != null, "dataSource cannot be null");
Preconditions.checkArgument(contextValues != null, "contextValues cannot be null");
LOGGER.debug("Evaluating expression {}", this.reference);
EvaluationResult resourceReferenceResult = null;
// Evaluate the resource first and add it to the list of additional resources generated
ResourceResult primaryResourceResult = evaluateResource(dataSource, contextValues, baseValue);
// If the primary resource is generated then create the reference
if (primaryResourceResult != null && primaryResourceResult.getValue() != null) {
List<ResourceValue> additionalResources = new ArrayList<>();
additionalResources.addAll(primaryResourceResult.getAdditionalResources());
additionalResources.add(primaryResourceResult.getValue());
EvaluationResult genBaseValue = EvaluationResultFactory.getEvaluationResult(primaryResourceResult.getValue().getResource());
Map<String, EvaluationResult> localContextValues = new HashMap<>(contextValues);
ResourceResult result = this.referenceModel.evaluate(dataSource, ImmutableMap.copyOf(localContextValues), genBaseValue);
if (result != null && result.getValue() != null) {
ResourceValue resolvedvalues = result.getValue();
LOGGER.debug("Evaluated expression {}, returning {} ", this.reference, resolvedvalues);
if (resolvedvalues != null) {
resourceReferenceResult = EvaluationResultFactory.getEvaluationResult(resolvedvalues.getResource(), additionalResources);
}
}
}
return resourceReferenceResult;
}
use of io.github.linuxforhealth.api.ResourceValue in project hl7v2-fhir-converter by LinuxForHealth.
the class ResourceExpression method evaluateExpression.
@Override
public EvaluationResult evaluateExpression(InputDataExtractor dataSource, Map<String, EvaluationResult> contextValues, EvaluationResult baseValue) {
Preconditions.checkArgument(dataSource != null, "dataSource cannot be null");
Preconditions.checkArgument(contextValues != null, "contextValues cannot be null");
LOGGER.debug("Evaluating expression {}", this.resourceToGenerate);
EvaluationResult evaluationResult = null;
ResourceResult result = this.data.evaluate(dataSource, ImmutableMap.copyOf(contextValues), baseValue);
if (result != null && result.getValue() != null) {
ResourceValue resolvedvalues = result.getValue();
LOGGER.debug("Evaluated expression {}, returning {} ", this.resourceToGenerate, resolvedvalues);
if (resolvedvalues != null) {
evaluationResult = EvaluationResultFactory.getEvaluationResult(resolvedvalues.getResource(), result.getAdditionalResources());
}
}
return evaluationResult;
}
Aggregations