mirror of
https://github.com/davidalves04/Trabalho-Pratico-SD.git
synced 2025-12-08 12:33:31 +00:00
added config based traffic
This commit is contained in:
101
main/src/main/java/sd/config/SimulationConfig.java
Normal file
101
main/src/main/java/sd/config/SimulationConfig.java
Normal file
@@ -0,0 +1,101 @@
|
||||
package sd.config;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* Class to load and manage simulation configurations.
|
||||
* Configurations are read from a .properties file.
|
||||
*/
|
||||
public class SimulationConfig {
|
||||
private final Properties properties;
|
||||
|
||||
public SimulationConfig(String filePath) throws IOException {
|
||||
properties = new Properties();
|
||||
try (InputStream input = new FileInputStream(filePath)) {
|
||||
properties.load(input);
|
||||
}
|
||||
}
|
||||
|
||||
// Network configurations
|
||||
public String getIntersectionHost(String intersectionId) {
|
||||
return properties.getProperty("intersection." + intersectionId + ".host", "localhost");
|
||||
}
|
||||
|
||||
public int getIntersectionPort(String intersectionId) {
|
||||
return Integer.parseInt(properties.getProperty("intersection." + intersectionId + ".port", "0"));
|
||||
}
|
||||
|
||||
public String getDashboardHost() {
|
||||
return properties.getProperty("dashboard.host", "localhost");
|
||||
}
|
||||
|
||||
public int getDashboardPort() {
|
||||
return Integer.parseInt(properties.getProperty("dashboard.port", "9000"));
|
||||
}
|
||||
|
||||
public String getExitHost() {
|
||||
return properties.getProperty("exit.host", "localhost");
|
||||
}
|
||||
|
||||
public int getExitPort() {
|
||||
return Integer.parseInt(properties.getProperty("exit.port", "9001"));
|
||||
}
|
||||
|
||||
// Simulation configurations
|
||||
public double getSimulationDuration() {
|
||||
return Double.parseDouble(properties.getProperty("simulation.duration", "3600.0"));
|
||||
}
|
||||
|
||||
public String getArrivalModel() {
|
||||
return properties.getProperty("simulation.arrival.model", "POISSON");
|
||||
}
|
||||
|
||||
public double getArrivalRate() {
|
||||
return Double.parseDouble(properties.getProperty("simulation.arrival.rate", "0.5"));
|
||||
}
|
||||
|
||||
public double getFixedArrivalInterval() {
|
||||
return Double.parseDouble(properties.getProperty("simulation.arrival.fixed.interval", "2.0"));
|
||||
}
|
||||
|
||||
// Traffic light configurations
|
||||
public double getTrafficLightGreenTime(String intersectionId, String direction) {
|
||||
String key = "trafficlight." + intersectionId + "." + direction + ".green";
|
||||
return Double.parseDouble(properties.getProperty(key, "30.0"));
|
||||
}
|
||||
|
||||
public double getTrafficLightRedTime(String intersectionId, String direction) {
|
||||
String key = "trafficlight." + intersectionId + "." + direction + ".red";
|
||||
return Double.parseDouble(properties.getProperty(key, "30.0"));
|
||||
}
|
||||
|
||||
// Vehicle configurations
|
||||
public double getLightVehicleProbability() {
|
||||
return Double.parseDouble(properties.getProperty("vehicle.probability.light", "0.7"));
|
||||
}
|
||||
|
||||
public double getLightVehicleCrossingTime() {
|
||||
return Double.parseDouble(properties.getProperty("vehicle.crossing.time.light", "2.0"));
|
||||
}
|
||||
|
||||
public double getHeavyVehicleCrossingTime() {
|
||||
return Double.parseDouble(properties.getProperty("vehicle.crossing.time.heavy", "4.0"));
|
||||
}
|
||||
|
||||
// Statistics
|
||||
public double getStatisticsUpdateInterval() {
|
||||
return Double.parseDouble(properties.getProperty("statistics.update.interval", "10.0"));
|
||||
}
|
||||
|
||||
// Generic method to get any property
|
||||
public String getProperty(String key, String defaultValue) {
|
||||
return properties.getProperty(key, defaultValue);
|
||||
}
|
||||
|
||||
public String getProperty(String key) {
|
||||
return properties.getProperty(key);
|
||||
}
|
||||
}
|
||||
61
main/src/main/java/sd/model/Event.java
Normal file
61
main/src/main/java/sd/model/Event.java
Normal file
@@ -0,0 +1,61 @@
|
||||
package sd.model;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* Represents an event in the discrete event simulation.
|
||||
* Events are ordered by timestamp for sequential processing.
|
||||
*/
|
||||
public class Event implements Comparable<Event>, Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private final double timestamp; // Time when the event occurs
|
||||
private final EventType type;
|
||||
private final Object data; // Data associated with the event (e.g., Vehicle, traffic light id, etc.)
|
||||
private final String location; // Intersection or location where the event occurs
|
||||
|
||||
public Event(double timestamp, EventType type, Object data, String location) {
|
||||
this.timestamp = timestamp;
|
||||
this.type = type;
|
||||
this.data = data;
|
||||
this.location = location;
|
||||
}
|
||||
|
||||
public Event(double timestamp, EventType type, Object data) {
|
||||
this(timestamp, type, data, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(Event other) {
|
||||
// Sort by timestamp (earlier events have priority)
|
||||
int cmp = Double.compare(this.timestamp, other.timestamp);
|
||||
if (cmp == 0) {
|
||||
// If timestamps are equal, sort by event type
|
||||
return this.type.compareTo(other.type);
|
||||
}
|
||||
return cmp;
|
||||
}
|
||||
|
||||
// Getters
|
||||
public double getTimestamp() {
|
||||
return timestamp;
|
||||
}
|
||||
|
||||
public EventType getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public Object getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public String getLocation() {
|
||||
return location;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("Event{t=%.2f, type=%s, loc=%s}",
|
||||
timestamp, type, location);
|
||||
}
|
||||
}
|
||||
13
main/src/main/java/sd/model/EventType.java
Normal file
13
main/src/main/java/sd/model/EventType.java
Normal file
@@ -0,0 +1,13 @@
|
||||
package sd.model;
|
||||
|
||||
/**
|
||||
* Enumeration representing event types in the simulation.
|
||||
*/
|
||||
public enum EventType {
|
||||
VEHICLE_ARRIVAL, // Vehicle arrives at an intersection
|
||||
TRAFFIC_LIGHT_CHANGE, // Traffic light changes state (green/red)
|
||||
CROSSING_START, // Vehicle starts crossing the intersection
|
||||
CROSSING_END, // Vehicle finishes crossing
|
||||
VEHICLE_GENERATION, // New vehicle is generated in the system
|
||||
STATISTICS_UPDATE // Time to send statistics to dashboard
|
||||
}
|
||||
9
main/src/main/java/sd/model/TrafficLightState.java
Normal file
9
main/src/main/java/sd/model/TrafficLightState.java
Normal file
@@ -0,0 +1,9 @@
|
||||
package sd.model;
|
||||
|
||||
/**
|
||||
* Enumeration representing the state of a traffic light.
|
||||
*/
|
||||
public enum TrafficLightState {
|
||||
GREEN, // Allows passage
|
||||
RED // Blocks passage
|
||||
}
|
||||
10
main/src/main/java/sd/model/VehicleType.java
Normal file
10
main/src/main/java/sd/model/VehicleType.java
Normal file
@@ -0,0 +1,10 @@
|
||||
package sd.model;
|
||||
|
||||
/**
|
||||
* Enumeration representing vehicle types in the simulation.
|
||||
*/
|
||||
public enum VehicleType {
|
||||
BIKE, // Motorcycle
|
||||
LIGHT, // Light vehicle (car)
|
||||
HEAVY // Heavy vehicle (truck, bus)
|
||||
}
|
||||
103
main/src/main/resources/simulation.properties
Normal file
103
main/src/main/resources/simulation.properties
Normal file
@@ -0,0 +1,103 @@
|
||||
# Traffic simulation configuration
|
||||
# This file contains all the necessary configurations to run the simulation
|
||||
|
||||
# === NETWORK CONFIGURATIONS ===
|
||||
# Intersections
|
||||
intersection.Cr1.host=localhost
|
||||
intersection.Cr1.port=8001
|
||||
intersection.Cr2.host=localhost
|
||||
intersection.Cr2.port=8002
|
||||
intersection.Cr3.host=localhost
|
||||
intersection.Cr3.port=8003
|
||||
intersection.Cr4.host=localhost
|
||||
intersection.Cr4.port=8004
|
||||
intersection.Cr5.host=localhost
|
||||
intersection.Cr5.port=8005
|
||||
|
||||
# Exit Node
|
||||
exit.host=localhost
|
||||
exit.port=9001
|
||||
|
||||
# Dashboard
|
||||
dashboard.host=localhost
|
||||
dashboard.port=9000
|
||||
|
||||
# === SIMULATION CONFIGURATIONS ===
|
||||
# Simulation duration in seconds (3600 = 1 hour)
|
||||
simulation.duration=3600.0
|
||||
|
||||
# Vehicle arrival model: FIXED or POISSON
|
||||
simulation.arrival.model=POISSON
|
||||
|
||||
# Arrival rate (λ) for Poisson model (vehicles per second)
|
||||
simulation.arrival.rate=0.5
|
||||
|
||||
# Fixed interval between arrivals (used if model = FIXED)
|
||||
simulation.arrival.fixed.interval=2.0
|
||||
|
||||
# === TRAFFIC LIGHT CONFIGURATIONS ===
|
||||
# Times in seconds for each traffic light (green and red)
|
||||
# Format: trafficlight.<intersection>.<direction>.<state>
|
||||
|
||||
# Intersection 1
|
||||
trafficlight.Cr1.North.green=30.0
|
||||
trafficlight.Cr1.North.red=30.0
|
||||
trafficlight.Cr1.South.green=30.0
|
||||
trafficlight.Cr1.South.red=30.0
|
||||
trafficlight.Cr1.East.green=30.0
|
||||
trafficlight.Cr1.East.red=30.0
|
||||
trafficlight.Cr1.West.green=30.0
|
||||
trafficlight.Cr1.West.red=30.0
|
||||
|
||||
# Intersection 2
|
||||
trafficlight.Cr2.North.green=25.0
|
||||
trafficlight.Cr2.North.red=35.0
|
||||
trafficlight.Cr2.South.green=25.0
|
||||
trafficlight.Cr2.South.red=35.0
|
||||
trafficlight.Cr2.East.green=35.0
|
||||
trafficlight.Cr2.East.red=25.0
|
||||
trafficlight.Cr2.West.green=35.0
|
||||
trafficlight.Cr2.West.red=25.0
|
||||
|
||||
# Intersection 3
|
||||
trafficlight.Cr3.North.green=30.0
|
||||
trafficlight.Cr3.North.red=30.0
|
||||
trafficlight.Cr3.South.green=30.0
|
||||
trafficlight.Cr3.South.red=30.0
|
||||
trafficlight.Cr3.East.green=30.0
|
||||
trafficlight.Cr3.East.red=30.0
|
||||
trafficlight.Cr3.West.green=30.0
|
||||
trafficlight.Cr3.West.red=30.0
|
||||
|
||||
# Intersection 4
|
||||
trafficlight.Cr4.North.green=30.0
|
||||
trafficlight.Cr4.North.red=30.0
|
||||
trafficlight.Cr4.South.green=30.0
|
||||
trafficlight.Cr4.South.red=30.0
|
||||
trafficlight.Cr4.East.green=30.0
|
||||
trafficlight.Cr4.East.red=30.0
|
||||
trafficlight.Cr4.West.green=30.0
|
||||
trafficlight.Cr4.West.red=30.0
|
||||
|
||||
# Intersection 5
|
||||
trafficlight.Cr5.North.green=30.0
|
||||
trafficlight.Cr5.North.red=30.0
|
||||
trafficlight.Cr5.South.green=30.0
|
||||
trafficlight.Cr5.South.red=30.0
|
||||
trafficlight.Cr5.East.green=30.0
|
||||
trafficlight.Cr5.East.red=30.0
|
||||
trafficlight.Cr5.West.green=30.0
|
||||
trafficlight.Cr5.West.red=30.0
|
||||
|
||||
# === VEHICLE CONFIGURATIONS ===
|
||||
# Probability of generating a light vehicle (0.0 to 1.0)
|
||||
# The rest will be heavy vehicles
|
||||
vehicle.probability.light=0.7
|
||||
|
||||
# Crossing time in seconds
|
||||
vehicle.crossing.time.light=2.0
|
||||
vehicle.crossing.time.heavy=4.0
|
||||
|
||||
# === STATISTICS CONFIGURATIONS ===
|
||||
# Interval to send updates to the dashboard (in seconds)
|
||||
statistics.update.interval=10.0
|
||||
Reference in New Issue
Block a user