[TASK] Add pattern A NOT B C to debug and stuff.

This commit is contained in:
Jan Philipp Timme 2016-09-29 17:41:04 +02:00
parent a14388c253
commit 13862de9b6
2 changed files with 154 additions and 11 deletions

View File

@ -14,6 +14,7 @@ import eu.larkc.csparql.core.engine.CsparqlEngineImpl;
import eu.larkc.csparql.core.engine.CsparqlQueryResultProxy; import eu.larkc.csparql.core.engine.CsparqlQueryResultProxy;
import lu.jpt.csparqltest.rentacar.RentACarSimulation; import lu.jpt.csparqltest.rentacar.RentACarSimulation;
import lu.jpt.csparqltest.util.RandomHelper; import lu.jpt.csparqltest.util.RandomHelper;
import lu.jpt.csparqltest.util.TestStreamGenerator;
public class Main { public class Main {
@ -35,12 +36,14 @@ public class Main {
engine.initialize(true); engine.initialize(true);
// Add local background knowledge from file into graph // Add local background knowledge from file into graph
/*
try { try {
engine.putStaticNamedModel("http://example.org/carSimKnowledgeGraph", CsparqlUtils.serializeRDFFile("data/carSimulationABox.rdf")); engine.putStaticNamedModel("http://example.org/carSimKnowledgeGraph", CsparqlUtils.serializeRDFFile("data/carSimulationABox.rdf"));
} catch (Exception e) { } catch (Exception e) {
logger.error(e.toString()); logger.error(e.toString());
logger.error(e.getStackTrace().toString()); logger.error(e.getStackTrace().toString());
} }
*/
/* Use a SPARQL Query to update the local knowledge from code instead of using CONSTRUCT within the engine. /* Use a SPARQL Query to update the local knowledge from code instead of using CONSTRUCT within the engine.
String updateQuery = "PREFIX : <http://www.streamreasoning.org/ontologies/sr4ld2014-onto#> " String updateQuery = "PREFIX : <http://www.streamreasoning.org/ontologies/sr4ld2014-onto#> "
@ -55,17 +58,21 @@ public class Main {
//logger.debug("Engine from: " + engine.getClass().getProtectionDomain().getCodeSource()); //logger.debug("Engine from: " + engine.getClass().getProtectionDomain().getCodeSource());
// Create and register stream generator at specific URI // Create and register stream generator at specific URI
RentACarSimulation simulation = new RentACarSimulation(); //RentACarSimulation simulation = new RentACarSimulation();
engine.registerStream(simulation.getCarStream()); //engine.registerStream(simulation.getCarStream());
engine.registerStream(simulation.getDriverStream()); //engine.registerStream(simulation.getDriverStream());
TestStreamGenerator testStreamGenerator = new TestStreamGenerator("http://example.org");
engine.registerStream(testStreamGenerator);
// Run simulation that is feeding the streams // Run simulation that is feeding the streams
Thread simulationThread = new Thread(simulation); //Thread simulationThread = new Thread(simulation);
simulationThread.start(); //simulationThread.start();
Thread testGeneratorThread = new Thread(testStreamGenerator);
testGeneratorThread.start();
// Now build a query to run - interchangeable // Now build a query to run - interchangeable
//String query = Main.getSPO(); String query = Main.getTestPatternQuery();
String query = RentACarSimulation.getEventUsingBackgroundKnowledge(); //String query = RentACarSimulation.getEventUsingBackgroundKnowledge();
// Create a result proxy by registering the query at the engine // Create a result proxy by registering the query at the engine
CsparqlQueryResultProxy resultProxy = null; CsparqlQueryResultProxy resultProxy = null;
@ -104,16 +111,104 @@ public class Main {
engine.unregisterQuery(resultProxy.getId()); engine.unregisterQuery(resultProxy.getId());
// Softly stop the simulation and unregister its streams // Softly stop the simulation and unregister its streams
simulation.pleaseStopSimulation(); //simulation.pleaseStopSimulation();
simulationThread.interrupt(); //simulationThread.interrupt();
engine.unregisterStream(simulation.getCarStream().getIRI()); //engine.unregisterStream(simulation.getCarStream().getIRI());
engine.unregisterStream(simulation.getDriverStream().getIRI()); //engine.unregisterStream(simulation.getDriverStream().getIRI());
testStreamGenerator.pleaseStop();
testGeneratorThread.interrupt();
engine.unregisterStream(testStreamGenerator.getIRI());
// That's it! // That's it!
System.exit(0); System.exit(0);
} }
private static String getSPO() { private static String getSPO() {
return "REGISTER QUERY getSPO AS "
+ "PREFIX f: <http://larkc.eu/csparql/sparql/jena/ext#> "
+ "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> "
+ "SELECT ?s ?p ?o "
+ "FROM STREAM <http://example.org> [RANGE 5s STEP 1s] "
+ "WHERE { "
+ " ?s ?p ?o . "
+ "}";
}
private static String getTestPatternQuery() {
return "REGISTER QUERY matchANotBC AS "
+ "PREFIX f: <http://larkc.eu/csparql/sparql/jena/ext#> "
+ "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> "
+ "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> "
+ "SELECT ?a ?b ?c "
+ "FROM STREAM <http://example.org> [RANGE 5s STEP 1s] "
+ "WHERE { "
+ " ?a a <http://example.org/type/A> . "
+ " ?c a <http://example.org/type/C> . "
+ " BIND(f:timestamp(?a,rdf:type,<http://example.org/type/A>) AS ?tsa) "
+ " BIND(f:timestamp(?c,rdf:type,<http://example.org/type/C>) AS ?tsc) "
+ " FILTER(?tsa < ?tsc) "
+ " OPTIONAL {"
+ " ?b a <http://example.org/type/B> . "
+ " BIND(f:timestamp(?b,rdf:type,<http://example.org/type/B>) AS ?tsb) "
+ " FILTER(?tsb < ?tsa || ?tsc < ?tsb)"
+ " } "
+ " BIND(BOUND(?b) AS ?bbound) "
+ " BIND(?tsa < ?tsb AS ?abeforeb) "
+ " BIND(?tsb < ?tsc AS ?bbeforec) "
+ "}";
}
private static String getPatternANOTBCQuery() {
return "REGISTER QUERY matchANotBC AS "
+ "PREFIX f: <http://larkc.eu/csparql/sparql/jena/ext#> "
+ "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> "
+ "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> "
+ "SELECT ?bbound ?abeforeb ?bbeforec ?tsa ?tsb ?tsc "
+ "FROM STREAM <http://example.org> [RANGE 5s STEP 1s] "
+ "WHERE { "
+ " ?a a <http://example.org/type/A> . "
+ " ?c a <http://example.org/type/C> . "
+ " BIND(f:timestamp(?a,rdf:type,<http://example.org/type/A>) AS ?tsa) "
+ " BIND(f:timestamp(?c,rdf:type,<http://example.org/type/C>) AS ?tsc) "
+ " FILTER(?tsa < ?tsc) "
+ " OPTIONAL {"
+ " ?b a <http://example.org/type/B> . "
+ " BIND(f:timestamp(?b,rdf:type,<http://example.org/type/B>) AS ?tsb) "
+ " FILTER(?tsb < ?tsa || ?tsc < ?tsb)"
+ " } "
+ " BIND(BOUND(?b) AS ?bbound) "
+ " BIND(?tsa < ?tsb AS ?abeforeb) "
+ " BIND(?tsb < ?tsc AS ?bbeforec) "
+ "}";
}
private static String getTestPatternABCQuery() {
return "REGISTER QUERY matchABC AS "
+ "PREFIX f: <http://larkc.eu/csparql/sparql/jena/ext#> "
+ "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> "
+ "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> "
+ "SELECT ?tsa ?tsb ?tsc "
+ "FROM STREAM <http://example.org> [RANGE 5s STEP 1s] "
+ "WHERE { "
+ " ?a a <http://example.org/type/A> . "
+ " ?a a ?atype . "
+ " OPTIONAL {"
+ " ?b a <http://example.org/type/B> . "
+ " ?b a ?btype . "
+ " } "
+ " ?c a <http://example.org/type/C> . "
+ " ?c a ?ctype . "
+ " BIND(f:timestamp(?a,rdf:type,?atype) AS ?tsa) "
+ " BIND(f:timestamp(?b,rdf:type,?btype) AS ?tsb) "
+ " BIND(f:timestamp(?c,rdf:type,?ctype) AS ?tsc) "
+ " FILTER(f:timestamp(?a,rdf:type,?atype) < f:timestamp(?b,rdf:type,?btype)) "
+ " FILTER(f:timestamp(?b,rdf:type,?btype) < f:timestamp(?c,rdf:type,?ctype)) "
+ "}";
}
private static String getSPOFromRentACar() {
return "REGISTER QUERY BasicCarInfo AS " return "REGISTER QUERY BasicCarInfo AS "
+ "PREFIX f: <http://larkc.eu/csparql/sparql/jena/ext#> " + "PREFIX f: <http://larkc.eu/csparql/sparql/jena/ext#> "
+ "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> " + "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> "

View File

@ -0,0 +1,48 @@
package lu.jpt.csparqltest.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import eu.larkc.csparql.cep.api.RdfQuadruple;
import eu.larkc.csparql.cep.api.RdfStream;
public class TestStreamGenerator extends RdfStream implements Runnable {
protected final Logger logger = LoggerFactory.getLogger(TestStreamGenerator.class);
private volatile boolean keepRunning = false;
public TestStreamGenerator(String iri) {
super(iri);
}
public void pleaseStop() {
keepRunning = false;
}
@Override
public void run() {
String[] EVENT_TYPES = {"A", "B", "C", "D", "E"};
this.keepRunning = true;
while (this.keepRunning) {
long currentTime = System.currentTimeMillis();
String eventID = this.getIRI() + "/event#" + currentTime;
int randomEventNumber = RandomHelper.getRandomNumberWithin(0, EVENT_TYPES.length-1);
String eventType = EVENT_TYPES[randomEventNumber];
this.put(new RdfQuadruple(
eventID,
"http://www.w3.org/1999/02/22-rdf-syntax-ns#type",
this.getIRI()+"/type/"+eventType,
currentTime
)
);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}