[TASK] Downgrade to engine 0.9.6

This commit is contained in:
Jan Philipp Timme 2016-10-04 15:04:35 +02:00
parent 1dfb6d1542
commit 78958c6b48
4 changed files with 80 additions and 25 deletions

View File

@ -27,7 +27,7 @@
<dependency> <dependency>
<groupId>eu.larkc.csparql</groupId> <groupId>eu.larkc.csparql</groupId>
<artifactId>csparql-core</artifactId> <artifactId>csparql-core</artifactId>
<version>0.9.7</version> <version>0.9.6</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>it.polimi.deib</groupId> <groupId>it.polimi.deib</groupId>

View File

@ -11,9 +11,11 @@ import eu.larkc.csparql.cep.api.RdfStream;
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 lu.jpt.csparqlproject.gui.FancyTextObserverWindow; import lu.jpt.csparqlproject.gui.FancyTextObserverWindow;
import lu.jpt.csparqlproject.rentacar.RentACarSimulation; import lu.jpt.csparqlproject.rentacar.RentACarSimulation;
import lu.jpt.csparqlproject.util.CsparqlQueryHelper; 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,
@ -153,23 +155,38 @@ 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);
// Register all the queries and add result observers!
Collection<String> queriesToRegister = new ArrayList<String>(); Collection<String> queriesToRegister = new ArrayList<String>();
queriesToRegister.add(RentACarSimulation.getEventsQuery()); // Collect the queries to use!
queriesToRegister.add(RentACarSimulation.getAverageDataByCar()); //queriesToRegister.add(RentACarSimulation.getEventsQuery());
queriesToRegister.add(RentACarSimulation.getAverageDataByCarAsStream());
queriesToRegister.add(RentACarSimulation.selectFromRegisteredStream());
// Now register each query appropriately!
for(String query : queriesToRegister) { for(String query : queriesToRegister) {
String queryName = CsparqlQueryHelper.getQueryName(query); CsparqlQueryInfo queryInfo = CsparqlQueryHelper.getQueryInfo(query);
CsparqlQueryResultProxy resultProxy = null; CsparqlQueryResultProxy resultProxy = null;
try { try {
resultProxy = this.engine.registerQuery(query, true); resultProxy = this.engine.registerQuery(query, false);
// Take care of streams and queries differently.
if(queryInfo.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;
RDFStreamFormatter rdfStreamFormatter = new RDFStreamFormatter(streamUri);
engine.registerStream(rdfStreamFormatter);
resultProxy.addObserver(rdfStreamFormatter);
Observer resultObserver = this.createResultObserverWindow(queryInfo.name);
resultProxy.addObserver(resultObserver);
} else {
// If it is a regular query, just attach a fitting observer
Observer resultObserver = this.createResultObserverWindow(queryInfo.name);
resultProxy.addObserver(resultObserver);
}
this.queryResultProxies.add(resultProxy);
SimulationContext.logger.info("Successfully registered query " + queryInfo.name + ": " + query);
} catch (Exception e) { } catch (Exception e) {
SimulationContext.logger.error(e.toString()); SimulationContext.logger.error(e.toString());
SimulationContext.logger.error("Could not register query "+queryName); SimulationContext.logger.error("Could not register query "+queryInfo.name);
SimulationContext.logger.error(query); SimulationContext.logger.error(query);
} }
this.queryResultProxies.add(resultProxy);
Observer resultObserver = this.createResultObserverWindow(queryName);
resultProxy.addObserver(resultObserver);
} }
// Setup complete, ready to run. // Setup complete, ready to run.
SimulationContext.logger.info("Simulation set up and ready to go!"); SimulationContext.logger.info("Simulation set up and ready to go!");

View File

