Skip to content

Commit

Permalink
Add SPARQL Algebra to audit graph #149
Browse files Browse the repository at this point in the history
  • Loading branch information
luigi-asprino committed Sep 2, 2024
1 parent 80ad2af commit de82c48
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import io.github.sparqlanything.metadata.MetadataTriplifier;
import io.github.sparqlanything.model.*;
import org.apache.jena.datatypes.RDFDatatype;
import org.apache.jena.datatypes.xsd.XSDDatatype;
import org.apache.jena.datatypes.xsd.XSDDateTime;
import org.apache.jena.graph.Node;
Expand All @@ -34,10 +33,8 @@
import org.apache.jena.sparql.core.DatasetGraph;
import org.apache.jena.sparql.core.DatasetGraphFactory;
import org.apache.jena.sparql.engine.ExecutionContext;
import org.apache.jena.vocabulary.DCTerms;
import org.apache.jena.vocabulary.RDF;
import org.apache.jena.vocabulary.VOID;
import org.apache.jena.vocabulary.XSD;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -68,7 +65,7 @@ public DatasetGraph getDatasetGraph(Triplifier t, Properties p, Op op) throws IO
// XXX Future implementations may use a caching system
if (use_cache && FacadeX.executedFacadeXIris.containsKey(getInMemoryCacheKey(p, op))) {
dg = FacadeX.executedFacadeXIris.get(getInMemoryCacheKey(p, op));
createAuditGraph(dg, p, true);
createAuditGraph(dg, p, true, op);
return dg;
}

Expand All @@ -77,7 +74,7 @@ public DatasetGraph getDatasetGraph(Triplifier t, Properties p, Op op) throws IO

logger.trace("Triplifier {}\n{}", t.getClass().toString(), op);
dg = triplify(op, p, t);
createAuditGraph(dg, p, false);
createAuditGraph(dg, p, false, op);

logger.debug("triplification done -- committing and ending the write txn");
dg.commit();
Expand Down Expand Up @@ -105,13 +102,6 @@ public DatasetGraph getDatasetGraph(Triplifier t, Properties p, Op op) throws IO
// TODO wrap this in a txn or move it to a place where we are already in a txn
// logger.trace("Triplified, #triples in default graph {} {}", dg.getDefaultGraph().size(), op.toString());

// else {
// logger.trace("No location, use content: {}", p.getProperty(IRIArgument.CONTENT.toString()));
// dg = t.triplify(p);
// logger.trace("Size: {} {}", dg.size(), dg.getDefaultGraph().size());
//
// }

return dg;
}

Expand All @@ -121,6 +111,45 @@ private String getInMemoryCacheKey(Properties properties, Op op) {
return key;
}

private void createAuditGraph(DatasetGraph dg, Properties p, boolean b, Op op) {
if (PropertyUtils.getBooleanProperty(p, IRIArgument.AUDIT)) {
String SD = "http://www.w3.org/ns/sparql-service-description#";
Model audit = ModelFactory.createDefaultModel();
Resource root = audit.createResource(Triplifier.AUDIT_GRAPH_IRI + "#root");
Node nodeGraph = NodeFactory.createURI(Triplifier.AUDIT_GRAPH_IRI);

// Check if the audit graph already exists (this could happen if the dataset graph comes from the cache)
// In this case the audit the value of the cached graph property is updated
if (dg.containsGraph(nodeGraph)) {
Set<Node> graphNodes = new HashSet<>();
dg.find(nodeGraph, null, NodeFactory.createURI(Triplifier.FACADE_X_CACHED_GRAPH), null).forEachRemaining(q -> {
graphNodes.add(q.getSubject());
});
for (Node g : graphNodes) {
dg.delete(nodeGraph, g, NodeFactory.createURI(Triplifier.FACADE_X_CACHED_GRAPH), NodeFactory.createLiteralByValue(false));
dg.add(nodeGraph, g, NodeFactory.createURI(Triplifier.FACADE_X_CACHED_GRAPH), NodeFactory.createLiteralByValue(true));
}
return;
}

// For each graph
Iterator<Node> graphs = dg.listGraphNodes();
while (graphs.hasNext()) {
Node g = graphs.next();
Resource auditGraph = audit.createResource(g.getURI());
root.addProperty(ResourceFactory.createProperty(SD.concat("namedGraph")), auditGraph);
auditGraph.addProperty(RDF.type, ResourceFactory.createResource(SD.concat("NamedGraph")));
auditGraph.addProperty(ResourceFactory.createProperty(SD.concat("name")), g.getURI());
auditGraph.addLiteral(VOID.triples, dg.getGraph(g).size());
auditGraph.addLiteral(auditGraph.getModel().createProperty(Triplifier.FACADE_X_CACHED_GRAPH), b);
auditGraph.addProperty(auditGraph.getModel().createProperty(Triplifier.FACADE_X_CACHED_GRAPH_CREATION), new XSDDateTime(Calendar.getInstance()).toString(), XSDDatatype.XSDdateTime);
auditGraph.addProperty(auditGraph.getModel().createProperty(Triplifier.FACADE_X_SPARQL_ALGEBRA), op.toString());

}
dg.addGraph(nodeGraph, audit.getGraph());
}
}

