Skip to content

Commit d206b09

Browse files
committed
added unit tests for #225
1 parent c462cef commit d206b09

File tree

2 files changed

+99
-6
lines changed

2 files changed

+99
-6
lines changed

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public class MicroReasoners {
3636
+ " WITH collect(label) as labels MATCH path = (c:`%1$s`)<-[:`%3$s`*]-(s:`%1$s`) "
3737
+ " WHERE s.`%2$s` in labels AND NOT (c)-[:`%3$s`]->() AND any(x in nodes (path) "
3838
+ " WHERE x.`%2$s` = $virtLabel ) RETURN COLLECT(DISTINCT s.`%2$s`) + $virtLabel as l";
39+
3940
private static final String subcatPathQuery =
4041
"MATCH (x:`%1$s` { `%2$s`: $oneOfCats } ) MATCH (y:`%1$s` { `%2$s`: $virtLabel } ) "
4142
+ " WHERE (x)-[:`%3$s`*]->(y) RETURN count(x) > 0 as isTrue ";
@@ -60,7 +61,7 @@ public class MicroReasoners {
6061
* semantics (cat:Cat { name: 'xyz'})-[:SCO]->(parent:Cat { name: ''}) */
6162

6263
@Procedure(mode = Mode.READ)
63-
@Description("semantics.inference.nodesLabelled('label') - returns all nodes with label 'label' or its sublabels.")
64+
@Description("n10s.inference.nodesLabelled('label') - returns all nodes with label 'label' or its sublabels.")
6465
public Stream<NodeResult> nodesLabelled(@Name("label") String virtLabel,
6566
@Name(value = "params", defaultValue = "{}") Map<String, Object> props) throws MicroReasonerException {
6667
final GraphConfig gc = getGraphConfig();
@@ -103,7 +104,7 @@ private GraphConfig getGraphConfig() {
103104
/* in this case the node representing the category exist in the graph and is explicitly linked to the instances of the category
104105
* hence the use of a node as param */
105106
@Procedure(mode = Mode.READ)
106-
@Description("semantics.inference.nodesInCategory('category') - returns all nodes connected to Node 'catNode' or its subcategories.")
107+
@Description("n10s.inference.nodesInCategory('category') - returns all nodes connected to Node 'catNode' or its subcategories.")
107108
public Stream<NodeResult> nodesInCategory(@Name("category") Node catNode,
108109
@Name(value = "params", defaultValue = "{}") Map<String, Object> props) throws MicroReasonerException {
109110

@@ -165,7 +166,7 @@ private List<Long> getSuperCatIds(long catNodeId, String subCatRelName, GraphCon
165166

166167
@Procedure(mode = Mode.READ)
167168
@Description(
168-
"semantics.inference.getRels(node,'rel', { relDir: '>'} ) - returns all relationships "
169+
"n10s.inference.getRels(node,'rel', { relDir: '>'} ) - returns all relationships "
169170
+ "of type 'rel' or its subtypes along with the target nodes.")
170171
public Stream<RelAndNodeResult> getRels(@Name("node") Node node, @Name("rel") String virtRel,
171172
@Name(value = "params", defaultValue = "{}") Map<String, Object> props) throws MicroReasonerException {
@@ -204,7 +205,7 @@ public Stream<RelAndNodeResult> getRels(@Name("node") Node node, @Name("rel") St
204205

205206
@UserFunction
206207
@Description(
207-
"semantics.inference.hasLabel(node,'label',{}) - checks whether node is explicitly or "
208+
"n10s.inference.hasLabel(node,'label',{}) - checks whether node is explicitly or "
208209
+ "implicitly labeled as 'label'.")
209210
public boolean hasLabel(
210211
@Name("node") Node individual,
@@ -248,7 +249,7 @@ private boolean missingParams(Map<String, Object> props, String... paramNames) {
248249

249250

250251
@UserFunction
251-
@Description("semantics.inference.inCategory(node, category, {}) - checks whether node is explicitly or implicitly in a category.")
252+
@Description("n10s.inference.inCategory(node, category, {}) - checks whether node is explicitly or implicitly in a category.")
252253
public boolean inCategory(
253254
@Name("node") Node individual, @Name("category") Node category,
254255
@Name(value = "params", defaultValue = "{}") Map<String, Object> props) {

src/test/java/n10s/RDFExportTest.java

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.eclipse.rdf4j.model.Statement;
1717
import org.eclipse.rdf4j.model.ValueFactory;
1818
import org.eclipse.rdf4j.model.impl.SimpleValueFactory;
19+
import org.eclipse.rdf4j.model.vocabulary.FOAF;
1920
import org.eclipse.rdf4j.model.vocabulary.RDF;
2021
import org.eclipse.rdf4j.model.vocabulary.XSD;
2122
import org.eclipse.rdf4j.rio.RDFFormat;
@@ -46,7 +47,7 @@ public class RDFExportTest {
4647

4748

4849
@Test
49-
public void testExportFromCypher() throws Exception {
50+
public void testExportFromCypherOnLPG() throws Exception {
5051
try (Driver driver = GraphDatabase.driver(neo4j.boltURI(),
5152
Config.builder().withoutEncryption().build()); Session session = driver.session()) {
5253

@@ -79,6 +80,97 @@ public void testExportFromCypher() throws Exception {
7980
}
8081
}
8182

83+
@Test
84+
public void testExportFromCypherOnLPGWithMappings() throws Exception {
85+
try (Driver driver = GraphDatabase.driver(neo4j.boltURI(),
86+
Config.builder().withoutEncryption().build()); Session session = driver.session()) {
87+
88+
session
89+
.run(
90+
"CREATE (n:Node { a: 1, b: 'hello' })-[:CONNECTED_TO]->(:Node { a:2, b2:'bye@en'})");
91+
92+
session.run("call n10s.nsprefixes.add('foaf','http://xmlns.com/foaf/0.1/')");
93+
session.run("call n10s.nsprefixes.add('myv','http://myvoc.org/testing#')");
94+
assertEquals(2L, session.run("call n10s.nsprefixes.list() yield prefix return count(*) as ct").next().get("ct").asLong());
95+
session.run("call n10s.mapping.add('http://xmlns.com/foaf/0.1/linkedTo','CONNECTED_TO')");
96+
session.run("call n10s.mapping.add('http://xmlns.com/foaf/0.1/Thang','Node')");
97+
session.run("call n10s.mapping.add('http://myvoc.org/testing#propA','a')");
98+
session.run("call n10s.mapping.add('http://myvoc.org/testing#propB','b')");
99+
List<Object> mappings = session.run("call n10s.mapping.list() yield schemaNs, schemaElement, elemName\n" +
100+
"return collect ({uri: schemaNs + schemaElement, elem: elemName}) as m").next().get("m").asList();
101+
assertEquals(4L, mappings.size());
102+
103+
104+
Result res
105+
= session
106+
.run(" CALL n10s.rdf.export.cypher(' MATCH path = (n)-[r]->(m) RETURN path ', {}) ");
107+
assertTrue(res.hasNext());
108+
109+
final ValueFactory vf = SimpleValueFactory.getInstance();
110+
Set<Statement> expectedStatememts = new HashSet<>(Arrays.asList(
111+
vf.createStatement(vf.createIRI(BASE_INDIV_NS + "0"), RDF.TYPE, vf.createIRI("http://xmlns.com/foaf/0.1/Thang")),
112+
vf.createStatement(vf.createIRI(BASE_INDIV_NS + "0"), vf.createIRI("http://myvoc.org/testing#propA"), vf.createLiteral(1L)),
113+
vf.createStatement(vf.createIRI(BASE_INDIV_NS + "0"), vf.createIRI("http://myvoc.org/testing#propB"), vf.createLiteral("hello")),
114+
vf.createStatement(vf.createIRI(BASE_INDIV_NS + "0"), vf.createIRI("http://xmlns.com/foaf/0.1/linkedTo"), vf.createIRI(BASE_INDIV_NS + "1")),
115+
vf.createStatement(vf.createIRI(BASE_INDIV_NS + "1"), RDF.TYPE, vf.createIRI("http://xmlns.com/foaf/0.1/Thang")),
116+
vf.createStatement(vf.createIRI(BASE_INDIV_NS + "1"), vf.createIRI(DEFAULT_BASE_SCH_NS + "b2"), vf.createLiteral("bye","en")),
117+
vf.createStatement(vf.createIRI(BASE_INDIV_NS + "1"), vf.createIRI("http://myvoc.org/testing#propA"), vf.createLiteral(2L))));
118+
119+
int resultCount = 0;
120+
while (res.hasNext()) {
121+
Statement returnedStatement = recordAsStatement(vf, res.next());
122+
assertTrue(expectedStatememts.contains(returnedStatement));
123+
resultCount++;
124+
}
125+
assertEquals(resultCount,expectedStatememts.size());
126+
}
127+
}
128+
129+
@Test
130+
public void testExportFromCypherOnRDF() throws Exception {
131+
try (Driver driver = GraphDatabase.driver(neo4j.boltURI(),
132+
Config.builder().withoutEncryption().build())) {
133+
134+
Session session = driver.session();
135+
136+
initialiseGraphDB(neo4j.defaultDatabaseService(),
137+
" { handleVocabUris: 'SHORTEN' } ");
138+
139+
}
140+
try (Driver driver = GraphDatabase.driver(neo4j.boltURI(),
141+
Config.builder().withoutEncryption().build()); Session session = driver.session()) {
142+
143+
Result importResults1 = session.run("CALL n10s.rdf.import.inline('" +
144+
jsonLdFragment + "','JSON-LD')");
145+
assertEquals(11L, importResults1.single().get("triplesLoaded").asLong());
146+
147+
Result res
148+
= session
149+
.run(" CALL n10s.rdf.export.cypher(' MATCH path = (n)-[r]->(m) RETURN path ', {}) ");
150+
assertTrue(res.hasNext());
151+
152+
final ValueFactory vf = SimpleValueFactory.getInstance();
153+
Set<Statement> expectedStatememts = new HashSet<>(Arrays.asList(
154+
vf.createStatement(vf.createIRI("http://me.markus-lanthaler.com/"), RDF.TYPE, vf.createIRI("http://xmlns.com/foaf/0.1/Individual")),
155+
vf.createStatement(vf.createIRI("http://me.markus-lanthaler.com/"), FOAF.NAME, vf.createLiteral("Markus Lanthaler")),
156+
vf.createStatement(vf.createIRI("http://me.markus-lanthaler.com/"), FOAF.KNOWS, vf.createIRI("http://manu.sporny.org/about#manu")),
157+
vf.createStatement(vf.createIRI("http://manu.sporny.org/about#manu"), RDF.TYPE,vf.createIRI("http://xmlns.com/foaf/0.1/Subject")),
158+
vf.createStatement(vf.createIRI("http://manu.sporny.org/about#manu"), FOAF.NAME, vf.createLiteral("Manu Sporny")),
159+
vf.createStatement(vf.createIRI("http://manu.sporny.org/about#manu"), RDF.TYPE,vf.createIRI("http://xmlns.com/foaf/0.1/Citizen"))
160+
));
161+
162+
int resultCount = 0;
163+
while (res.hasNext()) {
164+
Statement returnedStatement = recordAsStatement(vf, res.next());
165+
assertTrue(returnedStatement.getSubject().stringValue().startsWith("bnode://") ||
166+
returnedStatement.getObject().stringValue().startsWith("bnode://") ||
167+
expectedStatememts.contains(returnedStatement));
168+
resultCount++;
169+
}
170+
assertEquals(9,resultCount);
171+
}
172+
}
173+
82174
private Statement recordAsStatement(ValueFactory vf, Record r) {
83175
IRI s = vf.createIRI(r.get("subject").asString());
84176
IRI p = vf.createIRI(r.get("predicate").asString());

0 commit comments

Comments
 (0)