[TASK] Cleanup
This commit is contained in:
parent
1879d657c6
commit
e907a4a481
|
@ -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;
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -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<Car> cars = new ArrayList<Car>();
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -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<Car> 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<Car>();
|
||||
for(int i=0; i<n; i++) {
|
||||
this.cars.add(new Car(i));
|
||||
}
|
||||
}
|
||||
|
||||
private void updateCars() {
|
||||
for(Car currentCar : this.cars) {
|
||||
currentCar.simulateNextState();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -39,6 +39,8 @@ import eu.larkc.csparql.core.engine.CsparqlQueryResultProxy;
|
|||
public class TextObserverWindow extends JFrame implements Observer {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
private static final int TEXTPANE_WIDTH = 800;
|
||||
private static final int TEXTPANE_HEIGHT = 240;
|
||||
|
||||
private JTextPane textPane;
|
||||
private volatile boolean autoScrollToBottom = true;
|
||||
|
@ -158,7 +160,7 @@ public class TextObserverWindow extends JFrame implements Observer {
|
|||
|
||||
// Add a ScrollPane
|
||||
JScrollPane scrollPane = new JScrollPane(this.textPane);
|
||||
scrollPane.setPreferredSize(new Dimension(800, 500));
|
||||
scrollPane.setPreferredSize(new Dimension(TextObserverWindow.TEXTPANE_WIDTH, TextObserverWindow.TEXTPANE_HEIGHT));
|
||||
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
|
||||
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
|
||||
contentPane.add(scrollPane, BorderLayout.CENTER);
|
||||
|
@ -207,6 +209,8 @@ public class TextObserverWindow extends JFrame implements Observer {
|
|||
|
||||
// Add bottomPanel to contentPane
|
||||
contentPane.add(bottomPanel, BorderLayout.SOUTH);
|
||||
|
||||
// Finally show the window
|
||||
this.setVisible(true);
|
||||
this.pack();
|
||||
}
|
||||
|
@ -326,5 +330,4 @@ public class TextObserverWindow extends JFrame implements Observer {
|
|||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ import java.util.List;
|
|||
|
||||
import eu.larkc.csparql.cep.api.RdfQuadruple;
|
||||
import eu.larkc.csparql.cep.api.RdfStream;
|
||||
import lu.jpt.csparqltest.util.WindowLoggingRdfStream;
|
||||
|
||||
public class RentACarSimulation implements Runnable {
|
||||
|
||||
|
@ -36,8 +37,8 @@ public class RentACarSimulation implements Runnable {
|
|||
this.drivers.add(driver);
|
||||
}
|
||||
// Create streams
|
||||
this.carStream = new RdfStream(CAR_STREAM_IRI);
|
||||
this.driverStream = new RdfStream(DRIVER_STREAM_IRI);
|
||||
this.carStream = new WindowLoggingRdfStream(CAR_STREAM_IRI);
|
||||
this.driverStream = new WindowLoggingRdfStream(DRIVER_STREAM_IRI);
|
||||
// Green light for simulation
|
||||
this.runSimulation = true;
|
||||
}
|
||||
|
|
|
@ -4,18 +4,14 @@ import org.slf4j.Logger;
|
|||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import eu.larkc.csparql.cep.api.RdfQuadruple;
|
||||
import lu.jpt.csparqltest.gui.TextObserverWindow;
|
||||
|
||||
public class TestStreamGenerator extends LoggableRdfStream implements Runnable {
|
||||
public class TestStreamGenerator extends WindowLoggingRdfStream implements Runnable {
|
||||
protected final Logger logger = LoggerFactory.getLogger(TestStreamGenerator.class);
|
||||
|
||||
private volatile boolean keepRunning = false;
|
||||
|
||||
private TextObserverWindow observerWindow;
|
||||
|
||||
public TestStreamGenerator(String iri) {
|
||||
super(iri);
|
||||
observerWindow = new TextObserverWindow("TestStreamGenerator");
|
||||
}
|
||||
|
||||
public void pleaseStop() {
|
||||
|
@ -48,9 +44,4 @@ public class TestStreamGenerator extends LoggableRdfStream implements Runnable {
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void log(RdfQuadruple quad) {
|
||||
this.observerWindow.showQuadruple(quad);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,24 @@
|
|||
package lu.jpt.csparqltest.util;
|
||||
|
||||
import eu.larkc.csparql.cep.api.RdfQuadruple;
|
||||
import lu.jpt.csparqltest.gui.TextObserverWindow;
|
||||
|
||||
public class WindowLoggingRdfStream extends LoggableRdfStream {
|
||||
|
||||
private TextObserverWindow observerWindow;
|
||||
|
||||
public WindowLoggingRdfStream(String iri) {
|
||||
super(iri);
|
||||
this.observerWindow = new TextObserverWindow("[RdfStream] "+iri);
|
||||
}
|
||||
|
||||
/**
|
||||
* This hook method gets called for every RdfQuadruple that is fed into the stream.
|
||||
* @param quad that has been fed into the stream.
|
||||
*/
|
||||
@Override
|
||||
public void log(RdfQuadruple quad) {
|
||||
observerWindow.showQuadruple(quad);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue