From 87f60f6769430469e2c87b95294fbafe3ac583c0 Mon Sep 17 00:00:00 2001 From: Pieter Bonte Date: Thu, 5 Aug 2021 10:56:31 +0200 Subject: [PATCH] Adds support for static data --- .../sr/rsp/csparql/engine/SDSManagerImpl.java | 14 +++- .../engine/CSPARQLStaticDataExample.java | 71 +++++++++++++++++++ jasper/src/test/resources/q52_static.rspql | 18 +++++ jasper/src/test/resources/static.ttl | 3 + 4 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 jasper/src/test/java/it/polimi/deib/sr/rsp/esper/engine/CSPARQLStaticDataExample.java create mode 100644 jasper/src/test/resources/q52_static.rspql create mode 100644 jasper/src/test/resources/static.ttl diff --git a/jasper/src/main/java/it/polimi/sr/rsp/csparql/engine/SDSManagerImpl.java b/jasper/src/main/java/it/polimi/sr/rsp/csparql/engine/SDSManagerImpl.java index 3f2dcd0..5898a44 100644 --- a/jasper/src/main/java/it/polimi/sr/rsp/csparql/engine/SDSManagerImpl.java +++ b/jasper/src/main/java/it/polimi/sr/rsp/csparql/engine/SDSManagerImpl.java @@ -29,9 +29,11 @@ import lombok.Getter; import lombok.extern.log4j.Log4j; import org.apache.jena.graph.Graph; +import org.apache.jena.graph.Triple; import org.apache.jena.mem.GraphMem; import org.apache.jena.rdf.model.Model; import org.apache.jena.rdf.model.ModelFactory; +import org.apache.jena.rdf.model.StmtIterator; import org.apache.jena.rdf.model.impl.InfModelImpl; import org.apache.jena.reasoner.InfGraph; import org.apache.jena.reasoner.Reasoner; @@ -39,6 +41,7 @@ import org.apache.jena.riot.system.IRIResolver; import org.apache.jena.sparql.engine.binding.Binding; import org.apache.jena.sparql.graph.GraphFactory; +import org.apache.jena.util.iterator.ExtendedIterator; import java.util.List; import java.util.Map; @@ -128,9 +131,18 @@ public SDS build() { InfGraph infGraph = new InfModelImpl(this.reasoner.bind(m.read(g).getGraph())).getInfGraph(); this.sds.add(new TimeVaryingStatic<>(sds, infGraph)); } else { - this.sds.add(new TimeVaryingStatic<>(sds, GraphFactory.createGraphMem())); + Graph staticGraph = GraphFactory.createGraphMem(); + // load static data + StmtIterator stmtIt = m.listStatements(); + while(stmtIt.hasNext()){ + staticGraph.add(stmtIt.nextStatement().asTriple()); + } + this.sds.add(new TimeVaryingStatic<>(sds, staticGraph)); } }); + // Remove graph and name graph definitions from query after loading + query.getGraphURIs().clear(); + query.getNamedGraphURIs().clear(); Map> registeredStreams = stream_registration_service.getRegisteredStreams(); diff --git a/jasper/src/test/java/it/polimi/deib/sr/rsp/esper/engine/CSPARQLStaticDataExample.java b/jasper/src/test/java/it/polimi/deib/sr/rsp/esper/engine/CSPARQLStaticDataExample.java new file mode 100644 index 0000000..0e5d169 --- /dev/null +++ b/jasper/src/test/java/it/polimi/deib/sr/rsp/esper/engine/CSPARQLStaticDataExample.java @@ -0,0 +1,71 @@ +package it.polimi.deib.sr.rsp.esper.engine; + +import it.polimi.sr.rsp.csparql.engine.CSPARQLEngine; +import it.polimi.yasper.core.engine.config.EngineConfiguration; +import it.polimi.yasper.core.querying.ContinuousQuery; +import it.polimi.yasper.core.querying.ContinuousQueryExecution; +import it.polimi.yasper.core.sds.SDSConfiguration; +import it.polimi.yasper.core.stream.data.DataStreamImpl; +import it.polimi.yasper.core.stream.data.WebDataStream; +import org.apache.commons.configuration.ConfigurationException; +import org.apache.commons.io.FileUtils; +import org.apache.jena.graph.Graph; + +import java.io.File; +import java.io.IOException; +import java.net.URL; + +/** + * Created by Riccardo on 03/08/16. + */ +public class CSPARQLStaticDataExample { + + static CSPARQLEngine sr; + + public static void main(String[] args) throws InterruptedException, IOException, ConfigurationException { + + URL resource = CSPARQLStaticDataExample.class.getResource("/csparql.properties"); + SDSConfiguration config = new SDSConfiguration(resource.getPath()); + EngineConfiguration ec = EngineConfiguration.loadConfig("/csparql.properties"); + + + sr = new CSPARQLEngine(0, ec); + + GraphStream writer = new GraphStream("Artist", "http://differenthost:12134/stream2", 1); + + DataStreamImpl register = sr.register(writer); + + writer.setWritable(register); + + ContinuousQueryExecution cqe = sr.register(getQuery(".rspql"), config); + + ContinuousQuery query = cqe.getContinuousQuery(); + + System.out.println(query.toString()); + + System.out.println("<<------>>"); + +// if (query.isConstructType()) { +// cqe.add(ResponseFormatterFactory.getConstructResponseSysOutFormatter("JSON-LD", true)); +// } else if (query.isSelectType()) { +// cqe.add(ResponseFormatterFactory.getSelectResponseSysOutFormatter("TABLE", true)); //or "CSV" or "JSON" or "JSON-LD" +// } + + WebDataStream outputStream = cqe.outstream(); + + if (outputStream != null) + outputStream.addConsumer((o, l) -> System.out.println(o)); + + //In real application we do not have to start the stream. + (new Thread(writer)).start(); + + } + + public static String getQuery(String suffix) throws IOException { + URL resource = CSPARQLStaticDataExample.class.getResource("/q52_static" + suffix); + System.out.println(resource.getPath()); + File file = new File(resource.getPath()); + return FileUtils.readFileToString(file); + } + +} diff --git a/jasper/src/test/resources/q52_static.rspql b/jasper/src/test/resources/q52_static.rspql new file mode 100644 index 0000000..aaed5ef --- /dev/null +++ b/jasper/src/test/resources/q52_static.rspql @@ -0,0 +1,18 @@ +PREFIX ars: +PREFIX afn: +PREFIX wikibase: +PREFIX : + +REGISTER RSTREAM AS +SELECT * +FROM NAMED WINDOW ON :stream2 [RANGE PT5S STEP PT0.5S] +FROM +WHERE { + ?a ars:hasName ?name. + WINDOW ?w { + ?a a ars:Artist ; + ars:hasAge ?age . + } + + BIND( UUID() as ?uuid ) +} \ No newline at end of file diff --git a/jasper/src/test/resources/static.ttl b/jasper/src/test/resources/static.ttl new file mode 100644 index 0000000..023540f --- /dev/null +++ b/jasper/src/test/resources/static.ttl @@ -0,0 +1,3 @@ +PREFIX ars: + + ars:hasName "someName". \ No newline at end of file