[TASK] Generic commit.

This commit is contained in:
Jan Philipp Timme 2016-08-09 16:29:10 +02:00
parent 6f6e317162
commit 32f2d21514
3 changed files with 93 additions and 59 deletions

View File

@ -7,30 +7,60 @@ import eu.larkc.csparql.cep.api.RdfQuadruple;
import lu.jpt.csparqltest.util.RandomHelper;
public class Car {
// State-change related enums
private enum CarAction {NONE, UNLOCKING, STARTING, ACCELERATING, BRAKING, DRIFTING, CRASHING, STOPPING, LOCKING};
private enum CarState {LOCKED, OFF, IDLE, DRIVE};
/* STATE-Übergänge
Überall: NONE
LOCKED
* UNLOCKING
STANDING
* LOCKING
* STARTING
IDLING
* ACCELERATING
* STOPPING
DRIVING
* ACCELERATING
* BRAKING
* DRIFTING
* CRASHING
* STOPPING
*/
// Fix knowledge about the available car types:
private static final String[] CAR_TYPENAME = {"A", "B", "C"};
private static final String[] REQUIRED_DRIVER_LICENSE = {"B", "B", "C"};
private static final int[] MIN_RPM = {2000, 800, 1400};
private static final int[] MAX_RPM = {4300, 1900, 2500};
private static final double[] MIN_TIRE_PRESSURE = {2.9, 3.5, 2.6};
private static final double[] MAX_TIRE_PRESSURE = {3.2, 3.8, 3.0};
// Sitzplätze? Reservierungen für X Personen?
// Generic stuff
private int id;
private final String IRI;
private List<RdfQuadruple> quads;
// Car-specific attributes
// Car-specific information and current state
private final int CAR_TYPE;
private CarState currentState;
private CarAction currentAction;
private int currentActionTicksLeft;
private boolean handbrakeEngaged;
private boolean motorOn;
private int motorRpm;
private int motorMinRpm;
private int motorMaxRpm;
private int speed;
private double tirePressure;
// Sitzplätze? Reservierungen für X Personen?
public Car(int id, String baseIri) {
this.id = id;
this.IRI = baseIri;
@ -40,51 +70,51 @@ public class Car {
}
private void initializeCarType(int carType) {
// Specific model data
this.handbrakeEngaged = true;
this.motorOn = false;
this.motorRpm = 0;
this.motorMinRpm = Car.MAX_RPM[carType];
this.motorMaxRpm = Car.MAX_RPM[carType];
this.speed = 0;
this.tirePressure = (Car.MIN_TIRE_PRESSURE[carType] + Car.MAX_TIRE_PRESSURE[carType]) / 2.0;
}
public void turnOn() {
}
public void turnOff() {
}
public void setHandBrake(boolean on) {
}
public void accelerate() {
}
public void slowDown() {
}
public void crash() {
}
public void load() {
}
public void unload() {
// State data
this.currentState = CarState.LOCKED;
this.currentAction = CarAction.NONE;
this.currentActionTicksLeft = 0;
}
/**
* Update internal stuff according to state
*/
public void tick() {
switch(this.currentAction) {
case STARTING:
break;
case NONE:
break;
default:
break;
}
// Decrement action tick counter and move to none if action is done
this.currentActionTicksLeft--;
if(this.currentActionTicksLeft == 0) {
this.currentAction = CarAction.NONE;
}
}
public CarState getState() {
return this.currentState;
}
public CarAction getCurrentAction() {
return this.currentAction;
}
public int getCurrentActionTicksLeft() {
return this.currentActionTicksLeft;
}
public boolean needsInput() {
return (this.currentAction == CarAction.NONE);
}
public int getID() {

View File

@ -10,6 +10,7 @@ public class CarPool {
private List<Car> availableCars;
private Random rand;
public CarPool(int numberOfCars, String carIri) {
this.rand = new Random();
this.cars = new ArrayList<Car>();

View File

@ -17,6 +17,8 @@ public class Driver {
private List<RdfQuadruple> quads;
private enum Trait {CAREFUL, REGULAR, FAST, INSANE};
public Driver(int id, CarPool carPool, String baseIri) {
this.id = id;
this.carPool = carPool;
@ -40,26 +42,20 @@ public class Driver {
*/
public void tick() {
if(car == null) {
// Nothing to do, maybe randomly take a car
// Nothing to do, maybe randomly take a free car
if(RandomHelper.isLuckyByChance(0.4)) {
this.assignCar(this.carPool.takeRandomCar(), RandomHelper.getRandomNumberWithin(5,20));
}
} else {
// Do random things with the car
this.useCar(this.car);
// Decrement use cycles
this.carUseCycles--;
if(this.carUseCycles == 0) {
this.returnCar();
this.assignCar(this.carPool.takeRandomCar(), RandomHelper.getRandomNumberWithin(10,50));
}
}
}
public void assignCar(Car car, int useCycles) {
this.car = car;
this.carUseCycles = useCycles;
this.quads.add(new RdfQuadruple(this.getIri(), this.IRI+"#took", this.car.getIri(), System.currentTimeMillis()));
this.quads.add(new RdfQuadruple(this.getIri(), this.IRI+"#usagePeriod", useCycles+"^^http://www.w3.org/2001/XMLSchema#integer", System.currentTimeMillis()));
// Do random things with the car
if(this.car.needsInput()) {
this.useCar(this.car);
}
// Decrement use cycles
this.carUseCycles--;
if(this.carUseCycles == 0) {
this.returnCar();
}
}
private void useCar(Car car) {
@ -67,6 +63,13 @@ public class Driver {
}
private void assignCar(Car car, int useCycles) {
this.car = car;
this.carUseCycles = useCycles;
this.quads.add(new RdfQuadruple(this.getIri(), this.IRI+"#took", this.car.getIri(), System.currentTimeMillis()));
this.quads.add(new RdfQuadruple(this.getIri(), this.IRI+"#usagePeriod", useCycles+"^^http://www.w3.org/2001/XMLSchema#integer", System.currentTimeMillis()));
}
private void returnCar() {
this.quads.add(new RdfQuadruple(this.getIri(), this.IRI+"#returned", this.car.getIri(), System.currentTimeMillis()));
this.car = null;