Skip to content

Commit

Permalink
Implement reify-slot-statements option -- see #377
Browse files Browse the repository at this point in the history
  • Loading branch information
luigi-asprino committed Jun 8, 2023
1 parent ff944bd commit 8cde200
Show file tree
Hide file tree
Showing 9 changed files with 202 additions and 121 deletions.
101 changes: 81 additions & 20 deletions Configuration.md

Large diffs are not rendered by default.

43 changes: 22 additions & 21 deletions README.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -865,37 +865,41 @@ public static void options() throws URISyntaxException {
// System.out.println(ResultSetFormatter.asText(QueryExecutionFactory.create(query, ds).execSelect()));
// System.out.println(query.toString(Syntax.defaultSyntax));

query = QueryFactory.create("PREFIX ex: <http://example/> \n" +
"PREFIX fx: <http://sparql.xyz/facade-x/ns/>\n" +
"PREFIX xyz: <http://sparql.xyz/facade-x/data/>\n" +
"\n" +
"CONSTRUCT {\n" +
" ?bnode ex:p ?A\n" +
"} WHERE {\n" +
" SERVICE <x-sparql-anything:> {\n" +
"\tfx:properties fx:content \"c1,c2\\n" +
"b0,A\\n" +
"b0,B\\n" +
"b0,C\\n" +
"b0,D\\n" +
"b0,E\\n" +
"b1,A\\n" +
"b2,B\\n" +
"b3,C\\n" +
"b4,D\\n" +
"b5,E\" ; fx:media-type 'text/csv'; fx:csv.headers true .\n" +
" \t[] xyz:c1 ?b0 ; xyz:c2 ?A\n" +
" }\n" +
" BIND ( fx:bnode ( ?b0 ) as ?bnode ) \n" +
"}");
query = QueryFactory.create("PREFIX xyz: <http://sparql.xyz/facade-x/data/> PREFIX fx: <http://sparql.xyz/facade-x/ns/> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> CONSTRUCT { ?s ?p ?o . } WHERE { SERVICE <x-sparql-anything:> { fx:properties fx:use-rdfs-member true ; fx:content \"[1,2,3]\" ; fx:reify-slot-statements true ; fx:media-type \"application/json\" . ?s ?p ?o } }");
// System.out.println(ResultSetFormatter.asText(QueryExecutionFactory.create(query, ds).execSelect()));
System.out.println(query.toString(Syntax.defaultSyntax));

// query = QueryFactory.create("PREFIX ex: <http://example/> \n" +
// "PREFIX fx: <http://sparql.xyz/facade-x/ns/>\n" +
// "PREFIX xyz: <http://sparql.xyz/facade-x/data/>\n" +
// "\n" +
// "CONSTRUCT {\n" +
// " ?bnode ex:p ?A\n" +
// "} WHERE {\n" +
// " SERVICE <x-sparql-anything:> {\n" +
// "\tfx:properties fx:content \"c1,c2\\n" +
// "b0,A\\n" +
// "b0,B\\n" +
// "b0,C\\n" +
// "b0,D\\n" +
// "b0,E\\n" +
// "b1,A\\n" +
// "b2,B\\n" +
// "b3,C\\n" +
// "b4,D\\n" +
// "b5,E\" ; fx:media-type 'text/csv'; fx:csv.headers true .\n" +
// " \t[] xyz:c1 ?b0 ; xyz:c2 ?A\n" +
// " }\n" +
// " BIND ( fx:bnode ( ?b0 ) as ?bnode ) \n" +
// "}");
//
m = QueryExecutionFactory.create(query, ds).execConstruct();
m.setNsPrefix("xyz", "http://sparql.xyz/facade-x/data/");
m.setNsPrefix("rdfs", RDFS.uri);
m.setNsPrefix("xsd", "http://www.w3.org/2001/XMLSchema#");
m.write(System.out, "TTL");
// System.out.println(ResultSetFormatter.asText(QueryExecutionFactory.create(query, ds).execSelect()));
System.out.println(query.toString(Syntax.defaultSyntax));
//// System.out.println(ResultSetFormatter.asText(QueryExecutionFactory.create(query, ds).execSelect()));
// System.out.println(query.toString(Syntax.defaultSyntax));

}

