Teh-Hai Julian Zheng 8 jaren geleden
bovenliggende
commit
cd1e759848

+ 17 - 0
src/classes/Category.java

@@ -1,6 +1,7 @@
 package classes;
 
 import java.util.ArrayList;
+import java.util.HashMap;
 
 
 public class Category{
@@ -8,11 +9,13 @@ public class Category{
 	private int ID;
 	private ArrayList<CpsObject> objects;
 	private String name;
+	private HashMap<String, Integer> ObjIdx;
 	
 	public Category(String name){
 		setObjects(new ArrayList<CpsObject>());
 		setName(name);
 		setID(-1);
+		setObjIdx(new HashMap<>());
 	
 	}
 
@@ -58,6 +61,20 @@ public class Category{
 		this.name = name;
 	}
 
+	/**
+	 * @return the objIdx
+	 */
+	public HashMap<String, Integer> getObjIdx() {
+		return ObjIdx;
+	}
+
+	/**
+	 * @param objIdx the objIdx to set
+	 */
+	public void setObjIdx(HashMap<String, Integer> objIdx) {
+		ObjIdx = objIdx;
+	}
+
 
 	
 }

+ 3 - 0
src/classes/CpsObject.java

@@ -1,6 +1,7 @@
 package classes;
 
 import java.util.ArrayList;
+import java.util.HashMap;
 
 import ui.model.idCounter;
 
@@ -18,6 +19,8 @@ public abstract class CpsObject {
 	/* Position with a X and Y value */
 	Position position;
 	/* Energy input and output of each object in the grid */
+
+
 	
 	String sav;
 	

+ 21 - 0
src/classes/HolonObject.java

@@ -1,7 +1,9 @@
 package classes;
 
 import java.util.ArrayList;
+import java.util.HashMap;
 
+import ui.controller.SearchController;
 import ui.model.idCounter;
 
 public class HolonObject extends CpsObject {
@@ -10,6 +12,7 @@ public class HolonObject extends CpsObject {
 	private ArrayList<HolonElement> elements;
 	/* Total of consumption */
 	private float currentEnergy;
+	private HashMap<String, Integer> EleIdx;
 	/**
 	 * State of the building: 0 = fully supplied (currentEnergy == 0) 1 = not
 	 * enough energy (currentEnergy > 0) 2 = oversupplied (currentEnergy < 0)
@@ -22,17 +25,21 @@ public class HolonObject extends CpsObject {
 	 */
 	public HolonObject(String ObjName) {
 		super(ObjName);
+		setEleIdx(new HashMap<>());
 		setElements(new ArrayList<HolonElement>());
+
 	}
 
 	public HolonObject(String ObjName, String obj) {
 		super(ObjName);
 		super.setName(obj);
+		setEleIdx(new HashMap<>());
 		setElements(new ArrayList<HolonElement>());
 	}
 
 	public HolonObject(CpsObject obj) {
 		super(obj);
+		setEleIdx(SearchController.copyHashMap(((HolonObject) obj).getEleIdx()));
 		setElements(((HolonObject) obj).getCopyofElements());
 	}
 
@@ -123,4 +130,18 @@ public class HolonObject extends CpsObject {
 		}
 		return temp;
 	}
+
+	/**
+	 * @return the eleIdx
+	 */
+	public HashMap<String, Integer> getEleIdx() {
+		return EleIdx;
+	}
+
+	/**
+	 * @param eleIdx the eleIdx to set
+	 */
+	public void setEleIdx(HashMap<String, Integer> eleIdx) {
+		EleIdx = eleIdx;
+	}
 }

+ 3 - 2
src/ui/controller/CanvasController.java

@@ -29,7 +29,7 @@ public class CanvasController {
 		// object.setSav("Canvas");
 		// object.setConnections(new ArrayList<>());
 		int i = 0;
-		while (sC.searchHolonObject(object.getName(), MODEL.getObjectsOnCanvas()) != null) {
+		while (sC.searchByID(object.getID()) != null) {
 			if (object.getObjName().contains("_"))
 				object.setObjName(object.getObjName().substring(0, object.getObjName().indexOf('_')));
 			String name = object.getObjName() + "_" + i;
@@ -37,7 +37,7 @@ public class CanvasController {
 			object.setName(name);
 			i++;
 		}
-
+		MODEL.getCvsObjIdx().put(object.getID(), MODEL.getObjectsOnCanvas().size());
 		MODEL.getObjectsOnCanvas().add(object);
 		notifyObjListeners();
 		// System.out.println("Added: " + objName);
@@ -79,6 +79,7 @@ public class CanvasController {
 			MODEL.getEdgesOnCanvas().remove(e);
 		}
 		MODEL.getObjectsOnCanvas().remove(obj);
+		sC.decIdx(obj.getID(), MODEL.getCvsObjIdx());
 	}
 
 	/**

+ 7 - 3
src/ui/controller/CategoryController.java

@@ -58,7 +58,7 @@ public class CategoryController {
 			category.setName(category.getName() + "_" + i);
 			i++;
 		}
-
+		MODEL.getCgIdx().put(category.getName(), MODEL.getCategories().size());
 		MODEL.getCategories().add(category);
 		notifyCatListeners();
 	}
@@ -79,6 +79,7 @@ public class CategoryController {
 	 */
 	public void removeCategory(Category c) {
 		MODEL.getCategories().remove(c);
+		sC.decIdx(c.getName(), MODEL.getCgIdx());
 		notifyCatListeners();
 	}
 
@@ -101,7 +102,7 @@ public class CategoryController {
 	 */
 	public void addObject(Category category, CpsObject object) {
 		int i = 0;
-		while (sC.searchHolonObject(object.getObjName(), category.getObjects()) != null) {
+		while (sC.searchCategoryObject(category, object.getObjName()) != null) {
 			if (object.getObjName().contains("_"))
 				object.setObjName(object.getObjName().substring(0, object.getObjName().indexOf('_')));
 			String name = object.getObjName() + "_" + i;
@@ -109,6 +110,7 @@ public class CategoryController {
 			object.setName(name);
 			i++;
 		}
+		category.getObjIdx().put(object.getObjName(), category.getObjects().size());
 		category.getObjects().add(object);
 		notifyCatListeners();
 	}
@@ -161,13 +163,15 @@ public class CategoryController {
 	}
 
 	public void removeObject(Category category, CpsObject cps) {
+
 		category.getObjects().remove(cps);
+		sC.decIdx(cps.getObjName(), category.getObjIdx());
 		notifyCatListeners();
 	}
 
 	public void deleteObject(String category, String obj) {
 		Category cat = sC.searchCategory(category);
-		removeObject(cat, sC.searchHolonObject(obj, cat.getObjects()));
+		removeObject(cat, sC.searchCategoryObject(cat, obj));
 	}
 
 	/**

+ 3 - 3
src/ui/controller/Control.java

@@ -50,11 +50,11 @@ public class Control {
 	}
 
 	public CpsObject searchCategoryObject(String category, String object) {
-		return searchController.searchHolonObject(object, searchController.searchCategory(category).getObjects());
+		return searchController.searchCategoryObject(searchController.searchCategory(category), object);
 	}
 
-	public CpsObject searchCanvasObject(String object) {
-		return searchController.searchHolonObject(object, MODEL.getObjectsOnCanvas());
+	public CpsObject searchCanvasObject(int ID) {
+		return searchController.searchByID(ID);
 	}
 
 	/* Operations for Categories and Objects */

+ 4 - 4
src/ui/controller/LoadController.java

@@ -29,7 +29,8 @@ public class LoadController {
 	private ObjectController objC;
 	private SearchController sC;
 
-	public LoadController(Model model, CategoryController cg, CanvasController cvs, ObjectController obj, SearchController sC) {
+	public LoadController(Model model, CategoryController cg, CanvasController cvs, ObjectController obj,
+			SearchController sC) {
 		this.MODEL = model;
 		this.cgC = cg;
 		this.cvsC = cvs;
@@ -200,8 +201,7 @@ public class LoadController {
 	public void readEdge(JSONArray arr) {
 		Iterator<Object> i = arr.iterator();
 
-		CpsEdge edge = new CpsEdge(sC.searchByID(Integer.parseInt(next(i))),
-				sC.searchByID(Integer.parseInt(next(i))));
+		CpsEdge edge = new CpsEdge(sC.searchByID(Integer.parseInt(next(i))), sC.searchByID(Integer.parseInt(next(i))));
 		edge.setCapacity(Float.parseFloat(next(i)));
 		edge.setFlow(Float.parseFloat(next(i)));
 
@@ -219,7 +219,7 @@ public class LoadController {
 			while (i.hasNext())
 				ele.getGraphPoints().add(new Point(Integer.parseInt(next(i)), Integer.parseInt(next(i))));
 		} else {
-			ele = sC.searchHolonElement((HolonObject) sC.searchHolonObject(next(i), sC.searchCategory(sav).getObjects()),
+			ele = sC.searchHolonElement((HolonObject) sC.searchCategoryObject(sC.searchCategory(sav), next(i)),
 					next(i));
 			while (i.hasNext())
 				ele.getGraphPoints().add(new Point(Integer.parseInt(next(i)), Integer.parseInt(next(i))));

+ 6 - 2
src/ui/controller/ObjectController.java

@@ -34,6 +34,7 @@ public class ObjectController {
 	 * Adds Element into a Object
 	 */
 	public void addElement(HolonObject object, HolonElement element) {
+		object.getEleIdx().put(element.getEleName(), object.getElements().size());
 		object.getElements().add(element);
 	}
 
@@ -71,7 +72,7 @@ public class ObjectController {
 	 * @param element
 	 */
 	public void addElementIntoCategoryObject(String category, String object, HolonElement element) {
-		addElement((HolonObject) sC.searchHolonObject(object, sC.searchCategory(category).getObjects()), element);
+		addElement((HolonObject) sC.searchCategoryObject(sC.searchCategory(category), object), element);
 	}
 
 	/**
@@ -85,6 +86,7 @@ public class ObjectController {
 	 */
 	public void addNewElementIntoCategoryObject(String category, String object, String element, int amount,
 			float energy) {
+
 		HolonElement ele = new HolonElement(element, amount, energy);
 		ele.setSav(category);
 		ele.setObj(object);
@@ -100,6 +102,8 @@ public class ObjectController {
 	 */
 	public void deleteElement(HolonObject obj, HolonElement ele) {
 		obj.getElements().remove(ele);
+		obj.getEleIdx().remove(ele.getEleName());
+		sC.decIdx(ele.getEleName(), obj.getEleIdx());
 	}
 
 	/**
@@ -127,7 +131,7 @@ public class ObjectController {
 	 */
 	public void deleteElementInCategory(String cat, String obj, String ele, int amount) {
 		Category category = sC.searchCategory(cat);
-		HolonObject object = (HolonObject) sC.searchHolonObject(obj, category.getObjects());
+		HolonObject object = (HolonObject) sC.searchCategoryObject(category, obj);
 		HolonElement element = sC.searchHolonElement(object, ele);
 
 		deleteElement(object, element);

+ 75 - 19
src/ui/controller/SearchController.java

@@ -1,6 +1,11 @@
 package ui.controller;
 
 import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map.Entry;
+
+import com.sun.xml.internal.bind.v2.runtime.unmarshaller.XsiNilLoader.Array;
 
 import classes.Category;
 import classes.CpsObject;
@@ -16,7 +21,7 @@ public class SearchController {
 		this.MODEL = model;
 
 	}
-	
+
 	/**
 	 * search for category
 	 * 
@@ -25,14 +30,20 @@ public class SearchController {
 	 */
 	public Category searchCategory(String category) {
 
-		for (Category cat : MODEL.getCategories()) {
-			if (cat.getName().equals(category)) {
-				return cat;
-			}
-		}
-		return null;
+		// for (Category cat : MODEL.getCategories()) {
+		// if (cat.getName().equals(category)) {
+		// return cat;
+		// }
+		// }
+		// return null;
+		Integer idx;
+
+		if ((idx = MODEL.getCgIdx().get(category)) != null)
+			return MODEL.getCategories().get(idx);
+		else
+			return null;
 	}
-	
+
 	/**
 	 * Search for Object
 	 * 
@@ -40,12 +51,18 @@ public class SearchController {
 	 * @param list
 	 * @return
 	 */
-	public CpsObject searchHolonObject(String object, ArrayList<CpsObject> list) {
-		for (CpsObject objects : list) {
-			if (objects.getObjName().equals(object))
-				return objects;
-		}
-		return null;
+	public CpsObject searchCategoryObject(Category category, String object) {
+		// for (CpsObject objects : list) {
+		// if (objects.getObjName().equals(object))
+		// return objects;
+		// }
+		// return null;
+		Integer idx;
+
+		if ((idx = category.getObjIdx().get(object)) != null)
+			return category.getObjects().get(idx);
+		else
+			return null;
 	}
 
 	/**
@@ -56,11 +73,17 @@ public class SearchController {
 	 * @return
 	 */
 	public CpsObject searchByID(int ID) {
-		for (CpsObject objects : MODEL.getObjectsOnCanvas()) {
-			if (objects.getID() == ID)
-				return objects;
-		}
-		return null;
+		// for (CpsObject objects : MODEL.getObjectsOnCanvas()) {
+		// if (objects.getID() == ID)
+		// return objects;
+		// }
+		// return null;
+		Integer idx;
+
+		if ((idx = MODEL.getCvsObjIdx().get(ID)) != null)
+			return MODEL.getObjectsOnCanvas().get(idx);
+		else
+			return null;
 	}
 
 	/**
@@ -81,4 +104,37 @@ public class SearchController {
 		return null;
 	}
 
+	/**
+	 * Decrement the Indices if a Key as been removed
+	 * 
+	 * @param key
+	 * @param map
+	 */
+	public <T> void decIdx(T key, HashMap<T, Integer> map) {
+
+		for (Entry<T, Integer> i : map.entrySet()) {
+			if (i.getValue() > map.get(key))
+				i.setValue(i.getValue() - 1);
+
+		}
+
+	}
+	
+	public static <T> ArrayList<T> copyArrayList(ArrayList<T> arr) {
+		ArrayList<T> newArr = new ArrayList<>();
+		for (T t : arr) {
+			newArr.add(t);
+		}
+		return newArr;
+	}
+	
+	public static <T,Integer> HashMap<T, Integer> copyHashMap(HashMap<T, Integer> map) {
+		
+		HashMap<T, Integer> newMap = new HashMap<>();
+		for (Entry<T, Integer> i : map.entrySet()) {
+			newMap.put(i.getKey(), i.getValue());
+		}
+		return newMap;
+	}
+
 }

+ 39 - 0
src/ui/model/Model.java

@@ -4,6 +4,7 @@ import Interfaces.CategoryListener;
 import Interfaces.ObjectListener;
 
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.LinkedList;
 import java.util.List;
 
@@ -34,11 +35,17 @@ public class Model {
 	 * categories ENERGY, BUILDINGS and COMPONENTS
 	 */
 	private ArrayList<Category> categories;
+
+
 	/*
 	 * Array of all CpsObjects in our canvas. It is set by default as an empty
 	 * list.
 	 */
 	private ArrayList<CpsObject> objectsOnCanvas;
+	
+	private HashMap<String, Integer> cgIdx;
+	private HashMap<Integer, Integer> cvsObjIdx;
+
 	/*
 	 * Array of all CpsObjects in our canvas. It is set by default as an empty
 	 * list.
@@ -57,11 +64,14 @@ public class Model {
 	 * default values.
 	 */
 	public Model() {
+		setCgIdx(new HashMap<>());
+		setCvsObjIdx(new HashMap<>());
 		setCategories(new ArrayList<Category>());
 		setObjectsOnCanvas(new ArrayList<CpsObject>());
 		setEdgesOnCanvas(new ArrayList<CpsEdge>());
 		setCategoryListeners(new LinkedList<CategoryListener>());
 		setObjectListeners(new LinkedList<ObjectListener>());
+
 	}
 
 	/**
@@ -277,4 +287,33 @@ public class Model {
 		return selectedEdge;
 	}
 
+	/**
+	 * @return the cgIdx
+	 */
+	public HashMap<String, Integer> getCgIdx() {
+		return cgIdx;
+	}
+
+	/**
+	 * @param cgIdx the cgIdx to set
+	 */
+	public void setCgIdx(HashMap<String, Integer> cgIdx) {
+		this.cgIdx = cgIdx;
+	}
+
+
+	/**
+	 * @return the cvsObjIdx
+	 */
+	public HashMap<Integer, Integer> getCvsObjIdx() {
+		return cvsObjIdx;
+	}
+
+	/**
+	 * @param cvsObjIdx the cvsObjIdx to set
+	 */
+	public void setCvsObjIdx(HashMap<Integer, Integer> cvsObjIdx) {
+		this.cvsObjIdx = cvsObjIdx;
+	}
+
 }

+ 3 - 0
src/ui/view/GUI.java

@@ -330,6 +330,7 @@ public class GUI<E> implements CategoryListener {
 					HolonElement ele = addElementPopUp.getElement();
 					controller.addElementCanvasObject(tempCpsObject.getID(), ele.getEleName(), ele.getAmount(),
 							ele.getEnergy());
+					System.out.println(((HolonObject)controller.searchCanvasObject(tempCpsObject.getID())).getEleIdx().size());
 					refreshTableHolonElement();
 					refreshTableProperties();
 				}
@@ -345,6 +346,8 @@ public class GUI<E> implements CategoryListener {
 					tempElement = getActualHolonElement(obj, yValueElements);
 					if (tempElement != null && obj.getClass() == HolonObject.class && obj.getID() != 0) {
 						controller.deleteElementCanvas(obj.getID(), tempElement.getEleName());
+						System.out.println(((HolonObject)controller.searchCanvasObject(obj.getID())).getEleIdx().size());
+
 						refreshTableHolonElement();
 						refreshTableProperties();
 					}