Skip to content

Commit 3822d08

Browse files
committed
fixed problem with n10s.inference.labels() when no ontology present
1 parent f72c882 commit 3822d08

File tree

3 files changed

+67
-10
lines changed

3 files changed

+67
-10
lines changed

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<groupId>org.neo4j</groupId>
77
<artifactId>neosemantics</artifactId>
8-
<version>5.14.0</version>
8+
<version>5.14.1</version>
99
<packaging>jar</packaging>
1010
<name>neosemantics (n10s)</name>
1111
<description>n10s is a plugin that enables the use of RDF in Neo4j</description>

src/main/java/n10s/inference/MicroReasoners.java

+30-9
Original file line numberDiff line numberDiff line change
@@ -215,21 +215,42 @@ public Stream<RelAndNodeResult> getRels(@Name("node") Node node, @Name("rel") St
215215
public Stream<LabelNameResult> labels(@Name(value = "params", defaultValue = "{}") Map<String, Object> props) throws MicroReasonerException {
216216
final GraphConfig gc = getGraphConfig();
217217

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

222-
String cypher = getInferredLabelsQuery((String) props.getOrDefault("catLabel",gc.getClassLabelName()),
223-
(String) props.getOrDefault("subCatRel",gc.getSubClassOfRelName()),
224-
(String) props.getOrDefault("catNameProp",gc.getClassNamePropName()));
222+
String cypher;
225223

224+
if (gc == null){
225+
cypher = getInferredLabelsQuery((String) props.get("catLabel"),
226+
(String) props.get("subCatRel"),
227+
(String) props.get("catNameProp"),
228+
(String) props.get("relLabel"),
229+
(String) props.get("propLabel"));
230+
} else {
231+
cypher = getInferredLabelsQuery((String) props.getOrDefault("catLabel", gc.getClassLabelName()),
232+
(String) props.getOrDefault("subCatRel", gc.getSubClassOfRelName()),
233+
(String) props.getOrDefault("catNameProp", gc.getClassNamePropName()),
234+
(String) props.getOrDefault("relLabel", gc.getObjectPropertyLabelName()),
235+
(String) props.getOrDefault("propLabel", gc.getDataTypePropertyLabelName()));
236+
}
226237
return tx.execute(cypher).stream().map(n -> (String) n.get("inferredlabel")).map(LabelNameResult::new);
227238
}
228239

229-
private static final String getInferredLabelsQuery(String catLabel, String subCatRel, String catNameProp ) {
230-
return "call db.labels() yield label\n" +
231-
" match hierarchy = (:`" + catLabel + "` { `" + catNameProp + "`: label })-[:`" + subCatRel + "`*0..]->(p) where size([(p)-[s:`" + subCatRel + "`]->() | s]) = 0\n" +
232-
" unwind [n in nodes(hierarchy) | n.`" + catNameProp + "` ] as inferredlabel return distinct inferredlabel";
240+
private static final String getInferredLabelsQuery(String catLabel, String subCatRel, String catNameProp,
241+
String relLabel, String propLabel ) {
242+
return "call db.labels() yield label " +
243+
" where not label in ['_GraphConfig', 'Resource', '_NsPrefDef', '" + relLabel + "', '" + propLabel + "', '" + catLabel + "', '_MapNs', '_MapDef'] " +
244+
" call { " +
245+
" with label " +
246+
" return label as inferredlabel " +
247+
" union " +
248+
" with label " +
249+
" match hierarchy = (:`" + catLabel + "` { `" + catNameProp + "`: label })-[:`" + subCatRel + "`*0..]->(p) where size([(p)-[s:`" + subCatRel + "`]->() | s]) = 0 " +
250+
" unwind [n in nodes(hierarchy) | n.`" + catNameProp + "` ] as inferredlabel " +
251+
" return distinct inferredlabel " +
252+
" } " +
253+
" return distinct inferredlabel " ;
233254
}
234255

235256
private static final String getClassRelsFromOntoQuery(boolean outgoing, boolean includeAll, String catLabel,

src/test/java/n10s/inference/MicroReasonersTest.java

+36
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,42 @@ public void testGetInferredLabels() throws Exception {
448448
assertEquals(Set.of("Movie", "ArticsticCreation","Person", "Critic"), inferredLabelSet);
449449
}
450450

451+
@Test
452+
public void testGetInferredLabelsNoOntoWithConfig() throws Exception {
453+
Session session = driver.session();
454+
455+
456+
session.run("CREATE CONSTRAINT n10s_unique_uri FOR (r:Resource) REQUIRE r.uri IS UNIQUE");
457+
session.run("call n10s.graphconfig.init({ handleVocabUris: 'IGNORE', classLabel: 'Label', " +
458+
"subClassOfRel: 'SLO' })");
459+
460+
session.run("create (:Actor { name: 'Keaanu Reeves', born: 1968 })" +
461+
"-[:ACTED_IN]->(:Movie { title: 'The Matrix', released: 1999 })");
462+
463+
String queryGetInferredLabels = "call n10s.inference.labels() yield label return collect(label) as labels";
464+
Set inferredLabelSet = new HashSet<>(session.run(queryGetInferredLabels).next().get("labels").asList());
465+
assertEquals(Set.of("Actor", "Movie"), inferredLabelSet);
466+
session.run("match (a:Actor { name: 'Keaanu Reeves'}) detach delete a");
467+
inferredLabelSet = new HashSet<>(session.run(queryGetInferredLabels).next().get("labels").asList());
468+
assertEquals(Set.of("Movie"), new HashSet<>(inferredLabelSet));
469+
}
470+
471+
@Test
472+
public void testGetInferredLabelsNoOntoNoConfig() throws Exception {
473+
Session session = driver.session();
474+
475+
476+
session.run("create (:Actor { name: 'Keaanu Reeves', born: 1968 })" +
477+
"-[:ACTED_IN]->(:Movie { title: 'The Matrix', released: 1999 })");
478+
479+
String queryGetInferredLabels = "call n10s.inference.labels({ catLabel:'Class', catNameProp: 'name', subCatRel: 'SCO', relLabel: 'Relationship', propLabel: 'Property'}) yield label return collect(label) as labels";
480+
Set inferredLabelSet = new HashSet<>(session.run(queryGetInferredLabels).next().get("labels").asList());
481+
assertEquals(Set.of("Actor", "Movie"), inferredLabelSet);
482+
session.run("match (a:Actor { name: 'Keaanu Reeves'}) detach delete a");
483+
inferredLabelSet = new HashSet<>(session.run(queryGetInferredLabels).next().get("labels").asList());
484+
assertEquals(Set.of("Movie"), new HashSet<>(inferredLabelSet));
485+
}
486+
451487

452488
@Test
453489
public void testHasLabelCustom() throws Exception {

0 commit comments

Comments
 (0)