From 32f2d2151436a823a6921f595e5fbe23dadbb03a Mon Sep 17 00:00:00 2001 From: Jan Philipp Timme Date: Tue, 9 Aug 2016 16:29:10 +0200 Subject: [PATCH] [TASK] Generic commit. --- .../java/lu/jpt/csparqltest/rentacar/Car.java | 114 +++++++++++------- .../lu/jpt/csparqltest/rentacar/CarPool.java | 1 + .../lu/jpt/csparqltest/rentacar/Driver.java | 37 +++--- 3 files changed, 93 insertions(+), 59 deletions(-) diff --git a/src/main/java/lu/jpt/csparqltest/rentacar/Car.java b/src/main/java/lu/jpt/csparqltest/rentacar/Car.java index 2ca7b2a..682e576 100644 --- a/src/main/java/lu/jpt/csparqltest/rentacar/Car.java +++ b/src/main/java/lu/jpt/csparqltest/rentacar/Car.java @@ -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 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() { diff --git a/src/main/java/lu/jpt/csparqltest/rentacar/CarPool.java b/src/main/java/lu/jpt/csparqltest/rentacar/CarPool.java index 6a00d87..e1d9ee1 100644 --- a/src/main/java/lu/jpt/csparqltest/rentacar/CarPool.java +++ b/src/main/java/lu/jpt/csparqltest/rentacar/CarPool.java @@ -10,6 +10,7 @@ public class CarPool { private List availableCars; private Random rand; + public CarPool(int numberOfCars, String carIri) { this.rand = new Random(); this.cars = new ArrayList(); diff --git a/src/main/java/lu/jpt/csparqltest/rentacar/Driver.java b/src/main/java/lu/jpt/csparqltest/rentacar/Driver.java index a88c443..14ebcf3 100644 --- a/src/main/java/lu/jpt/csparqltest/rentacar/Driver.java +++ b/src/main/java/lu/jpt/csparqltest/rentacar/Driver.java @@ -17,6 +17,8 @@ public class Driver { private List 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;