MultiPurposeController.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. *
  73. * @param upperNode
  74. * @param id
  75. * @return
  76. */
  77. public AbstractCpsObject searchByIDUpperNode(int id, CpsUpperNode upperNode) {
  78. Integer idx;
  79. if ((idx = upperNode.getNodesIdx().get(id)) == null || upperNode.getNodesIdx().size() < 1)
  80. return null;
  81. else
  82. return upperNode.getNodes().get(idx);
  83. }
  84. /**
  85. * Search the Element by ID.
  86. *
  87. * @param object
  88. * the Holon Object
  89. * @param idEle
  90. * the Element ID
  91. * @return The Holon Element
  92. */
  93. public HolonElement searchEleById(HolonObject object, int idEle) {
  94. return object.searchElementById(idEle);
  95. }
  96. /**
  97. *
  98. * @param id
  99. * @return
  100. */
  101. public AbstractCpsObject searchTracked(int id) {
  102. for (AbstractCpsObject cps : model.getTrackingObj()) {
  103. if (cps.getId() == id)
  104. return cps;
  105. }
  106. return null;
  107. }
  108. /**
  109. * Search Edge between 2 Objects.
  110. *
  111. * @param a
  112. * ID of Object a
  113. * @param b
  114. * ID of Object b
  115. * @return The Edge
  116. */
  117. public CpsEdge searchEdge(int a, int b) {
  118. AbstractCpsObject objA = searchByID(a);
  119. AbstractCpsObject objB = searchByID(b);
  120. for (CpsEdge edge : model.getEdgesOnCanvas()) {
  121. // if (edge.getA().getObjName().equals(A.getObjName()) &&
  122. // (edge.getB().getObjName().equals(B.getObjName()))
  123. // || edge.getB().getObjName().equals(A.getObjName())
  124. // && (edge.getA().getObjName().equals(B.getObjName())))
  125. if ((edge.getA().equals(objA) && edge.getB().equals(objB))
  126. || (edge.getB().equals(objA)) && edge.getA().equals(objB))
  127. return edge;
  128. }
  129. return null;
  130. }
  131. /**
  132. * Decrement the Indices if a Key as been removed.
  133. *
  134. * @param key
  135. * the Key
  136. * @param <T>
  137. * key type
  138. * @param map
  139. * the Map
  140. */
  141. public <T> void decIdx(T key, HashMap<T, Integer> map) {
  142. for (Entry<T, Integer> i : map.entrySet()) {
  143. if (i.getValue() > map.get(key))
  144. i.setValue(i.getValue() - 1);
  145. }
  146. }
  147. /**
  148. * Adjust Indices before porting them from one map to another
  149. *
  150. * @param key
  151. * the Key
  152. * @param <T>
  153. * key type
  154. * @param map
  155. * the Map
  156. */
  157. public <T> void adjustIdx(int x, HashMap<T, Integer> map) {
  158. for (Entry<T, Integer> i : map.entrySet()) {
  159. i.setValue(i.getValue() + x + 1);
  160. }
  161. }
  162. /**
  163. * Returns the highest Id
  164. *
  165. * @param map
  166. * @return
  167. */
  168. public <T> int getHighestIdx(HashMap<T, Integer> map) {
  169. int max = 0;
  170. for (T i : map.keySet()) {
  171. if (map.get(i) > max)
  172. max = map.get(i);
  173. }
  174. return max;
  175. }
  176. /**
  177. * Copies a HashMap into a new One.
  178. *
  179. * @param map
  180. * the HashMap
  181. * @param <T>
  182. * type
  183. * @return Copy of the HashMap
  184. */
  185. public static <T, Integer> HashMap<T, Integer> copyHashMap(HashMap<T, Integer> map) {
  186. HashMap<T, Integer> newMap = new HashMap<>();
  187. for (Entry<T, Integer> i : map.entrySet()) {
  188. newMap.put(i.getKey(), i.getValue());
  189. }
  190. return newMap;
  191. }
  192. /**
  193. * Set the Algorithm.
  194. *
  195. * @param obj
  196. * the Algorithm
  197. */
  198. public void setAlgorithm(Object obj) {
  199. model.setAlgorithm(obj);
  200. }
  201. }