From 7572cd7ddde201f2906f24317bafb0cad11e2640 Mon Sep 17 00:00:00 2001 From: Jan Philipp Timme Date: Sun, 9 Oct 2016 17:34:12 +0200 Subject: [PATCH] [TASK] Add a bunch of proper queries. --- .../jpt/csparqlproject/SimulationContext.java | 13 +- .../lu/jpt/csparqlproject/rentacar/Car.java | 27 +- .../rentacar/RentACarSimulation.java | 245 ++++++++++++++++++ 3 files changed, 277 insertions(+), 8 deletions(-) diff --git a/src/main/java/lu/jpt/csparqlproject/SimulationContext.java b/src/main/java/lu/jpt/csparqlproject/SimulationContext.java index ee53806..cd4fa94 100644 --- a/src/main/java/lu/jpt/csparqlproject/SimulationContext.java +++ b/src/main/java/lu/jpt/csparqlproject/SimulationContext.java @@ -145,7 +145,7 @@ public class SimulationContext { this.engine.execUpdateQueryOverDatasource(updateQuery); */ // 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 simulation = new RentACarSimulation(); // Register all the event streams @@ -159,9 +159,14 @@ public class SimulationContext { this.registeredStreams.add(driverStream); Collection queriesToRegister = new ArrayList(); // Collect the queries to use! - //queriesToRegister.add(RentACarSimulation.getEventsQuery()); - queriesToRegister.add(RentACarSimulation.getAverageDataByCarAsStream()); - queriesToRegister.add(RentACarSimulation.selectFromRegisteredStream()); + queriesToRegister.add(RentACarSimulation.getStandingCarsStream()); + queriesToRegister.add(RentACarSimulation.getMovingCarsStream()); + 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! for(QueryContainer queryContainer : queriesToRegister) { CsparqlQueryResultProxy resultProxy = null; diff --git a/src/main/java/lu/jpt/csparqlproject/rentacar/Car.java b/src/main/java/lu/jpt/csparqlproject/rentacar/Car.java index 30fb730..37cb734 100644 --- a/src/main/java/lu/jpt/csparqlproject/rentacar/Car.java +++ b/src/main/java/lu/jpt/csparqlproject/rentacar/Car.java @@ -108,10 +108,10 @@ public class Car { this.motorOn = false; this.motorRpm = 0; this.speed = 0; - this.tirePressure1 = (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.tirePressure3 = (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.tirePressure1 = 0*(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 = 0*(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 this.currentState = CarState.LOCKED; this.currentAction = CarAction.NONE; @@ -187,6 +187,25 @@ public class Car { if(RandomHelper.isLuckyByChance(0.002)) { 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 int rpmBonus = 0; Car.logger.debug("Old state: " + this.currentState + ", Action: " + this.currentAction); diff --git a/src/main/java/lu/jpt/csparqlproject/rentacar/RentACarSimulation.java b/src/main/java/lu/jpt/csparqlproject/rentacar/RentACarSimulation.java index d88ce58..bf7f21a 100644 --- a/src/main/java/lu/jpt/csparqlproject/rentacar/RentACarSimulation.java +++ b/src/main/java/lu/jpt/csparqlproject/rentacar/RentACarSimulation.java @@ -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: " + + "PREFIX f: " + + "PREFIX xsd: " + + "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: " + + "PREFIX f: " + + "PREFIX xsd: " + + "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: " + + "PREFIX f: " + + "PREFIX xsd: " + + "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: " + + "PREFIX f: " + + "PREFIX xsd: " + + "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: " + + "PREFIX f: " + + "PREFIX xsd: " + + "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 " + + "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: " + + "PREFIX f: " + + "PREFIX xsd: " + + "PREFIX car: <"+RentACarSimulation.BASE_ONTOLOGY_IRI+"> " + + "CONSTRUCT { " + + " [] rdf:type car:CarBrakeWearEvent " + + " ; car:relatedCar ?car . " + + "} " + + "FROM STREAM [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: " + + "PREFIX f: " + + "PREFIX xsd: " + + "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: " + + "PREFIX f: " + + "PREFIX xsd: " + + "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 " + + "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() { String query = "REGISTER QUERY getCarStatusEvents AS " + "PREFIX rdf: "