change interface for evaluating constants in FHIRPath (allow collections)

This commit is contained in:
Grahame Grieve 2021-09-01 09:46:51 +10:00
parent e28a0d9d52
commit f22855e82a
9 changed files with 47 additions and 61 deletions

View File

@ -212,10 +212,14 @@ public class ComparisonRenderer implements IEvaluationContext {
}
@Override
public Base resolveConstant(Object appContext, String name, boolean beforeContext) throws PathEngineException {
public List<Base> resolveConstant(Object appContext, String name, boolean beforeContext) throws PathEngineException {
@SuppressWarnings("unchecked")
Map<String, Base> vars = (Map<String, Base>) appContext;
return vars.get(name);
List<Base> res = new ArrayList<>();
if (vars.containsKey(name)) {
res.add(vars.get(name));
}
return res;
}
@Override

View File

@ -105,6 +105,7 @@ import org.hl7.fhir.r5.model.ValueSet;
import org.hl7.fhir.r5.model.ValueSet.ValueSetExpansionComponent;
import org.hl7.fhir.r5.model.ValueSet.ValueSetExpansionContainsComponent;
import org.hl7.fhir.r5.renderers.TerminologyRenderer;
import org.hl7.fhir.r5.renderers.spreadsheets.SpreadsheetGenerator;
import org.hl7.fhir.r5.terminologies.ValueSetExpander.ValueSetExpansionOutcome;
import org.hl7.fhir.r5.utils.FHIRLexer;
import org.hl7.fhir.r5.utils.FHIRPathEngine;
@ -113,7 +114,6 @@ import org.hl7.fhir.r5.utils.TranslatingUtilities;
import org.hl7.fhir.r5.utils.XVerExtensionManager;
import org.hl7.fhir.r5.utils.XVerExtensionManager.XVerExtensionStatus;
import org.hl7.fhir.r5.utils.formats.CSVWriter;
import org.hl7.fhir.r5.utils.formats.XLSXWriter;
import org.hl7.fhir.utilities.CommaSeparatedStringBuilder;
import org.hl7.fhir.utilities.MarkDownProcessor;
import org.hl7.fhir.utilities.Utilities;
@ -2033,7 +2033,6 @@ public class ProfileUtilities extends TranslatingUtilities {
}
}
private void removeStatusExtensions(ElementDefinition outcome) {
outcome.removeExtension(ToolingExtensions.EXT_FMM_LEVEL);
outcome.removeExtension(ToolingExtensions.EXT_STANDARDS_STATUS);
@ -2043,12 +2042,10 @@ public class ProfileUtilities extends TranslatingUtilities {
outcome.removeExtension(ToolingExtensions.EXT_FMM_DERIVED);
}
private String descED(List<ElementDefinition> list, int index) {
return index >=0 && index < list.size() ? list.get(index).present() : "X";
}
private boolean baseHasChildren(StructureDefinitionSnapshotComponent base, ElementDefinition ed) {
int index = base.getElement().indexOf(ed);
if (index == -1 || index >= base.getElement().size()-1)
@ -4115,7 +4112,7 @@ public class ProfileUtilities extends TranslatingUtilities {
if (extDefn == null) {
res.add(genCardinality(gen, element, row, hasDef, used, null));
res.add(addCell(row, gen.new Cell(null, null, "?gen-e1? "+element.getType().get(0).getProfile(), null, null)));
res.add(generateDescription(gen, row, element, (ElementDefinition) element.getUserData(DERIVATION_POINTER), used.used, profile.getUrl(), eurl, profile, corePath, imagePath, root, logicalModel, allInvariants, snapshot, mustSupport));
res.add(generateDescription(gen, row, element, (ElementDefinition) element.getUserData(DERIVATION_POINTER), used.used, profile == null ? "" : profile.getUrl(), eurl, profile, corePath, imagePath, root, logicalModel, allInvariants, snapshot, mustSupport));
} else {
String name = urltail(eurl);
nameCell.getPieces().get(0).setText(name);
@ -4598,7 +4595,7 @@ public class ProfileUtilities extends TranslatingUtilities {
c.getPieces().add(checkForNoChange(inv, gen.new Piece(null, gt(inv.getHumanElement()), null)));
}
}
if ((definition.hasBase() && definition.getBase().getMax().equals("*")) || (definition.hasMax() && definition.getMax().equals("*"))) {
if ((definition.hasBase() && "*".equals(definition.getBase().getMax())) || (definition.hasMax() && "*".equals(definition.getMax()))) {
if (c.getPieces().size() > 0)
c.addPiece(gen.new Piece("br"));
if (definition.hasOrderMeaning()) {
@ -5583,23 +5580,6 @@ public class ProfileUtilities extends TranslatingUtilities {
csv.dump();
}
// generate an Excel representation of the structure definition
public void generateXlsx(OutputStream dest, StructureDefinition structure, boolean asXml, boolean hideMustSupportFalse) throws IOException, DefinitionException, Exception {
if (structure == null) {
System.out.println("no structure!");
}
if (!structure.hasSnapshot()) {
throw new DefinitionException(context.formatMessage(I18nConstants.NEEDS_A_SNAPSHOT));
}
XLSXWriter xlsx = new XLSXWriter(dest, structure, asXml, hideMustSupportFalse);
for (ElementDefinition child : structure.getSnapshot().getElement()) {
xlsx.processElement(child);
}
xlsx.dump();
xlsx.close();
}
private class Slicer extends ElementDefinitionSlicingComponent {
String criteria = "";

View File

@ -4,6 +4,7 @@ import java.math.BigDecimal;
import java.math.RoundingMode;
import java.rmi.server.LoaderHandler;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.Calendar;
import java.util.Date;
@ -251,7 +252,7 @@ public class FHIRPathEngine {
* @param beforeContext - whether this is being called before the name is resolved locally, or not
* @return the value of the reference (or null, if it's not valid, though can throw an exception if desired)
*/
public Base resolveConstant(Object appContext, String name, boolean beforeContext) throws PathEngineException;
public List<Base> resolveConstant(Object appContext, String name, boolean beforeContext) throws PathEngineException;
public TypeDetails resolveConstantType(Object appContext, String name) throws PathEngineException;
/**
@ -1366,10 +1367,7 @@ public class FHIRPathEngine {
work.addAll(work2);
break;
case Constant:
Base b = resolveConstant(context, exp.getConstant(), false, exp);
if (b != null) {
work.add(b);
}
work.addAll(resolveConstant(context, exp.getConstant(), false, exp));
break;
case Group:
work2 = execute(context, focus, exp.getGroup(), atEntry);
@ -1507,15 +1505,18 @@ public class FHIRPathEngine {
return result;
}
private Base resolveConstant(ExecutionContext context, Base constant, boolean beforeContext, ExpressionNode expr) throws PathEngineException {
private List<Base> resolveConstant(ExecutionContext context, Base constant, boolean beforeContext, ExpressionNode expr) throws PathEngineException {
if (constant == null) {
return new ArrayList<Base>();
}
if (!(constant instanceof FHIRConstant)) {
return constant;
return new ArrayList<Base>(Arrays.asList(constant));
}
FHIRConstant c = (FHIRConstant) constant;
if (c.getValue().startsWith("%")) {
return resolveConstant(context, c.getValue(), beforeContext, expr);
} else if (c.getValue().startsWith("@")) {
return processDateConstant(context.appInfo, c.getValue().substring(1), expr);
return new ArrayList<Base>(Arrays.asList(processDateConstant(context.appInfo, c.getValue().substring(1), expr)));
} else {
throw makeException(expr, I18nConstants.FHIRPATH_UNKNOWN_CONSTANT, c.getValue());
}
@ -1585,33 +1586,33 @@ public class FHIRPathEngine {
}
private Base resolveConstant(ExecutionContext context, String s, boolean beforeContext, ExpressionNode expr) throws PathEngineException {
private List<Base> resolveConstant(ExecutionContext context, String s, boolean beforeContext, ExpressionNode expr) throws PathEngineException {
if (s.equals("%sct")) {
return new StringType("http://snomed.info/sct").noExtensions();
return new ArrayList<Base>(Arrays.asList(new StringType("http://snomed.info/sct").noExtensions()));
} else if (s.equals("%loinc")) {
return new StringType("http://loinc.org").noExtensions();
return new ArrayList<Base>(Arrays.asList(new StringType("http://loinc.org").noExtensions()));
} else if (s.equals("%ucum")) {
return new StringType("http://unitsofmeasure.org").noExtensions();
return new ArrayList<Base>(Arrays.asList(new StringType("http://unitsofmeasure.org").noExtensions()));
} else if (s.equals("%resource")) {
if (context.focusResource == null) {
throw makeException(expr, I18nConstants.FHIRPATH_CANNOT_USE, "%resource", "no focus resource");
}
return context.focusResource;
return new ArrayList<Base>(Arrays.asList(context.focusResource));
} else if (s.equals("%rootResource")) {
if (context.rootResource == null) {
throw makeException(expr, I18nConstants.FHIRPATH_CANNOT_USE, "%rootResource", "no focus resource");
}
return context.rootResource;
return new ArrayList<Base>(Arrays.asList(context.rootResource));
} else if (s.equals("%context")) {
return context.context;
return new ArrayList<Base>(Arrays.asList(context.context));
} else if (s.equals("%us-zip")) {
return new StringType("[0-9]{5}(-[0-9]{4}){0,1}").noExtensions();
return new ArrayList<Base>(Arrays.asList(new StringType("[0-9]{5}(-[0-9]{4}){0,1}").noExtensions()));
} else if (s.startsWith("%`vs-")) {
return new StringType("http://hl7.org/fhir/ValueSet/"+s.substring(5, s.length()-1)+"").noExtensions();
return new ArrayList<Base>(Arrays.asList(new StringType("http://hl7.org/fhir/ValueSet/"+s.substring(5, s.length()-1)+"").noExtensions()));
} else if (s.startsWith("%`cs-")) {
return new StringType("http://hl7.org/fhir/"+s.substring(5, s.length()-1)+"").noExtensions();
return new ArrayList<Base>(Arrays.asList(new StringType("http://hl7.org/fhir/"+s.substring(5, s.length()-1)+"").noExtensions()));
} else if (s.startsWith("%`ext-")) {
return new StringType("http://hl7.org/fhir/StructureDefinition/"+s.substring(6, s.length()-1)).noExtensions();
return new ArrayList<Base>(Arrays.asList(new StringType("http://hl7.org/fhir/StructureDefinition/"+s.substring(6, s.length()-1)).noExtensions()));
} else if (hostServices == null) {
throw makeException(expr, I18nConstants.FHIRPATH_UNKNOWN_CONSTANT, s);
} else {
@ -2886,9 +2887,9 @@ public class FHIRPathEngine {
List<Base> result = new ArrayList<Base>();
if (atEntry && context.appInfo != null && hostServices != null) {
// we'll see if the name matches a constant known by the context.
Base temp = hostServices.resolveConstant(context.appInfo, exp.getName(), true);
if (temp != null) {
result.add(temp);
List<Base> temp = hostServices.resolveConstant(context.appInfo, exp.getName(), true);
if (!temp.isEmpty()) {
result.addAll(temp);
return result;
}
}
@ -2914,10 +2915,7 @@ public class FHIRPathEngine {
if (atEntry && context.appInfo != null && hostServices != null && result.isEmpty()) {
// well, we didn't get a match on the name - we'll see if the name matches a constant known by the context.
// (if the name does match, and the user wants to get the constant value, they'll have to try harder...
Base temp = hostServices.resolveConstant(context.appInfo, exp.getName(), false);
if (temp != null) {
result.add(temp);
}
result.addAll(hostServices.resolveConstant(context.appInfo, exp.getName(), false));
}
return result;
}

View File

@ -1,6 +1,7 @@
package org.hl7.fhir.r5.utils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
@ -592,12 +593,12 @@ public class LiquidEngine implements IEvaluationContext {
}
@Override
public Base resolveConstant(Object appContext, String name, boolean beforeContext) throws PathEngineException {
public List<Base> resolveConstant(Object appContext, String name, boolean beforeContext) throws PathEngineException {
LiquidEngineContext ctxt = (LiquidEngineContext) appContext;
if (ctxt.vars.containsKey(name))
return ctxt.vars.get(name);
return new ArrayList<Base>(Arrays.asList(ctxt.vars.get(name)));
if (externalHostServices == null)
return null;
return new ArrayList<Base>();
return externalHostServices.resolveConstant(ctxt.externalContext, name, beforeContext);
}

View File

@ -23,12 +23,15 @@ public class FFHIRPathHostServices implements FHIRPathEngine.IEvaluationContext
this.structureMapUtilities = structureMapUtilities;
}
public Base resolveConstant(Object appContext, String name, boolean beforeContext) throws PathEngineException {
public List<Base> resolveConstant(Object appContext, String name, boolean beforeContext) throws PathEngineException {
Variables vars = (Variables) appContext;
Base res = vars.get(VariableMode.INPUT, name);
if (res == null)
res = vars.get(VariableMode.OUTPUT, name);
return res;
List<Base> result = new ArrayList<Base>();
if (res != null)
result.add(res);
return result;
}
@Override

View File

@ -47,7 +47,7 @@ public class FHIRPathTests {
public class FHIRPathTestEvaluationServices implements IEvaluationContext {
@Override
public Base resolveConstant(Object appContext, String name, boolean beforeContext) throws PathEngineException {
public List<Base> resolveConstant(Object appContext, String name, boolean beforeContext) throws PathEngineException {
throw new NotImplementedException("Not done yet (FHIRPathTestEvaluationServices.resolveConstant), when item is element");
}

View File

@ -350,7 +350,7 @@ public class SnapShotGenerationTests {
// FHIRPath methods
@Override
public Base resolveConstant(Object appContext, String name, boolean beforeContext) throws PathEngineException {
public List<Base> resolveConstant(Object appContext, String name, boolean beforeContext) throws PathEngineException {
throw new Error("Not implemented yet");
}

View File

@ -308,7 +308,7 @@ public class SnapShotGenerationXTests {
// FHIRPath methods
@Override
public Base resolveConstant(Object appContext, String name, boolean beforeContext) throws PathEngineException {
public List<Base> resolveConstant(Object appContext, String name, boolean beforeContext) throws PathEngineException {
throw new Error("Not implemented yet");
}

View File

@ -430,8 +430,8 @@ public class ValidationTests implements IEvaluationContext, IValidatorResourceFe
}
@Override
public Base resolveConstant(Object appContext, String name, boolean beforeContext) throws PathEngineException {
return null;
public List<Base> resolveConstant(Object appContext, String name, boolean beforeContext) throws PathEngineException {
return new ArrayList<Base>();
}
@Override