[TASK] Add QueryContainer class.
This commit is contained in:
parent
6e584ad301
commit
28afcb4b7f
@ -9,16 +9,14 @@ import org.slf4j.LoggerFactory;
|
||||
|
||||
import eu.larkc.csparql.cep.api.RdfStream;
|
||||
import eu.larkc.csparql.common.utils.CsparqlUtils;
|
||||
import eu.larkc.csparql.common.utils.ReasonerChainingType;
|
||||
import eu.larkc.csparql.core.engine.CsparqlEngine;
|
||||
import eu.larkc.csparql.core.engine.CsparqlEngineImpl;
|
||||
import eu.larkc.csparql.core.engine.CsparqlQueryResultProxy;
|
||||
import eu.larkc.csparql.core.engine.RDFStreamFormatter;
|
||||
import lu.jpt.csparqlproject.gui.FancyTextObserverWindow;
|
||||
import lu.jpt.csparqlproject.misc.QueryContainer;
|
||||
import lu.jpt.csparqlproject.misc.ReasoningTester;
|
||||
import lu.jpt.csparqlproject.rentacar.RentACarSimulation;
|
||||
import lu.jpt.csparqlproject.util.CsparqlQueryHelper;
|
||||
import lu.jpt.csparqlproject.util.CsparqlQueryHelper.CsparqlQueryInfo;
|
||||
|
||||
/**
|
||||
* This class encapsulates the use of the C-SPARQL Engine. Here, the RentACarSimulation,
|
||||
@ -159,38 +157,39 @@ public class SimulationContext {
|
||||
RdfStream driverStream = simulation.getDriverStream();
|
||||
this.engine.registerStream(driverStream);
|
||||
this.registeredStreams.add(driverStream);
|
||||
Collection<String> queriesToRegister = new ArrayList<String>();
|
||||
Collection<QueryContainer> queriesToRegister = new ArrayList<QueryContainer>();
|
||||
// Collect the queries to use!
|
||||
//queriesToRegister.add(RentACarSimulation.getEventsQuery());
|
||||
queriesToRegister.add(RentACarSimulation.getAverageDataByCarAsStream());
|
||||
queriesToRegister.add(RentACarSimulation.selectFromRegisteredStream());
|
||||
// Now register each query appropriately!
|
||||
for(String query : queriesToRegister) {
|
||||
CsparqlQueryInfo queryInfo = CsparqlQueryHelper.getQueryInfo(query);
|
||||
for(QueryContainer queryContainer : queriesToRegister) {
|
||||
CsparqlQueryResultProxy resultProxy = null;
|
||||
try {
|
||||
resultProxy = this.engine.registerQuery(query, true);
|
||||
this.enableReasoningForResultProxy(resultProxy);
|
||||
resultProxy = this.engine.registerQuery(queryContainer.query, queryContainer.reasoningEnabled);
|
||||
if(queryContainer.reasoningEnabled) {
|
||||
this.enableReasoningForResultProxy(resultProxy, queryContainer);
|
||||
}
|
||||
// Take care of streams and queries differently.
|
||||
if(queryInfo.isStream) {
|
||||
if(queryContainer.isStream) {
|
||||
// If the query is a stream, we need additional components to feed it back into the engine.
|
||||
String streamUri = "http://example.org/stream/"+queryInfo.name;
|
||||
String streamUri = "http://example.org/stream/"+queryContainer.name;
|
||||
RDFStreamFormatter rdfStreamFormatter = new RDFStreamFormatter(streamUri);
|
||||
engine.registerStream(rdfStreamFormatter);
|
||||
resultProxy.addObserver(rdfStreamFormatter);
|
||||
Observer resultObserver = this.createResultObserverWindow(queryInfo.name);
|
||||
Observer resultObserver = this.createResultObserverWindow(queryContainer.name);
|
||||
resultProxy.addObserver(resultObserver);
|
||||
} else {
|
||||
// If it is a regular query, just attach a fitting observer
|
||||
Observer resultObserver = this.createResultObserverWindow(queryInfo.name);
|
||||
Observer resultObserver = this.createResultObserverWindow(queryContainer.name);
|
||||
resultProxy.addObserver(resultObserver);
|
||||
}
|
||||
this.queryResultProxies.add(resultProxy);
|
||||
SimulationContext.logger.info("Successfully registered query " + queryInfo.name + ": " + query);
|
||||
SimulationContext.logger.info("Successfully registered query " + queryContainer.name + ": " + queryContainer);
|
||||
} catch (Exception e) {
|
||||
SimulationContext.logger.error(e.toString());
|
||||
SimulationContext.logger.error("Could not register query "+queryInfo.name);
|
||||
SimulationContext.logger.error(query);
|
||||
SimulationContext.logger.error("Could not register query "+queryContainer.name);
|
||||
SimulationContext.logger.error(queryContainer.query);
|
||||
}
|
||||
}
|
||||
// Setup complete, ready to run.
|
||||
@ -216,13 +215,19 @@ public class SimulationContext {
|
||||
return observer;
|
||||
}
|
||||
|
||||
private void enableReasoningForResultProxy(CsparqlQueryResultProxy resultProxy) {
|
||||
private void enableReasoningForResultProxy(CsparqlQueryResultProxy resultProxy, QueryContainer queryContainer) {
|
||||
try {
|
||||
engine.updateReasoner(
|
||||
/* If not using the queryContainer, these would be valid parameters:
|
||||
resultProxy.getSparqlQueryId(),
|
||||
CsparqlUtils.fileToString("data/rdfs.rules"),
|
||||
ReasonerChainingType.HYBRID,
|
||||
CsparqlUtils.serializeRDFFile("data/carSimulationTBox.rdf")
|
||||
*/
|
||||
engine.updateReasoner(
|
||||
resultProxy.getSparqlQueryId(),
|
||||
queryContainer.ruleSet,
|
||||
queryContainer.reasonerChainingType,
|
||||
queryContainer.TBox
|
||||
);
|
||||
} catch(Exception e) {
|
||||
e.printStackTrace();
|
||||
|
40
src/main/java/lu/jpt/csparqlproject/misc/QueryContainer.java
Normal file
40
src/main/java/lu/jpt/csparqlproject/misc/QueryContainer.java
Normal file
@ -0,0 +1,40 @@
|
||||
package lu.jpt.csparqlproject.misc;
|
||||
|
||||
import eu.larkc.csparql.common.utils.ReasonerChainingType;
|
||||
|
||||
/**
|
||||
* Container class that allows for easier encapsulation of a query with
|
||||
* configuration.
|
||||
*/
|
||||
public class QueryContainer {
|
||||
public String name;
|
||||
public String query;
|
||||
public boolean isStream;
|
||||
public boolean reasoningEnabled;
|
||||
public String TBox;
|
||||
public String ruleSet;
|
||||
public ReasonerChainingType reasonerChainingType;
|
||||
public boolean useObserverWindow;
|
||||
|
||||
public QueryContainer(String name, String query, boolean isStream) {
|
||||
this.name = name;
|
||||
this.query = query;
|
||||
this.isStream = isStream;
|
||||
this.reasoningEnabled = false;
|
||||
this.TBox = "REASONING NOT PROPERLY ENABLED";
|
||||
this.ruleSet = "REASONING NOT PROPERLY ENABLED";
|
||||
this.reasonerChainingType = null;
|
||||
this.useObserverWindow = false;
|
||||
}
|
||||
|
||||
public void enableReasoning(String ruleSet, String TBox, ReasonerChainingType reasonerChainingType) {
|
||||
this.reasoningEnabled = true;
|
||||
this.ruleSet = ruleSet;
|
||||
this.TBox = TBox;
|
||||
this.reasonerChainingType = reasonerChainingType;
|
||||
}
|
||||
|
||||
public void useObserverWindow() {
|
||||
this.useObserverWindow = true;
|
||||
}
|
||||
}
|
@ -9,6 +9,7 @@ import org.slf4j.LoggerFactory;
|
||||
import eu.larkc.csparql.cep.api.RdfQuadruple;
|
||||
import eu.larkc.csparql.cep.api.RdfStream;
|
||||
import lu.jpt.csparqlproject.Main;
|
||||
import lu.jpt.csparqlproject.misc.QueryContainer;
|
||||
import lu.jpt.csparqlproject.util.WindowLoggingRdfStream;
|
||||
|
||||
/**
|
||||
@ -146,8 +147,8 @@ public class RentACarSimulation implements Runnable {
|
||||
}
|
||||
|
||||
|
||||
public static String getEventsQuery() {
|
||||
return "REGISTER QUERY getEvents AS "
|
||||
public static QueryContainer getEventsQuery() {
|
||||
String query = "REGISTER QUERY getCarStatusEvents AS "
|
||||
+ "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> "
|
||||
+ "PREFIX f: <http://larkc.eu/csparql/sparql/jena/ext#> "
|
||||
+ "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> "
|
||||
@ -168,10 +169,13 @@ public class RentACarSimulation implements Runnable {
|
||||
+ " ?e car:tirePressure3 ?tirePressure3 . "
|
||||
+ " ?e car:tirePressure4 ?tirePressure4 . "
|
||||
+ "}";
|
||||
QueryContainer queryContainer = new QueryContainer("getCarStatusEvents", query, false);
|
||||
queryContainer.useObserverWindow();
|
||||
return queryContainer;
|
||||
}
|
||||
|
||||
public static String getEventUsingBackgroundKnowledge() {
|
||||
return "REGISTER QUERY getEventsCombinedWithBackgroundKnowledge AS "
|
||||
public static QueryContainer getEventUsingBackgroundKnowledge() {
|
||||
String query = "REGISTER QUERY getEventsCombinedWithBackgroundKnowledge AS "
|
||||
+ "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> "
|
||||
+ "PREFIX f: <http://larkc.eu/csparql/sparql/jena/ext#> "
|
||||
+ "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> "
|
||||
@ -187,10 +191,13 @@ public class RentACarSimulation implements Runnable {
|
||||
+ " ?driver car:hasName ?driverName . "
|
||||
+ " ?driver car:hasPhoneNumber ?driverPhone . "
|
||||
+ "}";
|
||||
QueryContainer queryContainer = new QueryContainer("getEventsCombinedWithBackgroundKnowledge", query, false);
|
||||
queryContainer.useObserverWindow();
|
||||
return queryContainer;
|
||||
}
|
||||
|
||||
public static String getSpeedByCar() {
|
||||
return "REGISTER QUERY getSpeedByCar AS "
|
||||
public static QueryContainer getSpeedByCar() {
|
||||
String query = "REGISTER QUERY getSpeedByCar AS "
|
||||
+ "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> "
|
||||
+ "PREFIX f: <http://larkc.eu/csparql/sparql/jena/ext#> "
|
||||
+ "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> "
|
||||
@ -202,10 +209,13 @@ public class RentACarSimulation implements Runnable {
|
||||
+ " ?e car:relatedCar ?car . "
|
||||
+ " ?e car:speed ?speed . "
|
||||
+ "}";
|
||||
QueryContainer queryContainer = new QueryContainer("getSpeedByCar", query, false);
|
||||
queryContainer.useObserverWindow();
|
||||
return queryContainer;
|
||||
}
|
||||
|
||||
public static String getAverageDataByCarAsStream() {
|
||||
return "REGISTER STREAM getAverageSpeedByCar AS "
|
||||
public static QueryContainer getAverageDataByCarAsStream() {
|
||||
String query = "REGISTER STREAM getAverageSpeedByCar AS "
|
||||
+ "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> "
|
||||
+ "PREFIX f: <http://larkc.eu/csparql/sparql/jena/ext#> "
|
||||
+ "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> "
|
||||
@ -227,13 +237,15 @@ public class RentACarSimulation implements Runnable {
|
||||
+ " GROUP BY (?car) "
|
||||
+ " }"
|
||||
+ "} ";
|
||||
QueryContainer queryContainer = new QueryContainer("getAverageSpeedByCar", query, true);
|
||||
return queryContainer;
|
||||
}
|
||||
|
||||
/**
|
||||
* IMPORTANT NOTE: STREAM gets matched everywhere, so don't call stuff "stream"!
|
||||
*/
|
||||
public static String selectFromRegisteredStream() {
|
||||
return "REGISTER Query askRegisteredStrm AS "
|
||||
public static QueryContainer selectFromRegisteredStream() {
|
||||
String query = "REGISTER Query askRegisteredStrm AS "
|
||||
+ "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> "
|
||||
+ "PREFIX f: <http://larkc.eu/csparql/sparql/jena/ext#> "
|
||||
+ "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> "
|
||||
@ -243,5 +255,8 @@ public class RentACarSimulation implements Runnable {
|
||||
+ "WHERE { "
|
||||
+ " ?s ?p ?o . "
|
||||
+ "} ";
|
||||
QueryContainer queryContainer = new QueryContainer("askRegisteredStrm", query, false);
|
||||
queryContainer.useObserverWindow();
|
||||
return queryContainer;
|
||||
}
|
||||
}
|
||||
|
@ -1,60 +0,0 @@
|
||||
package lu.jpt.csparqlproject.util;
|
||||
|
||||
import java.util.StringTokenizer;
|
||||
|
||||
/**
|
||||
* Helper class that provides small methods to handle
|
||||
* (strings containing) csparql queries.
|
||||
*/
|
||||
public class CsparqlQueryHelper {
|
||||
|
||||
/**
|
||||
* Small inner class containing query info
|
||||
*/
|
||||
public class CsparqlQueryInfo {
|
||||
public boolean isStream;
|
||||
public String name;
|
||||
|
||||
public CsparqlQueryInfo() {
|
||||
this.name = "[UNRESOLVED NAME]";
|
||||
this.isStream = false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the csparql query name stated within the query.
|
||||
* @param query given csparql query
|
||||
* @return query info using class CsparqlQueryInfo
|
||||
*/
|
||||
public static CsparqlQueryInfo getQueryInfo(String query) {
|
||||
CsparqlQueryHelper.CsparqlQueryInfo result = new CsparqlQueryHelper().new CsparqlQueryInfo();
|
||||
StringTokenizer tokenizer = new StringTokenizer(query);
|
||||
boolean gotName = false;
|
||||
int stateCounter = 0;
|
||||
while(tokenizer.hasMoreElements() && gotName == false) {
|
||||
String token = tokenizer.nextToken();
|
||||
String trimmedLowerToken = token.toLowerCase().trim();
|
||||
switch(stateCounter) {
|
||||
case 0:
|
||||
if(trimmedLowerToken.equals("register")) {
|
||||
stateCounter++;
|
||||
}
|
||||
break;
|
||||
case 1:
|
||||
if(trimmedLowerToken.equals("query") || trimmedLowerToken.equals("stream")) {
|
||||
if(trimmedLowerToken.equals("stream")) result.isStream = true;
|
||||
stateCounter++;
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
result.name = token.trim();
|
||||
gotName = true;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user