Expand Down Expand Up @@ -927,12 +931,12 @@ public static void main(String[] args) throws URISyntaxException, IOException {
// }

// archive();
spreadsheet();
// spreadsheet();
// doc();
// yaml();
// metadata();
// bibtex();
// options();
options();

// FileUtils.write(new File("/Users/lgu/Desktop/utf16.txt"), "UTF-16 test file", Charset.forName("UTF16"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@

import java.util.Properties;

import io.github.sparqlanything.model.IRIArgument;
import org.apache.jena.datatypes.RDFDatatype;
import org.apache.jena.riot.Lang;
import org.apache.jena.riot.RDFDataMgr;
import org.junit.Test;

import io.github.sparqlanything.csv.CSVTriplifier;
Expand Down Expand Up @@ -49,50 +53,47 @@ protected void properties(Properties properties) {
} else if (name.getMethodName().equals("testMemberFalse")) {
properties.setProperty("use-rdfs-member", "false");
properties.setProperty("blank-nodes", "true");
} else if(name.getMethodName().equals("testReifySlotStatements")){
properties.setProperty("use-rdfs-member", "true");
properties.setProperty("blank-nodes", "false");
properties.setProperty(IRIArgument.REIFY_SLOT_STATEMENTS.toString(), "true");
}
}

@Test
public void testTrimStringsTrue() {
// ModelFactory.createModelForGraph(expected).write(System.err, "TTL");
// ModelFactory.createModelForGraph(result).write(System.err, "TTL");
assertResultIsIsomorphicWithExpected();
}

@Test
public void testTrimStringsFalse() {
// ModelFactory.createModelForGraph(expected).write(System.err, "TTL");
// ModelFactory.createModelForGraph(result).write(System.err, "TTL");
assertResultIsIsomorphicWithExpected();
}

@Test
public void testNullStringsTrue() {
// ModelFactory.createModelForGraph(expected).write(System.err, "TTL");
// ModelFactory.createModelForGraph(result).write(System.err, "TTL");
assertResultIsIsomorphicWithExpected();
}

@Test
public void testNullStringsFalse() {
// ModelFactory.createModelForGraph(expected).write(System.err, "TTL");
// ModelFactory.createModelForGraph(result).write(System.err, "TTL");
assertResultIsIsomorphicWithExpected();
}

@Test
public void testMemberTrue() {
// ModelFactory.createModelForGraph(expected).write(System.err, "TTL");
// ModelFactory.createModelForGraph(result).write(System.err, "TTL");

assertResultIsIsomorphicWithExpected();
}

@Test
public void testMemberFalse() {
// ModelFactory.createModelForGraph(expected).write(System.err, "TTL");
// ModelFactory.createModelForGraph(result).write(System.err, "TTL");
assertResultIsIsomorphicWithExpected();
}

@Test
public void testReifySlotStatements() {
assertResultIsIsomorphicWithExpected();
}
}


2 changes: 2 additions & 0 deletions sparql-anything-it/src/test/resources/ReifySlotStatements.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
A,B,C
1,2,3
18 changes: 18 additions & 0 deletions sparql-anything-it/src/test/resources/ReifySlotStatements.ttl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

<< <http://www.example.org/document#row1> <http://www.w3.org/2000/01/rdf-schema#member> "C" >> <http://sparql.xyz/facade-x/ns/slot-key> "3"^^<http://www.w3.org/2001/XMLSchema#integer> .
<http://www.example.org/document> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://sparql.xyz/facade-x/ns/root> .
<http://www.example.org/document> <http://www.w3.org/2000/01/rdf-schema#member> <http://www.example.org/document#row1> .
<http://www.example.org/document> <http://www.w3.org/2000/01/rdf-schema#member> <http://www.example.org/document#row2> .
<< <http://www.example.org/document> <http://www.w3.org/2000/01/rdf-schema#member> <http://www.example.org/document#row2> >> <http://sparql.xyz/facade-x/ns/slot-key> "2"^^<http://www.w3.org/2001/XMLSchema#integer> .
<< <http://www.example.org/document> <http://www.w3.org/2000/01/rdf-schema#member> <http://www.example.org/document#row1> >> <http://sparql.xyz/facade-x/ns/slot-key> "1"^^<http://www.w3.org/2001/XMLSchema#integer> .
<< <http://www.example.org/document#row1> <http://www.w3.org/2000/01/rdf-schema#member> "B" >> <http://sparql.xyz/facade-x/ns/slot-key> "2"^^<http://www.w3.org/2001/XMLSchema#integer> .
<< <http://www.example.org/document#row1> <http://www.w3.org/2000/01/rdf-schema#member> "A" >> <http://sparql.xyz/facade-x/ns/slot-key> "1"^^<http://www.w3.org/2001/XMLSchema#integer> .
<< <http://www.example.org/document#row2> <http://www.w3.org/2000/01/rdf-schema#member> "1" >> <http://sparql.xyz/facade-x/ns/slot-key> "1"^^<http://www.w3.org/2001/XMLSchema#integer> .
<< <http://www.example.org/document#row2> <http://www.w3.org/2000/01/rdf-schema#member> "2" >> <http://sparql.xyz/facade-x/ns/slot-key> "2"^^<http://www.w3.org/2001/XMLSchema#integer> .
<< <http://www.example.org/document#row2> <http://www.w3.org/2000/01/rdf-schema#member> "3" >> <http://sparql.xyz/facade-x/ns/slot-key> "3"^^<http://www.w3.org/2001/XMLSchema#integer> .
<http://www.example.org/document#row1> <http://www.w3.org/2000/01/rdf-schema#member> "A" .
<http://www.example.org/document#row1> <http://www.w3.org/2000/01/rdf-schema#member> "B" .
<http://www.example.org/document#row1> <http://www.w3.org/2000/01/rdf-schema#member> "C" .
<http://www.example.org/document#row2> <http://www.w3.org/2000/01/rdf-schema#member> "1" .
<http://www.example.org/document#row2> <http://www.w3.org/2000/01/rdf-schema#member> "2" .
<http://www.example.org/document#row2> <http://www.w3.org/2000/01/rdf-schema#member> "3" .
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package io.github.sparqlanything.model;

