diff --git a/fhirpath/src/main/java/au/csiro/pathling/fhirpath/execution/DataRoot.java b/fhirpath/src/main/java/au/csiro/pathling/fhirpath/execution/DataRoot.java index 384f880174..769a422ebb 100644 --- a/fhirpath/src/main/java/au/csiro/pathling/fhirpath/execution/DataRoot.java +++ b/fhirpath/src/main/java/au/csiro/pathling/fhirpath/execution/DataRoot.java @@ -30,9 +30,8 @@ public interface DataRoot { @Nonnull ResourceType getParentResourceType(); - default int depth() { - return 0; - } + @Nonnull + String toDisplayString(); @Value(staticConstructor = "of") class ResourceRoot implements DataRoot { @@ -51,6 +50,12 @@ public ResourceType getParentResourceType() { public String getTag() { return resourceType.toCode(); } + + @Override + @Nonnull + public String toDisplayString() { + return resourceType.toCode(); + } } @@ -65,10 +70,6 @@ default ResourceType getParentResourceType() { @Nonnull DataRoot getMaster(); - default int depth() { - return getMaster().depth() + 1; - } - @Nonnull JoinTag asTag(); @@ -101,6 +102,13 @@ public JoinTag asTag() { return JoinTag.ReverseResolveTag.of(foreignResourceType, foreignKeyPath); } + @Override + @Nonnull + public String toDisplayString() { + return foreignResourceType.toCode() + "<-" + foreignKeyPath; + } + + public static ReverseResolveRoot ofResource(@Nonnull final ResourceType masterType, @Nonnull final ResourceType foreignResourceType, @Nonnull final String foreignResourcePath) { @@ -126,17 +134,19 @@ public ResourceType getResourceType() { return foreignResourceType; } - @Override - public int depth() { - return master.depth() + 1; - } - @Nonnull @Override public JoinTag asTag() { return JoinTag.ResolveTag.of(foreignResourceType); } + @Override + @Nonnull + public String toDisplayString() { + return masterResourcePath + "->" + foreignResourceType.toCode(); + } + + public static ResolveRoot ofResource(@Nonnull final ResourceType masterType, @Nonnull final ResourceType foreignResourceType, @Nonnull final String masterResourcePath) { diff --git a/fhirpath/src/main/java/au/csiro/pathling/fhirpath/execution/JoinSet.java b/fhirpath/src/main/java/au/csiro/pathling/fhirpath/execution/JoinSet.java index 4e3894fe06..b5c00b6134 100644 --- a/fhirpath/src/main/java/au/csiro/pathling/fhirpath/execution/JoinSet.java +++ b/fhirpath/src/main/java/au/csiro/pathling/fhirpath/execution/JoinSet.java @@ -14,7 +14,7 @@ import lombok.AllArgsConstructor; import lombok.Value; -@Value() +@Value @AllArgsConstructor(access = AccessLevel.PRIVATE) public class JoinSet { @@ -59,7 +59,7 @@ static List toPath(@Nonnull final DataRoot root) { } @Nonnull - public static List mergeRoots(@Nonnull final List> paths) { + private static List mergeRoots(@Nonnull final List> paths) { // convert to paths and then group by recurively by common prefixes // we have got it already somwhere else final Map>> suffixesByHeads = paths.stream() @@ -74,10 +74,47 @@ public static List mergeRoots(@Nonnull final List> paths .toList(); } + /** + * Merges a set of roots into a tree of join sets by recursively grouping by common prefixes. + * + * @param roots the roots to merge + * @return the merged join sets + */ @Nonnull public static List mergeRoots(@Nonnull final Set roots) { - // convert to paths and then group by recurively by common prefixes - // we have got it already somwhere else + // convert to paths and then group by recursively by common prefixes return mergeRoots(roots.stream().map(JoinSet::toPath).toList()); } + + /** + * Returns a string representation of the join set as a tree. + */ + @Nonnull + public String toTreeString() { + return toTreeString(0); + } + + /** + * Prints a string representation of the join set as a tree. + */ + public void printTree() { + System.out.println(toTreeString()); + } + + @Nonnull + private String toTreeString(final int depth) { + final StringBuilder sb = new StringBuilder(); + sb.append(" ".repeat(depth > 0 + ? depth - 1 + : 0)); + sb.append(depth > 0 + ? "+-" + : ""); + sb.append(master.toDisplayString()); + sb.append("\n"); + children.forEach(child -> sb.append(child.toTreeString(depth + 1))); + return sb.toString(); + } + + } diff --git a/fhirpath/src/main/java/au/csiro/pathling/fhirpath/execution/MultiFhirPathEvaluator.java b/fhirpath/src/main/java/au/csiro/pathling/fhirpath/execution/MultiFhirPathEvaluator.java index d76346f3e8..40d2daef5f 100644 --- a/fhirpath/src/main/java/au/csiro/pathling/fhirpath/execution/MultiFhirPathEvaluator.java +++ b/fhirpath/src/main/java/au/csiro/pathling/fhirpath/execution/MultiFhirPathEvaluator.java @@ -102,8 +102,10 @@ public Dataset createInitialDataset() { // createInitialDataset(), null) // : createInitialDataset(); + final JoinSet subjectJoinSet = JoinSet.mergeRoots(joinRoots).iterator().next(); + System.out.println("Subject join set: \n" + subjectJoinSet.toTreeString()); Dataset resolvedDataset = resolveJoins( - JoinSet.mergeRoots(joinRoots).iterator().next(), + subjectJoinSet, createInitialDataset()); System.out.println("Resolved dataset:"); @@ -419,7 +421,6 @@ private Dataset computeReverseJoin(@Nonnull final Dataset parentDatase @Nonnull private Dataset resolveJoins(@Nonnull final JoinSet joinSet, @Nonnull final Dataset parentDataset) { - // now just reduce current children return joinSet.getChildren().stream() .reduce(parentDataset, (dataset, subset) ->