[TASK] Add QueryContainer class.

This commit is contained in:
Jan Philipp Timme 2016-10-08 18:46:54 +02:00
parent 6e584ad301
commit 28afcb4b7f
4 changed files with 87 additions and 87 deletions

View File

@ -9,16 +9,14 @@ import org.slf4j.LoggerFactory;
import eu.larkc.csparql.cep.api.RdfStream; import eu.larkc.csparql.cep.api.RdfStream;
import eu.larkc.csparql.common.utils.CsparqlUtils; 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.CsparqlEngine;
import eu.larkc.csparql.core.engine.CsparqlEngineImpl; import eu.larkc.csparql.core.engine.CsparqlEngineImpl;
import eu.larkc.csparql.core.engine.CsparqlQueryResultProxy; import eu.larkc.csparql.core.engine.CsparqlQueryResultProxy;
import eu.larkc.csparql.core.engine.RDFStreamFormatter; import eu.larkc.csparql.core.engine.RDFStreamFormatter;
import lu.jpt.csparqlproject.gui.FancyTextObserverWindow; import lu.jpt.csparqlproject.gui.FancyTextObserverWindow;
import lu.jpt.csparqlproject.misc.QueryContainer;
import lu.jpt.csparqlproject.misc.ReasoningTester; import lu.jpt.csparqlproject.misc.ReasoningTester;
import lu.jpt.csparqlproject.rentacar.RentACarSimulation; 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, * This class encapsulates the use of the C-SPARQL Engine. Here, the RentACarSimulation,
@ -159,38 +157,39 @@ public class SimulationContext {
RdfStream driverStream = simulation.getDriverStream(); RdfStream driverStream = simulation.getDriverStream();
this.engine.registerStream(driverStream); this.engine.registerStream(driverStream);
this.registeredStreams.add(driverStream); this.registeredStreams.add(driverStream);
Collection<String> queriesToRegister = new ArrayList<String>(); Collection<QueryContainer> queriesToRegister = new ArrayList<QueryContainer>();
// Collect the queries to use! // Collect the queries to use!
//queriesToRegister.add(RentACarSimulation.getEventsQuery()); //queriesToRegister.add(RentACarSimulation.getEventsQuery());
queriesToRegister.add(RentACarSimulation.getAverageDataByCarAsStream()); queriesToRegister.add(RentACarSimulation.getAverageDataByCarAsStream());
queriesToRegister.add(RentACarSimulation.selectFromRegisteredStream()); queriesToRegister.add(RentACarSimulation.selectFromRegisteredStream());
// Now register each query appropriately! // Now register each query appropriately!
for(String query : queriesToRegister) { for(QueryContainer queryContainer : queriesToRegister) {
CsparqlQueryInfo queryInfo = CsparqlQueryHelper.getQueryInfo(query);
CsparqlQueryResultProxy resultProxy = null; CsparqlQueryResultProxy resultProxy = null;
try { try {
resultProxy = this.engine.registerQuery(query, true); resultProxy = this.engine.registerQuery(queryContainer.query, queryContainer.reasoningEnabled);
this.enableReasoningForResultProxy(resultProxy); if(queryContainer.reasoningEnabled) {
this.enableReasoningForResultProxy(resultProxy, queryContainer);
}
// Take care of streams and queries differently. // 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. // 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); RDFStreamFormatter rdfStreamFormatter = new RDFStreamFormatter(streamUri);
engine.registerStream(rdfStreamFormatter); engine.registerStream(rdfStreamFormatter);
resultProxy.addObserver(rdfStreamFormatter); resultProxy.addObserver(rdfStreamFormatter);
Observer resultObserver = this.createResultObserverWindow(queryInfo.name); Observer resultObserver = this.createResultObserverWindow(queryContainer.name);
resultProxy.addObserver(resultObserver); resultProxy.addObserver(resultObserver);
} else { } else {
// If it is a regular query, just attach a fitting observer // 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); resultProxy.addObserver(resultObserver);
} }
this.queryResultProxies.add(resultProxy); 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) { } catch (Exception e) {
SimulationContext.logger.error(e.toString()); SimulationContext.logger.error(e.toString());
SimulationContext.logger.error("Could not register query "+queryInfo.name); SimulationContext.logger.error("Could not register query "+queryContainer.name);
SimulationContext.logger.error(query); SimulationContext.logger.error(queryContainer.query);
} }
} }
// Setup complete, ready to run. // Setup complete, ready to run.
@ -216,13 +215,19 @@ public class SimulationContext {
return observer; return observer;
} }
private void enableReasoningForResultProxy(CsparqlQueryResultProxy resultProxy) { private void enableReasoningForResultProxy(CsparqlQueryResultProxy resultProxy, QueryContainer queryContainer) {
try { try {
engine.updateReasoner( /* If not using the queryContainer, these would be valid parameters:
resultProxy.getSparqlQueryId(), resultProxy.getSparqlQueryId(),
CsparqlUtils.fileToString("data/rdfs.rules"), CsparqlUtils.fileToString("data/rdfs.rules"),
ReasonerChainingType.HYBRID, ReasonerChainingType.HYBRID,
CsparqlUtils.serializeRDFFile("data/carSimulationTBox.rdf") CsparqlUtils.serializeRDFFile("data/carSimulationTBox.rdf")
*/
engine.updateReasoner(
resultProxy.getSparqlQueryId(),
queryContainer.ruleSet,
queryContainer.reasonerChainingType,
queryContainer.TBox
); );
} catch(Exception e) { } catch(Exception e) {
e.printStackTrace(); e.printStackTrace();

View 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;
}
}

View File

@ -9,6 +9,7 @@ import org.slf4j.LoggerFactory;
import eu.larkc.csparql.cep.api.RdfQuadruple; import eu.larkc.csparql.cep.api.RdfQuadruple;
import eu.larkc.csparql.cep.api.RdfStream; import eu.larkc.csparql.cep.api.RdfStream;
import lu.jpt.csparqlproject.Main; import lu.jpt.csparqlproject.Main;
import lu.jpt.csparqlproject.misc.QueryContainer;
import lu.jpt.csparqlproject.util.WindowLoggingRdfStream; import lu.jpt.csparqlproject.util.WindowLoggingRdfStream;
/** /**
@ -146,8 +147,8 @@ public class RentACarSimulation implements Runnable {
} }
public static String getEventsQuery() { public static QueryContainer getEventsQuery() {
return "REGISTER QUERY getEvents AS " String query = "REGISTER QUERY getCarStatusEvents AS "
+ "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> " + "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> "
+ "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#> "
@ -168,10 +169,13 @@ public class RentACarSimulation implements Runnable {
+ " ?e car:tirePressure3 ?tirePressure3 . " + " ?e car:tirePressure3 ?tirePressure3 . "
+ " ?e car:tirePressure4 ?tirePressure4 . " + " ?e car:tirePressure4 ?tirePressure4 . "
+ "}"; + "}";
QueryContainer queryContainer = new QueryContainer("getCarStatusEvents", query, false);
queryContainer.useObserverWindow();
return queryContainer;
} }
public static String getEventUsingBackgroundKnowledge() { public static QueryContainer getEventUsingBackgroundKnowledge() {
return "REGISTER QUERY getEventsCombinedWithBackgroundKnowledge AS " String query = "REGISTER QUERY getEventsCombinedWithBackgroundKnowledge AS "
+ "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> " + "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> "
+ "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#> "
@ -187,10 +191,13 @@ public class RentACarSimulation implements Runnable {
+ " ?driver car:hasName ?driverName . " + " ?driver car:hasName ?driverName . "
+ " ?driver car:hasPhoneNumber ?driverPhone . " + " ?driver car:hasPhoneNumber ?driverPhone . "
+ "}"; + "}";
QueryContainer queryContainer = new QueryContainer("getEventsCombinedWithBackgroundKnowledge", query, false);
queryContainer.useObserverWindow();
return queryContainer;
} }
public static String getSpeedByCar() { public static QueryContainer getSpeedByCar() {
return "REGISTER QUERY getSpeedByCar AS " String query = "REGISTER QUERY getSpeedByCar AS "
+ "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> " + "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> "
+ "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#> "
@ -202,10 +209,13 @@ public class RentACarSimulation implements Runnable {
+ " ?e car:relatedCar ?car . " + " ?e car:relatedCar ?car . "
+ " ?e car:speed ?speed . " + " ?e car:speed ?speed . "
+ "}"; + "}";
QueryContainer queryContainer = new QueryContainer("getSpeedByCar", query, false);
queryContainer.useObserverWindow();
return queryContainer;
} }
public static String getAverageDataByCarAsStream() { public static QueryContainer getAverageDataByCarAsStream() {
return "REGISTER STREAM getAverageSpeedByCar AS " String query = "REGISTER STREAM getAverageSpeedByCar AS "
+ "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> " + "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> "
+ "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#> "
@ -227,13 +237,15 @@ public class RentACarSimulation implements Runnable {
+ " GROUP BY (?car) " + " GROUP BY (?car) "
+ " }" + " }"
+ "} "; + "} ";
QueryContainer queryContainer = new QueryContainer("getAverageSpeedByCar", query, true);
return queryContainer;
} }
/** /**
* IMPORTANT NOTE: STREAM gets matched everywhere, so don't call stuff "stream"! * IMPORTANT NOTE: STREAM gets matched everywhere, so don't call stuff "stream"!
*/ */
public static String selectFromRegisteredStream() { public static QueryContainer selectFromRegisteredStream() {
return "REGISTER Query askRegisteredStrm AS " String query = "REGISTER Query askRegisteredStrm AS "
+ "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> " + "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> "
+ "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#> "
@ -243,5 +255,8 @@ public class RentACarSimulation implements Runnable {
+ "WHERE { " + "WHERE { "
+ " ?s ?p ?o . " + " ?s ?p ?o . "
+ "} "; + "} ";
QueryContainer queryContainer = new QueryContainer("askRegisteredStrm", query, false);
queryContainer.useObserverWindow();
return queryContainer;
} }
} }

View File

@ -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;
}
}