import org.apache.jena.datatypes.xsd.XSDDatatype;
import org.apache.jena.graph.Node;
import org.apache.jena.graph.NodeFactory;
import org.apache.jena.vocabulary.RDF;
Expand All @@ -34,6 +35,7 @@ public abstract class BaseFacadeXBuilder implements FacadeXNodeBuilder, FacadeXQ
protected final boolean p_trim_strings;
protected final String p_null_string;
protected final boolean p_use_rdfs_member;
protected final boolean p_reify_slot_statements;

public BaseFacadeXBuilder(String resourceId, Properties properties) {
this.properties = properties;
Expand All @@ -44,63 +46,62 @@ public BaseFacadeXBuilder(String resourceId, Properties properties) {
this.p_trim_strings = Triplifier.getTrimStringsArgument(properties);
this.p_null_string = Triplifier.getNullStringArgument(properties);
this.p_use_rdfs_member = Triplifier.useRDFsMember(properties);
this.p_reify_slot_statements = PropertyUtils.getBooleanProperty(properties, IRIArgument.REIFY_SLOT_STATEMENTS);
}

public boolean addContainer(String dataSourceId, String containerId, String slotKey, String childContainerId) {
return add(NodeFactory.createURI(dataSourceId), container2node(containerId), key2predicate(slotKey),
container2node(childContainerId));
return add(NodeFactory.createURI(dataSourceId), container2node(containerId), key2predicate(slotKey), container2node(childContainerId));
}

public boolean addContainer(String dataSourceId, String containerId, URI customKey, String childContainerId) {
return add(NodeFactory.createURI(dataSourceId), container2node(containerId),
NodeFactory.createURI(customKey.toString()), container2node(childContainerId));
return add(NodeFactory.createURI(dataSourceId), container2node(containerId), NodeFactory.createURI(customKey.toString()), container2node(childContainerId));
}

public boolean addContainer(String dataSourceId, String containerId, Integer slotKey, String childContainerId) {
if (p_use_rdfs_member) {
return add(NodeFactory.createURI(dataSourceId), container2node(containerId), RDFS.member.asNode(),
container2node(childContainerId));
} else {
return add(NodeFactory.createURI(dataSourceId), container2node(containerId), RDF.li(slotKey).asNode(),
container2node(childContainerId));
}
return addSlotStatement(dataSourceId, containerId, slotKey, childContainerId, true);
}

public boolean addType(String dataSourceId, String containerId, String typeId) {
return add(NodeFactory.createURI(dataSourceId), container2node(containerId), RDF.type.asNode(),
NodeFactory.createURI(typeId));
return add(NodeFactory.createURI(dataSourceId), container2node(containerId), RDF.type.asNode(), NodeFactory.createURI(typeId));
}

public boolean addType(String dataSourceId, String containerId, URI type) {
return add(NodeFactory.createURI(dataSourceId), container2node(containerId), RDF.type.asNode(),
NodeFactory.createURI(type.toString()));
return add(NodeFactory.createURI(dataSourceId), container2node(containerId), RDF.type.asNode(), NodeFactory.createURI(type.toString()));
}

public boolean addValue(String dataSourceId, String containerId, String slotKey, Object value) {
return add(NodeFactory.createURI(dataSourceId), container2node(containerId), key2predicate(slotKey),
value2node(value));
return add(NodeFactory.createURI(dataSourceId), container2node(containerId), key2predicate(slotKey), value2node(value));
}

public boolean addValue(String dataSourceId, String containerId, Integer slotKey, Object value) {
public boolean addValue(String dataSourceId, String containerId, URI customKey, Object value) {
return add(NodeFactory.createURI(dataSourceId), container2node(containerId), NodeFactory.createURI(customKey.toString()), value2node(value));
}

if (p_use_rdfs_member) {
return add(NodeFactory.createURI(dataSourceId), container2node(containerId), RDFS.member.asNode(),
value2node(value));
} else {
return add(NodeFactory.createURI(dataSourceId), container2node(containerId), RDF.li(slotKey).asNode(),
value2node(value));
}
public boolean addValue(String dataSourceId, String containerId, Integer slotKey, Object value) {
return addSlotStatement(dataSourceId, containerId, slotKey, value, false);
}

public boolean addRoot(String dataSourceId, String rootId) {
return add(NodeFactory.createURI(dataSourceId), container2node(rootId), RDF.type.asNode(), NodeFactory.createURI(Triplifier.FACADE_X_TYPE_ROOT));
}

public boolean addValue(String dataSourceId, String containerId, URI customKey, Object value) {
return add(NodeFactory.createURI(dataSourceId), container2node(containerId),
NodeFactory.createURI(customKey.toString()), value2node(value));
private boolean addSlotStatement(String dataSourceId, String containerId, Integer slotKey, Object object, boolean isObjectContainer) {
Node g = NodeFactory.createURI(dataSourceId);
Node s = container2node(containerId);
Node p = p_use_rdfs_member ? RDFS.member.asNode() : RDF.li(slotKey).asNode();
Node o = isObjectContainer ? container2node(object.toString()) : value2node(object);
if (p_reify_slot_statements) {
add(g, NodeFactory.createTripleNode(s, p, o), NodeFactory.createURI(Triplifier.FACADE_X_SLOT_KEY), NodeFactory.createLiteral(slotKey.toString(), XSDDatatype.XSDinteger));
}
return add(g, s, p, o);
}

public boolean addRoot(String dataSourceId, String rootId) {
return add(NodeFactory.createURI(dataSourceId), container2node(rootId), RDF.type.asNode(),
NodeFactory.createURI(Triplifier.FACADE_X_TYPE_ROOT));
public Node value2node(Object value) {
// trims_strings == true and if object is string, trim it
if (p_trim_strings && value instanceof String) {
value = ((String) value).trim();
}
return FacadeXNodeBuilder.super.value2node(value);
}

public Node container2node(String container) {
Expand All @@ -116,12 +117,4 @@ public Node container2node(String container) {
public Node key2predicate(String key) {
return key2predicate(this.p_namespace, key);
}

public Node value2node(Object value) {
// trims_strings == true and if object is string, trim it
if (p_trim_strings && value instanceof String) {
value = ((String) value).trim();
}
return FacadeXNodeBuilder.super.value2node(value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

public enum IRIArgument {

LOCATION("location"), MEDIA_TYPE("media-type"), NAMESPACE("namespace", "http://sparql.xyz/facade-x/data/"), ROOT("root"), BLANK_NODES("blank-nodes", "true"),NO_CACHE("no-cache", "false"), TRIPLIFIER("triplifier"), CHARSET("charset", "UTF-8"), METADATA("metadata", "false"), CONTENT("content"), FROM_ARCHIVE("from-archive"), TRIM_STRINGS("trim-strings", "false"), NULL_STRING("null-string"), STRATEGY("strategy", "1"), SLICE("slice", "false"), COMMAND("command"), USE_RDFS_MEMBER("use-rdfs-member", "false"), ONDISK_REUSE("ondisk.reuse", "true"), ONDISK("ondisk"), OP_SERVICE_SILENT("opservice.silent");
LOCATION("location"), MEDIA_TYPE("media-type"), NAMESPACE("namespace", "http://sparql.xyz/facade-x/data/"), ROOT("root"), BLANK_NODES("blank-nodes", "true"),NO_CACHE("no-cache", "false"), TRIPLIFIER("triplifier"), CHARSET("charset", "UTF-8"), METADATA("metadata", "false"), CONTENT("content"), FROM_ARCHIVE("from-archive"), TRIM_STRINGS("trim-strings", "false"), NULL_STRING("null-string"), STRATEGY("strategy", "1"), SLICE("slice", "false"), COMMAND("command"), USE_RDFS_MEMBER("use-rdfs-member", "false"), ONDISK_REUSE("ondisk.reuse", "true"), ONDISK("ondisk"), OP_SERVICE_SILENT("opservice.silent"), REIFY_SLOT_STATEMENTS("reify-slot-statements", "false"),;

private final String s;
private String defaultValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public interface Triplifier {
String XYZ_NS = "http://sparql.xyz/facade-x/data/";
String FACADE_X_CONST_NAMESPACE_IRI = "http://sparql.xyz/facade-x/ns/";
String FACADE_X_TYPE_ROOT = FACADE_X_CONST_NAMESPACE_IRI + "root";
String FACADE_X_SLOT_KEY = FACADE_X_CONST_NAMESPACE_IRI + "slot-key";
String FACADE_X_TYPE_PROPERTIES = FACADE_X_CONST_NAMESPACE_IRI + "properties";

Logger log = LoggerFactory.getLogger(Triplifier.class);
Expand Down

0 comments on commit 8cde200

Please sign in to comment.