|
| 1 | +package kruskal; |
| 2 | + |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.FileNotFoundException; |
| 5 | +import java.io.FileReader; |
| 6 | +import java.io.IOException; |
| 7 | + |
| 8 | +public class CVSCargador { |
| 9 | + |
| 10 | + private BufferedReader lector = null; |
| 11 | + |
| 12 | + public CVSCargador(String ruta) { |
| 13 | + try { |
| 14 | + this.lector = new BufferedReader(new FileReader(ruta)); |
| 15 | + } catch (FileNotFoundException ex) { |
| 16 | + System.out.println("File not found"); |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + public Grafo carga() { |
| 21 | + |
| 22 | + String line = readLine(); |
| 23 | + String[] splitLine; |
| 24 | + Arista arista; |
| 25 | + Vertice a, b; |
| 26 | + AristContainer contAristas = new AristContainer(); |
| 27 | + VerticesSet vertices = new VerticesSet(); |
| 28 | + |
| 29 | + while (line != null) { |
| 30 | + //si comienza por # es un comentario y nos lo saltamos |
| 31 | + if ((line.length() > 0) && (line.charAt(0) != '#')) { |
| 32 | + splitLine = line.split(","); |
| 33 | + a = translateLineToVertice(splitLine, 0); |
| 34 | + b = translateLineToVertice(splitLine, 1); |
| 35 | + |
| 36 | + if (a != null && b != null) { |
| 37 | + if (vertices.contains(a)) { |
| 38 | + a = vertices.getVertice(a.getId()); |
| 39 | + } |
| 40 | + if (vertices.contains(b)) { |
| 41 | + b = vertices.getVertice(b.getId()); |
| 42 | + } |
| 43 | + a.link(b); |
| 44 | + b.link(a); |
| 45 | + } |
| 46 | + vertices.addVertice(a); |
| 47 | + vertices.addVertice(b); |
| 48 | + |
| 49 | + arista = genArista(splitLine); |
| 50 | + if (arista != null) { |
| 51 | + contAristas.addArista(arista); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + line = readLine(); |
| 56 | + } |
| 57 | + |
| 58 | + Grafo resultado = new Grafo(vertices.size()); |
| 59 | + resultado.addAristsContainer(contAristas); |
| 60 | + resultado.addVerticesSet(vertices); |
| 61 | + |
| 62 | + return resultado; |
| 63 | + } |
| 64 | + |
| 65 | + private String readLine() { |
| 66 | + String line; |
| 67 | + try { |
| 68 | + line = lector.readLine(); |
| 69 | + } catch (IOException ex) { |
| 70 | + line = null; |
| 71 | + } |
| 72 | + |
| 73 | + return line; |
| 74 | + } |
| 75 | + |
| 76 | + private Integer[] traduceLinea(String[] lineaPartida) { |
| 77 | + |
| 78 | + //si es Inf entonces no existe arista pero existen los vértices |
| 79 | + for (int i = 0; i < lineaPartida.length; i++) { |
| 80 | + if (lineaPartida[i].equalsIgnoreCase("Inf")) { |
| 81 | + lineaPartida[i] = "-1"; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + Integer[] resultado = { |
| 86 | + Integer.parseInt(lineaPartida[0]), |
| 87 | + Integer.parseInt(lineaPartida[1]), |
| 88 | + Integer.parseInt(lineaPartida[2]) |
| 89 | + }; |
| 90 | + |
| 91 | + if (resultado[0] < 0) { |
| 92 | + resultado[0] = null; |
| 93 | + } else if (resultado[1] < 0) { |
| 94 | + resultado[1] = null; |
| 95 | + } else if (resultado[2] < 0) { |
| 96 | + resultado[2] = null; |
| 97 | + } |
| 98 | + |
| 99 | + return resultado; |
| 100 | + } |
| 101 | + |
| 102 | + private Vertice translateLineToVertice(String[] lineaPartida, int i) { |
| 103 | + |
| 104 | + if (Character.isDigit(lineaPartida[i].charAt(0))) { |
| 105 | + return new Vertice(Integer.parseInt(lineaPartida[i])); |
| 106 | + } |
| 107 | + return null; |
| 108 | + |
| 109 | + } |
| 110 | + |
| 111 | + private Arista genArista(String[] lineaPartida) { |
| 112 | + Integer[] lineaTraducida = traduceLinea(lineaPartida); |
| 113 | + if (lineaTraducida[0] != null |
| 114 | + && lineaTraducida[1] != null |
| 115 | + && lineaTraducida[2] != null) { |
| 116 | + return new Arista( |
| 117 | + lineaTraducida[0], |
| 118 | + lineaTraducida[1], |
| 119 | + lineaTraducida[2] |
| 120 | + ); |
| 121 | + } |
| 122 | + return null; |
| 123 | + } |
| 124 | +} |
0 commit comments