private DatasetGraph triplify(final Op op, Properties p, Triplifier t) throws IOException {
DatasetGraph dg;

Expand Down Expand Up @@ -163,44 +192,6 @@ private DatasetGraph triplify(final Op op, Properties p, Triplifier t) throws IO
return dg;
}

private void createAuditGraph(DatasetGraph dg, Properties p, boolean b) {
if (PropertyUtils.getBooleanProperty(p, IRIArgument.AUDIT)) {
String SD = "http://www.w3.org/ns/sparql-service-description#";
Model audit = ModelFactory.createDefaultModel();
Resource root = audit.createResource(Triplifier.AUDIT_GRAPH_IRI + "#root");
Node nodeGraph = NodeFactory.createURI(Triplifier.AUDIT_GRAPH_IRI);

// Check if the audit graph already exists (this could happen if the dataset graph comes from the cache)
// In this case the audit the value of the cached graph property is updated
if(dg.containsGraph(nodeGraph)){
Set<Node> graphNodes = new HashSet<>();
dg.find(nodeGraph, null, NodeFactory.createURI(Triplifier.FACADE_X_CACHED_GRAPH), null).forEachRemaining(q->{
graphNodes.add(q.getSubject());
});
for (Node g: graphNodes) {
dg.delete(nodeGraph, g, NodeFactory.createURI(Triplifier.FACADE_X_CACHED_GRAPH), NodeFactory.createLiteralByValue(false));
dg.add(nodeGraph, g, NodeFactory.createURI(Triplifier.FACADE_X_CACHED_GRAPH), NodeFactory.createLiteralByValue(true));
}
return;
}

// For each graph
Iterator<Node> graphs = dg.listGraphNodes();
while (graphs.hasNext()) {
Node g = graphs.next();
Resource auditGraph = audit.createResource(g.getURI());
root.addProperty(ResourceFactory.createProperty(SD.concat("namedGraph")), auditGraph);
auditGraph.addProperty(RDF.type, ResourceFactory.createResource(SD.concat("NamedGraph")));
auditGraph.addProperty(ResourceFactory.createProperty(SD.concat("name")), g.getURI());
auditGraph.addLiteral(VOID.triples, dg.getGraph(g).size());
auditGraph.addLiteral(auditGraph.getModel().createProperty(Triplifier.FACADE_X_CACHED_GRAPH), b);
auditGraph.addProperty(auditGraph.getModel().createProperty(Triplifier.FACADE_X_CACHED_GRAPH_CREATION),new XSDDateTime(Calendar.getInstance()).toString(), XSDDatatype.XSDdateTime);

}
dg.addGraph(nodeGraph, audit.getGraph());
}
}

private void createMetadataGraph(DatasetGraph dg, Properties p) throws IOException {
if (triplifyMetadata(p)) {
FacadeXGraphBuilder builder = new BaseFacadeXGraphBuilder(p);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public interface Triplifier {
String FACADE_X_TYPE_PROPERTIES = FACADE_X_CONST_NAMESPACE_IRI + "properties";
String FACADE_X_CACHED_GRAPH = FACADE_X_CONST_NAMESPACE_IRI + "cachedGraph";
String FACADE_X_CACHED_GRAPH_CREATION = FACADE_X_CONST_NAMESPACE_IRI + "cachedGraphCreation";
String FACADE_X_SPARQL_ALGEBRA = FACADE_X_CONST_NAMESPACE_IRI + "sparqlAlgebra";

Logger log = LoggerFactory.getLogger(Triplifier.class);
UnicodeEscaper basicEscaper = new PercentEscaper("_.-~", false);
Expand Down

0 comments on commit de82c48

Please sign in to comment.