MultiPurposeController.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. package ui.controller;
  2. import java.util.HashMap;
  3. import java.util.Map.Entry;
  4. import classes.Category;
  5. import classes.CpsEdge;
  6. import classes.CpsUpperNode;
  7. import classes.AbstractCpsObject;
  8. import classes.HolonElement;
  9. import classes.HolonObject;
  10. import ui.model.Model;
  11. /**
  12. * Controller for Multiple Purposes.
  13. *
  14. * @author Gruppe14
  15. */
  16. public class MultiPurposeController {
  17. private Model model;
  18. /**
  19. * Constructor.
  20. *
  21. * @param model
  22. * Model
  23. */
  24. public MultiPurposeController(Model model) {
  25. this.model = model;
  26. }
  27. /**
  28. * search for category.
  29. *
  30. * @param category
  31. * name of the Category
  32. * @return the Category
  33. */
  34. public Category searchCat(String category) {
  35. Integer idx;
  36. if ((idx = model.getCgIdx().get(category)) == null || model.getCgIdx().size() < 1)
  37. return null;
  38. else
  39. return model.getCategories().get(idx);
  40. }
  41. /**
  42. * Search for Object in a Category.
  43. *
  44. * @param category
  45. * name of the Category
  46. * @param object
  47. * Name of the Object
  48. * @return The Object
  49. */
  50. public AbstractCpsObject searchCatObj(Category category, String object) {
  51. Integer idx;
  52. if ((idx = category.getObjIdx().get(object)) == null || category.getObjIdx().size() < 1)
  53. return null;
  54. else
  55. return category.getObjects().get(idx);
  56. }
  57. /**
  58. * Search for Object by ID.
  59. *
  60. * @param id
  61. * the ID of the Object
  62. * @return the CpsObject
  63. */
  64. public AbstractCpsObject searchByID(int id) {
  65. Integer idx;
  66. if ((idx = model.getCvsObjIdx().get(id)) == null || model.getCvsObjIdx().size() < 1)
  67. return null;
  68. else
  69. return model.getObjectsOnCanvas().get(idx);
  70. }
  71. /**
  72. * Search for Element.
  73. *
  74. * @param object
  75. * the Holon Object
  76. * @param element
  77. * name of the Element
  78. * @return the Holon Element
  79. */
  80. public HolonElement searchEle(HolonObject object, String element) {
  81. Integer idx;
  82. if ((idx = object.getEleIdx().get(element)) == null || object.getEleIdx().size() < 1)
  83. return null;
  84. else
  85. return object.getElements().get(idx);
  86. }
  87. /**
  88. * Search the Element by ID.
  89. *
  90. * @param object
  91. * the Holon Object
  92. * @param idEle
  93. * the Element ID
  94. * @return The Holon Element
  95. */
  96. public HolonElement searchEleById(HolonObject object, int idEle) {
  97. return object.searchElementById(idEle);
  98. }
  99. /**
  100. *
  101. * @param upperNode
  102. * @param id
  103. * @return
  104. */
  105. public AbstractCpsObject searchByIDUpperNode(int id, CpsUpperNode upperNode) {
  106. Integer idx;
  107. if ((idx = upperNode.getNodesIdx().get(id)) == null || upperNode.getNodesIdx().size() < 1)
  108. return null;
  109. else
  110. return upperNode.getNodes().get(idx);
  111. }
  112. /**
  113. *
  114. * @param id
  115. * @return
  116. */
  117. public AbstractCpsObject searchTracked(int id) {
  118. for (AbstractCpsObject cps : model.getTrackingObj()) {
  119. if (cps.getId() == id)
  120. return cps;
  121. }
  122. return null;
  123. }
  124. /**
  125. * Search Edge between 2 Objects.
  126. *
  127. * @param a
  128. * ID of Object a
  129. * @param b
  130. * ID of Object b
  131. * @return The Edge
  132. */
  133. public CpsEdge searchEdge(int a, int b) {
  134. AbstractCpsObject objA = searchByID(a);
  135. AbstractCpsObject objB = searchByID(b);
  136. for (CpsEdge edge : model.getEdgesOnCanvas()) {
  137. // if (edge.getA().getObjName().equals(A.getObjName()) &&
  138. // (edge.getB().getObjName().equals(B.getObjName()))
  139. // || edge.getB().getObjName().equals(A.getObjName())
  140. // && (edge.getA().getObjName().equals(B.getObjName())))
  141. if ((edge.getA().equals(objA) && edge.getB().equals(objB))
  142. || (edge.getB().equals(objA)) && edge.getA().equals(objB))
  143. return edge;
  144. }
  145. return null;
  146. }
  147. /**
  148. * Decrement the Indices if a Key as been removed.
  149. *
  150. * @param key
  151. * the Key
  152. * @param <T>
  153. * key type
  154. * @param map
  155. * the Map
  156. */
  157. public <T> void decIdx(T key, HashMap<T, Integer> map) {
  158. for (Entry<T, Integer> i : map.entrySet()) {
  159. if (i.getValue() > map.get(key))
  160. i.setValue(i.getValue() - 1);
  161. }
  162. }
  163. /**
  164. * Adjust Indices before porting them from one map to another
  165. *
  166. * @param key
  167. * the Key
  168. * @param <T>
  169. * key type
  170. * @param map
  171. * the Map
  172. */
  173. public <T> void adjustIdx(int x, HashMap<T, Integer> map) {
  174. for (Entry<T, Integer> i : map.entrySet()) {
  175. i.setValue(i.getValue() + x + 1);
  176. }
  177. }
  178. /**
  179. * Returns the highest Id
  180. *
  181. * @param map
  182. * @return
  183. */
  184. public <T> int getHighestIdx(HashMap<T, Integer> map) {
  185. int max = 0;
  186. for (T i : map.keySet()) {
  187. if (map.get(i) > max)
  188. max = map.get(i);
  189. }
  190. return max;
  191. }
  192. /**
  193. * Copies a HashMap into a new One.
  194. *
  195. * @param map
  196. * the HashMap
  197. * @param <T>
  198. * type
  199. * @return Copy of the HashMap
  200. */
  201. public static <T, Integer> HashMap<T, Integer> copyHashMap(HashMap<T, Integer> map) {
  202. HashMap<T, Integer> newMap = new HashMap<>();
  203. for (Entry<T, Integer> i : map.entrySet()) {
  204. newMap.put(i.getKey(), i.getValue());
  205. }
  206. return newMap;
  207. }
  208. /**
  209. * Set the Algorithm.
  210. *
  211. * @param obj
  212. * the Algorithm
  213. */
  214. public void setAlgorithm(Object obj) {
  215. model.setAlgorithm(obj);
  216. }
  217. }