Skip to content

Commit

Permalink
Issue #1980 - add property support to GraphTermServiceProvider
Browse files Browse the repository at this point in the history
Signed-off-by: John T.E. Timm <johntimm@us.ibm.com>
  • Loading branch information
JohnTimm committed Mar 2, 2021
1 parent bb3aa9f commit a7e76a2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@
import com.ibm.fhir.model.resource.CodeSystem;
import com.ibm.fhir.model.resource.CodeSystem.Concept;
import com.ibm.fhir.model.resource.CodeSystem.Concept.Designation;
import com.ibm.fhir.model.resource.CodeSystem.Concept.Property;
import com.ibm.fhir.model.resource.ValueSet.Compose.Include.Filter;
import com.ibm.fhir.model.type.Code;
import com.ibm.fhir.model.type.Coding;
import com.ibm.fhir.model.type.Element;
import com.ibm.fhir.model.type.Uri;
import com.ibm.fhir.term.graph.FHIRTermGraph;
import com.ibm.fhir.term.graph.FHIRTermGraphFactory;
Expand Down Expand Up @@ -63,18 +65,19 @@ public boolean hasConcept(CodeSystem codeSystem, Code code) {
@Override
public Concept getConcept(CodeSystem codeSystem, Code code) {
Objects.requireNonNull(codeSystem.getUrl(), "CodeSystem.url");
return getConcept(codeSystem, code, true);
return getConcept(codeSystem, code, true, true);
}

private Concept getConcept(CodeSystem codeSystem, Code code, boolean includeDesignations) {
private Concept getConcept(CodeSystem codeSystem, Code code, boolean includeDesignations, boolean includeProperties) {
Objects.requireNonNull(codeSystem.getUrl(), "CodeSystem.url");
return createConcept(
codeSystem,
code.getValue(),
whereCodeSystem(hasCode(g.V(), code.getValue(), isCaseSensitive(codeSystem)), codeSystem)
.elementMap()
.tryNext(),
includeDesignations);
includeDesignations,
includeProperties);
}

@Override
Expand Down Expand Up @@ -112,7 +115,7 @@ public Set<Concept> getConcepts(CodeSystem codeSystem, List<Filter> filters) {
public Set<Concept> closure(CodeSystem codeSystem, Code code) {
Objects.requireNonNull(codeSystem.getUrl(), "CodeSystem.url");
Set<Concept> concepts = new LinkedHashSet<>();
concepts.add(getConcept(codeSystem, code, false));
concepts.add(getConcept(codeSystem, code, false, false));
whereCodeSystem(hasCode(g.V(), code.getValue(), isCaseSensitive(codeSystem)), codeSystem)
.repeat(__.in("isA")
.simplePath()
Expand Down Expand Up @@ -164,22 +167,23 @@ private GraphTraversal<Vertex, Vertex> whereCodeSystem(GraphTraversal<Vertex, Ve
return g.where(hasVersion(hasUrl(__.in("concept").hasLabel("CodeSystem"), codeSystem.getUrl()), codeSystem.getVersion()));
}

private Concept createConcept(CodeSystem codeSystem, String code, Optional<Map<Object, Object>> optional, boolean includeDesignations) {
private Concept createConcept(CodeSystem codeSystem, String code, Optional<Map<Object, Object>> optional, boolean includeDesignations, boolean includeProperties) {
if (optional.isPresent()) {
return createConcept(optional.get(), includeDesignations ? getDesignations(codeSystem, code) : Collections.emptyList());
return createConcept(optional.get(), includeDesignations ? getDesignations(codeSystem, code) : Collections.emptyList(), includeProperties ? getProperties(codeSystem, code) : Collections.emptyList());
}
return null;
}

private Concept createConcept(Map<Object, Object> elementMap) {
return createConcept(elementMap, Collections.emptyList());
return createConcept(elementMap, Collections.emptyList(), Collections.emptyList());
}

private Concept createConcept(Map<Object, Object> elementMap, List<Designation> designations) {
private Concept createConcept(Map<Object, Object> elementMap, List<Designation> designations, List<Property> properties) {
return Concept.builder()
.code(Code.of((String) elementMap.get("code")))
.display(string((String) elementMap.get("display")))
.designation(designations)
.property(properties)
.build();
}

Expand Down Expand Up @@ -210,6 +214,33 @@ private List<Designation> getDesignations(CodeSystem codeSystem, String code) {
return designations;
}

private List<Property> getProperties(CodeSystem codeSystem, String code) {
Objects.requireNonNull(codeSystem.getUrl(), "CodeSystem.url");
List<Property> properties = new ArrayList<>();
whereCodeSystem(hasCode(g.V(), code, isCaseSensitive(codeSystem)), codeSystem)
.out("property_")
.elementMap()
.toStream()
.forEach(elementMap -> properties.add(createProperty(elementMap)));
return properties;
}

private Property createProperty(Map<Object, Object> elementMap) {
return Property.builder()
.code(Code.of((String) elementMap.get("code")))
.value(getElement(elementMap))
.build();
}

private Element getElement(Map<Object, Object> elementMap) {
for (Object value : elementMap.values()) {
if (value instanceof Element) {
return (Element) value;
}
}
return null;
}

private String getDesignationUseSystem(CodeSystem codeSystem) {
Objects.requireNonNull(codeSystem.getUrl(), "CodeSystem.url");
Optional<Map<Object, Object>> optional = hasVersion(hasUrl(g.V(), codeSystem.getUrl()), codeSystem.getVersion())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public static void main(String[] args) throws Exception {

GraphTermServiceProvider provider = new GraphTermServiceProvider(new PropertiesConfiguration("conf/local-graph.properties"));
System.out.println(provider.subsumes(codeSystem, Code.of("m"), Code.of("p")));
System.out.println(provider.getConcept(codeSystem, Code.of("o")));
provider.getGraph().close();
}
}
Expand Down

0 comments on commit a7e76a2

Please sign in to comment.