Package and objects creation for the Graph. Temporary Main class to start the graph creation. Issue #15

This commit is contained in:
RODRIGUEZ lucas 2024-03-25 16:36:21 +01:00
parent b5d686f3da
commit 4d2ec04534
8 changed files with 274 additions and 10 deletions

View file

@ -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;
}
}

View file

@ -0,0 +1,21 @@
package fr.u_paris.gla.project.itinerary;
import java.util.List;
public class Finder <T extends GraphNode> {
private final Graph<T> graph;
public Finder(Graph<T> graph) {
this.graph = graph;
}
public List<T> findPath(T from, T to) {
// TODO
return null;
}
public List<T> findPath(double longitude, double latitude) {
// TODO
return null;
}
}

View file

@ -0,0 +1,15 @@
package fr.u_paris.gla.project.itinerary;
import java.util.Map;
import java.util.Set;
public class Graph <T extends GraphNode> {
private final Set<T> nodes;
private final Map<T, Set<Connection>> connections;
public Graph(Set<T> nodes, Map<T, Set<Connection>> connections) {
this.nodes = nodes;
this.connections = connections;
}
}

View file

@ -0,0 +1,7 @@
package fr.u_paris.gla.project.itinerary;
import java.util.List;
public interface GraphNode{
int getId();
}

View file

@ -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<Stop> getStops(HashSet<Stop> 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<Stop> nodes, String name, String gps, String lineId) {
List<Stop> 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<Stop> nodes, HashMap<Integer, Set<Connection>> 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<Connection> 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<Stop> nodes = new HashSet<>();
HashMap<Integer, Set<Connection>> connections = new HashMap<>();
CSVTools.readCSVFromFile(TRACE_FILE_NAME,
(String[] line) -> addLine(line, nodes, connections));
Graph<Stop> graph = new Graph<>(nodes, connections);
} catch (IOException e) {
LOGGER.log(Level.SEVERE, "Error while reading the line paths", e);
}
*/
}
}

View file

@ -0,0 +1,18 @@
package fr.u_paris.gla.project.itinerary;
public class Path <T extends GraphNode> {
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;
}
}

View file

@ -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<String> 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);
}
}

View file

@ -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,11 +27,10 @@ public final class CSVTools {
// Tool class
}
public static void readCSVFromURL(String url, Consumer<String[]> contentLineConsumer)
private static void readCSVFromInputStream(InputStream is, Consumer<String[]> contentLineConsumer)
throws IOException {
ICSVParser parser = new CSVParserBuilder().withSeparator(';').build();
try (InputStream is = new URL(url).openStream();
Reader reader = new BufferedReader(
try (Reader reader = new BufferedReader(
new InputStreamReader(is, StandardCharsets.UTF_8))) {
CSVReaderBuilder csvBuilder = new CSVReaderBuilder(reader)
.withCSVParser(parser);
@ -52,6 +46,17 @@ public final class CSVTools {
}
}
public static void readCSVFromFile(String filename, Consumer<String[]> contentLineConsumer)
throws IOException {
File file = new File(filename);
readCSVFromInputStream(new FileInputStream(file), contentLineConsumer);
}
public static void readCSVFromURL(String url, Consumer<String[]> contentLineConsumer)
throws IOException {
readCSVFromInputStream(new URL(url).openStream(), contentLineConsumer);
}
public static void writeCSVToFile(String filename,
Stream<String[]> contentLineConsumer) throws IOException {
try (FileWriter writer = new FileWriter(filename, StandardCharsets.UTF_8)) {