-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
1. Added a FHIRTermService.lookup overload which allows the caller to pass the specific version of the CodeSystem to use. Previously, I found that we were passing a CodeSystem to validateCode but this method delegated to lookup which fetched its own CodeSystem. This was problematic when the Coding didn't specify a particular CodeSystem version when the underlying profile constraint did. 2. Moved logic for generating `conformsTo` constraints (generated-ext-1) for each extension in the resource instance. Now it will be grouped with the rest of the constraints and we can introspect those other constraints to shape the constraint generation. - To implement this, I introduced a new "PathAwareCollectingVisitor" although alternatively I could have executed a FHIRPath like `descendents(extension)` - Instead of a constraint like `conformTo(url)` which will only validate against the latest version of this extension in the registry, we now generate an expression like `conformTo(url|1) or conformTo(url|2) or conformTo(url|3)` ...one clause for each version in the registry. Signed-off-by: Lee Surprenant <lmsurpre@us.ibm.com>
- Loading branch information
Showing
5 changed files
with
348 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
fhir-model/src/main/java/com/ibm/fhir/model/visitor/PathAwareCollectingVisitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
* (C) Copyright IBM Corp. 2022 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package com.ibm.fhir.model.visitor; | ||
|
||
import java.math.BigDecimal; | ||
import java.time.LocalDate; | ||
import java.time.LocalTime; | ||
import java.time.Year; | ||
import java.time.YearMonth; | ||
import java.time.ZonedDateTime; | ||
import java.util.Collections; | ||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* Visits a Resource or Element and collects all of its descendants of a given type into a collection | ||
* of those elements, indexed by their simple FHIRPath path. | ||
* | ||
* @param <T> The type of object to collect | ||
* @implNote The order of the list will be consistent with a depth-first traversal of the visited object | ||
*/ | ||
public class PathAwareCollectingVisitor<T> extends PathAwareVisitor { | ||
protected final Map<String, T> result = new LinkedHashMap<>(); | ||
protected final Class<T> type; | ||
|
||
public PathAwareCollectingVisitor(Class<T> type) { | ||
super(); | ||
this.type = type; | ||
} | ||
|
||
protected void collect(Object object) { | ||
if (type.isInstance(object)) { | ||
result.put(getPath(), type.cast(object)); | ||
} | ||
} | ||
|
||
public Map<String, T> getResult() { | ||
return Collections.unmodifiableMap(result); | ||
} | ||
|
||
@Override | ||
public boolean visit(java.lang.String elementName, int elementIndex, Visitable visitable) { | ||
collect(visitable); | ||
return true; | ||
} | ||
|
||
@Override | ||
public void doVisit(java.lang.String elementName, byte[] value) { | ||
collect(value); | ||
} | ||
|
||
@Override | ||
public void doVisit(java.lang.String elementName, BigDecimal value) { | ||
collect(value); | ||
} | ||
|
||
@Override | ||
public void doVisit(java.lang.String elementName, java.lang.Boolean value) { | ||
collect(value); | ||
} | ||
|
||
@Override | ||
public void doVisit(java.lang.String elementName, java.lang.Integer value) { | ||
collect(value); | ||
} | ||
|
||
@Override | ||
public void doVisit(java.lang.String elementName, LocalDate value) { | ||
collect(value); | ||
} | ||
|
||
@Override | ||
public void doVisit(java.lang.String elementName, LocalTime value) { | ||
collect(value); | ||
} | ||
|
||
@Override | ||
public void doVisit(java.lang.String elementName, java.lang.String value) { | ||
collect(value); | ||
} | ||
|
||
@Override | ||
public void doVisit(java.lang.String elementName, Year value) { | ||
collect(value); | ||
} | ||
|
||
@Override | ||
public void doVisit(java.lang.String elementName, YearMonth value) { | ||
collect(value); | ||
} | ||
|
||
@Override | ||
public void doVisit(java.lang.String elementName, ZonedDateTime value) { | ||
collect(value); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
fhir-model/src/test/java/com/ibm/fhir/model/visitor/test/PathAwareCollectingVisitorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* (C) Copyright IBM Corp. 2022 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package com.ibm.fhir.model.visitor.test; | ||
|
||
import java.time.LocalDate; | ||
import java.time.ZoneOffset; | ||
import java.time.ZonedDateTime; | ||
|
||
import org.testng.annotations.Test; | ||
|
||
import com.ibm.fhir.model.resource.Patient; | ||
import com.ibm.fhir.model.resource.ValueSet; | ||
import com.ibm.fhir.model.resource.ValueSet.Expansion; | ||
import com.ibm.fhir.model.type.DateTime; | ||
import com.ibm.fhir.model.type.Extension; | ||
import com.ibm.fhir.model.type.HumanName; | ||
import com.ibm.fhir.model.type.Meta; | ||
import com.ibm.fhir.model.type.Narrative; | ||
import com.ibm.fhir.model.type.Xhtml; | ||
import com.ibm.fhir.model.type.code.NarrativeStatus; | ||
import com.ibm.fhir.model.type.code.PublicationStatus; | ||
import com.ibm.fhir.model.visitor.PathAwareCollectingVisitor; | ||
|
||
public class PathAwareCollectingVisitorTest { | ||
@Test | ||
public void testPrimitiveSetterEquivalence() { | ||
Patient p1 = Patient.builder() | ||
.text(Narrative.builder() | ||
.status(NarrativeStatus.ADDITIONAL) | ||
.div(Xhtml.of(Xhtml.DIV_OPEN + "<div>this<br/>is<br/>a test</div>" + Xhtml.DIV_CLOSE)) | ||
.build()) | ||
.meta(Meta.builder() | ||
.lastUpdated(ZonedDateTime.of(2021, 8, 19, 00, 59, 59, 0, ZoneOffset.of("-05:00"))) // Instant | ||
.build()) | ||
.extension(Extension.builder() | ||
.url("test") | ||
.value("string") | ||
.build()) | ||
.contained(ValueSet.builder() | ||
.status(PublicationStatus.DRAFT) | ||
.expansion(Expansion.builder() | ||
.timestamp(DateTime.of("2021-08-19T00:59:59-05:00")) | ||
.total(0) // Integer | ||
.build()) | ||
.build()) | ||
.active(false) // Boolean | ||
.birthDate(LocalDate.of(1984, 9, 4)) // Date | ||
.multipleBirth(1) | ||
.name(HumanName.builder() | ||
.given("Lee") // String | ||
.build()) | ||
.build(); | ||
|
||
PathAwareCollectingVisitor<Extension> extCollector = new PathAwareCollectingVisitor<Extension>(Extension.class); | ||
p1.accept(extCollector); | ||
System.out.println(extCollector.getResult()); | ||
} | ||
} |
Oops, something went wrong.