Save/Load

* implemented saving/loading of graphical graph
* fixed save/load graph
* fixed and improved tests
This commit is contained in:
2019-06-14 21:36:31 +02:00
parent e9f7375ca4
commit 1a06692f02
11 changed files with 231 additions and 77 deletions

View File

@@ -13,6 +13,7 @@ import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
@@ -24,6 +25,8 @@ import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.google.gson.JsonSyntaxException;
import berack96.sim.util.graph.Edge;
import berack96.sim.util.graph.Graph;
import berack96.sim.util.graph.MapGraph;
@@ -1332,6 +1335,8 @@ public class TestGraph {
String fileName = "test/resources/test.json";
Set<String> vertices = new HashSet<>();
Set<Edge<String, Integer>> edges = new HashSet<>();
Map<String, Set<Object>> marks = new HashMap<>();
Set<Object> temp = new HashSet<>();
vertices.add("1");
vertices.add("2");
@@ -1351,30 +1356,63 @@ public class TestGraph {
edges.add(new Edge<>("6", "2", 2));
edges.add(new Edge<>("8", "7", 9));
temp.add(1);
marks.put("2", new HashSet<>(temp));
temp.add("blue");
marks.put("1", new HashSet<>(temp));
marks.put("7", new HashSet<>(temp));
temp.remove(1);
temp.add(4.0);
marks.put("4", new HashSet<>(temp));
marks.put("8", new HashSet<>(temp));
temp.remove(4.0);
temp.add("red");
marks.put("5", new HashSet<>(temp));
temp.remove("blue");
marks.put("6", new HashSet<>(temp));
temp.clear();
graph.addAllVertices(vertices);
graph.addAllEdges(edges);
/* because GSON convert to double */
/*
Set<Edge<String, Double>> edgesD = new HashSet<>();
edges.forEach((e) -> edgesD.add(new Edge<String, Double>(e.getSource(), e.getDestination(), e.getWeight().doubleValue())));
*/
Set<Edge<String, Integer>> edgesD = edges;
marks.forEach((v, m) -> m.forEach(mk -> graph.mark(v, mk)));
try {
Graph.save(graph, fileName);
Graph.load(graph, fileName, String.class, Integer.class);
shouldContain(graph.vertices(), vertices.toArray());
shouldContain(graph.edges(), edgesD.toArray());
shouldContain(graph.edges(), edges.toArray());
//marks.forEach((v, m) -> shouldContain(graph.getMarks(v), m.toArray()));
graph.removeAllVertex();
Graph.load(graph, fileName, String.class, Integer.class);
shouldContain(graph.vertices(), vertices.toArray());
shouldContain(graph.edges(), edgesD.toArray());
shouldContain(graph.edges(), edges.toArray());
//marks.forEach((v, m) -> shouldContain(graph.getMarks(v), m.toArray()));
} catch (Exception e) {
fail(e.getMessage());
}
try {
Graph.load(graph, "sadadafacensi", String.class, Integer.class);
fail("Should have been thrown IOException");
} catch (Exception ignore) {
if (!(ignore instanceof IOException))
fail("Should have been thrown IOException " + ignore.getMessage());
}
shouldContain(graph.vertices(), vertices.toArray());
shouldContain(graph.edges(), edges.toArray());
//marks.forEach((v, m) -> shouldContain(graph.getMarks(v), m.toArray()));
try {
Graph.load(graph, fileName + ".fail", String.class, Integer.class);
fail("Should have been thrown JsonSyntaxException");
} catch (Exception ignore) {
if (!(ignore instanceof JsonSyntaxException))
fail("Should have been thrown JsonSyntaxException " + ignore.getMessage());
}
graph = null;
shouldThrow(new NullPointerException(), () -> { try {
Graph.load(graph, fileName, String.class, Integer.class);

View File

@@ -1 +1 @@
{"vertices":["1","2","3","4","5","6","7","8"],"edges":[{"source":"1","destination":"2","weight":1},{"source":"1","destination":"3","weight":1},{"source":"5","destination":"4","weight":5},{"source":"6","destination":"2","weight":2},{"source":"5","destination":"3","weight":2},{"source":"8","destination":"7","weight":9},{"source":"4","destination":"6","weight":6},{"source":"2","destination":"5","weight":4}],"other":""}
{"vertices":["\"1\"","\"2\"","\"3\"","\"4\"","\"5\"","\"6\"","\"7\"","\"8\""],"edges":[{"src":"\"1\"","dest":"\"2\"","weight":"1"},{"src":"\"1\"","dest":"\"3\"","weight":"1"},{"src":"\"5\"","dest":"\"4\"","weight":"5"},{"src":"\"6\"","dest":"\"2\"","weight":"2"},{"src":"\"5\"","dest":"\"3\"","weight":"2"},{"src":"\"8\"","dest":"\"7\"","weight":"9"},{"src":"\"4\"","dest":"\"6\"","weight":"6"},{"src":"\"2\"","dest":"\"5\"","weight":"4"}],"other":""}

View File

@@ -0,0 +1 @@
{"vertices":["\"1\"","\"2\"","\"3\"","\"4\"","\"5\"","gfadad\"6\"","\"7\"","\"8\""],"edges":{"src":["\"1\"","\"1\"","\"5\"","\"6\"","\"5\"","\"8\"","\"4\"","\"2\""],"dest":["\"2\"","\"3\"","\"4\"","\"2\"","\"3\"","\"7\"","\"6ads\"","\"5\""],ad"wedight":["1","1","5","2","2","9","6","4"]},"other":""dsada}