Skip to content

Commit

Permalink
fixed problem with n10s.inference.labels() when no ontology present
Browse files Browse the repository at this point in the history
  • Loading branch information
jbarrasa committed Jan 16, 2024
1 parent f72c882 commit 3822d08
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 10 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.neo4j</groupId>
<artifactId>neosemantics</artifactId>
<version>5.14.0</version>
<version>5.14.1</version>
<packaging>jar</packaging>
<name>neosemantics (n10s)</name>
<description>n10s is a plugin that enables the use of RDF in Neo4j</description>
Expand Down
39 changes: 30 additions & 9 deletions src/main/java/n10s/inference/MicroReasoners.java
Original file line number Diff line number Diff line change
Expand Up @@ -215,21 +215,42 @@ public Stream<RelAndNodeResult> getRels(@Name("node") Node node, @Name("rel") St
public Stream<LabelNameResult> labels(@Name(value = "params", defaultValue = "{}") Map<String, Object> props) throws MicroReasonerException {
final GraphConfig gc = getGraphConfig();

if (gc == null && missingParams(props, "catLabel", "catNameProp", "subCatRel")) {
throw new MicroReasonerException("No GraphConfig or in-procedure params (catLabel, catNameProp, subCatRel). Method cannot be run.");
if (gc == null && missingParams(props, "catLabel", "catNameProp", "subCatRel", "relLabel", "propLabel")) {
throw new MicroReasonerException("No GraphConfig or in-procedure params (catLabel, catNameProp, subCatRel, relLabel, propLabel). Method cannot be run.");
}

String cypher = getInferredLabelsQuery((String) props.getOrDefault("catLabel",gc.getClassLabelName()),
(String) props.getOrDefault("subCatRel",gc.getSubClassOfRelName()),
(String) props.getOrDefault("catNameProp",gc.getClassNamePropName()));
String cypher;

if (gc == null){
cypher = getInferredLabelsQuery((String) props.get("catLabel"),
(String) props.get("subCatRel"),
(String) props.get("catNameProp"),
(String) props.get("relLabel"),
(String) props.get("propLabel"));
} else {
cypher = getInferredLabelsQuery((String) props.getOrDefault("catLabel", gc.getClassLabelName()),
(String) props.getOrDefault("subCatRel", gc.getSubClassOfRelName()),
(String) props.getOrDefault("catNameProp", gc.getClassNamePropName()),
(String) props.getOrDefault("relLabel", gc.getObjectPropertyLabelName()),
(String) props.getOrDefault("propLabel", gc.getDataTypePropertyLabelName()));
}
return tx.execute(cypher).stream().map(n -> (String) n.get("inferredlabel")).map(LabelNameResult::new);
}

private static final String getInferredLabelsQuery(String catLabel, String subCatRel, String catNameProp ) {
return "call db.labels() yield label\n" +
" match hierarchy = (:`" + catLabel + "` { `" + catNameProp + "`: label })-[:`" + subCatRel + "`*0..]->(p) where size([(p)-[s:`" + subCatRel + "`]->() | s]) = 0\n" +
" unwind [n in nodes(hierarchy) | n.`" + catNameProp + "` ] as inferredlabel return distinct inferredlabel";
private static final String getInferredLabelsQuery(String catLabel, String subCatRel, String catNameProp,
String relLabel, String propLabel ) {
return "call db.labels() yield label " +
" where not label in ['_GraphConfig', 'Resource', '_NsPrefDef', '" + relLabel + "', '" + propLabel + "', '" + catLabel + "', '_MapNs', '_MapDef'] " +
" call { " +
" with label " +
" return label as inferredlabel " +
" union " +
" with label " +
" match hierarchy = (:`" + catLabel + "` { `" + catNameProp + "`: label })-[:`" + subCatRel + "`*0..]->(p) where size([(p)-[s:`" + subCatRel + "`]->() | s]) = 0 " +
" unwind [n in nodes(hierarchy) | n.`" + catNameProp + "` ] as inferredlabel " +
" return distinct inferredlabel " +
" } " +
" return distinct inferredlabel " ;
}

private static final String getClassRelsFromOntoQuery(boolean outgoing, boolean includeAll, String catLabel,
Expand Down
36 changes: 36 additions & 0 deletions src/test/java/n10s/inference/MicroReasonersTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,42 @@ public void testGetInferredLabels() throws Exception {
assertEquals(Set.of("Movie", "ArticsticCreation","Person", "Critic"), inferredLabelSet);
}

@Test
public void testGetInferredLabelsNoOntoWithConfig() throws Exception {
Session session = driver.session();


session.run("CREATE CONSTRAINT n10s_unique_uri FOR (r:Resource) REQUIRE r.uri IS UNIQUE");
session.run("call n10s.graphconfig.init({ handleVocabUris: 'IGNORE', classLabel: 'Label', " +
"subClassOfRel: 'SLO' })");

session.run("create (:Actor { name: 'Keaanu Reeves', born: 1968 })" +
"-[:ACTED_IN]->(:Movie { title: 'The Matrix', released: 1999 })");

String queryGetInferredLabels = "call n10s.inference.labels() yield label return collect(label) as labels";
Set inferredLabelSet = new HashSet<>(session.run(queryGetInferredLabels).next().get("labels").asList());
assertEquals(Set.of("Actor", "Movie"), inferredLabelSet);
session.run("match (a:Actor { name: 'Keaanu Reeves'}) detach delete a");
inferredLabelSet = new HashSet<>(session.run(queryGetInferredLabels).next().get("labels").asList());
assertEquals(Set.of("Movie"), new HashSet<>(inferredLabelSet));
}

@Test
public void testGetInferredLabelsNoOntoNoConfig() throws Exception {
Session session = driver.session();


session.run("create (:Actor { name: 'Keaanu Reeves', born: 1968 })" +
"-[:ACTED_IN]->(:Movie { title: 'The Matrix', released: 1999 })");

String queryGetInferredLabels = "call n10s.inference.labels({ catLabel:'Class', catNameProp: 'name', subCatRel: 'SCO', relLabel: 'Relationship', propLabel: 'Property'}) yield label return collect(label) as labels";
Set inferredLabelSet = new HashSet<>(session.run(queryGetInferredLabels).next().get("labels").asList());
assertEquals(Set.of("Actor", "Movie"), inferredLabelSet);
session.run("match (a:Actor { name: 'Keaanu Reeves'}) detach delete a");
inferredLabelSet = new HashSet<>(session.run(queryGetInferredLabels).next().get("labels").asList());
assertEquals(Set.of("Movie"), new HashSet<>(inferredLabelSet));
}


@Test
public void testHasLabelCustom() throws Exception {
Expand Down

0 comments on commit 3822d08

Please sign in to comment.