|
@@ -5,6 +5,7 @@ import java.awt.Point;
|
|
import java.io.FileWriter;
|
|
import java.io.FileWriter;
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
|
|
+import java.util.LinkedList;
|
|
import java.util.ListIterator;
|
|
import java.util.ListIterator;
|
|
|
|
|
|
import org.json.simple.JSONArray;
|
|
import org.json.simple.JSONArray;
|
|
@@ -19,9 +20,8 @@ import com.google.gson.stream.JsonWriter;
|
|
|
|
|
|
import TypeAdapter.AbstractCpsObjectAdapter;
|
|
import TypeAdapter.AbstractCpsObjectAdapter;
|
|
import TypeAdapter.ColorAdapter;
|
|
import TypeAdapter.ColorAdapter;
|
|
-import TypeAdapter.CpsEdgeAdapter;
|
|
|
|
-
|
|
|
|
import TypeAdapter.PositionAdapter;
|
|
import TypeAdapter.PositionAdapter;
|
|
|
|
+
|
|
import classes.Category;
|
|
import classes.Category;
|
|
import classes.CpsEdge;
|
|
import classes.CpsEdge;
|
|
import classes.CpsUpperNode;
|
|
import classes.CpsUpperNode;
|
|
@@ -55,11 +55,16 @@ public class StoreController {
|
|
}
|
|
}
|
|
|
|
|
|
public enum NUMTYPE {
|
|
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 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.
|
|
* Constructor.
|
|
@@ -70,6 +75,18 @@ public class StoreController {
|
|
public StoreController(Model model) {
|
|
public StoreController(Model model) {
|
|
this.model = model;
|
|
this.model = model;
|
|
initNumeration();
|
|
initNumeration();
|
|
|
|
+ initGson();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private void initGson() {
|
|
|
|
+ // TODO Auto-generated method stub
|
|
|
|
+ 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());
|
|
|
|
+ // use the builder and make a instance of the Gson
|
|
|
|
+ Gson gson = builder.create();
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -85,44 +102,17 @@ public class StoreController {
|
|
public void writeSaveFile(String path) throws IOException {
|
|
public void writeSaveFile(String path) throws IOException {
|
|
|
|
|
|
// instance of the builder and give the wanted options
|
|
// instance of the builder and give the wanted options
|
|
- 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());
|
|
|
|
- // use the builder and make a instance of the Gson
|
|
|
|
- Gson gson = builder.create();
|
|
|
|
|
|
|
|
JsonObject file = new JsonObject();
|
|
JsonObject file = new JsonObject();
|
|
|
|
|
|
initialize(MODE.COMPLETE, file);
|
|
initialize(MODE.COMPLETE, file);
|
|
- storeCategory(gson, file);
|
|
|
|
- storeCanvas(gson, file);
|
|
|
|
- System.out.println(file.toString());
|
|
|
|
- // writeCanvasObjects(json);
|
|
|
|
- // writeCategoryElements(json);
|
|
|
|
- // writeCanvasElements(json);
|
|
|
|
- // writeEdges(json);
|
|
|
|
- // writeElementGraph(json);
|
|
|
|
- int i = 1;
|
|
|
|
- // for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
|
|
|
|
- // String k = objectGson.toJson(cps, AbstractCpsObject.class);
|
|
|
|
- // System.out.println(k);
|
|
|
|
- // }
|
|
|
|
-
|
|
|
|
- // for (CpsEdge edge : model.getEdgesOnCanvas()) {
|
|
|
|
- //
|
|
|
|
- // JsonObject k = new JsonObject();
|
|
|
|
- // k.add("Edge", objectGson.toJsonTree(edge));
|
|
|
|
- // k.add("a", new JsonPrimitive(edge.getA().getID()));
|
|
|
|
- // k.add("b", new JsonPrimitive(edge.getB().getID()));
|
|
|
|
- // System.out.println(k);
|
|
|
|
- // }
|
|
|
|
|
|
+ storeCategory(file);
|
|
|
|
+ storeCanvas(file);
|
|
|
|
|
|
FileWriter writer = new FileWriter(path);
|
|
FileWriter writer = new FileWriter(path);
|
|
- // writer.write(json.toJSONString());
|
|
|
|
- getClass();
|
|
|
|
-
|
|
|
|
|
|
+
|
|
|
|
+ writer.write(gson.toJson(file));
|
|
|
|
+
|
|
writer.flush();
|
|
writer.flush();
|
|
writer.close();
|
|
writer.close();
|
|
}
|
|
}
|
|
@@ -159,7 +149,7 @@ public class StoreController {
|
|
* @param gson
|
|
* @param gson
|
|
* @param file
|
|
* @param file
|
|
*/
|
|
*/
|
|
- private void storeCategory(Gson gson, JsonObject file) {
|
|
|
|
|
|
+ private void storeCategory(JsonObject file) {
|
|
// TODO Auto-generated method stub
|
|
// TODO Auto-generated method stub
|
|
for (Category cat : model.getCategories()) {
|
|
for (Category cat : model.getCategories()) {
|
|
String key = "CATEGORY" + getNumerator(NUMTYPE.CATEGORY);
|
|
String key = "CATEGORY" + getNumerator(NUMTYPE.CATEGORY);
|
|
@@ -178,7 +168,7 @@ public class StoreController {
|
|
* @param gson
|
|
* @param gson
|
|
* @param file
|
|
* @param file
|
|
*/
|
|
*/
|
|
- private void storeCanvas(Gson gson, JsonObject file) {
|
|
|
|
|
|
+ private void storeCanvas(JsonObject file) {
|
|
// TODO Auto-generated method stub
|
|
// TODO Auto-generated method stub
|
|
Queue<AbstractCpsObject> queue = new Queue<>();
|
|
Queue<AbstractCpsObject> queue = new Queue<>();
|
|
AbstractCpsObject u = null;
|
|
AbstractCpsObject u = null;
|
|
@@ -202,6 +192,8 @@ public class StoreController {
|
|
|
|
|
|
if (u instanceof HolonObject)
|
|
if (u instanceof HolonObject)
|
|
elementsToJson(TYPE.CANVAS, gson, file, u);
|
|
elementsToJson(TYPE.CANVAS, gson, file, u);
|
|
|
|
+ if (u instanceof HolonSwitch)
|
|
|
|
+ unitgraphToJson(GRAPHTYPE.SWITCH, gson, file, u.getID(), ((HolonSwitch) u).getGraphPoints());
|
|
if (u instanceof CpsUpperNode) {
|
|
if (u instanceof CpsUpperNode) {
|
|
for (AbstractCpsObject adjacent : ((CpsUpperNode) u).getNodes()) {
|
|
for (AbstractCpsObject adjacent : ((CpsUpperNode) u).getNodes()) {
|
|
queue.enqueue(adjacent);
|
|
queue.enqueue(adjacent);
|
|
@@ -224,17 +216,8 @@ public class StoreController {
|
|
*/
|
|
*/
|
|
public void writeCanvasFile(String path) throws IOException {
|
|
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);
|
|
FileWriter writer = new FileWriter(path);
|
|
- writer.write(json.toJSONString());
|
|
|
|
|
|
+
|
|
getClass();
|
|
getClass();
|
|
|
|
|
|
writer.flush();
|
|
writer.flush();
|
|
@@ -242,203 +225,65 @@ public class StoreController {
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
- * 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 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()) {
|
|
|
|
- // arr.add(cps.getSav());
|
|
|
|
- // arr.add(cps.getObjName());
|
|
|
|
- // arr.add(cps.getImage());
|
|
|
|
- json.put("CGO" + i++, gson.toJson(cps));
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
+ private void elementsToJson(TYPE type, Gson gson, JsonObject file, AbstractCpsObject obj) {
|
|
|
|
+ // TODO Auto-generated method stub
|
|
|
|
+ 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(getObjectType(cps));
|
|
|
|
- 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) {
|
|
|
|
- // TODO Auto-generated method stub
|
|
|
|
- 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) {
|
|
private void edgeToJson(EDGETYPE type, Gson gson, JsonObject file, int id, ArrayList<CpsEdge> arr) {
|
|
// TODO Auto-generated method stub
|
|
// TODO Auto-generated method stub
|
|
String k = null;
|
|
String k = null;
|
|
@@ -477,6 +322,7 @@ public class StoreController {
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
|
|
+ * Get the wanted numerator and increment it
|
|
*
|
|
*
|
|
* @param type
|
|
* @param type
|
|
* @return
|
|
* @return
|
|
@@ -498,6 +344,8 @@ public class StoreController {
|
|
return nNodeEdge++;
|
|
return nNodeEdge++;
|
|
case OLDEDGE:
|
|
case OLDEDGE:
|
|
return nOldEdge++;
|
|
return nOldEdge++;
|
|
|
|
+ case UNITGRAPH:
|
|
|
|
+ return nUnitGraph++;
|
|
default:
|
|
default:
|
|
break;
|
|
break;
|
|
}
|
|
}
|