diff --git a/Src/java/cql-to-elm/src/main/java/org/cqframework/cql/cql2elm/SystemFunctionResolver.java b/Src/java/cql-to-elm/src/main/java/org/cqframework/cql/cql2elm/SystemFunctionResolver.java index c89eccc66..39376caf5 100644 --- a/Src/java/cql-to-elm/src/main/java/org/cqframework/cql/cql2elm/SystemFunctionResolver.java +++ b/Src/java/cql-to-elm/src/main/java/org/cqframework/cql/cql2elm/SystemFunctionResolver.java @@ -338,7 +338,8 @@ public Invocation resolveSystemFunction(FunctionRef fun) { case "ToTime": case "ToQuantity": case "ToRatio": - case "ToConcept": { + case "ToConcept": + case "ToChars": { return resolveUnary(fun); } diff --git a/Src/java/engine-fhir/src/test/resources/org/hl7/fhirpath/cql/CqlTypeOperatorsTest.xml b/Src/java/engine-fhir/src/test/resources/org/hl7/fhirpath/cql/CqlTypeOperatorsTest.xml index a6c06ad98..9d18b5b3b 100644 --- a/Src/java/engine-fhir/src/test/resources/org/hl7/fhirpath/cql/CqlTypeOperatorsTest.xml +++ b/Src/java/engine-fhir/src/test/resources/org/hl7/fhirpath/cql/CqlTypeOperatorsTest.xml @@ -57,6 +57,23 @@ false + + + ToChars('abc123') + { 'a', 'b', 'c', '1', '2', '3' } + + + ToChars('') + {} + + + ToChars(null) + null + + + ToChars(123) + + ToConcept(Code { code: '8480-6' }) diff --git a/Src/java/engine/src/main/java/org/opencds/cqf/cql/engine/elm/executing/ToCharsEvaluator.java b/Src/java/engine/src/main/java/org/opencds/cqf/cql/engine/elm/executing/ToCharsEvaluator.java new file mode 100644 index 000000000..8627a938d --- /dev/null +++ b/Src/java/engine/src/main/java/org/opencds/cqf/cql/engine/elm/executing/ToCharsEvaluator.java @@ -0,0 +1,35 @@ +package org.opencds.cqf.cql.engine.elm.executing; + +import org.opencds.cqf.cql.engine.exception.InvalidOperatorArgument; + +import java.util.ArrayList; +import java.util.List; + +/* +ToChars(argument String) List + +The ToChars operator takes a string and returns a list with one string for each character in the input, in the order in which they appear in the string. + +If the argument is null, the result is null. +*/ + +public class ToCharsEvaluator { + + public static List toChars(Object operand) { + if (operand == null) { + return null; + } + + if (operand instanceof String) { + List result = new ArrayList<>(); + for (char c : ((String) operand).toCharArray()) { + result.add(String.valueOf(c)); + } + return result; + } + + throw new InvalidOperatorArgument( + "ToChars(String)", + String.format("ToInteger(%s)", operand.getClass().getName())); + } +} diff --git a/Src/java/engine/src/main/java/org/opencds/cqf/cql/engine/execution/EvaluationVisitor.java b/Src/java/engine/src/main/java/org/opencds/cqf/cql/engine/execution/EvaluationVisitor.java index e6491ba84..acf25dc0d 100644 --- a/Src/java/engine/src/main/java/org/opencds/cqf/cql/engine/execution/EvaluationVisitor.java +++ b/Src/java/engine/src/main/java/org/opencds/cqf/cql/engine/execution/EvaluationVisitor.java @@ -420,6 +420,12 @@ public Object visitToConcept(ToConcept elm, State state) { return ToConceptEvaluator.toConcept(operand); } + @Override + public Object visitToChars(ToChars elm, State state) { + Object operand = visitExpression(elm.getOperand(), state); + return ToCharsEvaluator.toChars(operand); + } + @Override public Object visitToDate(ToDate elm, State state) { Object operand = visitExpression(elm.getOperand(), state); diff --git a/Src/java/engine/src/main/java/org/opencds/cqf/cql/engine/execution/Libraries.java b/Src/java/engine/src/main/java/org/opencds/cqf/cql/engine/execution/Libraries.java index 11d68f27b..130d63765 100644 --- a/Src/java/engine/src/main/java/org/opencds/cqf/cql/engine/execution/Libraries.java +++ b/Src/java/engine/src/main/java/org/opencds/cqf/cql/engine/execution/Libraries.java @@ -41,7 +41,7 @@ public static List resolveAllExpressionRef(final String name, fin int index = Collections.binarySearch( defs, name, (x, k) -> ((ExpressionDef) x).getName().compareTo((String) k)); - if (index == -1) { + if (index < 0) { return Collections.emptyList(); }