Optimize the Stop search. Issue #15
This commit is contained in:
parent
4d2ec04534
commit
7fdb281ede
4 changed files with 19 additions and 53 deletions
|
@ -1,5 +1,6 @@
|
|||
package fr.u_paris.gla.project.itinerary;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
|
|
|
@ -48,88 +48,50 @@ public class Main{
|
|||
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()) {
|
||||
private static Stop getOrCreateStop(HashSet<Stop> nodes, HashMap<String, Stop> tmp, String name, String gps, String lineId) {
|
||||
Stop stop = tmp.get(gps);
|
||||
if (stop == null) {
|
||||
double[] coords = getCoords(gps);
|
||||
Stop newStop = new Stop(lineId, name, coords[0], coords[1]);
|
||||
nodes.add(newStop);
|
||||
tmp.put(gps, 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;
|
||||
stop.addLine(lineId);
|
||||
return stop;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
private static void addLine(String[] line, HashSet<Stop> nodes, HashMap<String, Stop> tmp, HashMap<Stop, Set<Connection>> connections) {
|
||||
Stop fromStop = getOrCreateStop(nodes, tmp, line[IDFM_TRACE_FROM_INDEX], line[IDFM_TRACE_FROM_GPS_INDEX], line[IDFM_TRACE_ID_INDEX]);
|
||||
Stop toStop = getOrCreateStop(nodes, tmp, line[IDFM_TRACE_TO_INDEX], line[IDFM_TRACE_TO_GPS_INDEX], line[IDFM_TRACE_ID_INDEX]);
|
||||
|
||||
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);
|
||||
Connection connection = new Connection(toStop, 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);
|
||||
connections.computeIfAbsent(fromStop, k -> new HashSet<>()).add(connection);
|
||||
}
|
||||
|
||||
}
|
||||
*/
|
||||
|
||||
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<>();
|
||||
HashMap<Stop, Set<Connection>> connections = new HashMap<>();
|
||||
HashMap<String, Stop> tmp = new HashMap<>();
|
||||
CSVTools.readCSVFromFile(TRACE_FILE_NAME,
|
||||
(String[] line) -> addLine(line, nodes, connections));
|
||||
(String[] line) -> addLine(line, nodes, tmp, connections));
|
||||
Graph<Stop> graph = new Graph<>(nodes, connections);
|
||||
} catch (IOException e) {
|
||||
LOGGER.log(Level.SEVERE, "Error while reading the line paths", e);
|
||||
}
|
||||
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,8 +9,11 @@ public class Stop implements GraphNode {
|
|||
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) {
|
||||
|
|
Reference in a new issue