Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updated logic to get label for relationship. #100

Merged
merged 1 commit into from
Nov 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public Map<String, Object> getGraphForIndividual(String iri, String ontologyId,
private Map<String, Object> getGraphForEntity(String iri, String type, String neo4jType, String ontologyId, String lang) {

String thisEntityId = ontologyId + "+" + type + "+" + iri;
List<JsonObject> selectedNode = new ArrayList<>();

// String parentsQuery =
// "MATCH path = (n:OntologyClass)-[r:directParent|relatedTo]-(parent)\n"
Expand Down Expand Up @@ -67,6 +68,10 @@ private Map<String, Object> getGraphForEntity(String iri, String type, String ne
List<Map<String,Object>> nodes = allNodes.stream().map(node -> {

JsonObject ontologyNodeObject = getOntologyNodeJson(node, lang);

if(iri.equals(JsonHelper.getString(ontologyNodeObject, "iri"))) {
selectedNode.add(ontologyNodeObject);
}

JsonObject linkedEntities = ontologyNodeObject.getAsJsonObject("linkedEntities");
if(linkedEntities != null) {
Expand Down Expand Up @@ -95,7 +100,7 @@ private Map<String, Object> getGraphForEntity(String iri, String type, String ne

JsonObject ontologyEdgeObject = getOntologyEdgeJson(relationship, lang);

String uri = JsonHelper.getString(ontologyEdgeObject, "property");
String uri = resolveUri(result, selectedNode, iri);
if (uri == null) {
uri = "http://www.w3.org/2000/01/rdf-schema#subClassOf";
}
Expand Down Expand Up @@ -166,5 +171,48 @@ JsonObject getOntologyEdgeJson(Relationship r, String lang) {
).getAsJsonObject();
}

private String resolveUri(Map<String, Object> result, List<JsonObject> selectedNode, String iri) {
if (selectedNode == null || selectedNode.isEmpty()) {
return null;
}

JsonObject selectedOntologyNodeObject = selectedNode.get(0);
if (iri.equals(result.get("source"))) {
return getRelatedProperty(selectedOntologyNodeObject, "relatedTo", (String) result.get("target"));
} else {
return getRelatedProperty(selectedOntologyNodeObject, "relatedFrom", (String) result.get("source"));
}
}

private String getRelatedProperty(JsonObject ontologyNode, String relationKey, String targetOrSourceIri) {
if (ontologyNode == null || !ontologyNode.has(relationKey)) {
return null;
}

JsonElement related = ontologyNode.get(relationKey);
if (related == null || related instanceof JsonNull) {
return null;
}

if (related.isJsonArray()) {
for (JsonElement element : related.getAsJsonArray()) {
JsonObject relationObject = element.getAsJsonObject();
if (relationObject != null
&& targetOrSourceIri.equals(JsonHelper.getString(relationObject, "value"))
&& relationObject.has("property")) {
return JsonHelper.getString(relationObject, "property");
}
}
} else if (related.isJsonObject()) {
JsonObject relationObject = related.getAsJsonObject();
if (relationObject != null
&& targetOrSourceIri.equals(JsonHelper.getString(relationObject, "value"))
&& relationObject.has("property")) {
return JsonHelper.getString(relationObject, "property");
}
}

return null;
}
}