Skip to content

Commit

Permalink
Issue #1980 - updates
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 10, 2021
1 parent 9e914ee commit 9bac1af
Show file tree
Hide file tree
Showing 9 changed files with 158 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ default Stream<Result<JanusGraphVertex>> indexQuery(String query) {
Stream<Result<JanusGraphVertex>> indexQuery(String query, int limit);
void close();
void drop();
void dropAllVertices();
}
Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,13 @@ public void drop() {
graph = null;
}
}

@Override
public void dropAllVertices() {
log.info("Dropping all vertices...");
if (traversal != null) {
traversal.V().drop().iterate();
traversal.tx().commit();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

import org.apache.commons.cli.Options;

import com.ibm.fhir.term.graph.FHIRTermGraph;

public interface FHIRTermGraphLoader {
enum Type {
CODESYSTEM {
Expand Down Expand Up @@ -49,4 +51,5 @@ public Options options() {
void load();
void close();
Map<String, String> options();
FHIRTermGraph getGraph();
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.Map;
import java.util.Objects;

import org.apache.commons.configuration.Configuration;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
import org.janusgraph.core.JanusGraph;

Expand All @@ -35,11 +36,30 @@ public AbstractTermGraphLoader(Map<String, String> options) {
g = graph.traversal();

// create label filter
if (options.containsKey("labels")) {
labelFilter = new LabelFilter(new HashSet<>(Arrays.asList(options.get("labels").split(","))));
} else {
labelFilter = LabelFilter.ACCEPT_ALL;
}
labelFilter = createLabelFilter(options);
}

public AbstractTermGraphLoader(Map<String, String> options, Configuration configuration) {
this.options = Objects.requireNonNull(options, "options");
Objects.requireNonNull(configuration, "configuration");

graph = FHIRTermGraphFactory.open(configuration);
janusGraph = graph.getJanusGraph();
g = graph.traversal();

// create label filter
labelFilter = createLabelFilter(options);
}

public AbstractTermGraphLoader(Map<String, String> options, FHIRTermGraph graph) {
this.options = Objects.requireNonNull(options, "options");

this.graph = Objects.requireNonNull(graph, "graph");
janusGraph = graph.getJanusGraph();
g = graph.traversal();

// create label filter
labelFilter = createLabelFilter(options);
}

@Override
Expand All @@ -54,4 +74,15 @@ public final void close() {
public final Map<String, String> options() {
return options;
}

@Override
public final FHIRTermGraph getGraph() {
return graph;
}

protected LabelFilter createLabelFilter(Map<String, String> options) {
return options.containsKey("labels") ?
new LabelFilter(new HashSet<>(Arrays.asList(options.get("labels").split(",")))) :
LabelFilter.ACCEPT_ALL;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
package com.ibm.fhir.term.graph.loader.impl;

import static com.ibm.fhir.term.graph.loader.util.FHIRTermGraphLoaderUtil.toMap;
import static com.ibm.fhir.term.graph.util.FHIRTermGraphUtil.toObject;
import static com.ibm.fhir.term.graph.util.FHIRTermGraphUtil.normalize;
import static com.ibm.fhir.term.graph.util.FHIRTermGraphUtil.toLong;
import static com.ibm.fhir.term.graph.util.FHIRTermGraphUtil.toObject;
import static com.ibm.fhir.term.util.CodeSystemSupport.getConcepts;
import static com.ibm.fhir.term.util.CodeSystemSupport.isCaseSensitive;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
Expand All @@ -27,6 +27,7 @@
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.MissingOptionException;
import org.apache.commons.cli.Options;
import org.apache.commons.configuration.Configuration;
import org.apache.tinkerpop.gremlin.structure.Vertex;

import com.ibm.fhir.model.format.Format;
Expand Down Expand Up @@ -72,6 +73,22 @@ public CodeSystemTermGraphLoader(Map<String, String> options) {
conceptVertexMap = new HashMap<>();
}

public CodeSystemTermGraphLoader(Configuration configuration, CodeSystem codeSystem) {
super(Collections.emptyMap(), configuration);

this.codeSystem = Objects.requireNonNull(codeSystem, "codeSystem");
concepts = getConcepts(codeSystem);
conceptVertexMap = new HashMap<>();
}

public CodeSystemTermGraphLoader(FHIRTermGraph graph, CodeSystem codeSystem) {
super(Collections.emptyMap(), graph);

this.codeSystem = Objects.requireNonNull(codeSystem, "codeSystem");
concepts = getConcepts(codeSystem);
conceptVertexMap = new HashMap<>();
}

@Override
public void load() {
createCodeSystemVertex();
Expand All @@ -88,7 +105,7 @@ private void createCodeSystemVertex() {

codeSystemVertex = g.addV("CodeSystem")
.property("url", url)
.property("caseSensitive", isCaseSensitive(codeSystem))
.property("count", concepts.size())
.next();

if (codeSystem.getVersion() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,18 @@
import com.ibm.fhir.term.spi.FHIRTermServiceProvider;

public class GraphTermServiceProvider implements FHIRTermServiceProvider {
private static final int DEFAULT_COUNT = 1000;
private final FHIRTermGraph graph;

public GraphTermServiceProvider(Configuration configuration) {
Objects.requireNonNull(configuration, "configuration");
graph = FHIRTermGraphFactory.open(configuration);
}

public GraphTermServiceProvider(FHIRTermGraph graph) {
this.graph = Objects.requireNonNull(graph, "graph");
}

@Override
public Set<Concept> closure(CodeSystem codeSystem, Code code) {
Objects.requireNonNull(codeSystem.getUrl(), "CodeSystem.url");
Expand All @@ -84,13 +90,13 @@ public Concept getConcept(CodeSystem codeSystem, Code code) {
@Override
public Set<Concept> getConcepts(CodeSystem codeSystem) {
Objects.requireNonNull(codeSystem.getUrl(), "CodeSystem.url");
List<Concept> concepts = new ArrayList<>(getCount(codeSystem));
Set<Concept> concepts = new LinkedHashSet<>(getCount(codeSystem));
hasVersion(hasUrl(vertices(), codeSystem.getUrl()), codeSystem.getVersion())
.out("concept")
.elementMap()
.toStream()
.forEach(elementMap -> concepts.add(createConcept(elementMap)));
return Collections.emptySet();
return concepts;
}

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -325,7 +331,7 @@ private int getCount(CodeSystem codeSystem) {
if (optional.isPresent()) {
return (Integer) optional.get().get("count");
}
return -1;
return DEFAULT_COUNT;
}

private List<Designation> getDesignations(CodeSystem codeSystem, String code) {
Expand Down Expand Up @@ -393,29 +399,20 @@ private Element getValue(Map<Object, Object> elementMap) {
}

private GraphTraversal<Vertex, Vertex> hasCode(GraphTraversal<Vertex, Vertex> g, String code, boolean caseSensitive) {
if (caseSensitive) {
return g.has("code", code);
}
return g.has("codeLowerCase", normalize(code));
return caseSensitive ? g.has("code", code) : g.has("codeLowerCase", normalize(code));
}

// anonymous graph traversal
private GraphTraversal<Vertex, Vertex> hasCode(String code, boolean caseSensitive) {
if (caseSensitive) {
return __.has("code", code);
}
return __.has("codeLowerCase", normalize(code));
return caseSensitive ? __.has("code", code) : __.has("codeLowerCase", normalize(code));
}

private GraphTraversal<Vertex, Vertex> hasUrl(GraphTraversal<Vertex, Vertex> g, Uri url) {
return g.has("url", url.getValue());
}

private GraphTraversal<Vertex, Vertex> hasVersion(GraphTraversal<Vertex, Vertex> g, com.ibm.fhir.model.type.String version) {
if (version != null) {
return g.has("version", version.getValue());
}
return g;
return (version != null) ? g.has("version", version.getValue()) : g;
}

private GraphTraversal<Vertex, Vertex> vertices(Object... vertexIds) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,62 @@
package com.ibm.fhir.term.graph.test;

import java.io.InputStream;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;

import org.apache.commons.configuration.PropertiesConfiguration;
import org.locationtech.jts.util.Assert;
import org.testng.annotations.Test;

import com.ibm.fhir.model.format.Format;
import com.ibm.fhir.model.parser.FHIRParser;
import com.ibm.fhir.model.resource.CodeSystem;
import com.ibm.fhir.model.type.Code;
import com.ibm.fhir.model.resource.CodeSystem.Concept;
import com.ibm.fhir.term.graph.FHIRTermGraph;
import com.ibm.fhir.term.graph.factory.FHIRTermGraphFactory;
import com.ibm.fhir.term.graph.loader.FHIRTermGraphLoader;
import com.ibm.fhir.term.graph.loader.impl.CodeSystemTermGraphLoader;
import com.ibm.fhir.term.graph.provider.GraphTermServiceProvider;
import com.ibm.fhir.term.graph.util.FHIRTermGraphUtil;
import com.ibm.fhir.term.spi.FHIRTermServiceProvider;
import com.ibm.fhir.term.util.CodeSystemSupport;

import ch.qos.logback.classic.Level;

public class CodeSystemTermGraphLoaderTest {
public static void main(String[] args) throws Exception {
InputStream in = CodeSystemTermGraphLoaderTest.class.getClassLoader().getResourceAsStream("JSON/CodeSystem-cs5.json");
CodeSystem codeSystem = FHIRParser.parser(Format.JSON).parse(in);
GraphTermServiceProvider provider = new GraphTermServiceProvider(new PropertiesConfiguration("conf/janusgraph-berkeleyje-lucene.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();
@Test
public void testCodeSystemTermGraphLoader() throws Exception {
FHIRTermGraphUtil.setRootLoggerLevel(Level.INFO);

FHIRTermGraph graph = null;
try (InputStream in = CodeSystemTermGraphLoaderTest.class.getClassLoader().getResourceAsStream("JSON/CodeSystem-cs5.json")) {
graph = FHIRTermGraphFactory.open(new PropertiesConfiguration("conf/janusgraph-berkeleyje-lucene.properties"));
graph.dropAllVertices();

CodeSystem codeSystem = FHIRParser.parser(Format.JSON).parse(in);
FHIRTermGraphLoader loader = new CodeSystemTermGraphLoader(graph, codeSystem);
loader.load();

FHIRTermServiceProvider provider = new GraphTermServiceProvider(graph);

Set<Concept> expected = new LinkedHashSet<>();
for (Concept concept : CodeSystemSupport.getConcepts(codeSystem)) {
expected.add(concept.toBuilder()
.concept(Collections.emptyList())
.build());
}

Set<Concept> actual = new LinkedHashSet<>();
for (Concept concept : provider.getConcepts(codeSystem)) {
actual.add(provider.getConcept(codeSystem, concept.getCode()));
}

Assert.equals(expected, actual);
} finally {
if (graph != null) {
graph.close();
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static void main(String[] args) throws Exception {
GraphTraversalSource g = graph.traversal();

g.V().drop().iterate();
g.E().drop().iterate();
// g.E().drop().iterate();

Vertex v1 = g.addV("Concept").property("code", "a").next();
System.out.println(v1.id());
Expand Down
Loading

0 comments on commit 9bac1af

Please sign in to comment.