Merge pull request #110 from ahdis/oliveregger_fhirpath_indexOf

add indexOf function to FHIRPath
This commit is contained in:
Grahame Grieve 2019-12-05 07:17:46 +13:00 committed by GitHub
commit e5af619105
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 58 additions and 2 deletions

View File

@ -60,7 +60,7 @@ public class ExpressionNode {
Custom,
Empty, Not, Exists, SubsetOf, SupersetOf, IsDistinct, Distinct, Count, Where, Select, All, Repeat, Aggregate, Item /*implicit from name[]*/, As, Is, Single,
First, Last, Tail, Skip, Take, Union, Combine, Intersect, Exclude, Iif, Upper, Lower, ToChars, Substring, StartsWith, EndsWith, Matches, ReplaceMatches, Contains, Replace, Length,
First, Last, Tail, Skip, Take, Union, Combine, Intersect, Exclude, Iif, Upper, Lower, ToChars, IndexOf, Substring, StartsWith, EndsWith, Matches, ReplaceMatches, Contains, Replace, Length,
Children, Descendants, MemberOf, Trace, Check, Today, Now, Resolve, Extension, AllFalse, AnyFalse, AllTrue, AnyTrue,
HasValue, AliasAs, Alias, HtmlChecks, OfType, Type,
ConvertsToBoolean, ConvertsToInteger, ConvertsToString, ConvertsToDecimal, ConvertsToQuantity, ConvertsToDateTime, ConvertsToTime, ToBoolean, ToInteger, ToString, ToDecimal, ToQuantity, ToDateTime, ToTime, ConformsTo;
@ -96,6 +96,7 @@ public class ExpressionNode {
if (name.equals("lower")) return Function.Lower;
if (name.equals("upper")) return Function.Upper;
if (name.equals("toChars")) return Function.ToChars;
if (name.equals("indexOf")) return Function.IndexOf;
if (name.equals("substring")) return Function.Substring;
if (name.equals("startsWith")) return Function.StartsWith;
if (name.equals("endsWith")) return Function.EndsWith;
@ -172,6 +173,7 @@ public class ExpressionNode {
case ToChars : return "toChars";
case Lower : return "lower";
case Upper : return "upper";
case IndexOf : return "indexOf";
case Substring : return "substring";
case StartsWith : return "startsWith";
case EndsWith : return "endsWith";

View File

@ -2486,6 +2486,11 @@ public class FHIRPathEngine {
checkContextString(focus, "toChars");
return new TypeDetails(CollectionStatus.ORDERED, TypeDetails.FP_String);
}
case IndexOf : {
checkContextString(focus, "indexOf");
checkParamTypes(exp.getFunction().toCode(), paramTypes, new TypeDetails(CollectionStatus.SINGLETON, TypeDetails.FP_String));
return new TypeDetails(CollectionStatus.SINGLETON, TypeDetails.FP_Integer);
}
case Substring : {
checkContextString(focus, "subString");
checkParamTypes(exp.getFunction().toCode(), paramTypes, new TypeDetails(CollectionStatus.SINGLETON, TypeDetails.FP_Integer), new TypeDetails(CollectionStatus.SINGLETON, TypeDetails.FP_Integer));
@ -2722,6 +2727,7 @@ public class FHIRPathEngine {
case Lower : return funcLower(context, focus, exp);
case Upper : return funcUpper(context, focus, exp);
case ToChars : return funcToChars(context, focus, exp);
case IndexOf : return funcIndexOf(context, focus, exp);
case Substring : return funcSubstring(context, focus, exp);
case StartsWith : return funcStartsWith(context, focus, exp);
case EndsWith : return funcEndsWith(context, focus, exp);
@ -3611,6 +3617,24 @@ public class FHIRPathEngine {
}
return result;
}
private List<Base> funcIndexOf(ExecutionContext context, List<Base> focus, ExpressionNode exp) throws FHIRException {
List<Base> result = new ArrayList<Base>();
String sw = convertToString(execute(context, focus, exp.getParameters().get(0), true));
if (focus.size() == 0) {
result.add(new IntegerType(0).noExtensions());
} else if (Utilities.noString(sw)) {
result.add(new IntegerType(0).noExtensions());
} else {
String s = convertToString(focus.get(0));
if (s == null)
result.add(new IntegerType(0).noExtensions());
else
result.add(new IntegerType(s.indexOf(sw)).noExtensions());
}
return result;
}
private List<Base> funcSubstring(ExecutionContext context, List<Base> focus, ExpressionNode exp) throws FHIRException {
List<Base> result = new ArrayList<Base>();

View File

@ -60,7 +60,7 @@ public class ExpressionNode {
Custom,
Empty, Not, Exists, SubsetOf, SupersetOf, IsDistinct, Distinct, Count, Where, Select, All, Repeat, Aggregate, Item /*implicit from name[]*/, As, Is, Single,
First, Last, Tail, Skip, Take, Union, Combine, Intersect, Exclude, Iif, Upper, Lower, ToChars, Substring, StartsWith, EndsWith, Matches, ReplaceMatches, Contains, Replace, Length,
First, Last, Tail, Skip, Take, Union, Combine, Intersect, Exclude, Iif, Upper, Lower, ToChars, IndexOf, Substring, StartsWith, EndsWith, Matches, ReplaceMatches, Contains, Replace, Length,
Children, Descendants, MemberOf, Trace, Check, Today, Now, Resolve, Extension, AllFalse, AnyFalse, AllTrue, AnyTrue,
HasValue, AliasAs, Alias, HtmlChecks, OfType, Type,
ConvertsToBoolean, ConvertsToInteger, ConvertsToString, ConvertsToDecimal, ConvertsToQuantity, ConvertsToDateTime, ConvertsToTime, ToBoolean, ToInteger, ToString, ToDecimal, ToQuantity, ToDateTime, ToTime, ConformsTo;
@ -96,6 +96,7 @@ public class ExpressionNode {
if (name.equals("lower")) return Function.Lower;
if (name.equals("upper")) return Function.Upper;
if (name.equals("toChars")) return Function.ToChars;
if (name.equals("indexOf")) return Function.IndexOf;
if (name.equals("substring")) return Function.Substring;
if (name.equals("startsWith")) return Function.StartsWith;
if (name.equals("endsWith")) return Function.EndsWith;
@ -173,6 +174,7 @@ public class ExpressionNode {
case ToChars : return "toChars";
case Lower : return "lower";
case Upper : return "upper";
case IndexOf : return "indexOf";
case Substring : return "substring";
case StartsWith : return "startsWith";
case EndsWith : return "endsWith";

View File

@ -9,6 +9,9 @@ import org.fhir.ucum.UcumException;
import org.hl7.fhir.exceptions.DefinitionException;
import org.hl7.fhir.exceptions.FHIRException;
import org.hl7.fhir.exceptions.PathEngineException;
import org.hl7.fhir.r5.model.Base;
import org.hl7.fhir.r5.model.ExpressionNode;
import org.hl7.fhir.r5.model.IntegerType;
import org.hl7.fhir.r5.conformance.ProfileUtilities;
import org.hl7.fhir.r5.context.IWorkerContext;
import org.hl7.fhir.r5.model.*;
@ -1078,6 +1081,7 @@ public class FHIRPathEngine {
case Lower: return checkParamCount(lexer, location, exp, 0);
case Upper: return checkParamCount(lexer, location, exp, 0);
case ToChars: return checkParamCount(lexer, location, exp, 0);
case IndexOf : return checkParamCount(lexer, location, exp, 1);
case Substring: return checkParamCount(lexer, location, exp, 1, 2);
case StartsWith: return checkParamCount(lexer, location, exp, 1);
case EndsWith: return checkParamCount(lexer, location, exp, 1);
@ -2486,6 +2490,11 @@ public class FHIRPathEngine {
checkContextString(focus, "toChars");
return new TypeDetails(CollectionStatus.ORDERED, TypeDetails.FP_String);
}
case IndexOf : {
checkContextString(focus, "indexOf");
checkParamTypes(exp.getFunction().toCode(), paramTypes, new TypeDetails(CollectionStatus.SINGLETON, TypeDetails.FP_String));
return new TypeDetails(CollectionStatus.SINGLETON, TypeDetails.FP_Integer);
}
case Substring : {
checkContextString(focus, "subString");
checkParamTypes(exp.getFunction().toCode(), paramTypes, new TypeDetails(CollectionStatus.SINGLETON, TypeDetails.FP_Integer), new TypeDetails(CollectionStatus.SINGLETON, TypeDetails.FP_Integer));
@ -2722,6 +2731,7 @@ public class FHIRPathEngine {
case Lower : return funcLower(context, focus, exp);
case Upper : return funcUpper(context, focus, exp);
case ToChars : return funcToChars(context, focus, exp);
case IndexOf : return funcIndexOf(context, focus, exp);
case Substring : return funcSubstring(context, focus, exp);
case StartsWith : return funcStartsWith(context, focus, exp);
case EndsWith : return funcEndsWith(context, focus, exp);
@ -3611,6 +3621,24 @@ public class FHIRPathEngine {
}
return result;
}
private List<Base> funcIndexOf(ExecutionContext context, List<Base> focus, ExpressionNode exp) throws FHIRException {
List<Base> result = new ArrayList<Base>();
String sw = convertToString(execute(context, focus, exp.getParameters().get(0), true));
if (focus.size() == 0) {
result.add(new IntegerType(0).noExtensions());
} else if (Utilities.noString(sw)) {
result.add(new IntegerType(0).noExtensions());
} else {
String s = convertToString(focus.get(0));
if (s == null)
result.add(new IntegerType(0).noExtensions());
else
result.add(new IntegerType(s.indexOf(sw)).noExtensions());
}
return result;
}
private List<Base> funcSubstring(ExecutionContext context, List<Base> focus, ExpressionNode exp) throws FHIRException {
List<Base> result = new ArrayList<Base>();