|
@@ -5,85 +5,315 @@ import java.io.File;
|
|
import java.io.FileReader;
|
|
import java.io.FileReader;
|
|
import java.io.FileWriter;
|
|
import java.io.FileWriter;
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
+import java.util.Iterator;
|
|
|
|
+
|
|
import org.json.simple.JSONArray;
|
|
import org.json.simple.JSONArray;
|
|
import org.json.simple.JSONObject;
|
|
import org.json.simple.JSONObject;
|
|
|
|
+import org.json.simple.parser.JSONParser;
|
|
|
|
+import org.json.simple.parser.ParseException;
|
|
|
|
+
|
|
import classes.Category;
|
|
import classes.Category;
|
|
|
|
+import classes.CpsEdge;
|
|
|
|
+import classes.CpsNode;
|
|
import classes.CpsObject;
|
|
import classes.CpsObject;
|
|
import classes.HolonElement;
|
|
import classes.HolonElement;
|
|
import classes.HolonObject;
|
|
import classes.HolonObject;
|
|
|
|
+import classes.HolonSwitch;
|
|
|
|
+import classes.HolonTransformer;
|
|
import ui.model.Model;
|
|
import ui.model.Model;
|
|
|
|
+import ui.model.idCounter;
|
|
|
|
|
|
public class LoadStoreController {
|
|
public class LoadStoreController {
|
|
|
|
+
|
|
private Model MODEL;
|
|
private Model MODEL;
|
|
|
|
+ private CategoryController categoryController;
|
|
|
|
+ private CanvasController canvasController;
|
|
|
|
+ private ObjectController objectController;
|
|
|
|
|
|
- public LoadStoreController(Model model) {
|
|
|
|
|
|
+ public LoadStoreController(Model model, CategoryController cg, CanvasController cvs, ObjectController obj) {
|
|
this.MODEL = model;
|
|
this.MODEL = model;
|
|
|
|
+ this.categoryController = cg;
|
|
|
|
+ this.canvasController = cvs;
|
|
|
|
+ this.objectController = obj;
|
|
|
|
+
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
-
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Writes a Savefile in .json
|
|
|
|
+ *
|
|
|
|
+ * @throws IOException
|
|
|
|
+ */
|
|
public void writeJSONFile() throws IOException {
|
|
public void writeJSONFile() throws IOException {
|
|
|
|
|
|
JSONObject json = new JSONObject();
|
|
JSONObject json = new JSONObject();
|
|
|
|
|
|
-
|
|
|
|
|
|
+ json.put("ID", idCounter.getCounter()-1);
|
|
writeCategory(json);
|
|
writeCategory(json);
|
|
- writeCategoryObjects(json);
|
|
|
|
- writeCategoryElements(json);
|
|
|
|
|
|
+ writeObjects(json);
|
|
|
|
+ writeElements(json);
|
|
|
|
+ writeEdges(json);
|
|
|
|
|
|
FileWriter writer = new FileWriter("//Users//zheng//Desktop//Tesst.json");
|
|
FileWriter writer = new FileWriter("//Users//zheng//Desktop//Tesst.json");
|
|
|
|
+ writer.write(json.toJSONString());
|
|
writer.flush();
|
|
writer.flush();
|
|
writer.close();
|
|
writer.close();
|
|
}
|
|
}
|
|
|
|
|
|
- public void writeCategory(JSONObject json) throws IOException {
|
|
|
|
|
|
+ /**
|
|
|
|
+ * writes all Categories into a JSONObject
|
|
|
|
+ *
|
|
|
|
+ * @param json
|
|
|
|
+ * @throws IOException
|
|
|
|
+ */
|
|
|
|
+ public void writeCategory(JSONObject json) {
|
|
JSONArray arr = new JSONArray();
|
|
JSONArray arr = new JSONArray();
|
|
-
|
|
|
|
|
|
+
|
|
for (Category cat : MODEL.getCategories())
|
|
for (Category cat : MODEL.getCategories())
|
|
arr.add(cat.getName());
|
|
arr.add(cat.getName());
|
|
- json.put("Category", arr);
|
|
|
|
|
|
+
|
|
|
|
+ json.put("CG", arr);
|
|
}
|
|
}
|
|
|
|
|
|
- public void writeCategoryObjects(JSONObject json) throws IOException {
|
|
|
|
-
|
|
|
|
|
|
+ /**
|
|
|
|
+ * writes all Objects in Category into a JSONObject
|
|
|
|
+ *
|
|
|
|
+ * @param json
|
|
|
|
+ */
|
|
|
|
+ public void writeObjects(JSONObject json) {
|
|
|
|
+
|
|
JSONArray arr = new JSONArray();
|
|
JSONArray arr = new JSONArray();
|
|
int i = 1;
|
|
int i = 1;
|
|
-
|
|
|
|
|
|
+
|
|
for (Category cats : MODEL.getCategories())
|
|
for (Category cats : MODEL.getCategories())
|
|
for (CpsObject cps : cats.getObjects()) {
|
|
for (CpsObject cps : cats.getObjects()) {
|
|
- arr.add(cps.getStored());
|
|
|
|
|
|
+ arr.add(getObjectType(cps));
|
|
|
|
+ arr.add(cps.getSav());
|
|
arr.add(cps.getObjName());
|
|
arr.add(cps.getObjName());
|
|
- arr.add(cps.getName());
|
|
|
|
arr.add(cps.getImage());
|
|
arr.add(cps.getImage());
|
|
- json.put("CategoryObject." + i++, arr);
|
|
|
|
|
|
+ json.put("CGO" + i++, arr);
|
|
arr = new JSONArray();
|
|
arr = new JSONArray();
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ i = 1;
|
|
|
|
+ for (CpsObject cps : MODEL.getObjectsOnCanvas()) {
|
|
|
|
+ arr.add(getObjectType(cps));
|
|
|
|
+ arr.add(cps.getObjName());
|
|
|
|
+ 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();
|
|
|
|
+ ;
|
|
|
|
+
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
- public void writeCategoryElements(JSONObject json) throws IOException {
|
|
|
|
-
|
|
|
|
|
|
+ /**
|
|
|
|
+ *
|
|
|
|
+ * @param cps
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public String getObjectType(CpsObject cps) {
|
|
|
|
+ if (cps instanceof HolonObject)
|
|
|
|
+ return "HolonObject";
|
|
|
|
+ if (cps instanceof HolonTransformer)
|
|
|
|
+ return "HolonTransformer";
|
|
|
|
+ if (cps instanceof HolonSwitch)
|
|
|
|
+ return "HolonSwitch";
|
|
|
|
+ else
|
|
|
|
+ return "CpsNode";
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * writes all Elements in Objects in Category into a JSONObject
|
|
|
|
+ *
|
|
|
|
+ * @param json
|
|
|
|
+ */
|
|
|
|
+ public void writeElements(JSONObject json) {
|
|
|
|
|
|
JSONArray arr = new JSONArray();
|
|
JSONArray arr = new JSONArray();
|
|
int i = 1;
|
|
int i = 1;
|
|
-
|
|
|
|
|
|
+
|
|
for (Category cats : MODEL.getCategories())
|
|
for (Category cats : MODEL.getCategories())
|
|
for (CpsObject cps : cats.getObjects())
|
|
for (CpsObject cps : cats.getObjects())
|
|
if (cps instanceof HolonObject)
|
|
if (cps instanceof HolonObject)
|
|
for (HolonElement ele : ((HolonObject) cps).getElements()) {
|
|
for (HolonElement ele : ((HolonObject) cps).getElements()) {
|
|
- arr.add(ele.getStored());
|
|
|
|
|
|
+ arr.add(ele.getSav());
|
|
|
|
+ arr.add(ele.getObj());
|
|
arr.add(ele.getEleName());
|
|
arr.add(ele.getEleName());
|
|
arr.add(ele.getAmount());
|
|
arr.add(ele.getAmount());
|
|
arr.add(ele.getEnergy());
|
|
arr.add(ele.getEnergy());
|
|
- json.put("CategoryElement." + i++, arr);
|
|
|
|
|
|
+ json.put("CGE" + i++, arr);
|
|
arr = new JSONArray();
|
|
arr = new JSONArray();
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ i = 1;
|
|
|
|
+ for (CpsObject 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());
|
|
|
|
+ json.put("CVSE" + i++, arr);
|
|
|
|
+ arr = new JSONArray();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
- public void readFromJSON(File jsonFile) throws IOException {
|
|
|
|
- String line;
|
|
|
|
- BufferedReader reader = new BufferedReader(new FileReader("textfile"));
|
|
|
|
- while ((line = reader.readLine()) != null) {
|
|
|
|
- // mach hier irgendwas mit der Gelesenen Zeile
|
|
|
|
|
|
+ /**
|
|
|
|
+ * write all Edges into a JSONObject
|
|
|
|
+ *
|
|
|
|
+ * @param json
|
|
|
|
+ */
|
|
|
|
+ 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();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ public void readJSON() throws IOException {
|
|
|
|
+ JSONParser parser = new JSONParser();
|
|
|
|
+ MODEL.setCategories(new ArrayList<>());
|
|
|
|
+ MODEL.setObjectsOnCanvas(new ArrayList<>());
|
|
|
|
+
|
|
|
|
+ ArrayList<String> obj = new ArrayList<>();
|
|
|
|
+ ArrayList<String> ele = new ArrayList<>();
|
|
|
|
+ ArrayList<String> edge = new ArrayList<>();
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+
|
|
|
|
+ JSONObject json = (JSONObject) parser.parse(new FileReader("//Users//zheng//Desktop//Tesst.json"));
|
|
|
|
+
|
|
|
|
+ for (Object key : json.keySet()) {
|
|
|
|
+
|
|
|
|
+ if (key.equals("CG"))
|
|
|
|
+ readCategory((JSONArray) json.get(key));
|
|
|
|
+ else if (key.toString().contains("CGO") || key.toString().contains("CVSO"))
|
|
|
|
+ obj.add(key.toString());
|
|
|
|
+ else if (key.toString().contains("CGE") || key.toString().contains("CVSE"))
|
|
|
|
+ ele.add(key.toString());
|
|
|
|
+ else if (key.toString().contains("EDGE"))
|
|
|
|
+ edge.add(key.toString());
|
|
|
|
+ else idCounter.setCounter(Integer.parseInt(json.get(key.toString()).toString()));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ dispatch(obj, json);
|
|
|
|
+ dispatch(ele, json);
|
|
|
|
+ dispatch(edge, json);
|
|
|
|
+
|
|
|
|
+ } catch (ParseException e) {
|
|
|
|
+ // TODO Auto-generated catch block
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void dispatch(ArrayList<String> input, JSONObject json) {
|
|
|
|
+
|
|
|
|
+ for (String str : input) {
|
|
|
|
+ if (str.contains("CGO")) {
|
|
|
|
+ readCategoryObject((JSONArray) json.get(str));
|
|
|
|
+ } else if (str.contains("CVSO")) {
|
|
|
|
+ readCanvasObject((JSONArray) json.get(str));
|
|
|
|
+ } else if (str.contains("CGE") || str.contains("CVSE")) {
|
|
|
|
+ readElement((JSONArray) json.get(str));
|
|
|
|
+ } else if (str.contains("EDGE")) {
|
|
|
|
+ readEdge((JSONArray) json.get(str));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void readCategory(JSONArray arr) {
|
|
|
|
+
|
|
|
|
+ Iterator<Object> i = arr.iterator();
|
|
|
|
+
|
|
|
|
+ while (i.hasNext()) {
|
|
|
|
+ categoryController.addNewCategory(i.next().toString());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void readCategoryObject(JSONArray arr) {
|
|
|
|
+ Iterator<Object> i = arr.iterator();
|
|
|
|
+
|
|
|
|
+ String type = i.next().toString();
|
|
|
|
+
|
|
|
|
+ if (type.equals("HolonObject")) {
|
|
|
|
+ categoryController.addNewHolonObject(categoryController.searchCatNode(i.next().toString()),
|
|
|
|
+ i.next().toString(), new ArrayList<>(), i.next().toString());
|
|
|
|
+ }
|
|
|
|
+ if (type.equals("HolonTransformer")) {
|
|
|
|
+ categoryController.addNewHolonTransformer(categoryController.searchCatNode(i.next().toString()),
|
|
|
|
+ i.next().toString(), i.next().toString());
|
|
|
|
+ }
|
|
|
|
+ if (type.equals("HolonSwitch")) {
|
|
|
|
+ categoryController.addNewHolonSwitch(categoryController.searchCatNode(i.next().toString()),
|
|
|
|
+ i.next().toString(), i.next().toString());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void readCanvasObject(JSONArray arr) {
|
|
|
|
+ Iterator<Object> i = arr.iterator();
|
|
|
|
+ CpsObject cps = null;
|
|
|
|
+
|
|
|
|
+ String type = i.next().toString();
|
|
|
|
+
|
|
|
|
+ if (type.equals("HolonObject")) {
|
|
|
|
+ cps = new HolonObject(i.next().toString());
|
|
|
|
+ }
|
|
|
|
+ if (type.equals("HolonTransformer")) {
|
|
|
|
+ cps = new HolonTransformer(i.next().toString());
|
|
|
|
+ }
|
|
|
|
+ if (type.equals("HolonSwitch")) {
|
|
|
|
+ cps = new HolonSwitch(i.next().toString());
|
|
|
|
+ }
|
|
|
|
+ if (type.equals("CpsNode")) {
|
|
|
|
+ cps = new CpsNode(i.next().toString());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ cps.setID(Integer.parseInt(i.next().toString()));
|
|
|
|
+ cps.setImage(i.next().toString());
|
|
|
|
+ cps.setPosition(Integer.parseInt(i.next().toString()), Integer.parseInt(i.next().toString()));
|
|
|
|
+
|
|
|
|
+ canvasController.addObjectIntoCanvas(cps);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void readElement(JSONArray arr) {
|
|
|
|
+ Iterator<Object> i = arr.iterator();
|
|
|
|
+
|
|
|
|
+ String sav = i.next().toString();
|
|
|
|
+ System.out.println(sav);
|
|
|
|
+
|
|
|
|
+ if (sav.equals("Canvas")) {
|
|
|
|
+ objectController.addNewElementIntoCanvasObject(Integer.parseInt(i.next().toString()), i.next().toString(),
|
|
|
|
+ Integer.parseInt(i.next().toString()), Float.parseFloat(i.next().toString()));
|
|
|
|
+ } else
|
|
|
|
+ objectController.addNewElementIntoCategoryObject(sav, i.next().toString(), i.next().toString(),
|
|
|
|
+ Integer.parseInt(i.next().toString()), Float.parseFloat(i.next().toString()));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void readEdge(JSONArray arr) {
|
|
|
|
+ Iterator<Object> i = arr.iterator();
|
|
|
|
+
|
|
|
|
+ CpsEdge edge = new CpsEdge(objectController.searchByID(Integer.parseInt(i.next().toString())),
|
|
|
|
+ objectController.searchByID(Integer.parseInt(i.next().toString())));
|
|
|
|
+ edge.setCapacity(Float.parseFloat(i.next().toString()));
|
|
|
|
+ edge.setFlow(Float.parseFloat(i.next().toString()));
|
|
|
|
+
|
|
|
|
+ canvasController.addEdgeOnCanvas(edge);
|
|
|
|
+ }
|
|
}
|
|
}
|