فهرست منبع

new StoreController nearly Finished

Teh-Hai Julian Zheng 7 سال پیش
والد
کامیت
753d5fa96c
3فایلهای تغییر یافته به همراه83 افزوده شده و 232 حذف شده
  1. 7 8
      src/TypeAdapter/AbstractCpsObjectAdapter.java
  2. 4 0
      src/classes/TrackedDataSet.java
  3. 72 224
      src/ui/controller/StoreController.java

+ 7 - 8
src/TypeAdapter/AbstractCpsObjectAdapter.java

@@ -12,20 +12,21 @@ import com.google.gson.JsonSerializationContext;
 import com.google.gson.JsonSerializer;
 
 import classes.AbstractCpsObject;
+import classes.HolonSwitch;
+import ui.controller.StoreController;
 
-public class AbstractCpsObjectAdapter implements JsonSerializer<AbstractCpsObject>, JsonDeserializer<AbstractCpsObject>{
+public class AbstractCpsObjectAdapter
+		implements JsonSerializer<AbstractCpsObject>, JsonDeserializer<AbstractCpsObject> {
 
-	
 	@Override
 	public JsonElement serialize(AbstractCpsObject arg0, Type arg1, JsonSerializationContext arg2) {
 		// TODO Auto-generated method stub
 		JsonObject object = new JsonObject();
 		object.add("type", new JsonPrimitive(arg0.getClass().getSimpleName()));
-		object.add("properties", arg2.serialize(arg0,arg0.getClass()));
-
+		object.add("properties", arg2.serialize(arg0, arg0.getClass()));
 		return object;
 	}
-	
+
 	@Override
 	public AbstractCpsObject deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2)
 			throws JsonParseException {
@@ -35,7 +36,7 @@ public class AbstractCpsObjectAdapter implements JsonSerializer<AbstractCpsObjec
 		JsonElement element = object.get("properties");
 		try {
 			String packageName = "classes.";
-			return arg2.deserialize(element, Class.forName(packageName+type));
+			return arg2.deserialize(element, Class.forName(packageName + type));
 		} catch (ClassNotFoundException cnfe) {
 			// TODO: handle exception
 			throw new JsonParseException("Unknown element type: " + type, cnfe);
@@ -43,6 +44,4 @@ public class AbstractCpsObjectAdapter implements JsonSerializer<AbstractCpsObjec
 
 	}
 
-
-
 }

+ 4 - 0
src/classes/TrackedDataSet.java

@@ -2,6 +2,8 @@ package classes;
 
 import java.awt.Color;
 
+import com.google.gson.annotations.Expose;
+
 public class TrackedDataSet {
 
 	//Property Integers
@@ -20,7 +22,9 @@ public class TrackedDataSet {
 	
 	//Variables of the Data Set
 	private AbstractCpsObject cps;
+	@Expose
 	private int property;
+	@Expose
 	private Color color;
 
 	//Value for each timeStep

+ 72 - 224
src/ui/controller/StoreController.java

@@ -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;
@@ -19,9 +20,8 @@ import com.google.gson.stream.JsonWriter;
 
 import TypeAdapter.AbstractCpsObjectAdapter;
 import TypeAdapter.ColorAdapter;
-import TypeAdapter.CpsEdgeAdapter;
-
 import TypeAdapter.PositionAdapter;
+
 import classes.Category;
 import classes.CpsEdge;
 import classes.CpsUpperNode;
@@ -55,11 +55,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.
@@ -70,6 +75,18 @@ public class StoreController {
 	public StoreController(Model model) {
 		this.model = model;
 		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 {
 
 		// 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();
 
 		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);
-		// writer.write(json.toJSONString());
-		getClass();
-
+		
+		writer.write(gson.toJson(file));
+		
 		writer.flush();
 		writer.close();
 	}
@@ -159,7 +149,7 @@ public class StoreController {
 	 * @param gson
 	 * @param file
 	 */
-	private void storeCategory(Gson gson, JsonObject file) {
+	private void storeCategory(JsonObject file) {
 		// TODO Auto-generated method stub
 		for (Category cat : model.getCategories()) {
 			String key = "CATEGORY" + getNumerator(NUMTYPE.CATEGORY);
@@ -178,7 +168,7 @@ public class StoreController {
 	 * @param gson
 	 * @param file
 	 */
-	private void storeCanvas(Gson gson, JsonObject file) {
+	private void storeCanvas(JsonObject file) {
 		// TODO Auto-generated method stub
 		Queue<AbstractCpsObject> queue = new Queue<>();
 		AbstractCpsObject u = null;
@@ -202,6 +192,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);
@@ -224,17 +216,8 @@ public class StoreController {
 	 */
 	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();
@@ -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 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) {
 		// TODO Auto-generated method stub
 		String k = null;
@@ -477,6 +322,7 @@ public class StoreController {
 	}
 
 	/**
+	 * Get the wanted numerator and increment it
 	 * 
 	 * @param type
 	 * @return
@@ -498,6 +344,8 @@ public class StoreController {
 			return nNodeEdge++;
 		case OLDEDGE:
 			return nOldEdge++;
+		case UNITGRAPH:
+			return nUnitGraph++;
 		default:
 			break;
 		}