DataMan and State:
- Save state of given time. Still not tested with recovering saved states.
This commit is contained in:
parent
cebf2a272b
commit
147d45e4fe
@ -1,7 +1,8 @@
|
|||||||
|
import java.io.Serializable;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
public class Campaign {
|
public class Campaign implements Serializable {
|
||||||
|
|
||||||
private String UID;
|
private String UID;
|
||||||
private int numberOfAriivedBuses;
|
private int numberOfAriivedBuses;
|
||||||
|
107
src/DataManeger.java
Normal file
107
src/DataManeger.java
Normal file
@ -0,0 +1,107 @@
|
|||||||
|
import java.io.*;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
public class DataManeger {
|
||||||
|
|
||||||
|
private Path workingDir;
|
||||||
|
|
||||||
|
// public DataManeger(Path path) {
|
||||||
|
// this.seperator = File.separatorChar;
|
||||||
|
// this.workingDir = path;
|
||||||
|
// }
|
||||||
|
|
||||||
|
public DataManeger(State state, Date time){
|
||||||
|
Path dir = Paths.get("");
|
||||||
|
}
|
||||||
|
|
||||||
|
public DataManeger() {
|
||||||
|
this(null, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean stateAvailable(Date time) {
|
||||||
|
File f = new File(workingDir.toString()+time.getTime());
|
||||||
|
return f.exists();
|
||||||
|
}
|
||||||
|
|
||||||
|
public State loadState(Date time){
|
||||||
|
State state = null;
|
||||||
|
if (stateAvailable(time)){
|
||||||
|
try {
|
||||||
|
ObjectInputStream objectInputStream = new ObjectInputStream(
|
||||||
|
new FileInputStream(String.format("%s%d",workingDir,time.getTime())));
|
||||||
|
state = (State)objectInputStream.readObject();
|
||||||
|
objectInputStream.close();
|
||||||
|
} catch (IOException | ClassNotFoundException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean saveState(State state, Date time){
|
||||||
|
try {
|
||||||
|
FileOutputStream fs = new FileOutputStream(String.format("%s%d",workingDir,time.getTime()));
|
||||||
|
BufferedOutputStream bfs = new BufferedOutputStream(fs);
|
||||||
|
ObjectOutputStream objectOutputStream = new ObjectOutputStream(bfs);
|
||||||
|
objectOutputStream.writeObject(state);
|
||||||
|
objectOutputStream.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class State implements Serializable {
|
||||||
|
|
||||||
|
//A better implementation is to make this more general or make Makkah an instance.
|
||||||
|
|
||||||
|
private ArrayList<Campaign> listOfCampaigns;
|
||||||
|
private ArrayList<Vehicle> listOfVehicles;
|
||||||
|
private Route[] stdRoutes;
|
||||||
|
private Street[] stdStreet;
|
||||||
|
private Date allArrivedToArafatTime;
|
||||||
|
private Date allArrivedToHotelsTime;
|
||||||
|
|
||||||
|
public State(ArrayList<Campaign> listOfCampaigns, ArrayList<Vehicle> listOfVehicles, Route[] stdRoutes, Street[] stdStreet, Date allArrivedToArafatTime, Date allArrivedToHotelsTime) {
|
||||||
|
//Make clones since values may change if this is running on a thread.
|
||||||
|
this.listOfCampaigns = (ArrayList<Campaign>) listOfCampaigns.clone();
|
||||||
|
this.listOfVehicles = (ArrayList<Vehicle>) listOfVehicles.clone();
|
||||||
|
this.stdRoutes = stdRoutes.clone();
|
||||||
|
this.stdStreet = stdStreet.clone();
|
||||||
|
if (allArrivedToArafatTime != null) {
|
||||||
|
this.allArrivedToArafatTime = (Date) allArrivedToArafatTime.clone();
|
||||||
|
}
|
||||||
|
if (allArrivedToHotelsTime != null) {
|
||||||
|
this.allArrivedToHotelsTime = (Date) allArrivedToHotelsTime.clone();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public ArrayList<Campaign> getListOfCampaigns() {
|
||||||
|
return listOfCampaigns;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ArrayList<Vehicle> getListOfVehicles() {
|
||||||
|
return listOfVehicles;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Route[] getStdRoutes() {
|
||||||
|
return stdRoutes;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Street[] getStdStreet() {
|
||||||
|
return stdStreet;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Date getAllArrivedToArafatTime() {
|
||||||
|
return allArrivedToArafatTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Date getAllArrivedToHotelsTime() {
|
||||||
|
return allArrivedToHotelsTime;
|
||||||
|
}
|
||||||
|
}
|
@ -53,6 +53,7 @@ public class MakkahCity {
|
|||||||
//Start of Every hour
|
//Start of Every hour
|
||||||
if (firstDayTimeMan.getCurrentCalendar().get(Calendar.MINUTE) == 0){
|
if (firstDayTimeMan.getCurrentCalendar().get(Calendar.MINUTE) == 0){
|
||||||
System.out.println("\n\n" + getStreetsReport());
|
System.out.println("\n\n" + getStreetsReport());
|
||||||
|
saveState();
|
||||||
}
|
}
|
||||||
else System.out.print(".");
|
else System.out.print(".");
|
||||||
|
|
||||||
@ -732,4 +733,23 @@ public class MakkahCity {
|
|||||||
}
|
}
|
||||||
return buses;
|
return buses;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void saveState(){
|
||||||
|
State s = new State(listOfCampaigns,
|
||||||
|
listOfVehicles,
|
||||||
|
stdRoutes,
|
||||||
|
stdStreet,
|
||||||
|
allArrivedToArafatTime,
|
||||||
|
allArrivedToHotelsTime);
|
||||||
|
DataManeger dataManeger = new DataManeger();
|
||||||
|
dataManeger.saveState(s, currenttimeManager.getCurrentTime());
|
||||||
|
|
||||||
|
boolean result = dataManeger.saveState(s, currenttimeManager.getCurrentTime());
|
||||||
|
if (!result) System.out.println("Could not save state "+currenttimeManager.getCurrentTime().getTime());
|
||||||
|
}
|
||||||
|
|
||||||
|
private static State loadState(Date time){
|
||||||
|
DataManeger dataManeger = new DataManeger();
|
||||||
|
return dataManeger.loadState(time);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
public class Route implements Travelable {
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
public class Route implements Travelable, Serializable {
|
||||||
|
|
||||||
private Street[] streets;
|
private Street[] streets;
|
||||||
private District hotelArea;
|
private District hotelArea;
|
||||||
|
@ -1,33 +1,24 @@
|
|||||||
|
import java.io.Serializable;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
||||||
public class Street implements Travelable {
|
public class Street implements Travelable, Serializable {
|
||||||
|
|
||||||
private double length;
|
private double length;
|
||||||
private int numberOfLanes;
|
private int numberOfLanes;
|
||||||
private ArrayList<Vehicle> vehicles; //Current
|
private ArrayList<Vehicle> vehicles; //Current
|
||||||
private HashMap<Vehicle, ArrayList<Date>> vehiclesHistory;//History of vehicles
|
|
||||||
private StreetNames name;
|
private StreetNames name;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public Street(double length, int numberOfLanes, StreetNames name) {
|
public Street(double length, int numberOfLanes, StreetNames name) {
|
||||||
vehicles = new ArrayList<>();
|
vehicles = new ArrayList<>();
|
||||||
vehiclesHistory = new HashMap<>();
|
|
||||||
setLength(length);
|
setLength(length);
|
||||||
setNumberOfLanes(numberOfLanes);
|
setNumberOfLanes(numberOfLanes);
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Date[] getHistoryForVehicle(Vehicle vehicle){
|
|
||||||
if (vehiclesHistory.containsKey(vehicle)){
|
|
||||||
Date[] hist = new Date[vehiclesHistory.get(vehicle).size()];
|
|
||||||
return vehiclesHistory.get(vehicle).toArray(hist);
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void setLength(double length) {
|
private void setLength(double length) {
|
||||||
if (length >= 0) this.length = length;
|
if (length >= 0) this.length = length;
|
||||||
else throw new IllegalArgumentException("Can not make a negative length street");
|
else throw new IllegalArgumentException("Can not make a negative length street");
|
||||||
@ -96,14 +87,6 @@ public class Street implements Travelable {
|
|||||||
|
|
||||||
public void addVehicle(Vehicle vehicle) {
|
public void addVehicle(Vehicle vehicle) {
|
||||||
vehicles.add(vehicle);
|
vehicles.add(vehicle);
|
||||||
addHistoryEntry(vehicle);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void addHistoryEntry(Vehicle vehicle) {
|
|
||||||
if (!vehiclesHistory.containsKey(vehicle)) {
|
|
||||||
vehiclesHistory.put(vehicle, new ArrayList<>());
|
|
||||||
}
|
|
||||||
vehiclesHistory.get(vehicle).add(MakkahCity.getTimeMan().getCurrentTime());//Add time
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public double capcityPoint(double min, double max) {
|
public double capcityPoint(double min, double max) {
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
|
import java.io.Serializable;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
||||||
public abstract class Vehicle {
|
public abstract class Vehicle implements Serializable {
|
||||||
|
|
||||||
private double vehicleSize;
|
private double vehicleSize;
|
||||||
private Route route;
|
private Route route;
|
||||||
|
Loading…
Reference in New Issue
Block a user