GraphHelper.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. package de.tu_darmstadt.informatik.tk.scopviz.graphs;
  2. import java.util.Collection;
  3. import java.util.HashMap;
  4. import java.util.Iterator;
  5. import java.util.Random;
  6. import org.graphstream.graph.Element;
  7. import org.graphstream.graph.Node;
  8. import org.graphstream.ui.geom.Point3;
  9. import org.graphstream.ui.graphicGraph.GraphPosLengthUtils;
  10. import de.tu_darmstadt.informatik.tk.scopviz.debug.Debug;
  11. import de.tu_darmstadt.informatik.tk.scopviz.main.Layer;
  12. import de.tu_darmstadt.informatik.tk.scopviz.main.Main;
  13. import de.tu_darmstadt.informatik.tk.scopviz.ui.OptionsManager;
  14. public class GraphHelper {
  15. public static MyGraph newMerge(boolean vertical, MyGraph... sources) {
  16. String newID = "Composite";
  17. for (MyGraph g : sources) {
  18. newID = newID.concat("_" + g.getId());
  19. }
  20. MyGraph result = new MyGraph(newID);
  21. for (MyGraph g : sources) {
  22. result.addSubGraph(g);
  23. mergeInto(result, g);
  24. }
  25. return result;
  26. }
  27. // TODO better way to do scaling
  28. private static void mergeInto(MyGraph target, MyGraph source) {
  29. double targetMinX = target.getMinX();
  30. double targetMaxX = target.getMaxX();
  31. double sourceMinX = source.getMinX();
  32. double sourceMaxX = source.getMaxX();
  33. double scalingFactorX = ((targetMaxX - targetMinX + 1) / (target.getNodeCount() + 1))
  34. / ((sourceMaxX - sourceMinX + 1) / (source.getNodeCount() + 1));
  35. double xOffset = targetMaxX - sourceMinX + 10;
  36. double targetMinY = target.getMinY();
  37. double sourceMinY = source.getMinY();
  38. double scalingFactorY = scalingFactorX;
  39. // adjust Cordinates and Attributes
  40. adjustSourceXCoordinates(source, scalingFactorX, xOffset, sourceMinX);
  41. graphAttribute(target);
  42. graphAttribute(source);
  43. adjustSourceYCoordinates(source, scalingFactorY, targetMinY, sourceMinY);
  44. // Copy Nodes and Edges
  45. HashMap<String, String> newIds = copyNodes(target, source);
  46. copyEdges(target, source, newIds);
  47. }
  48. private static void copyEdges(MyGraph target, MyGraph source, HashMap<String, String> newIds) {
  49. Random ran = new Random();
  50. boolean searchingForId = true;
  51. for (MyEdge e : source.<MyEdge>getEdgeSet()) {
  52. // finding a new ID for the Node
  53. searchingForId = true;
  54. String newId = source.getId() + e.getId();
  55. while (searchingForId) {
  56. if (target.getEdge(newId) == null) {
  57. searchingForId = false;
  58. target.addEdge(newId, newIds.get(e.getSourceNode().getId()), newIds.get(e.getTargetNode().getId()),
  59. e.isDirected());
  60. if (e.getAttribute("originalElement") == null) {
  61. target.getEdge(newId).addAttribute("originalElement", source.getId().concat("+#" + e.getId()));
  62. } else {
  63. target.getEdge(newId).addAttribute("originalElement", e.getAttribute("originalElement"));
  64. }
  65. } else {
  66. newId = newId.concat(String.valueOf((char) (ran.nextInt(52) + 'a')));
  67. }
  68. }
  69. for (String s : e.getAttributeKeySet()) {
  70. target.getEdge(newId).addAttribute(s, (Object) e.getAttribute(s));
  71. }
  72. }
  73. }
  74. private static HashMap<String, String> copyNodes(MyGraph target, MyGraph source) {
  75. HashMap<String, String> newIds = new HashMap<>();
  76. Random ran = new Random();
  77. boolean searchingForId = true;
  78. for (MyNode n : source.<MyNode>getNodeSet()) {
  79. // finding a new ID for the Node
  80. searchingForId = true;
  81. String newId = source.getId() + n.getId();
  82. while (searchingForId) {
  83. if (target.getNode(newId) == null) {
  84. searchingForId = false;
  85. target.addNode(newId);
  86. newIds.put(n.getId(), newId);
  87. if (n.getAttribute("originalElement") == null) {
  88. target.getNode(newId).addAttribute("originalElement", source.getId().concat("+#" + n.getId()));
  89. } else {
  90. target.getNode(newId).addAttribute("originalElement", n.getAttribute("originalElement"));
  91. }
  92. } else {
  93. newId = newId.concat(String.valueOf((char) (ran.nextInt(52) + 'a')));
  94. }
  95. }
  96. for (String s : n.getAttributeKeySet()) {
  97. Debug.out(s);
  98. target.getNode(newId).addAttribute(s, (Object) n.getAttribute(s));
  99. }
  100. }
  101. return newIds;
  102. }
  103. private static void adjustSourceYCoordinates(MyGraph g, double scalingFactor, double targetMinY,
  104. double sourceMinY) {
  105. for (MyNode n : g.<MyNode>getNodeSet()) {
  106. double d = (Double) n.getAttribute("y");
  107. d = d - sourceMinY;
  108. d = d * scalingFactor;
  109. d = d + targetMinY;
  110. n.addAttribute("y", d);
  111. }
  112. }
  113. private static void graphAttribute(MyGraph g) {
  114. for (MyNode n : g.<MyNode>getNodeSet()) {
  115. if (n.getAttribute("originalGraph") == null) {
  116. n.addAttribute("originalGraph", g.getId());
  117. }
  118. }
  119. }
  120. private static void adjustSourceXCoordinates(MyGraph g, Double scalingFactor, Double xOffset, Double SourceMinX) {
  121. for (MyNode n : g.<MyNode>getNodeSet()) {
  122. Double d = (Double) n.getAttribute("x");
  123. d = d - SourceMinX;
  124. d = d * scalingFactor;
  125. d = d + xOffset;
  126. d = d + SourceMinX;
  127. n.addAttribute("x", d);
  128. }
  129. }
  130. /**
  131. * Converts the Coordinates of all Nodes into a saveable and uniform Format.
  132. */
  133. public static void correctCoordinates(MyGraph g) {
  134. Point3 coords;
  135. MyNode n = null;
  136. Iterator<MyNode> allNodes = g.getNodeIterator();
  137. while (allNodes.hasNext()) {
  138. n = allNodes.next();
  139. if (n.hasAttribute("xyz")) {
  140. coords = GraphPosLengthUtils.nodePointPosition(n);
  141. n.setAttribute("x", coords.x);
  142. propagateAttribute(g, n, "x", coords.x);
  143. n.setAttribute("y", coords.y);
  144. propagateAttribute(g, n, "y", coords.y);
  145. n.removeAttribute("xyz");
  146. }
  147. }
  148. }
  149. /**
  150. * Converts the weight property into a label to display on the Graph.
  151. * Removes all labels if that option is set
  152. */
  153. public static void handleEdgeWeight(MyGraph g) {
  154. if (!Layer.UNDERLAY.equals(g.getAttribute("layer"))) {
  155. return;
  156. }
  157. MyEdge e = null;
  158. Iterator<MyEdge> allEdges = g.getEdgeIterator();
  159. while (allEdges.hasNext()) {
  160. e = allEdges.next();
  161. if (!e.hasAttribute("weight")) {
  162. e.addAttribute("weight", OptionsManager.getDefaultWeight());
  163. }
  164. if (OptionsManager.isWeightShown()) {
  165. e.setAttribute("ui.label", e.getAttribute("weight").toString());
  166. } else {
  167. e.removeAttribute("ui.label");
  168. }
  169. }
  170. }
  171. /**
  172. * adds default to all Nodes and converts yEd attributes to regular ones.
  173. *
  174. * @param g
  175. * the graph that the attributes will be added onto
  176. */
  177. public static void setAllDefaults(MyGraph g) {
  178. for (Node n : g.getNodeSet()) {
  179. // general defaults
  180. if (!n.hasAttribute("ui.label")) {
  181. n.addAttribute("ui.label", "");
  182. }
  183. if (!n.hasAttribute("typeofNode") || n.getAttribute("typeofNode").equals("")) {
  184. n.addAttribute("typeofNode", "standard");
  185. }
  186. // underlay defaults
  187. if (Layer.UNDERLAY.equals(g.getAttribute("layer"))) {
  188. if (!n.hasAttribute("typeofDevice") || n.getAttribute("typeofDevice").equals("")) {
  189. n.addAttribute("typeofDevice", "unknown");
  190. }
  191. if (!n.hasAttribute("lat") || n.getAttribute("long").equals("")) {
  192. n.addAttribute("lat", OptionsManager.getDefaultLat());
  193. }
  194. if (!n.hasAttribute("long") || n.getAttribute("long").equals("")) {
  195. n.addAttribute("long", OptionsManager.getDefaultLong());
  196. }
  197. if (!n.hasAttribute("process-max") || n.getAttribute("process-max").equals("")) {
  198. n.addAttribute("process-max", 0.0);
  199. }
  200. }
  201. // operator defaults
  202. if (Layer.OPERATOR.equals(g.getAttribute("layer"))) {
  203. if (!n.hasAttribute("process-need") || n.getAttribute("process-need").equals("")) {
  204. n.addAttribute("process-need", 0.0);
  205. }
  206. }
  207. }
  208. }
  209. public static void propagateAttribute(MyGraph g, Element n, String attribute, Object value) {
  210. if (n.getAttribute("originalElement") == null) {
  211. Debug.out("Debug: Attribute originalElement does not Exist");
  212. return;
  213. }
  214. String origGraph = n.getAttribute("originalElement").toString().split("\\+#")[0];
  215. String origNode = n.getAttribute("originalElement").toString().split("\\+#")[1];
  216. MyNode oldNode = null;
  217. MyEdge oldEdge = null;
  218. MyGraph old = null;
  219. Iterator<MyGraph> graphIter = g.getAllSubGraphs().iterator();
  220. while (graphIter.hasNext()) {
  221. old = graphIter.next();
  222. if (old.getId().equals(origGraph)) {
  223. Iterator<MyNode> nodeIter = old.getNodeIterator();
  224. while (nodeIter.hasNext()) {
  225. oldNode = nodeIter.next();
  226. if (oldNode.getId().equals(origNode)) {
  227. if (value == null) {
  228. oldNode.removeAttribute(attribute);
  229. } else {
  230. oldNode.addAttribute(attribute, value);
  231. }
  232. Debug.out("Debug: propagating successfull");
  233. return;
  234. }
  235. }
  236. Iterator<MyEdge> edgeIter = old.getEdgeIterator();
  237. while (edgeIter.hasNext()) {
  238. oldEdge = edgeIter.next();
  239. if (oldEdge.getId().equals(origNode)) {
  240. if (value == null) {
  241. oldEdge.removeAttribute(attribute);
  242. } else {
  243. oldEdge.addAttribute(attribute, value);
  244. }
  245. Debug.out("Debug: propagating successfull");
  246. return;
  247. }
  248. }
  249. Debug.out("WARNING: could not find the specified Element " + origNode + " in the Graph " + origGraph,
  250. 2);
  251. return;
  252. }
  253. }
  254. Debug.out("WARNING: could not find the specified Graph " + origGraph, 2);
  255. }
  256. public static String propagateElementDeletion(MyGraph g, Collection<? extends Element> col) {
  257. Iterator<? extends Element> elementIter = col.iterator();
  258. while (elementIter.hasNext()) {
  259. Element e = elementIter.next();
  260. return propagateElementDeletion(g, e);
  261. }
  262. return null;
  263. }
  264. public static String propagateElementDeletion(MyGraph g, Element e) {
  265. if (e.getAttribute("originalElement") == null) {
  266. return null;
  267. }
  268. String origGraph = e.getAttribute("originalElement").toString().split("\\+#")[0];
  269. String origId = e.getAttribute("originalElement").toString().split("\\+#")[1];
  270. Iterator<MyGraph> graphIter = g.getAllSubGraphs().iterator();
  271. while (graphIter.hasNext()) {
  272. MyGraph temp = graphIter.next();
  273. if (temp.getId().equals(origGraph)) {
  274. if (e instanceof MyNode && temp.getNode(origId) != null) {
  275. temp.removeNode(origId);
  276. return temp.getId();
  277. } else if (e instanceof MyEdge && temp.getEdge(origId) != null) {
  278. temp.removeEdge(origId);
  279. return temp.getId();
  280. } else {
  281. Debug.out("INFORMATION: could not Delete Element bećause it didn't exist: " + origGraph + ":"
  282. + origId, 1);
  283. }
  284. return null;
  285. }
  286. }
  287. Debug.out("WARNING: could not find the specified Graph " + origGraph, 2);
  288. return null;
  289. }
  290. public static String propagateElementUndeletion(MyGraph g, Element e, String newNodeId) {
  291. if (e.getAttribute("originalElement") == null) {
  292. return null;
  293. }
  294. String origGraph = e.getAttribute("originalElement").toString().split("\\+#")[0];
  295. // String origId =
  296. // e.getAttribute("originalElement").toString().split("\\+#")[1];
  297. Iterator<MyGraph> graphIter = g.getAllSubGraphs().iterator();
  298. HashMap<String, Object> attributes = new HashMap<String, Object>();
  299. for (String s : e.getAttributeKeySet()) {
  300. attributes.put(s, e.getAttribute(s));
  301. }
  302. while (graphIter.hasNext()) {
  303. MyGraph temp = graphIter.next();
  304. if (temp.getId().equals(origGraph)) {
  305. String newId = Main.getInstance().getUnusedID(new GraphManager(temp));
  306. if (e instanceof MyNode) {
  307. temp.addNode(newId);
  308. temp.getNode(newId).addAttributes(attributes);
  309. return temp.getId() + "+#" + newId;// the id of
  310. // Graph+newNode
  311. } else if (e instanceof MyEdge) {
  312. MyEdge ed = (MyEdge) e;
  313. String sourceId = ed.getSourceNode().getAttribute("originalElement").toString()
  314. .split("\\+#")[newNodeId.split("\\+#").length - 1];
  315. String targetId = ed.getTargetNode().getAttribute("originalElement").toString()
  316. .split("\\+#")[newNodeId.split("\\+#").length - 1];
  317. if (temp.getNode(sourceId) == null) {
  318. sourceId = newNodeId.split("\\+#")[newNodeId.split("\\+#").length - 1];
  319. } else {
  320. targetId = newNodeId.split("\\+#")[newNodeId.split("\\+#").length - 1];
  321. }
  322. temp.addEdge(newId, sourceId, targetId, ed.isDirected());
  323. temp.getEdge(newId).addAttributes(attributes);
  324. return temp.getId() + "+#" + newId;// the id of
  325. // graph+newEdge
  326. }
  327. }
  328. }
  329. Debug.out("WARNING: could not find the specified Graph " + origGraph, 2);
  330. return null;
  331. }
  332. public static void resetUndelete() {
  333. }
  334. }