[TASK] Add queries that use aggregation functions.

This commit is contained in:
Jan Philipp Timme 2016-10-03 18:28:42 +02:00
parent 744b9c0dc2
commit 614fa9f06b
3 changed files with 39 additions and 6 deletions

View File

@ -1,6 +1,5 @@
package lu.jpt.csparqlproject;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collection;
@ -156,14 +155,16 @@ public class SimulationContext {
// Register all the queries and add result observers!
Collection<String> queriesToRegister = new ArrayList<String>();
queriesToRegister.add(RentACarSimulation.getEventsQuery());
queriesToRegister.add(RentACarSimulation.getAverageDataByCar());
for(String query : queriesToRegister) {
String queryName = CsparqlQueryHelper.getQueryName(query);
CsparqlQueryResultProxy resultProxy = null;
try {
resultProxy = this.engine.registerQuery(query, true);
} catch (ParseException e) {
} catch (Exception e) {
SimulationContext.logger.error(e.toString());
SimulationContext.logger.error(e.getStackTrace().toString());
SimulationContext.logger.error("Could not register query "+queryName);
SimulationContext.logger.error(query);
}
this.queryResultProxies.add(resultProxy);
resultProxy.addObserver(new TextObserverWindow("[ResultProxy] " + queryName));

View File

@ -89,7 +89,7 @@ public class Driver {
private void useCar(Car car) {
Car.CarState carState = car.getState();
if(carState == Car.CarState.WRECKED) {
// Sad fade :-(
// Sad face :-(
return;
}
switch(carState) {

View File

@ -42,8 +42,8 @@ public class RentACarSimulation implements Runnable {
public RentACarSimulation() {
this.registerOwnPrefixes();
int numberOfCars = 1;
int numberOfCustomers = 1;
int numberOfCars = 5;
int numberOfCustomers = 5;
// Create a car pool and drivers
this.carPool = new CarPool(numberOfCars);
this.drivers = new ArrayList<Driver>();
@ -188,4 +188,36 @@ public class RentACarSimulation implements Runnable {
+ " ?driver car:hasPhoneNumber ?driverPhone . "
+ "}";
}
public static String getSpeedByCar() {
return "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#> "
+ "PREFIX car: <"+RentACarSimulation.BASE_ONTOLOGY_IRI+"> "
+ "SELECT ?car ?speed "
+ "FROM STREAM <"+RentACarSimulation.CAR_STREAM_IRI+"> [RANGE 5s STEP 1s] "
+ "WHERE { "
+ " ?e rdf:type car:CarStatusEvent . "
+ " ?e car:relatedCar ?car . "
+ " ?e car:speed ?speed . "
+ "}";
}
public static String getAverageDataByCar() {
return "REGISTER QUERY 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#> "
+ "PREFIX car: <"+RentACarSimulation.BASE_ONTOLOGY_IRI+"> "
+ "SELECT ?car (AVG(?speed) AS ?avgSpeed) (AVG(?rpm) AS ?avgRPM) "
+ "FROM STREAM <"+RentACarSimulation.CAR_STREAM_IRI+"> [RANGE 5s STEP 1s] "
+ "WHERE { "
+ " ?e rdf:type car:CarStatusEvent . "
+ " ?e car:relatedCar ?car . "
+ " ?e car:motorRPM ?rpm . "
+ " ?e car:speed ?speed . "
+ "} "
+ "GROUP BY (?car) ";
}
}