AbstractCpsObject.java 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. package classes;
  2. import com.google.gson.annotations.Expose;
  3. import java.awt.*;
  4. import java.util.ArrayList;
  5. /**
  6. * The abstract class "CpsObject" represents any possible object in the system
  7. * (except Edges). The representation of any object contains following
  8. * variables: see description of variables
  9. *
  10. * @author Gruppe14
  11. *
  12. */
  13. public abstract class AbstractCpsObject {
  14. /* Type of the Object. */
  15. @Expose
  16. String objName;
  17. /* Name given by the user. */
  18. @Expose
  19. String name;
  20. /* ID of the Obj. */
  21. @Expose
  22. int id;
  23. /* Path of the image for the Obj. */
  24. @Expose
  25. String image;
  26. /* Array of neighbors */
  27. ArrayList<CpsEdge> connections;
  28. /* Position with a X and Y value */
  29. @Expose
  30. Position position;
  31. /*
  32. * Energy input and output of each object in the grid Where the Object is
  33. * Stored
  34. */
  35. @Expose
  36. String sav;
  37. /* borderColor the user sets */
  38. @Expose
  39. Color borderColor = Color.WHITE;
  40. /* a Tag that can be used */
  41. ArrayList<Integer> tags;
  42. /* a Tag that can be used */
  43. ArrayList<Integer> pseudoTags;
  44. /* indicates wheter this object is used for the genetic Algorithm */
  45. public boolean useForGA = false;
  46. /**
  47. * Constructor for a CpsObejct with an unique ID.
  48. *
  49. * @param objName
  50. * of the Object
  51. */
  52. public AbstractCpsObject(String objName) {
  53. setObjName(objName);
  54. setName(objName);
  55. setImage("/Images/Dummy_House.png");
  56. tags = new ArrayList<>();
  57. pseudoTags = new ArrayList<>();
  58. //position = new Position(500, 500);
  59. }
  60. /**
  61. * Constructor for a new CpsObject with an unique ID (This constructor
  62. * correspond to the interaction between the Categories and Canvas)-->
  63. * actually the "new" Object is a copy.
  64. *
  65. * @param obj
  66. * Object to be copied
  67. */
  68. public AbstractCpsObject(AbstractCpsObject obj) {
  69. setObjName(obj.getObjName());
  70. setName(obj.getObjName());
  71. setConnections(new ArrayList<>());
  72. setPosition(new Position());
  73. setId(IdCounter.nextId());
  74. setImage(obj.getImage());
  75. tags = new ArrayList<>();
  76. pseudoTags = new ArrayList<>();
  77. }
  78. /**
  79. * Getter for the type of the Object.
  80. *
  81. * @return String
  82. */
  83. public String getObjName() {
  84. return objName;
  85. }
  86. /**
  87. * Set the type of Object.
  88. *
  89. * @param objName
  90. * String
  91. */
  92. public void setObjName(String objName) {
  93. this.objName = objName;
  94. }
  95. /**
  96. * Getter for the user-defined name (no unique).
  97. *
  98. * @return String
  99. */
  100. public String getName() {
  101. return name;
  102. }
  103. /**
  104. * Set the name.
  105. *
  106. * @param name
  107. * String
  108. */
  109. public void setName(String name) {
  110. this.name = name;
  111. }
  112. /**
  113. * Getter of the unique ID.
  114. *
  115. * @return int
  116. */
  117. public int getId() {
  118. return id;
  119. }
  120. /**
  121. * Set the ID to a new one.
  122. *
  123. * @param id
  124. * the iD to set
  125. */
  126. public void setId(int id) {
  127. this.id = id;
  128. }
  129. /**
  130. * Get the path of the image for the selected Object.
  131. *
  132. * @return String
  133. */
  134. public String getImage() {
  135. return image;
  136. }
  137. /**
  138. * Set the path of the image.
  139. *
  140. * @param image
  141. * the Image to set
  142. */
  143. public void setImage(String image) {
  144. this.image = image;
  145. }
  146. /**
  147. * List of all existing connections.
  148. *
  149. * @return the connections ArrayList
  150. */
  151. public ArrayList<CpsEdge> getConnections() {
  152. return connections;
  153. }
  154. /**
  155. * Set a new ArrayList of connections (Update).
  156. *
  157. * @param arrayList
  158. * the connections to set
  159. */
  160. public void setConnections(ArrayList<CpsEdge> arrayList) {
  161. this.connections = arrayList;
  162. }
  163. /**
  164. * Add a new connection to the selected Object.
  165. *
  166. * @param toConnect
  167. * Edge
  168. */
  169. public void addConnection(CpsEdge toConnect) {
  170. connections.add(toConnect);
  171. }
  172. /**
  173. * Set the position of the Object in the canvas.
  174. *
  175. * @param x
  176. * X-Coord
  177. * @param y
  178. * Y-Coord
  179. */
  180. public void setPosition(int x, int y) {
  181. setPosition(new Position(x, y));
  182. }
  183. /**
  184. * Get the actual position of the Object.
  185. *
  186. * @return Position Position of this Object
  187. */
  188. public Position getPosition() {
  189. return position;
  190. }
  191. /**
  192. * Set the position of the Object in the canvas.
  193. *
  194. * @param pos Coordinates
  195. */
  196. public void setPosition(Position pos) {
  197. this.position = pos;
  198. }
  199. /**
  200. * For save purpose.
  201. *
  202. * @return the stored
  203. */
  204. public String getSav() {
  205. return sav;
  206. }
  207. /**
  208. * For save purpose.
  209. *
  210. * @param sav
  211. * the stored to set
  212. */
  213. public void setSav(String sav) {
  214. this.sav = sav;
  215. }
  216. /**
  217. * Get the color of the border.
  218. *
  219. * @return the BorderColor
  220. */
  221. public Color getBorderColor() {
  222. return borderColor;
  223. }
  224. /**
  225. * Set the Border Color of this CpsObject.
  226. *
  227. * @param c
  228. * the BorderColor
  229. */
  230. public void setBorderColor(Color c) {
  231. this.borderColor = c;
  232. }
  233. /**
  234. * For internal purpose (energy flow).
  235. *
  236. * @param tag
  237. * for internal purpose
  238. */
  239. public void addTag(int tag) {
  240. if (!(tags.contains(tag))) {
  241. this.tags.add(tag);
  242. }
  243. }
  244. public void addAllTags(ArrayList<Integer> tags) {
  245. for (Integer tag : tags) {
  246. addTag(tag);
  247. }
  248. }
  249. /**
  250. * Get the actual tags.
  251. *
  252. * @return ArrayList
  253. */
  254. public ArrayList<Integer> getTag() {
  255. return tags;
  256. }
  257. /**
  258. * Rest the tags to Null.
  259. */
  260. public void resetTags() {
  261. this.tags = new ArrayList<>();
  262. this.pseudoTags = new ArrayList<>();
  263. }
  264. /**
  265. * For internal purpose (energy flow).
  266. *
  267. * @param tags
  268. * for internal purpose
  269. */
  270. public void setTags(ArrayList<Integer> tags) {
  271. this.tags = tags;
  272. }
  273. /**
  274. * Get the pseudo tags.
  275. *
  276. * @return ArrayList
  277. */
  278. public ArrayList<Integer> getPseudoTags() {
  279. return this.pseudoTags;
  280. }
  281. /**
  282. * For internal purpose (energy flow).
  283. *
  284. * @param tags for internal purpose
  285. */
  286. public void setPseudoTags(ArrayList<Integer> tags) {
  287. this.pseudoTags = tags;
  288. }
  289. /**
  290. * add a pseudo tag.
  291. *
  292. * @return ArrayList
  293. */
  294. public void addPseudoTag(int tag) {
  295. if (!pseudoTags.contains(tag)) {
  296. pseudoTags.add(tag);
  297. }
  298. }
  299. /**
  300. * adds all given pseudotags to the tags
  301. *
  302. * @param pseudoTags
  303. */
  304. public void addAllPseudoTags(ArrayList<Integer> pseudoTags) {
  305. for (Integer tag : pseudoTags) {
  306. addPseudoTag(tag);
  307. }
  308. }
  309. /**
  310. * adds All pseudoTags to tags without duplicates
  311. */
  312. public void recalculateTags() {
  313. for (Integer tag : pseudoTags) {
  314. if (!tags.contains(tag)) {
  315. tags.add(tag);
  316. }
  317. }
  318. }
  319. /**
  320. * needed for Genetic Algorithm
  321. * @return
  322. */
  323. public abstract AbstractCpsObject makeCopy();
  324. }