@ -42,7 +42,7 @@ public class RentACarSimulation implements Runnable {
public RentACarSimulation() { public RentACarSimulation() {
this.registerOwnPrefixes(); this.registerOwnPrefixes();
int numberOfCars = 5; int numberOfCars = 2;
int numberOfCustomers = 1; int numberOfCustomers = 1;
// Create a car pool and drivers // Create a car pool and drivers
this.carPool = new CarPool(numberOfCars); this.carPool = new CarPool(numberOfCars);
@ -204,20 +204,44 @@ public class RentACarSimulation implements Runnable {
+ "}"; + "}";
} }
public static String getAverageDataByCar() { public static String getAverageDataByCarAsStream() {
return "REGISTER QUERY getAverageSpeedByCar AS " return "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#> "
+ "PREFIX car: <"+RentACarSimulation.BASE_ONTOLOGY_IRI+"> " + "PREFIX car: <"+RentACarSimulation.BASE_ONTOLOGY_IRI+"> "
+ "SELECT ?car (AVG(?speed) AS ?avgSpeed) (AVG(?rpm) AS ?avgRPM) " + "CONSTRUCT { "
+ " [] rdf:type car:AverageSpeedEvent . "
+ " [] car:relatedCar ?car . "
+ " [] car:averageSpeed ?avgSpeed . "
+ "} "
+ "FROM STREAM <"+RentACarSimulation.CAR_STREAM_IRI+"> [RANGE 5s STEP 1s] " + "FROM STREAM <"+RentACarSimulation.CAR_STREAM_IRI+"> [RANGE 5s STEP 1s] "
+ "WHERE { " + "WHERE { "
+ " ?e rdf:type car:CarStatusEvent . " + " { "
+ " ?e car:relatedCar ?car . " + " SELECT (AVG(?speed) AS ?avgSpeed) "
+ " ?e car:motorRPM ?rpm . " + " WHERE { "
+ " ?e car:speed ?speed . " + " ?e rdf:type car:CarStatusEvent . "
+ "} " + " ?e car:relatedCar ?car . "
+ "GROUP BY (?car) "; + " ?e car:speed ?speed . "
+ " } "
+ " GROUP BY (?car) "
+ " }"
+ "} ";
}
/**
* IMPORTANT NOTE: STREAM gets matched everywhere, so don't call stuff "stream"!
*/
public static String selectFromRegisteredStream() {
return "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#> "
+ "PREFIX car: <"+RentACarSimulation.BASE_ONTOLOGY_IRI+"> "
+ "SELECT ?s ?p ?o "
+ "FROM STREAM <http://example.org/stream/getAverageSpeedByCar> [RANGE 5s STEP 1s] "
+ "WHERE { "
+ " ?s ?p ?o . "
+ "} ";
} }
} }

View File

@ -8,13 +8,26 @@ import java.util.StringTokenizer;
*/ */
public class CsparqlQueryHelper { 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. * Returns the csparql query name stated within the query.
* @param query given csparql query * @param query given csparql query
* @return name of query * @return query info using class CsparqlQueryInfo
*/ */
public static String getQueryName(String query) { public static CsparqlQueryInfo getQueryInfo(String query) {
String queryName = "[UNRESOLVED NAME]"; CsparqlQueryHelper.CsparqlQueryInfo result = new CsparqlQueryHelper().new CsparqlQueryInfo();
StringTokenizer tokenizer = new StringTokenizer(query); StringTokenizer tokenizer = new StringTokenizer(query);
boolean gotName = false; boolean gotName = false;
int stateCounter = 0; int stateCounter = 0;
@ -29,18 +42,19 @@ public class CsparqlQueryHelper {
break; break;
case 1: case 1:
if(trimmedLowerToken.equals("query") || trimmedLowerToken.equals("stream")) { if(trimmedLowerToken.equals("query") || trimmedLowerToken.equals("stream")) {
if(trimmedLowerToken.equals("stream")) result.isStream = true;
stateCounter++; stateCounter++;
} }
break; break;
case 2: case 2:
queryName = token.trim(); result.name = token.trim();
gotName = true; gotName = true;
break; break;
default: default:
break; break;
} }
} }
return queryName; return result;
} }
} }