Skip to content

Commit

Permalink
fixed vehicle editing and added funtionality for importing/exporting …
Browse files Browse the repository at this point in the history
…vehicles, vehicles are now persistent in disk
  • Loading branch information
DenisCobeti committed Sep 2, 2020
1 parent ec3b711 commit dac8d1e
Show file tree
Hide file tree
Showing 15 changed files with 407 additions and 65 deletions.
117 changes: 116 additions & 1 deletion src/control/MogenControl.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static control.ViewListener.TableTypes;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
Expand All @@ -13,13 +14,21 @@
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Attribute;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;
import model.Config;

import model.MogenModel;
Expand Down Expand Up @@ -58,6 +67,17 @@ public class MogenControl implements ViewListener{

private final MogenModel model;
private final MogenView view;

private static final String VEHICLE_START_XML = "vType";
private static final String VEHICLE_ID_XML = "id";
private static final String VEHICLE_ACCEL_XML = "accel";
private static final String VEHICLE_DECEL_XML = "decel";
private static final String VEHICLE_TAU_XML = "tau";
private static final String VEHICLE_LENGTH_XML = "length";
private static final String VEHICLE_SPEED_XML = "maxSpeed";
private static final String VEHICLE_FOLLOW_XML = "carFollowModel";
private static final String VEHICLE_PROB_XML = "probability";


public static final String DEFAULT_MAP_NAME = "mapNetconvert";
public static final String DEFAULT_VTYPE_LOCATION = "vehicles";
Expand Down Expand Up @@ -85,6 +105,16 @@ public MogenControl(String[] args) {
roads = new HashSet();
converter = new MapConverter();


File vehicles = new File(DEFAULT_VTYPE_LOCATION
+ FilesExtension.VEHICLES.getExtension());

try {
this.importVehicles(vehicles);
} catch (FileNotFoundException | XMLStreamException ex) {
model.defaultVTypes();
}

for(RoadTypes road : MapConverter.DEFAULT_ROADS) roads.add(road.toString());
//obtainMap(0,0,0,0);
Config.load();
Expand Down Expand Up @@ -261,11 +291,33 @@ public void producedEvent(Event event, Object obj) {
Config.setSumoLocation((String)obj);
break;

case IMPORT_VEHICLES:

try {
importVehicles(new File((String)obj));
} catch (FileNotFoundException | XMLStreamException ex) {
view.update(model, ex);
} finally{
model.getvTypes().forEach((k, v) -> view.update
(model, new Tuple(k, v)));
}
break;

case EXPORT_VEHICLES:

try {
exportVehicles((String)obj);
} catch (IOException ex) {
view.update(model, ex);
}
break;

}
}


public void saveMap(MapSelection selection, String location) throws ProtocolException,
public void saveMap(MapSelection selection, String location) throws
ProtocolException,
IOException,
InterruptedException,
FileNotFoundException,
Expand Down Expand Up @@ -497,6 +549,65 @@ public String exportVehicles(String file) throws FileNotFoundException, IOExcept
return vehicles.getAbsolutePath();
}

public void importVehicles(File vehicles) throws FileNotFoundException,
XMLStreamException{


XMLInputFactory inputFactory = XMLInputFactory.newInstance();
InputStream in = new FileInputStream(vehicles);
XMLEventReader reader = inputFactory.createXMLEventReader(in);

XMLEvent event;

while (reader.hasNext()){
event = reader.nextEvent();

if(event.isStartElement()){
StartElement startElement = event.asStartElement();

if(startElement.getName().getLocalPart().equals(VEHICLE_START_XML)){
VType vehicle = new VType();
String idVehicle = "default";

Iterator<Attribute> attributes = startElement.getAttributes();

while (attributes.hasNext()){
Attribute attribute = attributes.next();

switch (attribute.getName().toString()){
case VEHICLE_ID_XML:
idVehicle = attribute.getValue();
break;
case VEHICLE_ACCEL_XML:
vehicle.setAccel(Double.valueOf(attribute.getValue()));
break;
case VEHICLE_DECEL_XML:
vehicle.setDecel(Double.valueOf(attribute.getValue()));
break;
case VEHICLE_TAU_XML:
vehicle.setTau(Double.valueOf(attribute.getValue()));
break;
case VEHICLE_LENGTH_XML:
vehicle.setLength(Integer.valueOf(attribute.getValue()));
break;
case VEHICLE_SPEED_XML:
vehicle.setMaxSpeed(Integer.valueOf(attribute.getValue()));
break;
case VEHICLE_FOLLOW_XML:
vehicle.assignFollowingModel(attribute.getValue(), startElement.getAttributes());
break;
case VEHICLE_PROB_XML:
vehicle.setProbability(Double.valueOf(attribute.getValue()));
break;
}
}
model.addElement(idVehicle, vehicle);

}

}
}
}
public void setRoadsFiltered(HashSet<String> roads) throws IOException,
ProtocolException, InterruptedException{
this.roads = roads;
Expand Down Expand Up @@ -624,6 +735,10 @@ public HashMap importOD(String path) throws FileNotFoundException,
}

private void salir() {
try {
exportVehicles(DEFAULT_VTYPE_LOCATION + FilesExtension.VEHICLES.getExtension());
} catch (IOException ex) {}

System.exit(0);
}

