use of io.github.linuxforhealth.core.exception.RequiredConstraintFailureException 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.core.exception.RequiredConstraintFailureException in project hl7v2-fhir-converter by LinuxForHealth.
the class HL7DataBasedResourceModel method evaluate.
@Override
public ResourceResult evaluate(InputDataExtractor dataSource, Map<String, EvaluationResult> context, EvaluationResult baseValue) {
ResourceResult resources = null;
try {
ResourceEvaluationResult result = ExpressionUtility.evaluate(dataSource, context, baseValue, this.expressions);
if (result != null && !result.getResolveValues().isEmpty()) {
String groupId = getGroupId(context);
resources = new ResourceResult(new SimpleResourceValue(result.getResolveValues(), this.name), result.getAdditionalResolveValues(), groupId, result.getPendingExpressions());
}
} catch (RequiredConstraintFailureException e) {
LOGGER.warn("Resource Constraint condition not satisfied for {}.", this.name);
LOGGER.debug("Resource Constraint condition not satisfied for {}, exception {}", this.name, e.toString());
return null;
} catch (IllegalArgumentException | IllegalStateException | DataExtractionException e) {
LOGGER.error("Exception during resource {} evaluation reason", this.name);
LOGGER.debug("Exception during resource {} evaluation reason {}", this.name, e.toString());
return null;
}
return resources;
}
use of io.github.linuxforhealth.core.exception.RequiredConstraintFailureException 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.core.exception.RequiredConstraintFailureException 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();
}
}
use of io.github.linuxforhealth.core.exception.RequiredConstraintFailureException in project hl7v2-fhir-converter by LinuxForHealth.
the class HL7MessageEngine method generateMultipleResources.
private static List<ResourceResult> generateMultipleResources(final HL7MessageData hl7DataInput, final ResourceModel rs, final Map<String, EvaluationResult> contextValues, final List<SegmentGroup> multipleSegments, boolean generateMultiple) {
List<ResourceResult> resourceResults = new ArrayList<>();
for (SegmentGroup currentGroup : multipleSegments) {
Map<String, EvaluationResult> localContextValues = new HashMap<>(contextValues);
localContextValues.put(Constants.GROUP_ID, EvaluationResultFactory.getEvaluationResult(currentGroup.getGroupId()));
// Resource needs to be generated for each base value in the group
List<EvaluationResult> baseValues = new ArrayList<>();
currentGroup.getSegments().forEach(struct -> baseValues.add(EvaluationResultFactory.getEvaluationResult(struct)));
localContextValues.putAll(getContextMap(currentGroup));
for (EvaluationResult baseValue : baseValues) {
try {
ResourceResult result = rs.evaluate(hl7DataInput, ImmutableMap.copyOf(localContextValues), baseValue);
if (result != null && result.getValue() != null) {
resourceResults.add(result);
if (!generateMultiple) {
// If only single resource needs to be generated then return.
return resourceResults;
}
}
} catch (RequiredConstraintFailureException | IllegalArgumentException | IllegalStateException e) {
LOGGER.warn("generateMultipleResources - Exception encountered");
LOGGER.debug("generateMultipleResources - Exception encountered", e);
}
}
}
return resourceResults;
}
Aggregations