This commit is contained in:
Mikael Capelle
2018-02-16 15:29:11 +01:00
parent 65c81b9921
commit cfb59ac0f1
37 changed files with 1511 additions and 473 deletions

View File

@@ -45,7 +45,7 @@ public class Arc {
/**
* @return Destination node of this arc.
*/
public Node getDest() {
public Node getDestination() {
return dest;
}

View File

@@ -10,6 +10,10 @@ public class Graph {
// Nodes of the graph.
private ArrayList<Node> nodes;
/**
* @param mapId
* @param nodes
*/
public Graph(int mapId, ArrayList<Node> nodes) {
this.mapId = mapId;
this.nodes = nodes;
@@ -24,5 +28,13 @@ public class Graph {
* @return Map ID of this graph.
*/
public int getMapId() { return mapId; }
/**
* @return Return the transpose graph of this graph.
*/
public Graph transpose() {
// TODO:
return null;
}
}

View File

@@ -2,7 +2,7 @@ package org.insa.graph;
import java.util.ArrayList;
public class Node {
public class Node implements Comparable<Node> {
// ID of the node.
private int id;
@@ -49,4 +49,17 @@ public class Node {
*/
public Point getPoint() { return point; }
@Override
public boolean equals(Object other) {
if (other instanceof Node) {
return getId() == ((Node) other).getId();
}
return false;
}
@Override
public int compareTo(Node other) {
return Integer.compare(getId(), other.getId());
}
}

View File

@@ -3,6 +3,9 @@ package org.insa.graph.io;
import java.io.DataInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import javax.sound.midi.ControllerEventListener;
import org.insa.graph.Arc;
import org.insa.graph.Graph;
@@ -149,7 +152,9 @@ public class BinaryGraphReader extends BinaryReader implements AbstractGraphRead
// And reverse arc if its a two-way road.
if (!info.isOneWay()) {
// Add without segments.
dest.addSuccessor(new Arc(orig, length, info));
ArrayList<Point> rPoints = new ArrayList<Point>(points);
Collections.reverse(rPoints);
dest.addSuccessor(new Arc(orig, length, info, rPoints));
}
}

View File

@@ -4,6 +4,7 @@ import java.io.DataInputStream;
import java.io.IOException;
import java.util.ArrayList;
import org.insa.graph.Arc;
import org.insa.graph.Graph;
import org.insa.graph.Node;
import org.insa.graph.Path;
@@ -35,23 +36,36 @@ public class BinaryPathReader extends BinaryReader implements AbstractPathReader
// Number of nodes in the path (without first and last).
int nbNodes = dis.readInt();
ArrayList<Node> nodes = new ArrayList<Node>(nbNodes + 2);
ArrayList<Arc> arcs = new ArrayList<Arc>();
// Read first node
nodes.add(readNode(graph));
// Read last node
Node lastNode = readNode(graph);
// Skip (duplicate) first and last node
readNode(graph);
readNode(graph);
// Read intermediate nodes:
for (int node = 0; node < nbNodes; ++node) {
ArrayList<Node> nodes = new ArrayList<Node>();
for (int i = 0; i < nbNodes; ++i) {
nodes.add(readNode(graph));
}
// Add last node
nodes.add(lastNode);
Node current = nodes.get(0);
for (int i = 1; i < nodes.size(); ++i) {
Node node = nodes.get(i);
Arc minArc = null;
for (Arc arc: current.getSuccessors()) {
if (arc.getDestination().equals(node)
&& (minArc == null || arc.getMinimumTravelTime() < minArc.getMinimumTravelTime())) {
minArc = arc;
}
}
arcs.add(minArc);
if (minArc == null) {
System.out.println("No arc found between nodes " + current.getId() + " and " + node.getId() + "\n");
}
current = node;
}
return new Path(graph, nodes);
return new Path(graph, nodes.get(0), arcs);
}
/**

View File

@@ -0,0 +1,83 @@
package org.insa.graph.io ;
import java.io.* ;
import java.util.zip.* ;
/**
* Class that can be used to open (compressed) files from a specified
* set of folders or for a full path.
*
*/
public class Openfile {
/**
* These folders will be looked up for the files.
*
*/
private static final String[] datadirs = {
// INSA folder containing maps.
"/home/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Maps",
// INSA folder containing paths.
"/home/commetud/3eme Annee MIC/Graphes-et-Algorithmes/",
// Maps sub-folder.
"Maps",
// Current folder.
"."
};
/**
* Available extensions.
*
*/
private static final String[] extensions = { ".map", ".gz", ".map.gz", ".path", ".path.gz", "" };
/**
* Open the given file and return a corresponding DataInputStream.
*
* @param filename Name of the file to open (without extension) or full path to the given file.
* @throws IOException
*/
public static DataInputStream open(String filename) throws IOException {
File file = null;
String fullpath = null;
// If the filename containing only a name (not a path):
if (filename.equals (new File(filename).getName())) {
for (String ext: extensions) {
String fname = filename + ext;
for (int index = 0; file == null && index < datadirs.length; ++index) {
fullpath = datadirs[index] + File.separator + fname;
file = new File(fullpath);
if (!file.exists()) {
file = null;
}
}
}
}
else {
fullpath = filename;
file = new File(filename);
}
InputStream fileInput = new FileInputStream(new File(fullpath));
// If the file is compressed.
if (fullpath.endsWith(".gz")) {
fileInput = new GZIPInputStream(fileInput) ;
}
else {
fileInput = new BufferedInputStream(fileInput) ;
}
return new DataInputStream(fileInput) ;
}
}