[TASK] Add queries that use aggregation functions.
This commit is contained in:
parent
744b9c0dc2
commit
614fa9f06b
|
@ -1,6 +1,5 @@
|
||||||
package lu.jpt.csparqlproject;
|
package lu.jpt.csparqlproject;
|
||||||
|
|
||||||
import java.text.ParseException;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
|
||||||
|
@ -156,14 +155,16 @@ public class SimulationContext {
|
||||||
// Register all the queries and add result observers!
|
// Register all the queries and add result observers!
|
||||||
Collection<String> queriesToRegister = new ArrayList<String>();
|
Collection<String> queriesToRegister = new ArrayList<String>();
|
||||||
queriesToRegister.add(RentACarSimulation.getEventsQuery());
|
queriesToRegister.add(RentACarSimulation.getEventsQuery());
|
||||||
|
queriesToRegister.add(RentACarSimulation.getAverageDataByCar());
|
||||||
for(String query : queriesToRegister) {
|
for(String query : queriesToRegister) {
|
||||||
String queryName = CsparqlQueryHelper.getQueryName(query);
|
String queryName = CsparqlQueryHelper.getQueryName(query);
|
||||||
CsparqlQueryResultProxy resultProxy = null;
|
CsparqlQueryResultProxy resultProxy = null;
|
||||||
try {
|
try {
|
||||||
resultProxy = this.engine.registerQuery(query, true);
|
resultProxy = this.engine.registerQuery(query, true);
|
||||||
} catch (ParseException e) {
|
} catch (Exception e) {
|
||||||
SimulationContext.logger.error(e.toString());
|
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);
|
this.queryResultProxies.add(resultProxy);
|
||||||
resultProxy.addObserver(new TextObserverWindow("[ResultProxy] " + queryName));
|
resultProxy.addObserver(new TextObserverWindow("[ResultProxy] " + queryName));
|
||||||
|
|
|
@ -89,7 +89,7 @@ public class Driver {
|
||||||
private void useCar(Car car) {
|
private void useCar(Car car) {
|
||||||
Car.CarState carState = car.getState();
|
Car.CarState carState = car.getState();
|
||||||
if(carState == Car.CarState.WRECKED) {
|
if(carState == Car.CarState.WRECKED) {
|
||||||
// Sad fade :-(
|
// Sad face :-(
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
switch(carState) {
|
switch(carState) {
|
||||||
|
|
|
@ -42,8 +42,8 @@ public class RentACarSimulation implements Runnable {
|
||||||
|
|
||||||
public RentACarSimulation() {
|
public RentACarSimulation() {
|
||||||
this.registerOwnPrefixes();
|
this.registerOwnPrefixes();
|
||||||
int numberOfCars = 1;
|
int numberOfCars = 5;
|
||||||
int numberOfCustomers = 1;
|
int numberOfCustomers = 5;
|
||||||
// Create a car pool and drivers
|
// Create a car pool and drivers
|
||||||
this.carPool = new CarPool(numberOfCars);
|
this.carPool = new CarPool(numberOfCars);
|
||||||
this.drivers = new ArrayList<Driver>();
|
this.drivers = new ArrayList<Driver>();
|
||||||
|
@ -188,4 +188,36 @@ public class RentACarSimulation implements Runnable {
|
||||||
+ " ?driver car:hasPhoneNumber ?driverPhone . "
|
+ " ?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) ";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue