|
@@ -5,6 +5,7 @@ import java.awt.Point;
|
|
|
import java.io.FileWriter;
|
|
|
import java.io.IOException;
|
|
|
import java.util.ArrayList;
|
|
|
+import java.util.LinkedList;
|
|
|
import java.util.ListIterator;
|
|
|
|
|
|
import org.json.simple.JSONArray;
|
|
@@ -14,13 +15,14 @@ import com.google.gson.Gson;
|
|
|
import com.google.gson.GsonBuilder;
|
|
|
import com.google.gson.JsonElement;
|
|
|
import com.google.gson.JsonObject;
|
|
|
+import com.google.gson.JsonParser;
|
|
|
import com.google.gson.JsonPrimitive;
|
|
|
import com.google.gson.stream.JsonWriter;
|
|
|
|
|
|
import TypeAdapter.AbstractCpsObjectAdapter;
|
|
|
import TypeAdapter.ColorAdapter;
|
|
|
-
|
|
|
import TypeAdapter.PositionAdapter;
|
|
|
+
|
|
|
import classes.Category;
|
|
|
import classes.CpsEdge;
|
|
|
import classes.CpsUpperNode;
|
|
@@ -31,6 +33,7 @@ import classes.HolonSwitch;
|
|
|
import classes.HolonTransformer;
|
|
|
import classes.IdCounter;
|
|
|
import classes.Position;
|
|
|
+import classes.TrackedDataSet;
|
|
|
import sun.misc.Queue;
|
|
|
import ui.model.Model;
|
|
|
|
|
@@ -54,11 +57,16 @@ public class StoreController {
|
|
|
}
|
|
|
|
|
|
public enum NUMTYPE {
|
|
|
- CATEGORY, OBJECT, ELEMENT, EDGE, CONNECTION, NODEEDGE, OLDEDGE
|
|
|
+ CATEGORY, OBJECT, ELEMENT, EDGE, CONNECTION, NODEEDGE, OLDEDGE, UNITGRAPH
|
|
|
+ }
|
|
|
+
|
|
|
+ public enum GRAPHTYPE {
|
|
|
+ SWITCH, ELEMENT
|
|
|
}
|
|
|
|
|
|
private Model model;
|
|
|
- private int nCat, nObj, nEle, nEdge, nConn, nNodeEdge, nOldEdge;
|
|
|
+ private Gson gson;
|
|
|
+ private int nCat, nObj, nEle, nEdge, nConn, nNodeEdge, nOldEdge, nUnitGraph;
|
|
|
|
|
|
|
|
|
* Constructor.
|
|
@@ -69,6 +77,7 @@ public class StoreController {
|
|
|
public StoreController(Model model) {
|
|
|
this.model = model;
|
|
|
initNumeration();
|
|
|
+ initGson();
|
|
|
}
|
|
|
|
|
|
|
|
@@ -83,45 +92,37 @@ public class StoreController {
|
|
|
*/
|
|
|
public void writeSaveFile(String path) throws IOException {
|
|
|
|
|
|
-
|
|
|
- GsonBuilder builder = new GsonBuilder();
|
|
|
- builder.excludeFieldsWithoutExposeAnnotation().serializeNulls();
|
|
|
- builder.registerTypeAdapter(AbstractCpsObject.class, new AbstractCpsObjectAdapter());
|
|
|
- builder.registerTypeAdapter(Position.class, new PositionAdapter());
|
|
|
- builder.registerTypeAdapter(Color.class, new ColorAdapter());
|
|
|
-
|
|
|
- Gson gson = builder.create();
|
|
|
-
|
|
|
JsonObject file = new JsonObject();
|
|
|
|
|
|
initialize(MODE.COMPLETE, file);
|
|
|
- storeCategory(gson, file);
|
|
|
- storeCanvas(gson, file);
|
|
|
- System.out.println(file.toString());
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- int i = 1;
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
+ storeCategory(file);
|
|
|
+ storeCanvas(file);
|
|
|
|
|
|
FileWriter writer = new FileWriter(path);
|
|
|
-
|
|
|
- getClass();
|
|
|
+ writer.write(gson.toJson(file));
|
|
|
+
|
|
|
+ writer.flush();
|
|
|
+ writer.close();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * Writes the Autosave File.
|
|
|
+ *
|
|
|
+ * @param path
|
|
|
+ * the Path
|
|
|
+ * @throws IOException
|
|
|
+ * Exception
|
|
|
+ */
|
|
|
+ public void writeAutosaveFile(String path) throws IOException {
|
|
|
|
|
|
+
|
|
|
+ JsonObject file = new JsonObject();
|
|
|
+ initialize(MODE.PARTIAL, file);
|
|
|
+ storeCanvas(file);
|
|
|
+
|
|
|
+ FileWriter writer = new FileWriter(path);
|
|
|
+ writer.write(gson.toJson(file));
|
|
|
+
|
|
|
writer.flush();
|
|
|
writer.close();
|
|
|
}
|
|
@@ -155,10 +156,9 @@ public class StoreController {
|
|
|
|
|
|
* Store all Categories and Object into a Json File via Serialization
|
|
|
*
|
|
|
- * @param gson
|
|
|
* @param file
|
|
|
*/
|
|
|
- private void storeCategory(Gson gson, JsonObject file) {
|
|
|
+ private void storeCategory(JsonObject file) {
|
|
|
|
|
|
for (Category cat : model.getCategories()) {
|
|
|
String key = "CATEGORY" + getNumerator(NUMTYPE.CATEGORY);
|
|
@@ -173,11 +173,11 @@ public class StoreController {
|
|
|
}
|
|
|
|
|
|
|
|
|
- *
|
|
|
- * @param gson
|
|
|
+ * Travers through all Objects via BFS and Stores everything relevant
|
|
|
+
|
|
|
* @param file
|
|
|
*/
|
|
|
- private void storeCanvas(Gson gson, JsonObject file) {
|
|
|
+ private void storeCanvas(JsonObject file) {
|
|
|
|
|
|
Queue<AbstractCpsObject> queue = new Queue<>();
|
|
|
AbstractCpsObject u = null;
|
|
@@ -201,6 +201,8 @@ public class StoreController {
|
|
|
|
|
|
if (u instanceof HolonObject)
|
|
|
elementsToJson(TYPE.CANVAS, gson, file, u);
|
|
|
+ if (u instanceof HolonSwitch)
|
|
|
+ unitgraphToJson(GRAPHTYPE.SWITCH, gson, file, u.getID(), ((HolonSwitch) u).getGraphPoints());
|
|
|
if (u instanceof CpsUpperNode) {
|
|
|
for (AbstractCpsObject adjacent : ((CpsUpperNode) u).getNodes()) {
|
|
|
queue.enqueue(adjacent);
|
|
@@ -210,238 +212,74 @@ public class StoreController {
|
|
|
edgeToJson(EDGETYPE.OLD, gson, file, u.getID(), ((CpsUpperNode) u).getOldEdges());
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ datasetToJson(gson, file);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
- * Write the Canvas File.
|
|
|
- *
|
|
|
- * @param path
|
|
|
- * the Path
|
|
|
- * @throws IOException
|
|
|
- * Exception
|
|
|
- */
|
|
|
- public void writeCanvasFile(String path) throws IOException {
|
|
|
-
|
|
|
- JSONObject json = new JSONObject();
|
|
|
-
|
|
|
- json.put("MODE", "CANVAS");
|
|
|
- json.put("ID", IdCounter.getCounter());
|
|
|
- writeCanvasObjects(json);
|
|
|
- writeCanvasElements(json);
|
|
|
- writeEdges(json);
|
|
|
- writeElementGraph(json);
|
|
|
-
|
|
|
- FileWriter writer = new FileWriter(path);
|
|
|
- writer.write(json.toJSONString());
|
|
|
- getClass();
|
|
|
-
|
|
|
- writer.flush();
|
|
|
- writer.close();
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- * writes all Categories into a JSONObject.
|
|
|
- *
|
|
|
- * @param gson
|
|
|
- *
|
|
|
- * @param json
|
|
|
- * JSON Object
|
|
|
- * @throws IOException
|
|
|
- * exception
|
|
|
- */
|
|
|
- public void writeCategory(Gson gson, JSONObject json) {
|
|
|
- int i = 1;
|
|
|
-
|
|
|
- for (Category cat : model.getCategories())
|
|
|
- json.put("CG" + i++, gson.toJson(cat));
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- * writes all Objects in Category into a JSONObject.
|
|
|
*
|
|
|
+ * @param type
|
|
|
* @param gson
|
|
|
- *
|
|
|
- * @param json
|
|
|
- * JSON Object
|
|
|
+ * @param file
|
|
|
+ * @param obj
|
|
|
*/
|
|
|
- public void writeCategoryObjects(Gson gson, JSONObject json) {
|
|
|
-
|
|
|
- int i = 1;
|
|
|
-
|
|
|
- for (Category cats : model.getCategories())
|
|
|
- for (AbstractCpsObject cps : cats.getObjects()) {
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- json.put("CGO" + i++, gson.toJson(cps));
|
|
|
-
|
|
|
- }
|
|
|
- }
|
|
|
+ private void elementsToJson(TYPE type, Gson gson, JsonObject file, AbstractCpsObject obj) {
|
|
|
+
|
|
|
+ JsonObject temp = new JsonObject();
|
|
|
|
|
|
-
|
|
|
- * Wrte Canvas Objects.
|
|
|
- *
|
|
|
- * @param json
|
|
|
- * JSON Object
|
|
|
- */
|
|
|
- public void writeCanvasObjects(JSONObject json) {
|
|
|
+ for (HolonElement ele : ((HolonObject) obj).getElements()) {
|
|
|
+ temp.add("properties", new JsonPrimitive(gson.toJson(ele)));
|
|
|
+ temp.add("ID", new JsonPrimitive(obj.getID()));
|
|
|
+ if (ele.getEnergyAt().length != 0)
|
|
|
+ unitgraphToJson(GRAPHTYPE.ELEMENT, gson, file, ele.getId(), ele.getGraphPoints());
|
|
|
|
|
|
- JSONArray arr = new JSONArray();
|
|
|
- int i = 1;
|
|
|
- for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
|
|
|
-
|
|
|
- arr.add(cps.getObjName());
|
|
|
- arr.add(cps.getName());
|
|
|
- arr.add(cps.getID());
|
|
|
- arr.add(cps.getImage());
|
|
|
- arr.add(cps.getPosition().x);
|
|
|
- arr.add(cps.getPosition().y);
|
|
|
- json.put("CVSO" + i++, arr);
|
|
|
- arr = new JSONArray();
|
|
|
+ file.add(type.name() + ".OBJECT.ELEMENT" + getNumerator(NUMTYPE.ELEMENT), gson.toJsonTree(temp));
|
|
|
+ temp = new JsonObject();
|
|
|
}
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- * writes all Elements in Objects in Category into a JSONObject.
|
|
|
- *
|
|
|
- * @param json
|
|
|
- * JSON Object
|
|
|
- */
|
|
|
- public void writeCategoryElements(JSONObject json) {
|
|
|
-
|
|
|
- JSONArray arr = new JSONArray();
|
|
|
- int i = 1;
|
|
|
-
|
|
|
- for (Category cats : model.getCategories())
|
|
|
- for (AbstractCpsObject cps : cats.getObjects())
|
|
|
- if (cps instanceof HolonObject)
|
|
|
- for (HolonElement ele : ((HolonObject) cps).getElements()) {
|
|
|
- arr.add(ele.getSav());
|
|
|
- arr.add(ele.getObj());
|
|
|
- arr.add(ele.getEleName());
|
|
|
- arr.add(ele.getAmount());
|
|
|
- arr.add(ele.getEnergy());
|
|
|
- json.put("CGE" + i++, arr);
|
|
|
- arr = new JSONArray();
|
|
|
- }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
- * Write Canvas Elements.
|
|
|
*
|
|
|
- * @param json
|
|
|
- * JSON Objects
|
|
|
+ * @param ele
|
|
|
*/
|
|
|
- public void writeCanvasElements(JSONObject json) {
|
|
|
+ private void unitgraphToJson(GRAPHTYPE type, Gson gson, JsonObject file, int id, LinkedList<Point> graph) {
|
|
|
|
|
|
- JSONArray arr = new JSONArray();
|
|
|
- int i = 1;
|
|
|
- for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
|
|
|
- if (cps instanceof HolonObject)
|
|
|
- for (HolonElement ele : ((HolonObject) cps).getElements()) {
|
|
|
- arr.add(ele.getSav());
|
|
|
- arr.add(cps.getID());
|
|
|
- arr.add(ele.getEleName());
|
|
|
- arr.add(ele.getAmount());
|
|
|
- arr.add(ele.getEnergy());
|
|
|
- arr.add(Boolean.compare(ele.getActive(), true) + 1);
|
|
|
- json.put("CVSE" + i++, arr);
|
|
|
- arr = new JSONArray();
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
+ JsonObject temp = new JsonObject();
|
|
|
+ String key = null;
|
|
|
|
|
|
-
|
|
|
- * write all Edges into a JSONObject.
|
|
|
- *
|
|
|
- * @param json
|
|
|
- * JSON Object
|
|
|
- */
|
|
|
- public void writeEdges(JSONObject json) {
|
|
|
-
|
|
|
- JSONArray arr = new JSONArray();
|
|
|
- int i = 1;
|
|
|
-
|
|
|
- for (CpsEdge edge : model.getEdgesOnCanvas()) {
|
|
|
- arr.add(edge.getA().getID());
|
|
|
- arr.add(edge.getB().getID());
|
|
|
- arr.add(edge.getCapacity());
|
|
|
- arr.add(edge.getFlow());
|
|
|
- json.put("EDGE" + i++, arr);
|
|
|
- arr = new JSONArray();
|
|
|
+ for (int i = 0; i < graph.size(); i++) {
|
|
|
+ temp.add("" + i, new JsonPrimitive(graph.get(i).getX() + ":" + graph.get(i).getY()));
|
|
|
}
|
|
|
- }
|
|
|
|
|
|
-
|
|
|
- * writes the Graph from all Elements into a JSONObject.
|
|
|
- *
|
|
|
- * @param json
|
|
|
- * JSON Object
|
|
|
- */
|
|
|
- public void writeElementGraph(JSONObject json) {
|
|
|
-
|
|
|
- ListIterator<Point> iterator;
|
|
|
- JSONArray arr = new JSONArray();
|
|
|
- int i = 1;
|
|
|
-
|
|
|
- for (Category cats : model.getCategories())
|
|
|
- for (AbstractCpsObject cps : cats.getObjects())
|
|
|
- if (cps instanceof HolonObject)
|
|
|
- for (HolonElement ele : ((HolonObject) cps).getElements())
|
|
|
- if (!ele.getGraphPoints().isEmpty()) {
|
|
|
- iterator = ele.getGraphPoints().listIterator();
|
|
|
-
|
|
|
- arr.add(ele.getSav());
|
|
|
- arr.add(ele.getObj());
|
|
|
- arr.add(ele.getEleName());
|
|
|
-
|
|
|
- while (iterator.hasNext()) {
|
|
|
- Point p = iterator.next();
|
|
|
- arr.add((int) p.getX());
|
|
|
- arr.add((int) p.getY());
|
|
|
- }
|
|
|
- json.put("CGGP" + i++, arr);
|
|
|
- arr = new JSONArray();
|
|
|
- }
|
|
|
- i = 1;
|
|
|
- for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
|
|
|
- if (cps instanceof HolonObject)
|
|
|
- for (HolonElement ele : ((HolonObject) cps).getElements())
|
|
|
- if (!ele.getGraphPoints().isEmpty()) {
|
|
|
- iterator = ele.getGraphPoints().listIterator();
|
|
|
-
|
|
|
- arr.add(ele.getSav());
|
|
|
- arr.add(cps.getID());
|
|
|
- arr.add(ele.getEleName());
|
|
|
-
|
|
|
- while (iterator.hasNext()) {
|
|
|
- Point p = iterator.next();
|
|
|
- arr.add((int) p.getX());
|
|
|
- arr.add((int) p.getY());
|
|
|
- }
|
|
|
- json.put("CVSGP" + i++, arr);
|
|
|
- arr = new JSONArray();
|
|
|
- }
|
|
|
+ switch (type) {
|
|
|
+ case SWITCH:
|
|
|
+ key = "SWITCH.UNIGRAPH" + getNumerator(NUMTYPE.UNITGRAPH);
|
|
|
+ break;
|
|
|
+ case ELEMENT:
|
|
|
+ key = "ELEMENT.UNIGRAPH" + getNumerator(NUMTYPE.UNITGRAPH);
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ break;
|
|
|
}
|
|
|
- }
|
|
|
|
|
|
- private void elementsToJson(TYPE type, Gson gson, JsonObject file, AbstractCpsObject obj) {
|
|
|
-
|
|
|
- JsonObject temp = new JsonObject();
|
|
|
-
|
|
|
- for (HolonElement ele : ((HolonObject) obj).getElements()) {
|
|
|
- file.add(type.name() + ".OBJECT.ELEMENT" + +getNumerator(NUMTYPE.ELEMENT),
|
|
|
- new JsonPrimitive(gson.toJson(ele)));
|
|
|
- }
|
|
|
+ temp.add("ID", new JsonPrimitive(id));
|
|
|
|
|
|
+ file.add(key, gson.toJsonTree(temp));
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ *
|
|
|
+ * @param type
|
|
|
+ * @param gson
|
|
|
+ * @param file
|
|
|
+ * @param id
|
|
|
+ * @param arr
|
|
|
+ */
|
|
|
private void edgeToJson(EDGETYPE type, Gson gson, JsonObject file, int id, ArrayList<CpsEdge> arr) {
|
|
|
|
|
|
String k = null;
|
|
|
- int i = 0;
|
|
|
JsonObject temp = new JsonObject();
|
|
|
|
|
|
for (CpsEdge edge : arr) {
|
|
@@ -474,8 +312,35 @@ public class StoreController {
|
|
|
}
|
|
|
|
|
|
}
|
|
|
+
|
|
|
+ private void datasetToJson(Gson gson, JsonObject file) {
|
|
|
+ JsonObject temp = new JsonObject();
|
|
|
+
|
|
|
+ }
|
|
|
|
|
|
|
|
|
+ * Initialize the Gson with wanted parameters
|
|
|
+ */
|
|
|
+ private void initGson() {
|
|
|
+
|
|
|
+ GsonBuilder builder = new GsonBuilder();
|
|
|
+ builder.excludeFieldsWithoutExposeAnnotation().serializeNulls().setPrettyPrinting();
|
|
|
+ builder.registerTypeAdapter(AbstractCpsObject.class, new AbstractCpsObjectAdapter());
|
|
|
+ builder.registerTypeAdapter(Position.class, new PositionAdapter());
|
|
|
+ builder.registerTypeAdapter(Color.class, new ColorAdapter());
|
|
|
+
|
|
|
+ this.gson = builder.create();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * Just initialize the Numerators for the Json Keys. Maybe bad Style..
|
|
|
+ */
|
|
|
+ private void initNumeration() {
|
|
|
+ this.nCat = this.nObj = this.nEle = this.nEdge = this.nConn = this.nNodeEdge = this.nOldEdge = 0;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * Get the wanted numerator and increment it
|
|
|
*
|
|
|
* @param type
|
|
|
* @return
|
|
@@ -497,17 +362,12 @@ public class StoreController {
|
|
|
return nNodeEdge++;
|
|
|
case OLDEDGE:
|
|
|
return nOldEdge++;
|
|
|
+ case UNITGRAPH:
|
|
|
+ return nUnitGraph++;
|
|
|
default:
|
|
|
break;
|
|
|
}
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- * Just initialize the Numerators for the Json Keys. Maybe bad Style..
|
|
|
- */
|
|
|
- private void initNumeration() {
|
|
|
- this.nCat = this.nObj = this.nEle = this.nEdge = this.nConn = this.nNodeEdge = this.nOldEdge = 0;
|
|
|
- }
|
|
|
-
|
|
|
}
|