All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
io.github.linuxforhealth.hl7.expression.variable.SimpleVariable Maven / Gradle / Ivy
/*
* (C) Copyright IBM Corp. 2020
*
* SPDX-License-Identifier: Apache-2.0
*/
package io.github.linuxforhealth.hl7.expression.variable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.google.common.collect.ImmutableMap;
import io.github.linuxforhealth.api.EvaluationResult;
import io.github.linuxforhealth.api.InputDataExtractor;
import io.github.linuxforhealth.api.Specification;
import io.github.linuxforhealth.api.Variable;
import io.github.linuxforhealth.core.expression.ContextValueUtils;
import io.github.linuxforhealth.core.expression.EvaluationResultFactory;
import io.github.linuxforhealth.core.expression.VariableUtils;
import io.github.linuxforhealth.hl7.data.SimpleDataTypeMapper;
import io.github.linuxforhealth.hl7.expression.specification.SpecificationParser;
/**
* Defines Variable object that can be used during the expression evaluation.
*
* @author pbhallam
*/
public class SimpleVariable implements Variable {
public static final String OBJECT_TYPE = Object.class.getSimpleName();
private String name;
private List spec;
private boolean extractMultiple;
private boolean combineMultiple;
private boolean retainEmpty;
public SimpleVariable(String name, List spec) {
this(name, spec, false, false, false);
}
public SimpleVariable(String name, List spec, boolean extractMultiple, boolean combineMultiple) {
this(name, spec, extractMultiple, combineMultiple, false);
}
public SimpleVariable(String name, List spec, boolean extractMultiple, boolean combineMultiple, boolean retainEmpty) {
this.name = name;
this.spec = new ArrayList<>();
if (spec != null && !spec.isEmpty()) {
this.spec.addAll(spec);
}
this.extractMultiple = extractMultiple;
this.combineMultiple = combineMultiple;
this.retainEmpty = retainEmpty;
}
@Override
public List getSpec() {
return new ArrayList<>(spec);
}
@Override
public String getType() {
return OBJECT_TYPE;
}
public String getName() {
return name;
}
// resolve variable value
@Override
public EvaluationResult extractVariableValue(Map contextValues,
InputDataExtractor dataSource) {
EvaluationResult result;
if (!this.spec.isEmpty()) {
List values =
getValuesFromSpecs(contextValues, dataSource, combineMultiple);
if (values.isEmpty()) {
result = null;
} else if (values.size() == 1) {
result = values.get(0);
} else {
result = generateCombinedValue(values);
}
} else {
result = null;
}
return result;
}
private static EvaluationResult generateCombinedValue(List values) {
StringBuilder sb = new StringBuilder();
for (EvaluationResult value : values) {
if (value.getValue() != null) {
sb.append(SimpleDataTypeMapper.getValueResolver("STRING").apply(value.getValue()));
}
}
return EvaluationResultFactory.getEvaluationResult(sb.toString());
}
protected EvaluationResult getValueFromSpecs(Map contextValues,
InputDataExtractor dataSource) {
List values = getValuesFromSpecs(contextValues, dataSource, false);
if (values.isEmpty()) {
return null;
} else {
return values.get(0);
}
}
protected List getValuesFromSpecs(Map contextValues,
InputDataExtractor dataSource, boolean fetchAll) {
List combineValue = new ArrayList<>();
for (String specValue : this.spec) {
EvaluationResult fetchedValue = null;
if (VariableUtils.isVar(specValue)) {
boolean fuzzyMatch = VariableUtils.isFuzzyMatch(specValue);
fetchedValue =
ContextValueUtils.getVariableValuesFromVariableContextMap(specValue,
ImmutableMap.copyOf(contextValues),
false, fuzzyMatch);
} else {
EvaluationResult gen;
Specification spec =
SpecificationParser.parse(specValue, this.extractMultiple, false, this.retainEmpty);
gen = spec.extractValueForSpec(dataSource, contextValues);
if (gen != null && !gen.isEmpty()) {
fetchedValue = gen;
}
}
// break the loop and return
if (fetchedValue != null) {
combineValue.add(fetchedValue);
if (!fetchAll) {
return combineValue;
}
}
}
return combineValue;
}
@Override
public String getVariableName() {
return VariableUtils.getVarName(this.name);
}
/**
* Return if variable value should extracted from all repetitions of the spec
*
* @return boolean
*/
public boolean extractMultiple() {
return this.extractMultiple;
}
public boolean retainEmpty() {
return this.retainEmpty;
}
}