Skip to content

Commit

Permalink
More test #233
Browse files Browse the repository at this point in the history
  • Loading branch information
enridaga committed Mar 23, 2022
1 parent 71b2c20 commit 07436e7
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -296,19 +296,20 @@ private void transformMap(Map o, String dataSourceId, String containerId,
public void triplify(Properties properties, FacadeXGraphBuilder builder)
throws IOException, TriplifierHTTPException {

if(properties.containsKey("json.path") || properties.containsKey("json.path.1`")){
transformFromJSONPath(properties, builder);
List<String> jsonPaths = Triplifier.getPropertyValues(properties, "json.path");
if(!jsonPaths.isEmpty()){
transformFromJSONPath(properties, builder, jsonPaths);
}else {
transform(properties, builder);
}
}

private void transformFromJSONPath(Properties properties, FacadeXGraphBuilder builder) throws TriplifierHTTPException, IOException {
private void transformFromJSONPath(Properties properties, FacadeXGraphBuilder builder, List<String> jsonPaths) throws TriplifierHTTPException, IOException {
JsonSurfer surfer = new JsonSurfer(JacksonParser.INSTANCE, JacksonProvider.INSTANCE);
final InputStream us = Triplifier.getInputStream(properties);
Collector collector = surfer.collector(us);
Set<ValueBox<Collection<Object>>> matches = new HashSet<ValueBox<Collection<Object>>>();
List<String> jsonPaths = Triplifier.getPropertyValues(properties, "json.path");

for(String jpath: jsonPaths) {
ValueBox<Collection<Object>> m = collector.collectAll(jpath);
matches.add(m);
Expand Down Expand Up @@ -488,7 +489,8 @@ public Slice next() {

@Override
public Iterable<Slice> slice(Properties properties) throws IOException, TriplifierHTTPException {
if(properties.containsKey("json.path") || properties.containsKey("json.path.1`")){
List<String> jsonPaths = Triplifier.getPropertyValues(properties, "json.path");
if(!jsonPaths.isEmpty()){
return sliceFromJSONPath(properties);
}else {
return sliceFromArray(properties);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
import org.jsfr.json.JsonSurferJackson;
import org.jsfr.json.ValueBox;
import org.jsfr.json.compiler.JsonPathCompiler;
import org.jsfr.json.provider.JacksonProvider;
import org.junit.Ignore;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -82,34 +84,27 @@ public void testContent() throws TriplifierHTTPException {

}
//
// @Test
// public void jsonPathTest(){
// InputStream is = getClass().getClassLoader().getResourceAsStream("testarray.json");
//// InputStream is = getClass().getClassLoader().getResourceAsStream("SliceArray_2.json");
////// JsonSurfer surfer = new JsonSurfer(JacksonParser.INSTANCE, JacksonProvider.INSTANCE);
//// Collector collector = surfer.collector(is);
//// ValueBox<Collection<Object>> matches = collector.collectAll("$.*");
//// collector.exec();
// final List<JsonParser> parsers = new ArrayList<JsonParser>();
// JsonSurfer surfer = JsonSurferJackson.createSurfer(new JsonFactory(){
// @Override
// public JsonParser createParser(InputStream f) throws IOException {
// parsers.add (super.createParser(f));
// return parsers.get(parsers.size() - 1);
// }
// });
// Collector collector = surfer.collector(is);
// ValueBox<Collection<Object>> matches = collector.collectAll("$.*");
// collector.exec();
// Iterator<Object> iter = matches.get().iterator();
// while(iter.hasNext()){
// Object o = iter.next();
// System.err.println(o.getClass());
// //ObjectNode on = (ObjectNode) o;
// Iterator<Entry> iten = ((Map) o).entrySet().iterator();
// while(iten.hasNext() ) {
// System.err.println(iten.next());
// }
// }
// }
@Ignore
@Test
public void jsonPathTest(){
InputStream is = getClass().getClassLoader().getResourceAsStream("MultiJsonPath.json");
// InputStream is = getClass().getClassLoader().getResourceAsStream("SliceArray_2.json");
JsonSurfer surfer = new JsonSurfer(JacksonParser.INSTANCE, JacksonProvider.INSTANCE);
Collector collector = surfer.collector(is);
ValueBox<Collection<Object>> matches = collector.collectAll("$..[?(@.number == 1.2)]");
collector.exec();

Iterator<Object> iter = matches.get().iterator();
while(iter.hasNext()){
Object o = iter.next();
System.err.println(o.getClass());
//ObjectNode on = (ObjectNode) o;
if(o instanceof Map) {
Iterator<Entry> iten = ((Map) o).entrySet().iterator();
while (iten.hasNext()) {
System.err.println(" > " + iten.next());
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ protected void properties(Properties properties) {
properties.setProperty("slice", "true");
properties.setProperty("json.path", "$.content");
}

// testMultiJsonPath$1
if(name.getMethodName().equals("testMultiJsonPath$1")){
properties.setProperty("blank-nodes", "false");
// properties.setProperty("slice", "true");
properties.setProperty("json.path.1", "$..[?(@.letter == 'A')]");
properties.setProperty("json.path.2", "$..[?(@.number == 2)]");
}
}

@Test
Expand Down Expand Up @@ -207,7 +215,17 @@ protected void properties(Properties properties) {
@Test
public void testSliceObject$3(){
L.info("Test simple Json object (slicing + JsonPath)");
RDFDataMgr.write(System.err, result, Lang.N3);
// RDFDataMgr.write(System.err, result, Lang.N3);
assertResultIsIsomorphicWithExpected();
// Assert.assertTrue(resultException.getMessage().equals("Not a JSON array"));
}

@Test
public void testMultiJsonPath$1(){
L.info("Test multiple json paths (one go + JsonPath)");
System.out.println(result.size());
RDFDataMgr.write(System.err, result, Lang.N3);
// XXX output graph is not deterministic as the order of the solutions can change ...
Assert.assertTrue(result.size() == 13);
}
}
6 changes: 6 additions & 0 deletions sparql-anything-json/src/test/resources/MultiJsonPath.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
{"letter" : "A", "number": 1, "boolean": true},
{"letter" : "B", "number": 2, "boolean": false, "next": {"letter" : "X", "number": 1.1, "boolean": true } },
{"letter" : "C", "number": 3, "boolean": true, "next": {"letter" : "X", "number": 1.2, "boolean": false } },
{"letter" : "D", "number": 4, "boolean": true, "next": {"letter" : "X", "number": 1.3, "boolean": true } }
]
38 changes: 38 additions & 0 deletions sparql-anything-json/src/test/resources/MultiJsonPath.ttl
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>.
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>.
@prefix fx: <http://sparql.xyz/facade-x/ns/>.
@prefix xyz: <http://sparql.xyz/facade-x/data/>.
@prefix xsd: <http://www.w3.org/2001/XMLSchema#>.

<http://www.example.org/document/_1>
<http://sparql.xyz/facade-x/data/boolean>
true ;
<http://sparql.xyz/facade-x/data/letter>
"A" ;
<http://sparql.xyz/facade-x/data/number>
"1"^^<http://www.w3.org/2001/XMLSchema#int> .

<http://www.example.org/document>
a <http://sparql.xyz/facade-x/ns/root> ;
<http://www.w3.org/1999/02/22-rdf-syntax-ns#_1>
<http://www.example.org/document/_1> ;
<http://www.w3.org/1999/02/22-rdf-syntax-ns#_2>
<http://www.example.org/document/_2> .

<http://www.example.org/document/_2/next>
<http://sparql.xyz/facade-x/data/boolean>
true ;
<http://sparql.xyz/facade-x/data/letter>
"X" ;
<http://sparql.xyz/facade-x/data/number>
"1.1"^^<http://www.w3.org/2001/XMLSchema#double> .

<http://www.example.org/document/_2>
<http://sparql.xyz/facade-x/data/boolean>
false ;
<http://sparql.xyz/facade-x/data/letter>
"B" ;
<http://sparql.xyz/facade-x/data/next>
<http://www.example.org/document/_2/next> ;
<http://sparql.xyz/facade-x/data/number>
"2"^^<http://www.w3.org/2001/XMLSchema#int> .

0 comments on commit 07436e7

Please sign in to comment.