|
|
|
@ -193,6 +193,7 @@ public class FHIRPathEngine {
|
|
|
|
|
/**
|
|
|
|
|
* A constant reference - e.g. a reference to a name that must be resolved in context.
|
|
|
|
|
* The % will be removed from the constant name before this is invoked.
|
|
|
|
|
* Variables created with defineVariable will not be processed by resolveConstant (or resolveConstantType)
|
|
|
|
|
*
|
|
|
|
|
* This will also be called if the host invokes the FluentPath engine with a context of null
|
|
|
|
|
*
|
|
|
|
@ -1010,6 +1011,7 @@ public class FHIRPathEngine {
|
|
|
|
|
private List<Base> total;
|
|
|
|
|
private Map<String, Base> aliases;
|
|
|
|
|
private int index;
|
|
|
|
|
private Map<String, List<Base>> definedVariables;
|
|
|
|
|
|
|
|
|
|
public ExecutionContext(Object appInfo, Base resource, Base rootResource, Base context, Map<String, Base> aliases, Base thisItem) {
|
|
|
|
|
this.appInfo = appInfo;
|
|
|
|
@ -1058,6 +1060,28 @@ public class FHIRPathEngine {
|
|
|
|
|
index = i;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean hasDefinedVariable(String name) {
|
|
|
|
|
return definedVariables != null && definedVariables.containsKey(name);
|
|
|
|
|
}
|
|
|
|
|
public List<Base> getDefinedVariable(String name) {
|
|
|
|
|
return definedVariables == null ? makeNull() : definedVariables.get(name);
|
|
|
|
|
}
|
|
|
|
|
public void setDefinedVariable(String name, List<Base> value) {
|
|
|
|
|
if (isSystemVariable(name))
|
|
|
|
|
throw new PathEngineException(worker.formatMessage(I18nConstants.FHIRPATH_REDEFINE_VARIABLE, name), I18nConstants.FHIRPATH_REDEFINE_VARIABLE);
|
|
|
|
|
|
|
|
|
|
if (definedVariables == null) {
|
|
|
|
|
definedVariables = new HashMap<String, List<Base>>();
|
|
|
|
|
} else {
|
|
|
|
|
if (definedVariables.containsKey(name)) {
|
|
|
|
|
// Can't do this, so throw an error
|
|
|
|
|
throw new PathEngineException(worker.formatMessage(I18nConstants.FHIRPATH_REDEFINE_VARIABLE, name), I18nConstants.FHIRPATH_REDEFINE_VARIABLE);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
definedVariables.put(name, value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static class ExecutionTypeContext {
|
|
|
|
@ -1066,7 +1090,7 @@ public class FHIRPathEngine {
|
|
|
|
|
private TypeDetails context;
|
|
|
|
|
private TypeDetails thisItem;
|
|
|
|
|
private TypeDetails total;
|
|
|
|
|
|
|
|
|
|
private Map<String, TypeDetails> definedVariables;
|
|
|
|
|
|
|
|
|
|
public ExecutionTypeContext(Object appInfo, String resource, TypeDetails context, TypeDetails thisItem) {
|
|
|
|
|
super();
|
|
|
|
@ -1083,7 +1107,27 @@ public class FHIRPathEngine {
|
|
|
|
|
return thisItem;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean hasDefinedVariable(String name) {
|
|
|
|
|
return definedVariables != null && definedVariables.containsKey(name);
|
|
|
|
|
}
|
|
|
|
|
public TypeDetails getDefinedVariable(String name) {
|
|
|
|
|
return definedVariables == null ? null : definedVariables.get(name);
|
|
|
|
|
}
|
|
|
|
|
public void setDefinedVariable(String name, TypeDetails value) {
|
|
|
|
|
if (isSystemVariable(name))
|
|
|
|
|
throw new PathEngineException("Redefine of variable "+name, I18nConstants.FHIRPATH_REDEFINE_VARIABLE);
|
|
|
|
|
|
|
|
|
|
if (definedVariables == null) {
|
|
|
|
|
definedVariables = new HashMap<String, TypeDetails>();
|
|
|
|
|
} else {
|
|
|
|
|
if (definedVariables.containsKey(name)) {
|
|
|
|
|
// Can't do this, so throw an error
|
|
|
|
|
throw new PathEngineException("Redefine of variable "+name, I18nConstants.FHIRPATH_REDEFINE_VARIABLE);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
definedVariables.put(name, value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ExpressionNode parseExpression(FHIRLexer lexer, boolean proximal) throws FHIRLexerException {
|
|
|
|
@ -1423,6 +1467,7 @@ public class FHIRPathEngine {
|
|
|
|
|
case Descendants: return checkParamCount(lexer, location, exp, 0);
|
|
|
|
|
case MemberOf: return checkParamCount(lexer, location, exp, 1);
|
|
|
|
|
case Trace: return checkParamCount(lexer, location, exp, 1, 2);
|
|
|
|
|
case DefineVariable: return checkParamCount(lexer, location, exp, 1, 2);
|
|
|
|
|
case Check: return checkParamCount(lexer, location, exp, 2);
|
|
|
|
|
case Today: return checkParamCount(lexer, location, exp, 0);
|
|
|
|
|
case Now: return checkParamCount(lexer, location, exp, 0);
|
|
|
|
@ -1480,8 +1525,9 @@ public class FHIRPathEngine {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private List<Base> execute(ExecutionContext context, List<Base> focus, ExpressionNode exp, boolean atEntry) throws FHIRException {
|
|
|
|
|
private List<Base> execute(ExecutionContext inContext, List<Base> focus, ExpressionNode exp, boolean atEntry) throws FHIRException {
|
|
|
|
|
// System.out.println("Evaluate {'"+exp.toString()+"'} on "+focus.toString());
|
|
|
|
|
ExecutionContext context = contextForParameter(inContext);
|
|
|
|
|
List<Base> work = new ArrayList<Base>();
|
|
|
|
|
switch (exp.getKind()) {
|
|
|
|
|
case Unary:
|
|
|
|
@ -1506,7 +1552,7 @@ public class FHIRPathEngine {
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case Function:
|
|
|
|
|
List<Base> work2 = evaluateFunction(context, focus, exp);
|
|
|
|
|
List<Base> work2 = evaluateFunction("aliasAs".equals(exp.getName()) ? inContext : context, focus, exp);
|
|
|
|
|
work.addAll(work2);
|
|
|
|
|
break;
|
|
|
|
|
case Constant:
|
|
|
|
@ -1525,6 +1571,7 @@ public class FHIRPathEngine {
|
|
|
|
|
ExpressionNode next = exp.getOpNext();
|
|
|
|
|
ExpressionNode last = exp;
|
|
|
|
|
while (next != null) {
|
|
|
|
|
context = contextForParameter(inContext);
|
|
|
|
|
List<Base> work2 = preOperate(work, last.getOperation(), exp);
|
|
|
|
|
if (work2 != null) {
|
|
|
|
|
work = work2;
|
|
|
|
@ -1588,8 +1635,8 @@ public class FHIRPathEngine {
|
|
|
|
|
return new TypeDetails(CollectionStatus.SINGLETON, exp.getName());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private TypeDetails executeType(ExecutionTypeContext context, TypeDetails focus, ExpressionNode exp, Set<ElementDefinition> elementDependencies, boolean atEntry, boolean canBeNone, ExpressionNode container) throws PathEngineException, DefinitionException {
|
|
|
|
|
|
|
|
|
|
private TypeDetails executeType(ExecutionTypeContext inContext, TypeDetails focus, ExpressionNode exp, Set<ElementDefinition> elementDependencies, boolean atEntry, boolean canBeNone, ExpressionNode container) throws PathEngineException, DefinitionException {
|
|
|
|
|
ExecutionTypeContext context = contextForParameter(inContext);
|
|
|
|
|
TypeDetails result = new TypeDetails(null);
|
|
|
|
|
switch (exp.getKind()) {
|
|
|
|
|
case Name:
|
|
|
|
@ -1639,6 +1686,7 @@ public class FHIRPathEngine {
|
|
|
|
|
ExpressionNode next = exp.getOpNext();
|
|
|
|
|
ExpressionNode last = exp;
|
|
|
|
|
while (next != null) {
|
|
|
|
|
context = contextForParameter(inContext);
|
|
|
|
|
TypeDetails work;
|
|
|
|
|
if (last.getOperation() == Operation.Is || last.getOperation() == Operation.As) {
|
|
|
|
|
work = executeTypeName(context, focus, next, atEntry);
|
|
|
|
@ -1676,6 +1724,10 @@ public class FHIRPathEngine {
|
|
|
|
|
}
|
|
|
|
|
FHIRConstant c = (FHIRConstant) constant;
|
|
|
|
|
if (c.getValue().startsWith("%")) {
|
|
|
|
|
String varName = c.getValue().substring(1);
|
|
|
|
|
if (context.hasDefinedVariable(varName)) {
|
|
|
|
|
return context.getDefinedVariable(varName);
|
|
|
|
|
}
|
|
|
|
|
return resolveConstant(context, c.getValue(), beforeContext, expr, true);
|
|
|
|
|
} else if (c.getValue().startsWith("@")) {
|
|
|
|
|
return new ArrayList<Base>(Arrays.asList(processDateConstant(context.appInfo, c.getValue().substring(1), expr)));
|
|
|
|
@ -1746,6 +1798,21 @@ public class FHIRPathEngine {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static boolean isSystemVariable(String name){
|
|
|
|
|
if (name.equals("sct"))
|
|
|
|
|
return true;
|
|
|
|
|
if (name.equals("loinc"))
|
|
|
|
|
return true;
|
|
|
|
|
if (name.equals("ucum"))
|
|
|
|
|
return true;
|
|
|
|
|
if (name.equals("resource"))
|
|
|
|
|
return true;
|
|
|
|
|
if (name.equals("rootResource"))
|
|
|
|
|
return true;
|
|
|
|
|
if (name.equals("context"))
|
|
|
|
|
return true;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private List<Base> resolveConstant(ExecutionContext context, String s, boolean beforeContext, ExpressionNode expr, boolean explicitConstant) throws PathEngineException {
|
|
|
|
|
if (s.equals("%sct")) {
|
|
|
|
@ -3151,6 +3218,10 @@ public class FHIRPathEngine {
|
|
|
|
|
} else if (hostServices == null) {
|
|
|
|
|
throw makeException(expr, I18nConstants.FHIRPATH_UNKNOWN_CONSTANT, s);
|
|
|
|
|
} else {
|
|
|
|
|
String varName = s.substring(1);
|
|
|
|
|
if (context.hasDefinedVariable(varName)) {
|
|
|
|
|
return context.getDefinedVariable(varName);
|
|
|
|
|
}
|
|
|
|
|
TypeDetails v = hostServices.resolveConstantType(this, context.appInfo, s, explicitConstant);
|
|
|
|
|
if (v == null) {
|
|
|
|
|
throw makeException(expr, I18nConstants.FHIRPATH_UNKNOWN_CONSTANT, s);
|
|
|
|
@ -3503,6 +3574,25 @@ public class FHIRPathEngine {
|
|
|
|
|
checkParamTypes(exp, exp.getFunction().toCode(), paramTypes, new TypeDetails(CollectionStatus.SINGLETON, TypeDetails.FP_String));
|
|
|
|
|
return focus;
|
|
|
|
|
}
|
|
|
|
|
case DefineVariable : {
|
|
|
|
|
checkParamTypes(exp, exp.getFunction().toCode(), paramTypes, new TypeDetails(CollectionStatus.UNORDERED, TypeDetails.FP_String));
|
|
|
|
|
// set the type of the variable
|
|
|
|
|
// Actually evaluate the value of the first parameter (to get the name of the variable if possible)
|
|
|
|
|
// and if have that, set it into the context
|
|
|
|
|
ExpressionNode p = exp.getParameters().get(0);
|
|
|
|
|
if (p.getKind() == Kind.Constant && p.getConstant() != null) {
|
|
|
|
|
String varName = exp.getParameters().get(0).getConstant().primitiveValue();
|
|
|
|
|
if (varName != null) {
|
|
|
|
|
if (paramTypes.size() > 1)
|
|
|
|
|
context.setDefinedVariable(varName, paramTypes.get(1));
|
|
|
|
|
else
|
|
|
|
|
context.setDefinedVariable(varName, focus);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// this variable is not a constant, so we can't analyze what name it could have
|
|
|
|
|
}
|
|
|
|
|
return focus;
|
|
|
|
|
}
|
|
|
|
|
case Check : {
|
|
|
|
|
checkParamTypes(exp, exp.getFunction().toCode(), paramTypes, new TypeDetails(CollectionStatus.SINGLETON, TypeDetails.FP_String));
|
|
|
|
|
return focus;
|
|
|
|
@ -3734,7 +3824,7 @@ public class FHIRPathEngine {
|
|
|
|
|
case 0:
|
|
|
|
|
return exp.getFunction() == Function.Where || exp.getFunction() == Function.Exists || exp.getFunction() == Function.All || exp.getFunction() == Function.Select || exp.getFunction() == Function.Repeat || exp.getFunction() == Function.Aggregate;
|
|
|
|
|
case 1:
|
|
|
|
|
return exp.getFunction() == Function.Trace;
|
|
|
|
|
return exp.getFunction() == Function.Trace || exp.getFunction() == Function.DefineVariable;
|
|
|
|
|
default:
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
@ -3887,6 +3977,7 @@ public class FHIRPathEngine {
|
|
|
|
|
case Descendants : return funcDescendants(context, focus, exp);
|
|
|
|
|
case MemberOf : return funcMemberOf(context, focus, exp);
|
|
|
|
|
case Trace : return funcTrace(context, focus, exp);
|
|
|
|
|
case DefineVariable : return funcDefineVariable(context, focus, exp);
|
|
|
|
|
case Check : return funcCheck(context, focus, exp);
|
|
|
|
|
case Today : return funcToday(context, focus, exp);
|
|
|
|
|
case Now : return funcNow(context, focus, exp);
|
|
|
|
@ -4652,13 +4743,50 @@ public class FHIRPathEngine {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private ExecutionContext changeThis(ExecutionContext context, Base newThis) {
|
|
|
|
|
return new ExecutionContext(context.appInfo, context.focusResource, context.rootResource, context.context, context.aliases, newThis);
|
|
|
|
|
ExecutionContext newContext = new ExecutionContext(context.appInfo, context.focusResource, context.rootResource, context.context, context.aliases, newThis);
|
|
|
|
|
// append all of the defined variables from the context into the new context
|
|
|
|
|
if (context.definedVariables != null) {
|
|
|
|
|
for (String s : context.definedVariables.keySet()) {
|
|
|
|
|
newContext.setDefinedVariable(s, context.definedVariables.get(s));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return newContext;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ExecutionContext contextForParameter(ExecutionContext context) {
|
|
|
|
|
ExecutionContext newContext = new ExecutionContext(context.appInfo, context.focusResource, context.rootResource, context.context, context.aliases, context.thisItem);
|
|
|
|
|
newContext.total = context.total;
|
|
|
|
|
newContext.index = context.index;
|
|
|
|
|
// append all of the defined variables from the context into the new context
|
|
|
|
|
if (context.definedVariables != null) {
|
|
|
|
|
for (String s : context.definedVariables.keySet()) {
|
|
|
|
|
newContext.setDefinedVariable(s, context.definedVariables.get(s));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return newContext;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ExecutionTypeContext changeThis(ExecutionTypeContext context, TypeDetails newThis) {
|
|
|
|
|
return new ExecutionTypeContext(context.appInfo, context.resource, context.context, newThis);
|
|
|
|
|
ExecutionTypeContext newContext = new ExecutionTypeContext(context.appInfo, context.resource, context.context, newThis);
|
|
|
|
|
// append all of the defined variables from the context into the new context
|
|
|
|
|
if (context.definedVariables != null) {
|
|
|
|
|
for (String s : context.definedVariables.keySet()) {
|
|
|
|
|
newContext.setDefinedVariable(s, context.definedVariables.get(s));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return newContext;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ExecutionTypeContext contextForParameter(ExecutionTypeContext context) {
|
|
|
|
|
ExecutionTypeContext newContext = new ExecutionTypeContext(context.appInfo, context.resource, context.context, context.thisItem);
|
|
|
|
|
// append all of the defined variables from the context into the new context
|
|
|
|
|
if (context.definedVariables != null) {
|
|
|
|
|
for (String s : context.definedVariables.keySet()) {
|
|
|
|
|
newContext.setDefinedVariable(s, context.definedVariables.get(s));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return newContext;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private List<Base> funcNow(ExecutionContext context, List<Base> focus, ExpressionNode exp) {
|
|
|
|
|
List<Base> result = new ArrayList<Base>();
|
|
|
|
@ -5496,6 +5624,20 @@ public class FHIRPathEngine {
|
|
|
|
|
return focus;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private List<Base> funcDefineVariable(ExecutionContext context, List<Base> focus, ExpressionNode exp) throws FHIRException {
|
|
|
|
|
List<Base> nl = execute(context, focus, exp.getParameters().get(0), true);
|
|
|
|
|
String name = nl.get(0).primitiveValue();
|
|
|
|
|
List<Base> value;
|
|
|
|
|
if (exp.getParameters().size() == 2) {
|
|
|
|
|
value = execute(context, focus, exp.getParameters().get(1), true);
|
|
|
|
|
} else {
|
|
|
|
|
value = focus;
|
|
|
|
|
}
|
|
|
|
|
// stash the variable into the context
|
|
|
|
|
context.setDefinedVariable(name, value);
|
|
|
|
|
return focus;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private List<Base> funcCheck(ExecutionContext context, List<Base> focus, ExpressionNode expr) throws FHIRException {
|
|
|
|
|
List<Base> n1 = execute(context, focus, expr.getParameters().get(0), true);
|
|
|
|
|
if (!convertToBoolean(n1)) {
|
|
|
|
|