MultiPurposeController.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. * Search Edge between 2 Objects.
  114. *
  115. * @param a
  116. * ID of Object a
  117. * @param b
  118. * ID of Object b
  119. * @return The Edge
  120. */
  121. public CpsEdge searchEdge(int a, int b) {
  122. AbstractCpsObject objA = searchByID(a);
  123. AbstractCpsObject objB = searchByID(b);
  124. for (CpsEdge edge : model.getEdgesOnCanvas()) {
  125. // if (edge.getA().getObjName().equals(A.getObjName()) &&
  126. // (edge.getB().getObjName().equals(B.getObjName()))
  127. // || edge.getB().getObjName().equals(A.getObjName())
  128. // && (edge.getA().getObjName().equals(B.getObjName())))
  129. if ((edge.getA().equals(objA) && edge.getB().equals(objB))
  130. || (edge.getB().equals(objA)) && edge.getA().equals(objB))
  131. return edge;
  132. }
  133. return null;
  134. }
  135. /**
  136. * Decrement the Indices if a Key as been removed.
  137. *
  138. * @param key
  139. * the Key
  140. * @param <T>
  141. * key type
  142. * @param map
  143. * the Map
  144. */
  145. public <T> void decIdx(T key, HashMap<T, Integer> map) {
  146. for (Entry<T, Integer> i : map.entrySet()) {
  147. if (i.getValue() > map.get(key))
  148. i.setValue(i.getValue() - 1);
  149. }
  150. }
  151. /**
  152. * Adjust Indices before porting them from one map to another
  153. *
  154. * @param key
  155. * the Key
  156. * @param <T>
  157. * key type
  158. * @param map
  159. * the Map
  160. */
  161. public <T> void adjustIdx(int x, HashMap<T, Integer> map) {
  162. for (Entry<T, Integer> i : map.entrySet()) {
  163. i.setValue(i.getValue() + x + 1);
  164. }
  165. }
  166. /**
  167. * Returns the highest Id
  168. * @param map
  169. * @return
  170. */
  171. public <T> int getHighestIdx(HashMap<T, Integer> map) {
  172. int max = 0;
  173. for (T i : map.keySet()) {
  174. if (map.get(i) > max)
  175. max = map.get(i);
  176. }
  177. return max;
  178. }
  179. /**
  180. * Copies a HashMap into a new One.
  181. *
  182. * @param map
  183. * the HashMap
  184. * @param <T>
  185. * type
  186. * @return Copy of the HashMap
  187. */
  188. public static <T, Integer> HashMap<T, Integer> copyHashMap(HashMap<T, Integer> map) {
  189. HashMap<T, Integer> newMap = new HashMap<>();
  190. for (Entry<T, Integer> i : map.entrySet()) {
  191. newMap.put(i.getKey(), i.getValue());
  192. }
  193. return newMap;
  194. }
  195. /**
  196. * Set the Algorithm.
  197. *
  198. * @param obj
  199. * the Algorithm
  200. */
  201. public void setAlgorithm(Object obj) {
  202. model.setAlgorithm(obj);
  203. }
  204. }