From 4d2ec04534e936919aa3e0fe810769eb3fbc62be Mon Sep 17 00:00:00 2001 From: RODRIGUEZ lucas Date: Mon, 25 Mar 2024 16:36:21 +0100 Subject: [PATCH] Package and objects creation for the Graph. Temporary Main class to start the graph creation. Issue #15 --- .../gla/project/itinerary/Connection.java | 18 +++ .../u_paris/gla/project/itinerary/Finder.java | 21 +++ .../u_paris/gla/project/itinerary/Graph.java | 15 ++ .../gla/project/itinerary/GraphNode.java | 7 + .../u_paris/gla/project/itinerary/Main.java | 135 ++++++++++++++++++ .../u_paris/gla/project/itinerary/Path.java | 18 +++ .../u_paris/gla/project/itinerary/Stop.java | 45 ++++++ .../u_paris/gla/project/utils/CSVTools.java | 25 ++-- 8 files changed, 274 insertions(+), 10 deletions(-) create mode 100644 src/main/java/fr/u_paris/gla/project/itinerary/Connection.java create mode 100644 src/main/java/fr/u_paris/gla/project/itinerary/Finder.java create mode 100644 src/main/java/fr/u_paris/gla/project/itinerary/Graph.java create mode 100644 src/main/java/fr/u_paris/gla/project/itinerary/GraphNode.java create mode 100644 src/main/java/fr/u_paris/gla/project/itinerary/Main.java create mode 100644 src/main/java/fr/u_paris/gla/project/itinerary/Path.java create mode 100644 src/main/java/fr/u_paris/gla/project/itinerary/Stop.java diff --git a/src/main/java/fr/u_paris/gla/project/itinerary/Connection.java b/src/main/java/fr/u_paris/gla/project/itinerary/Connection.java new file mode 100644 index 0000000..4c7b855 --- /dev/null +++ b/src/main/java/fr/u_paris/gla/project/itinerary/Connection.java @@ -0,0 +1,18 @@ +package fr.u_paris.gla.project.itinerary; + +public class Connection{ + private final Stop stop; + + private final String lineName; + + private final double distance; + + private final int time; + + public Connection(Stop stop, String lineName, double distance, int time){ + this.stop = stop; + this.lineName=lineName; + this.distance = distance; + this.time = time; + } +} diff --git a/src/main/java/fr/u_paris/gla/project/itinerary/Finder.java b/src/main/java/fr/u_paris/gla/project/itinerary/Finder.java new file mode 100644 index 0000000..23c3378 --- /dev/null +++ b/src/main/java/fr/u_paris/gla/project/itinerary/Finder.java @@ -0,0 +1,21 @@ +package fr.u_paris.gla.project.itinerary; + +import java.util.List; + +public class Finder { + private final Graph graph; + + public Finder(Graph graph) { + this.graph = graph; + } + + public List findPath(T from, T to) { + // TODO + return null; + } + + public List findPath(double longitude, double latitude) { + // TODO + return null; + } +} diff --git a/src/main/java/fr/u_paris/gla/project/itinerary/Graph.java b/src/main/java/fr/u_paris/gla/project/itinerary/Graph.java new file mode 100644 index 0000000..9f263e8 --- /dev/null +++ b/src/main/java/fr/u_paris/gla/project/itinerary/Graph.java @@ -0,0 +1,15 @@ +package fr.u_paris.gla.project.itinerary; + +import java.util.Map; +import java.util.Set; + +public class Graph { + private final Set nodes; + + private final Map> connections; + + public Graph(Set nodes, Map> connections) { + this.nodes = nodes; + this.connections = connections; + } +} diff --git a/src/main/java/fr/u_paris/gla/project/itinerary/GraphNode.java b/src/main/java/fr/u_paris/gla/project/itinerary/GraphNode.java new file mode 100644 index 0000000..2b9e2a6 --- /dev/null +++ b/src/main/java/fr/u_paris/gla/project/itinerary/GraphNode.java @@ -0,0 +1,7 @@ +package fr.u_paris.gla.project.itinerary; + +import java.util.List; + +public interface GraphNode{ + int getId(); +} diff --git a/src/main/java/fr/u_paris/gla/project/itinerary/Main.java b/src/main/java/fr/u_paris/gla/project/itinerary/Main.java new file mode 100644 index 0000000..f141637 --- /dev/null +++ b/src/main/java/fr/u_paris/gla/project/itinerary/Main.java @@ -0,0 +1,135 @@ +package fr.u_paris.gla.project.itinerary; + +import fr.u_paris.gla.project.idfm.*; +import fr.u_paris.gla.project.utils.CSVTools; + +import java.io.IOException; +import java.util.*; +import java.util.logging.Level; +import java.util.logging.Logger; + +public class Main{ + private static final Logger LOGGER = Logger + .getLogger(IDFMNetworkExtractor.class.getName()); + + // IDF mobilite generated file + private static final String TRACE_FILE_NAME = "./trace.csv"; + + private static final String HOURS_FILE_NAME = "./hours.csv"; + + // IDF mobilite file format + + private static final int IDFM_TRACE_ID_INDEX = 0; + + private static final int IDFM_TRACE_DERIV_INDEX = 1; + + private static final int IDFM_TRACE_FROM_INDEX = 2; + + private static final int IDFM_TRACE_FROM_GPS_INDEX = 3; + + private static final int IDFM_TRACE_TO_INDEX= 4; + + private static final int IDFM_TRACE_TO_GPS_INDEX = 5; + + private static final int IDFM_TRACE_TIME_INDEX = 6; + + private static final int IDFM_TRACE_DISTANCE_INDEX = 7; + + // Parser helper values + + /** + * Returns the coordinates from a String to a double array: + * "49.08, 3.07" -> {49.08, 3.07} + * @param gps the string representation + * @return the double array + */ + private static double[] getCoords(String gps) { + String []stringCoords = gps.split(", "); + return new double[] {Double.parseDouble(stringCoords[0]), Double.parseDouble(stringCoords[1])}; + } + + private static List getStops(HashSet nodes, String name, String gps) { + double []coords = getCoords(gps); + return nodes.stream().filter( + stop -> stop.getName().equals(name) + && stop.getLatitude() == coords[0] + && stop.getLongitude() == coords[1] + ).toList(); + } + + /* + private static Stop getOrCreateStop(HashSet nodes, String name, String gps, String lineId) { + List stops = getStops(nodes, name, gps); + if (stops.isEmpty()) { + double[] coords = getCoords(gps); + Stop newStop = new Stop(lineId, name, coords[0], coords[1]); + nodes.add(newStop); + return newStop; + } + else if (stops.size() == 1) { + stops.get(0).addLine(lineId); + return stops.get(0); + } + else { + LOGGER.severe("Error in graph creation"); + return null; + } + } + + public static int lineNB = 0; + + private static void addLine(String[] line, HashSet nodes, HashMap> connections) { + // TODO Cas particulier ou une ligne a le même nom + + Stop fromStop = getOrCreateStop(nodes, line[IDFM_TRACE_FROM_INDEX], line[IDFM_TRACE_FROM_GPS_INDEX], line[IDFM_TRACE_ID_INDEX]); + Stop toStop = getOrCreateStop(nodes, line[IDFM_TRACE_TO_INDEX], line[IDFM_TRACE_TO_GPS_INDEX], line[IDFM_TRACE_ID_INDEX]); + + if (fromStop == null || toStop == null) + return; + + String[] timeString = line[IDFM_TRACE_TIME_INDEX].split(":"); + int time = Integer.parseInt(timeString[0]) * 60 + Integer.parseInt(timeString[1]); + + Connection connection = new Connection(toStop.getId(), line[IDFM_TRACE_ID_INDEX], Double.parseDouble(line[IDFM_TRACE_DISTANCE_INDEX]), time); + + connections.computeIfAbsent(fromStop.getId(), k -> new HashSet<>()).add(connection); + +/* + Set linesConnections = connections.get(fromStop.getId()); + + String[] timeString = line[IDFM_TRACE_TIME_INDEX].split(":"); + int time = Integer.parseInt(timeString[0]) * 60 + Integer.parseInt(timeString[1]); + Connection connection = new Connection(toStop.getId(), line[IDFM_TRACE_ID_INDEX], Double.parseDouble(line[IDFM_TRACE_DISTANCE_INDEX]), time); + if (linesConnections == null) + connections.put(fromStop.getId(), new HashSet<>(List.of(connection))); + else + linesConnections.add(connection); + + if (lineNB++ % 1000 == 0) { + LOGGER.log(Level.INFO,"Added line " + line[IDFM_TRACE_ID_INDEX] + " from " + line[IDFM_TRACE_FROM_INDEX] + " to " + line[IDFM_TRACE_TO_INDEX] + ", line " + lineNB); + } + + } + */ + + public static void main(String[] args){ + // TODO ajouter option du nom en argument + if (args.length != 0) { + LOGGER.severe("Invalid command line. Target file names are in the main file for now."); + return; + } + + /* + try { + HashSet nodes = new HashSet<>(); + HashMap> connections = new HashMap<>(); + CSVTools.readCSVFromFile(TRACE_FILE_NAME, + (String[] line) -> addLine(line, nodes, connections)); + Graph graph = new Graph<>(nodes, connections); + } catch (IOException e) { + LOGGER.log(Level.SEVERE, "Error while reading the line paths", e); + } + + */ + } +} diff --git a/src/main/java/fr/u_paris/gla/project/itinerary/Path.java b/src/main/java/fr/u_paris/gla/project/itinerary/Path.java new file mode 100644 index 0000000..a492b99 --- /dev/null +++ b/src/main/java/fr/u_paris/gla/project/itinerary/Path.java @@ -0,0 +1,18 @@ +package fr.u_paris.gla.project.itinerary; + +public class Path { + private T current; + + private T previous; + + private double currentScore; + + private final double targetScore; + + public Path(T current, T previous, double currentScore, double targetScore) { + this.current = current; + this.previous = previous; + this.currentScore = currentScore; + this.targetScore = targetScore; + } +} diff --git a/src/main/java/fr/u_paris/gla/project/itinerary/Stop.java b/src/main/java/fr/u_paris/gla/project/itinerary/Stop.java new file mode 100644 index 0000000..2cbd2db --- /dev/null +++ b/src/main/java/fr/u_paris/gla/project/itinerary/Stop.java @@ -0,0 +1,45 @@ +package fr.u_paris.gla.project.itinerary; + +import java.util.HashSet; +import java.util.Set; + +public class Stop implements GraphNode { + private static int counter = 0; + + private final int id; + + private final Set lines; + private final String name; + private final double latitude; + private final double longitude; + + public Stop(String line, String name, double latitude, double longitude) { + lines = new HashSet<>(); + lines.add(line); + this.id = counter++; + this.name = name; + this.latitude = latitude; + this.longitude = longitude; + } + + @Override + public int getId(){ + return id; + } + + public String getName(){ + return name; + } + + public double getLatitude(){ + return latitude; + } + + public double getLongitude(){ + return longitude; + } + + public void addLine(String s){ + lines.add(s); + } +} diff --git a/src/main/java/fr/u_paris/gla/project/utils/CSVTools.java b/src/main/java/fr/u_paris/gla/project/utils/CSVTools.java index b3194f9..7d50613 100644 --- a/src/main/java/fr/u_paris/gla/project/utils/CSVTools.java +++ b/src/main/java/fr/u_paris/gla/project/utils/CSVTools.java @@ -3,12 +3,7 @@ */ package fr.u_paris.gla.project.utils; -import java.io.BufferedReader; -import java.io.FileWriter; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.Reader; +import java.io.*; import java.net.URL; import java.nio.charset.StandardCharsets; import java.util.function.Consumer; @@ -32,12 +27,11 @@ public final class CSVTools { // Tool class } - public static void readCSVFromURL(String url, Consumer contentLineConsumer) + private static void readCSVFromInputStream(InputStream is, Consumer contentLineConsumer) throws IOException { ICSVParser parser = new CSVParserBuilder().withSeparator(';').build(); - try (InputStream is = new URL(url).openStream(); - Reader reader = new BufferedReader( - new InputStreamReader(is, StandardCharsets.UTF_8))) { + try (Reader reader = new BufferedReader( + new InputStreamReader(is, StandardCharsets.UTF_8))) { CSVReaderBuilder csvBuilder = new CSVReaderBuilder(reader) .withCSVParser(parser); try (CSVReader csv = csvBuilder.build()) { @@ -52,6 +46,17 @@ public final class CSVTools { } } + public static void readCSVFromFile(String filename, Consumer contentLineConsumer) + throws IOException { + File file = new File(filename); + readCSVFromInputStream(new FileInputStream(file), contentLineConsumer); + } + + public static void readCSVFromURL(String url, Consumer contentLineConsumer) + throws IOException { + readCSVFromInputStream(new URL(url).openStream(), contentLineConsumer); + } + public static void writeCSVToFile(String filename, Stream contentLineConsumer) throws IOException { try (FileWriter writer = new FileWriter(filename, StandardCharsets.UTF_8)) {