[TASK] Add a bunch of proper queries.

This commit is contained in:
Jan Philipp Timme 2016-10-09 17:34:12 +02:00
parent bdf7191c18
commit 7572cd7ddd
3 changed files with 277 additions and 8 deletions

View File

@ -145,7 +145,7 @@ public class SimulationContext {
this.engine.execUpdateQueryOverDatasource(updateQuery); this.engine.execUpdateQueryOverDatasource(updateQuery);
*/ */
// DEBUGGING ONLY // DEBUGGING ONLY
ReasoningTester rt = new ReasoningTester(this.engine); //ReasoningTester rt = new ReasoningTester(this.engine);
// Spawn the whole simulation - this takes care of simulation-specific things // Spawn the whole simulation - this takes care of simulation-specific things
simulation = new RentACarSimulation(); simulation = new RentACarSimulation();
// Register all the event streams // Register all the event streams
@ -159,9 +159,14 @@ public class SimulationContext {
this.registeredStreams.add(driverStream); this.registeredStreams.add(driverStream);
Collection<QueryContainer> queriesToRegister = new ArrayList<QueryContainer>(); Collection<QueryContainer> queriesToRegister = new ArrayList<QueryContainer>();
// Collect the queries to use! // Collect the queries to use!
//queriesToRegister.add(RentACarSimulation.getEventsQuery()); queriesToRegister.add(RentACarSimulation.getStandingCarsStream());
queriesToRegister.add(RentACarSimulation.getAverageDataByCarAsStream()); queriesToRegister.add(RentACarSimulation.getMovingCarsStream());
queriesToRegister.add(RentACarSimulation.selectFromRegisteredStream()); queriesToRegister.add(RentACarSimulation.getStronglyAcceleratingCarsStream());
queriesToRegister.add(RentACarSimulation.getStronglyBrakingCarsStream());
queriesToRegister.add(RentACarSimulation.getEngineWearStream());
queriesToRegister.add(RentACarSimulation.getBrakeWearStream());
queriesToRegister.add(RentACarSimulation.getHandbrakeWearStream());
queriesToRegister.add(RentACarSimulation.getTireWearStream());
// Now register each query appropriately! // Now register each query appropriately!
for(QueryContainer queryContainer : queriesToRegister) { for(QueryContainer queryContainer : queriesToRegister) {
CsparqlQueryResultProxy resultProxy = null; CsparqlQueryResultProxy resultProxy = null;

View File

@ -108,10 +108,10 @@ public class Car {
this.motorOn = false; this.motorOn = false;
this.motorRpm = 0; this.motorRpm = 0;
this.speed = 0; this.speed = 0;
this.tirePressure1 = (Car.MIN_TIRE_PRESSURE[carType] + Car.MAX_TIRE_PRESSURE[carType]) / 2.0; this.tirePressure1 = 0*(Car.MIN_TIRE_PRESSURE[carType] + Car.MAX_TIRE_PRESSURE[carType]) / 2.0;
this.tirePressure2 = (Car.MIN_TIRE_PRESSURE[carType] + Car.MAX_TIRE_PRESSURE[carType]) / 2.0; this.tirePressure2 = 0*(Car.MIN_TIRE_PRESSURE[carType] + Car.MAX_TIRE_PRESSURE[carType]) / 2.0;
this.tirePressure3 = (Car.MIN_TIRE_PRESSURE[carType] + Car.MAX_TIRE_PRESSURE[carType]) / 2.0; this.tirePressure3 = 0*(Car.MIN_TIRE_PRESSURE[carType] + Car.MAX_TIRE_PRESSURE[carType]) / 2.0;
this.tirePressure4 = (Car.MIN_TIRE_PRESSURE[carType] + Car.MAX_TIRE_PRESSURE[carType]) / 2.0; this.tirePressure4 = 0*(Car.MIN_TIRE_PRESSURE[carType] + Car.MAX_TIRE_PRESSURE[carType]) / 2.0;
// State data // State data
this.currentState = CarState.LOCKED; this.currentState = CarState.LOCKED;
this.currentAction = CarAction.NONE; this.currentAction = CarAction.NONE;
@ -187,6 +187,25 @@ public class Car {
if(RandomHelper.isLuckyByChance(0.002)) { if(RandomHelper.isLuckyByChance(0.002)) {
this.checkEngineLightOn = true; this.checkEngineLightOn = true;
} }
// See whether a random tire will lose some pressure
if(RandomHelper.isLuckyByChance(0.5)) {
switch(RandomHelper.getRandomNumberWithin(1, 4)) {
case 1:
this.tirePressure1 -= 0.1;
break;
case 2:
this.tirePressure2 -= 0.1;
break;
case 3:
this.tirePressure3 -= 0.1;
break;
case 4:
this.tirePressure4 -= 0.1;
break;
default:
break;
}
}
// Do the simulation stuff // Do the simulation stuff
int rpmBonus = 0; int rpmBonus = 0;
Car.logger.debug("Old state: " + this.currentState + ", Action: " + this.currentAction); Car.logger.debug("Old state: " + this.currentState + ", Action: " + this.currentAction);

View File

@ -161,6 +161,251 @@ public class RentACarSimulation implements Runnable {
} }
} }
/**
* 1) Query to determine cars that are standing still
*/
public static QueryContainer getStandingCarsStream() {
String query = "REGISTER STREAM getStandingCars 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+"> "
+ "CONSTRUCT { "
+ " [] rdf:type car:CarStandingEvent "
+ " ; car:relatedCar ?car . "
+ "} "
+ "FROM STREAM <"+RentACarSimulation.CAR_STREAM_IRI+"> [RANGE 2s STEP 1s] "
+ "WHERE { "
+ " ?e rdf:type car:CarStatusEvent . "
+ " ?e car:relatedCar ?car . "
+ " ?e car:speed ?speed . "
+ " FILTER(?speed = 0) "
+ "}";
QueryContainer queryContainer = new QueryContainer("getStandingCars", query, true);
return queryContainer;
}
/**
* 1) Query to determine cars that are moving
*/
public static QueryContainer getMovingCarsStream() {
String query = "REGISTER STREAM getMovingCars 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+"> "
+ "CONSTRUCT { "
+ " [] rdf:type car:CarMovingEvent "
+ " ; car:relatedCar ?car . "
+ "} "
+ "FROM STREAM <"+RentACarSimulation.CAR_STREAM_IRI+"> [RANGE 2s STEP 1s] "
+ "WHERE { "
+ " ?e rdf:type car:CarStatusEvent . "
+ " ?e car:relatedCar ?car . "
+ " ?e car:speed ?speed . "
+ " FILTER(?speed > 0) "
+ "}";
QueryContainer queryContainer = new QueryContainer("getMovingCars", query, true);
return queryContainer;
}
/**
* 2) Query to determine cars that are strongly accelerating
*/
public static QueryContainer getStronglyAcceleratingCarsStream() {
String query = "REGISTER STREAM getStronglyAcceleratingCars 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+"> "
+ "CONSTRUCT { "
+ " [] rdf:type car:CarStrongAcceleratingEvent "
+ " ; car:relatedCar ?car "
+ " ; car:deltaSpeed ?deltaSpeed . "
+ "} "
+ "FROM STREAM <"+RentACarSimulation.CAR_STREAM_IRI+"> [RANGE 5s STEP 1s] "
+ "WHERE { "
+ " ?e1 rdf:type car:CarStatusEvent . "
+ " ?e1 car:relatedCar ?car . "
+ " ?e1 car:speed ?speed1 ."
+ " ?e2 rdf:type car:CarStatusEvent . "
+ " ?e2 car:relatedCar ?car . "
+ " ?e2 car:speed ?speed2 ."
+ " FILTER NOT EXISTS {"
+ " ?ex rdf:type car:CarStatusEvent . "
+ " FILTER(f:timestamp(?e1,rdf:type,car:CarStatusEvent) < f:timestamp(?ex,rdf:type,car:CarStatusEvent)) "
+ " FILTER(f:timestamp(?ex,rdf:type,car:CarStatusEvent) < f:timestamp(?e2,rdf:type,car:CarStatusEvent)) "
+ " } "
+ " BIND(?speed2 - ?speed1 AS ?deltaSpeed) "
+ " FILTER(f:timestamp(?e1,rdf:type,car:CarStatusEvent) < f:timestamp(?e2,rdf:type,car:CarStatusEvent)) "
+ " FILTER(?deltaSpeed > 25) "
+ "}";
QueryContainer queryContainer = new QueryContainer("getStronglyAcceleratingCars", query, true);
return queryContainer;
}
/**
* 2) Query to determine cars that are strongly braking
*/
public static QueryContainer getStronglyBrakingCarsStream() {
String query = "REGISTER STREAM getStronglyBrakingCars 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+"> "
+ "CONSTRUCT { "
+ " [] rdf:type car:CarStrongBrakingEvent "
+ " ; car:relatedCar ?car "
+ " ; car:deltaSpeed ?deltaSpeed . "
+ "} "
+ "FROM STREAM <"+RentACarSimulation.CAR_STREAM_IRI+"> [RANGE 5s STEP 1s] "
+ "WHERE { "
+ " ?e1 rdf:type car:CarStatusEvent . "
+ " ?e1 car:relatedCar ?car . "
+ " ?e1 car:speed ?speed1 . "
+ " ?e2 rdf:type car:CarStatusEvent . "
+ " ?e2 car:relatedCar ?car . "
+ " ?e2 car:speed ?speed2 . "
+ " FILTER NOT EXISTS {"
+ " ?ex rdf:type car:CarStatusEvent . "
+ " FILTER(f:timestamp(?e1,rdf:type,car:CarStatusEvent) < f:timestamp(?ex,rdf:type,car:CarStatusEvent)) "
+ " FILTER(f:timestamp(?ex,rdf:type,car:CarStatusEvent) < f:timestamp(?e2,rdf:type,car:CarStatusEvent)) "
+ " } "
+ " BIND(?speed2 - ?speed1 AS ?deltaSpeed) "
+ " FILTER(f:timestamp(?e1,rdf:type,car:CarStatusEvent) < f:timestamp(?e2,rdf:type,car:CarStatusEvent)) "
+ " FILTER(?deltaSpeed < -25) "
+ "}";
QueryContainer queryContainer = new QueryContainer("getStronglyBrakingCars", query, true);
return queryContainer;
}
/**
* 3) Query to determine engine wear aka higher motor rpms over time
* than the engine allows (see domain knowledge)
*/
public static QueryContainer getEngineWearStream() {
String query = "REGISTER STREAM getEngineWear 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+"> "
+ "CONSTRUCT { "
+ " [] rdf:type car:CarEngineWearEvent "
+ " ; car:relatedCar ?car"
+ " ; car:avgMotorRPM ?avgMotorRPM "
+ " ; car:maxMotorRPM ?maxMotorRPM . "
+ "} "
+ "FROM STREAM <"+RentACarSimulation.CAR_STREAM_IRI+"> [RANGE 3s STEP 1s] "
+ "FROM <http://example.org/carSimKnowledgeGraph> "
+ "WHERE { "
+ " { "
+ " SELECT ?car (AVG(?motorRPM) AS ?avgMotorRPM) "
+ " WHERE { "
+ " ?e rdf:type car:CarStatusEvent . "
+ " ?e car:relatedCar ?car . "
+ " ?e car:motorRPM ?motorRPM . "
+ " } "
+ " GROUP BY (?car) "
+ " }"
+ " ?car car:isCarModel ?carModel . "
+ " ?carModel car:maximumMotorRPM ?maxMotorRPM . "
+ " FILTER(?avgMotorRPM > ?maxMotorRPM) "
+ "}";
QueryContainer queryContainer = new QueryContainer("getEngineWear", query, true);
return queryContainer;
}
/**
* 3) Query to transform the CarStrongBrakingEvent into a
* CarBrakeWearEvent.
*/
public static QueryContainer getBrakeWearStream() {
String query = "REGISTER STREAM getBrakeWear 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+"> "
+ "CONSTRUCT { "
+ " [] rdf:type car:CarBrakeWearEvent "
+ " ; car:relatedCar ?car . "
+ "} "
+ "FROM STREAM <http://example.org/stream/getStronglyBrakingCars> [RANGE 2s STEP 1s] "
+ "WHERE { "
+ " ?e rdf:type car:CarStrongBrakingEvent . "
+ " ?e car:relatedCar ?car . "
+ "}";
QueryContainer queryContainer = new QueryContainer("getBrakeWear", query, true);
return queryContainer;
}
/**
* 3) Query to create CarHandbrakeWearEvents when cars are moving with their handbrake engaged
*/
public static QueryContainer getHandbrakeWearStream() {
String query = "REGISTER STREAM getHandbrakeWear 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+"> "
+ "CONSTRUCT { "
+ " [] rdf:type car:CarHandbrakeWearEvent "
+ " ; car:relatedCar ?car . "
+ "} "
+ "FROM STREAM <"+RentACarSimulation.CAR_STREAM_IRI+"> [RANGE 3s STEP 1s] "
+ "WHERE { "
+ " ?e rdf:type car:CarStatusEvent . "
+ " ?e car:relatedCar ?car ."
+ " ?e car:handbrakeEngaged ?handbrakeEngaged . "
+ " ?e car:speed ?speed . "
+ " FILTER(?speed > 0) "
+ " FILTER(?handbrakeEngaged = true) "
+ "}";
QueryContainer queryContainer = new QueryContainer("getHandbrakeWear", query, true);
return queryContainer;
}
/**
* 3) Query to created CarTireWearEvents when tires run with lower pressure
* than allowed in domain knowledge.
*/
public static QueryContainer getTireWearStream() {
String query = "REGISTER STREAM getTireWear 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+"> "
+ "CONSTRUCT { "
+ " [] rdf:type car:CarTireWearEvent "
+ " ; car:relatedCar ?car "
+ " ; car:tirePressureD1 ?tpdelta1 "
+ " ; car:tirePressureD2 ?tpdelta2 "
+ " ; car:tirePressureD3 ?tpdelta3 "
+ " ; car:tirePressureD4 ?tpdelta4 "
+ " ; car:minimumTirePressure ?minTirePressure . "
+ "} "
+ "FROM STREAM <"+RentACarSimulation.CAR_STREAM_IRI+"> [RANGE 3s STEP 1s] "
+ "FROM <http://example.org/carSimKnowledgeGraph> "
+ "WHERE { "
+ " ?e rdf:type car:CarStatusEvent . "
+ " ?e car:relatedCar ?car . "
+ " ?e car:tirePressure1 ?tpress1 . "
+ " ?e car:tirePressure2 ?tpress2 . "
+ " ?e car:tirePressure3 ?tpress3 . "
+ " ?e car:tirePressure4 ?tpress4 . "
+ " ?car car:isCarModel ?carModel . "
+ " ?carModel car:minimumTirePressure ?minTirePressure . "
+ " BIND(?minTirePressure - ?tpress1 AS ?tpdelta1) "
+ " BIND(?minTirePressure - ?tpress2 AS ?tpdelta2) "
+ " BIND(?minTirePressure - ?tpress3 AS ?tpdelta3) "
+ " BIND(?minTirePressure - ?tpress4 AS ?tpdelta4) "
+ " FILTER(?tpdelta1 > 0) "
+ "}";
QueryContainer queryContainer = new QueryContainer("getTireWear", query, true);
return queryContainer;
}
//////////////////////////////////////////////////////////////
public static QueryContainer getEventsQuery() { public static QueryContainer getEventsQuery() {
String query = "REGISTER QUERY getCarStatusEvents 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#> "