From e907a4a481ce6cb062c2c002cbe4d8e61279f34b Mon Sep 17 00:00:00 2001 From: Jan Philipp Timme Date: Sun, 2 Oct 2016 18:10:14 +0200 Subject: [PATCH] [TASK] Cleanup --- .../lu/jpt/csparqltest/carexample/Car.java | 144 ------------------ .../csparqltest/carexample/CarSimulator.java | 29 ---- .../carexample/CarStreamGenerator.java | 81 ---------- .../csparqltest/gui/TextObserverWindow.java | 7 +- .../rentacar/RentACarSimulation.java | 5 +- .../csparqltest/util/TestStreamGenerator.java | 11 +- .../util/WindowLoggingRdfStream.java | 24 +++ 7 files changed, 33 insertions(+), 268 deletions(-) delete mode 100644 src/main/java/lu/jpt/csparqltest/carexample/Car.java delete mode 100644 src/main/java/lu/jpt/csparqltest/carexample/CarSimulator.java delete mode 100644 src/main/java/lu/jpt/csparqltest/carexample/CarStreamGenerator.java create mode 100644 src/main/java/lu/jpt/csparqltest/util/WindowLoggingRdfStream.java diff --git a/src/main/java/lu/jpt/csparqltest/carexample/Car.java b/src/main/java/lu/jpt/csparqltest/carexample/Car.java deleted file mode 100644 index a95b082..0000000 --- a/src/main/java/lu/jpt/csparqltest/carexample/Car.java +++ /dev/null @@ -1,144 +0,0 @@ -package lu.jpt.csparqltest.carexample; - -import java.util.Random; - -public class Car { - - private int id; - private int motorTemperature; - private int kilometersTotal; - private int kilometersPerHour; - - private CarState state; - - private enum CarState { - PARK, DRIVE, FAIL, POST_FAIL - } - - private final int maxSpeed; - - private static final int MAX_MOTOR_TEMPERATURE = 125; - private static final int MIN_MOTOR_TEMPERATURE = 20; - - private static final Random rand = new Random(); - - public Car(int number) { - this.id = number; - this.motorTemperature = this.getRandomNumberWithin(20, 30); - this.kilometersTotal = this.getRandomNumberWithin(1000, 270000); - this.kilometersPerHour = 0; - this.state = CarState.PARK; - this.maxSpeed = this.getRandomNumberWithin(130, 210); - } - - public void simulateNextState() { - switch(this.state) { - case PARK: - if(this.isLucky(0.35)) { - this.state = CarState.DRIVE; - this.kilometersPerHour = this.getRandomNumberWithin(7, 15); - } - break; - case DRIVE: - double willAccelerate = Math.abs(0.5 - this.getSpeedRatio()) + 0.2; - if(this.isLucky(willAccelerate)) { - this.kilometersPerHour += this.getRandomNumberWithin(5, 14); - } else { - this.kilometersPerHour += (-1) * this.getRandomNumberWithin(5, 14); - } - double willFail = 0.001; - if(this.kilometersTotal > 100000) willFail += 0.001; - if(this.motorTemperature > 100) willFail += 0.01; - if(this.isLucky(willFail)) { - this.state = CarState.FAIL; - } - break; - case FAIL: - if(this.kilometersPerHour == 0) { - this.state = CarState.POST_FAIL; - } - this.kilometersPerHour += (-1) * this.getRandomNumberWithin(15, 24); - break; - case POST_FAIL: - break; - } - this.enforceLimits(); - this.adaptStateToCurrentParams(); - this.enforceLimits(); - } - - public int getID() { - return this.id; - } - - public int getMotorTemperature() { - return this.motorTemperature; - } - - public int getKilometersTotal() { - return this.kilometersTotal; - } - - public int getKilometersPerHour() { - return this.kilometersPerHour; - } - - public String toString() { - StringBuilder sb = new StringBuilder("[#"); - sb.append(this.id); - sb.append(" STATE="); - sb.append(this.state); - sb.append(", KMH="); - sb.append(this.kilometersPerHour); - sb.append(", TEMP="); - sb.append(this.motorTemperature); - sb.append(", TOTAL_KM="); - sb.append(this.kilometersTotal); - sb.append("]"); - return sb.toString(); - } - - private void adaptStateToCurrentParams() { - double speedRatio = this.getSpeedRatio(); - int resultingHeatDelta = 0; - switch(this.state) { - case PARK: - resultingHeatDelta = (-1) * this.getRandomNumberWithin(2, 5); - break; - case DRIVE: - int heatDifficulty = (int) (5 * ( (double) this.motorTemperature / (double) Car.MAX_MOTOR_TEMPERATURE ) ); - resultingHeatDelta = -1 * heatDifficulty; - resultingHeatDelta += (int) (10 * speedRatio); - break; - case FAIL: - resultingHeatDelta = Math.abs((int) (this.motorTemperature * (speedRatio + 0.85))); - break; - case POST_FAIL: - resultingHeatDelta = (-1) * this.getRandomNumberWithin(2, 4); - break; - } - this.motorTemperature += resultingHeatDelta; - this.kilometersTotal += (int) (((double) this.kilometersPerHour) / 60.0); - } - - private int getRandomNumberWithin(int min, int max) { - return min + Car.rand.nextInt((max + 1) - min); - } - - private boolean isLucky(double chance) { - return Car.rand.nextDouble() < chance; - } - - private double getSpeedRatio() { - return ((double) this.kilometersPerHour) / ((double) this.maxSpeed); - } - - private void enforceLimits() { - if(this.kilometersPerHour < 0) this.kilometersPerHour = 0; - if(this.kilometersPerHour > this.maxSpeed) this.kilometersPerHour = this.maxSpeed; - if(this.motorTemperature < Car.MIN_MOTOR_TEMPERATURE) this.motorTemperature = MIN_MOTOR_TEMPERATURE; - if(this.motorTemperature > Car.MAX_MOTOR_TEMPERATURE) this.motorTemperature = MAX_MOTOR_TEMPERATURE; - } - - -} diff --git a/src/main/java/lu/jpt/csparqltest/carexample/CarSimulator.java b/src/main/java/lu/jpt/csparqltest/carexample/CarSimulator.java deleted file mode 100644 index 37be6ca..0000000 --- a/src/main/java/lu/jpt/csparqltest/carexample/CarSimulator.java +++ /dev/null @@ -1,29 +0,0 @@ -package lu.jpt.csparqltest.carexample; - -import java.util.ArrayList; - -public class CarSimulator { - - /** - * Simple debugging programm to check the simulators properties - */ - public static void main(String[] args) { - System.out.println("Let's go!"); - ArrayList cars = new ArrayList(); - cars.add(new Car(0)); - while(true) { - for(Car currentCar : cars) { - currentCar.simulateNextState(); - System.out.println(currentCar); - try { - Thread.sleep(100); - } catch (InterruptedException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - } - } - } - -} diff --git a/src/main/java/lu/jpt/csparqltest/carexample/CarStreamGenerator.java b/src/main/java/lu/jpt/csparqltest/carexample/CarStreamGenerator.java deleted file mode 100644 index 892e923..0000000 --- a/src/main/java/lu/jpt/csparqltest/carexample/CarStreamGenerator.java +++ /dev/null @@ -1,81 +0,0 @@ -package lu.jpt.csparqltest.carexample; - -import java.util.ArrayList; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import eu.larkc.csparql.cep.api.RdfQuadruple; -import eu.larkc.csparql.cep.api.RdfStream; - -public class CarStreamGenerator extends RdfStream implements Runnable { - protected final Logger logger = LoggerFactory.getLogger(CarStreamGenerator.class); - - private volatile boolean keepRunning = false; - - private ArrayList cars; - - public CarStreamGenerator(String iri) { - super(iri); - this.initializeCars(10); - } - - public void pleaseStop() { - keepRunning = false; - } - - @Override - public void run() { - this.keepRunning = true; - while (this.keepRunning) { - this.updateCars(); - for(Car currentCar : this.cars) { - long currentTime = System.currentTimeMillis(); - String eventID = this.getIRI() + "/event#" + currentTime; - - this.put(new RdfQuadruple( - eventID, - getIRI() + "#carID", - ""+currentCar.getID(), - currentTime - ) - ); - this.put(new RdfQuadruple( - eventID, - getIRI() + "#currentSpeed", - ""+currentCar.getKilometersPerHour() + "^^http://www.w3.org/2001/XMLSchema#integer", - currentTime - ) - ); - this.put(new RdfQuadruple( - eventID, - getIRI() + "#currentTemperature", - ""+currentCar.getMotorTemperature() + "^^http://www.w3.org/2001/XMLSchema#integer", - currentTime - ) - ); - } - - try { - Thread.sleep(1000); - } catch (InterruptedException e) { - e.printStackTrace(); - } - - } - } - - private void initializeCars(int n) { - this.cars = new ArrayList(); - for(int i=0; i