Expand Down
2 changes: 1 addition & 1 deletion src/control/ViewListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public enum Event {EXIT, NEW_MAP, NEW_VEHICLE_TYPE, NEW_SIMULATION,
EDIT_SUMO, REMOVE_VTYPE, FILTER_ROADS, NEW_TAZ,
SAVE_VEHICLES, NEW_ODELEMENT, EXPORT_ODMATRIX,
REMOVE_TAZ, REMOVE_OD_ELEMENT, REMOVE_FLOW, EDIT_FLOW,
ROADS_OPTIONS,IMPORT_OD}
ROADS_OPTIONS,IMPORT_OD, IMPORT_VEHICLES, EXPORT_VEHICLES}

public enum TableTypes{TAZType, FlowType, ODElementType}

Expand Down
5 changes: 3 additions & 2 deletions src/model/MogenModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public MogenModel(MogenView view) {
this.tazs = new HashMap<>();
this.elements = new HashMap<>();

defaultVTypes();
//defaultVTypes();

this.simulations = new HashMap<>();
}
Expand Down Expand Up @@ -113,7 +113,8 @@ public void removeTAZ(String id){
public void removeODE(String id){
elements.remove(id);
}
private void defaultVTypes(){

public void defaultVTypes(){
vTypes.put("Car", new VType());

VType sport = new VType();
Expand Down
4 changes: 4 additions & 0 deletions src/model/followingmodels/FollowingModel.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package model.followingmodels;

import java.util.Iterator;
import javax.xml.stream.events.Attribute;

/**
*
* @author Denis Florin Cobeti
*/
public interface FollowingModel {

public String toSimulation();
public void importAttributes(Iterator<Attribute> attributes);

}
36 changes: 36 additions & 0 deletions src/model/followingmodels/FollowingModelFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package model.followingmodels;

/**
*
* @author darkm
*/
public class FollowingModelFactory {

public static FollowingModel getFollowingModel(String type){
FollowingModel model;

switch(type){
case IDM.NAME:
model = new IDM();
break;
case Kerner.NAME:
model = new Kerner();
break;
case Krauss.NAME:
model = new Krauss();
break;
case PW2009.NAME:
model = new PW2009();
break;
default:
model = new Kerner();
break;
}
return model;
}
}
31 changes: 30 additions & 1 deletion src/model/followingmodels/IDM.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
package model.followingmodels;

import java.util.Iterator;
import javax.xml.stream.events.Attribute;

/**
*
* @author Neblis
*/
public class IDM implements FollowingModel{
private final static String FORMAT = "carFollowModel=\"%s\" minGap=\"%d\" "
+ "stepping=\"%d\" delta=\"%.1f\"";
private final static String NAME = "IDM";
public final static String NAME = "IDM";
public static final String EXPLANATION = "IDM model\n"
+ "the intelligent driver model (IDM) is a "
+ "time-continuous car-following model for the "
+ "simulation of freeway and urban traffic.";

private final static String MIN_GAP = "minGap";
private final static String STEPPING = "stepping";
private final static String DELTA = "delta";

private int minGap;
private int stepping;
private double delta;
Expand Down Expand Up @@ -61,4 +68,26 @@ public double getDelta() {
public String toSimulation() {
return String.format(FORMAT, NAME, minGap, stepping, delta);
}

@Override
public void importAttributes(Iterator<Attribute> attributes) {
while (attributes.hasNext()){
Attribute attribute = attributes.next();

switch (attribute.getName().toString()){
case MIN_GAP:
this.setMinGap(Integer.valueOf(attribute.getValue()));
break;

case STEPPING:
this.setStepping(Integer.valueOf(attribute.getValue()));
break;

case DELTA:
this.setDelta(Double.valueOf(attribute.getValue()));
break;

}
}
}
}
31 changes: 30 additions & 1 deletion src/model/followingmodels/Kerner.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
package model.followingmodels;

import java.util.Iterator;
import javax.xml.stream.events.Attribute;

/**
*
* @author Neblis
*/
public class Kerner implements FollowingModel{
private final static String FORMAT = "carFollowModel=\"%s\" minGap=\"%d\" "
+ "phi=\"%d\" k=\"%d\"";
private final static String NAME = "BKerner";
public final static String NAME = "BKerner";
public final static String EXPLANATION = "BKerner Model \n"
+ "In addition "
+ "to the free flow traffic phase (F), there are two traffic "
+ "phases in congested traffic: the synchronized flow traffic "
+ "phase (S) and the wide moving jam phase (J).";

private final static String MIN_GAP = "minGap";
private final static String PHI = "phi";
private final static String K = "k";

private int minGap;
private int k;
private int phi;
Expand Down Expand Up @@ -62,4 +69,26 @@ public void setPhi(int phi) {
public String toSimulation() {
return String.format(FORMAT, NAME, minGap, phi, k);
}

@Override
public void importAttributes(Iterator<Attribute> attributes) {
while (attributes.hasNext()){
Attribute attribute = attributes.next();

switch (attribute.getName().toString()){
case MIN_GAP:
this.setMinGap(Integer.valueOf(attribute.getValue()));
break;

case PHI:
this.setPhi(Integer.valueOf(attribute.getValue()));
break;

case K:
this.setK(Integer.valueOf(attribute.getValue()));
break;

}
}
}
}
Loading

0 comments on commit dac8d1e

Please sign in to comment.