Add support for $index on aggregators in FHIRPath

This commit is contained in:
Grahame Grieve 2021-06-15 08:25:59 +10:00
parent e5d2cf0f9c
commit f51c7115ce
2 changed files with 23 additions and 2 deletions

View File

@ -489,7 +489,7 @@ public class ExpressionNode {
if (!name.startsWith("$")) if (!name.startsWith("$"))
return true; return true;
else else
return Utilities.existsInList(name, "$this", "$total"); return Utilities.existsInList(name, "$this", "$total", "$index");
} }
public Kind getKind() { public Kind getKind() {

View File

@ -873,6 +873,7 @@ public class FHIRPathEngine {
private Base thisItem; private Base thisItem;
private List<Base> total; private List<Base> total;
private Map<String, Base> aliases; private Map<String, Base> aliases;
private int index;
public ExecutionContext(Object appInfo, Base resource, Base rootResource, Base context, Map<String, Base> aliases, Base thisItem) { public ExecutionContext(Object appInfo, Base resource, Base rootResource, Base context, Map<String, Base> aliases, Base thisItem) {
this.appInfo = appInfo; this.appInfo = appInfo;
@ -881,6 +882,7 @@ public class FHIRPathEngine {
this.rootResource = rootResource; this.rootResource = rootResource;
this.aliases = aliases; this.aliases = aliases;
this.thisItem = thisItem; this.thisItem = thisItem;
this.index = 0;
} }
public Base getFocusResource() { public Base getFocusResource() {
return focusResource; return focusResource;
@ -894,6 +896,14 @@ public class FHIRPathEngine {
public List<Base> getTotal() { public List<Base> getTotal() {
return total; return total;
} }
public void next() {
index++;
}
public Base getIndex() {
return new IntegerType(index);
}
public void addAlias(String name, List<Base> focus) throws FHIRException { public void addAlias(String name, List<Base> focus) throws FHIRException {
if (aliases == null) { if (aliases == null) {
aliases = new HashMap<String, Base>(); aliases = new HashMap<String, Base>();
@ -908,6 +918,10 @@ public class FHIRPathEngine {
public Base getAlias(String name) { public Base getAlias(String name) {
return aliases == null ? null : aliases.get(name); return aliases == null ? null : aliases.get(name);
} }
public ExecutionContext setIndex(int i) {
index = i;
return this;
}
} }
private class ExecutionTypeContext { private class ExecutionTypeContext {
@ -1334,6 +1348,8 @@ public class FHIRPathEngine {
work.add(context.getThisItem()); work.add(context.getThisItem());
} else if (atEntry && exp.getName().equals("$total")) { } else if (atEntry && exp.getName().equals("$total")) {
work.addAll(context.getTotal()); work.addAll(context.getTotal());
} else if (atEntry && exp.getName().equals("$index")) {
work.add(context.getIndex());
} else { } else {
for (Base item : focus) { for (Base item : focus) {
List<Base> outcome = execute(context, item, exp, atEntry); List<Base> outcome = execute(context, item, exp, atEntry);
@ -1439,6 +1455,8 @@ public class FHIRPathEngine {
result.update(context.getThisItem()); result.update(context.getThisItem());
} else if (atEntry && exp.getName().equals("$total")) { } else if (atEntry && exp.getName().equals("$total")) {
result.update(anything(CollectionStatus.UNORDERED)); result.update(anything(CollectionStatus.UNORDERED));
} else if (atEntry && exp.getName().equals("$index")) {
result.addType(TypeDetails.FP_Integer);
} else if (atEntry && focus == null) { } else if (atEntry && focus == null) {
result.update(executeContextType(context, exp.getName(), exp)); result.update(executeContextType(context, exp.getName(), exp));
} else { } else {
@ -4346,6 +4364,7 @@ public class FHIRPathEngine {
for (Base item : focus) { for (Base item : focus) {
ExecutionContext c = changeThis(context, item); ExecutionContext c = changeThis(context, item);
c.total = total; c.total = total;
c.next();
total = execute(c, pc, exp.getParameters().get(0), true); total = execute(c, pc, exp.getParameters().get(0), true);
} }
return total; return total;
@ -5117,10 +5136,12 @@ public class FHIRPathEngine {
private List<Base> funcSelect(ExecutionContext context, List<Base> focus, ExpressionNode exp) throws FHIRException { private List<Base> funcSelect(ExecutionContext context, List<Base> focus, ExpressionNode exp) throws FHIRException {
List<Base> result = new ArrayList<Base>(); List<Base> result = new ArrayList<Base>();
List<Base> pc = new ArrayList<Base>(); List<Base> pc = new ArrayList<Base>();
int i = 0;
for (Base item : focus) { for (Base item : focus) {
pc.clear(); pc.clear();
pc.add(item); pc.add(item);
result.addAll(execute(changeThis(context, item), pc, exp.getParameters().get(0), true)); result.addAll(execute(changeThis(context, item).setIndex(i), pc, exp.getParameters().get(0), true));
i++;
} }
return result; return result;
} }