Skip to main content
summaryrefslogtreecommitdiffstats
blob: dbb1d4743f7037749a431ccad0d356393d156eaa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
/*******************************************************************************
 * Copyright (c) 2011, 2012, 2013 Red Hat, Inc.
 * All rights reserved.
 * This program is made available under the terms of the
 * Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 * 	Red Hat, Inc. - initial API and implementation
 ******************************************************************************/
package org.eclipse.bpmn2.modeler.core.features;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

import org.eclipse.bpmn2.modeler.core.utils.AnchorUtil;
import org.eclipse.bpmn2.modeler.core.utils.AnchorUtil.BoundaryAnchor;
import org.eclipse.bpmn2.modeler.core.utils.BusinessObjectUtil;
import org.eclipse.bpmn2.modeler.core.utils.GraphicsUtil;
import org.eclipse.bpmn2.modeler.core.utils.ModelUtil;
import org.eclipse.graphiti.mm.algorithms.styles.Point;
import org.eclipse.graphiti.mm.pictograms.Anchor;
import org.eclipse.graphiti.mm.pictograms.Connection;
import org.eclipse.graphiti.mm.pictograms.FreeFormConnection;
import org.eclipse.graphiti.mm.pictograms.Shape;

/**
 * The Class ConnectionRoute.
 */
public class ConnectionRoute implements Comparable<ConnectionRoute>, Comparator<ConnectionRoute> {
		
		/**
		 * Records a collision of a line segment with a shape.
		 */
		class Collision {
			
			/** The shape. */
			Shape shape;
			/** The line segment start point. */
			Point start;
			/** The line segment end point. */
			Point end;
			
			/**
			 * Instantiates a new collision.
			 *
			 * @param shape the collision shape
			 * @param start the line segment start point
			 * @param end the line segment end point
			 */
			public Collision(Shape shape, Point start, Point end) {
				this.shape = shape;
				this.start = start;
				this.end = end;
			}
			
			/* (non-Javadoc)
			 * @see java.lang.Object#toString()
			 */
			public String toString() {
				Object o = BusinessObjectUtil.getFirstBaseElement(shape);
				return ModelUtil.getTextValue(o);
			}
		}
		
		/**
		 * Records the crossing of a line segment with an existing connection.
		 */
		class Crossing {
			
			/** The connection. */
			Connection connection;
			/** The line segment start point. */
			Point start;
			/** The line segment end point. */
			Point end;
			
			/**
			 * Instantiates a new crossing.
			 *
			 * @param connection the crossed connection
			 * @param start the line segment start point
			 * @param end the line segment end point
			 */
			public Crossing(Connection connection, Point start, Point end) {
				this.connection = connection;
				this.start = start;
				this.end = end;
			}
			
			/* (non-Javadoc)
			 * @see java.lang.Object#toString()
			 */
			public String toString() {
				Object o = BusinessObjectUtil.getFirstBaseElement(connection);
				return ModelUtil.getTextValue(o);
			}
		}
		
		/** The router. */
		DefaultConnectionRouter router;
		/** The route id. */
		int id;
		private List<Point> points = new ArrayList<Point>();
		
		/** The list of shape collisions. */
		List<Collision> collisions = new ArrayList<Collision>();
		
		/** The list of connection crossings. */
		List<Crossing> crossings = new ArrayList<Crossing>();
		
		/** The source shape of the route being calculated. */
		Shape source;
		
		/** The target shape of the route being calculated. */
		Shape target;
		
		boolean valid = true;
		private int rank = 0;
		
		/**
		 * Instantiates a new connection route.
		 *
		 * @param router the router
		 * @param id the id
		 * @param source the source
		 * @param target the target
		 */
		public ConnectionRoute(DefaultConnectionRouter router, int id, Shape source, Shape target) {
			this.router = router;
			this.id = id;
			this.source = source;
			this.target = target;
		}

		/**
		 * Apply.
		 *
		 * @param ffc the ffc
		 */
		public void apply(FreeFormConnection ffc) {
			apply(ffc,null,null);
		}
		
		/**
		 * Apply.
		 *
		 * @param ffc the ffc
		 * @param sourceAnchor the source anchor
		 * @param targetAnchor the target anchor
		 */
		public void apply(FreeFormConnection ffc, Anchor sourceAnchor, Anchor targetAnchor) {
			
			// set connection's source and target anchors if they are Boundary Anchors
			if (sourceAnchor==null) {
				BoundaryAnchor ba = AnchorUtil.findNearestBoundaryAnchor(source, this.get(0));
				sourceAnchor = ba.anchor;
				ffc.setStart(sourceAnchor);
			}
			
			if (targetAnchor==null) {
				// NOTE: a route with only a starting point indicates that it could not be calculated.
				// In this case, make the connection a straight line from source to target.
				Point p = this.get(this.size() - 1);
				BoundaryAnchor ba = AnchorUtil.findNearestBoundaryAnchor(target, p);
				targetAnchor = ba.anchor;
				ffc.setEnd(targetAnchor);
			}
			
			// add the bendpoints
			ffc.getBendpoints().clear();
			for (int i=1; i<this.size()-1; ++i) {
				ffc.getBendpoints().add(this.get(i));
			}
		}
		
		/* (non-Javadoc)
		 * @see java.lang.Object#toString()
		 */
		public String toString() {
			String text;
			if (isValid()) {
				BoundaryAnchor sa = AnchorUtil.findNearestBoundaryAnchor(source, get(0));
				BoundaryAnchor ta = AnchorUtil.findNearestBoundaryAnchor(target, get(size()-1));
				text = id+": length="+getLength()+" points="+getPoints().size()+ //$NON-NLS-1$ //$NON-NLS-2$
						" source="+sa.locationType+" target="+ta.locationType; //$NON-NLS-1$ //$NON-NLS-2$
				if (collisions.size()>0) {
					text += " collisions="; //$NON-NLS-1$
					Iterator<Collision> iter=collisions.iterator();
					while (iter.hasNext()) {
						Collision c = iter.next();
						text += "'" + c.toString() + "'"; //$NON-NLS-1$ //$NON-NLS-2$
						if (iter.hasNext())
							text += ", "; //$NON-NLS-1$
					}
				}
				if (crossings.size()>0) {
					text += " crossings="; //$NON-NLS-1$
					Iterator<Crossing> iter=crossings.iterator();
					while (iter.hasNext()) {
						Crossing c = iter.next();
						text += "'" + c.toString() + "'"; //$NON-NLS-1$ //$NON-NLS-2$
						if (iter.hasNext())
							text += ", "; //$NON-NLS-1$
					}
				}
			}
			else
				text = "not valid"; //$NON-NLS-1$
			return text;
		}
		
		/**
		 * Adds the.
		 *
		 * @param newPoint the new point
		 * @return true, if successful
		 */
		public boolean add(Point newPoint) {
			for (Point p : getPoints()) {
				if (GraphicsUtil.pointsEqual(newPoint, p)) {
					valid = false;
					return false;
				}
			}
			getPoints().add(newPoint);
			return true;
		}
		
		/**
		 * Gets the.
		 *
		 * @param index the index
		 * @return the point
		 */
		public Point get(int index) {
			return getPoints().get(index);
		}
		
		/**
		 * Size.
		 *
		 * @return the int
		 */
		public int size() {
			return getPoints().size();
		}
		
		/**
		 * Adds the collision.
		 *
		 * @param shape the shape
		 * @param start the start
		 * @param end the end
		 */
		public void addCollision(Shape shape, Point start, Point end) {
			collisions.add( new Collision(shape, start, end) );
		}
		
		/**
		 * Adds the crossing.
		 *
		 * @param connection the connection
		 * @param start the start
		 * @param end the end
		 */
		public void addCrossing(Connection connection, Point start, Point end) {
			crossings.add( new Crossing(connection, start, end) );
		}
		
		/**
		 * Sets the valid.
		 */
		public void setValid() {
			valid = true;
		}
		
		/**
		 * Checks if is valid.
		 *
		 * @return true, if is valid
		 */
		public boolean isValid() {
			if (valid)
				return getLength() < Integer.MAX_VALUE;
			return false;
		}
		
		/**
		 * Gets the length.
		 *
		 * @return the length
		 */
		public int getLength() {
			int length = 0;
			if (getPoints().size()>1) {
				Point p1 = getPoints().get(0);
				for (int i=1; i<getPoints().size(); ++i) {
					Point p2 = getPoints().get(i);
//					if (isHorizontal(p1,p2) || isVertical(p1,p2))
						length += (int)GraphicsUtil.getLength(p1, p2);
//					else 
//						return Integer.MAX_VALUE;
					p1 = p2;
				}
			}
			else {
				// this route could not be calculated
				return Integer.MAX_VALUE;
			}
			return length;
		}

		/* (non-Javadoc)
		 * @see java.lang.Comparable#compareTo(java.lang.Object)
		 */
		@Override
		public int compareTo(ConnectionRoute arg0) {
			return compare(this,arg0);
		}

		/* (non-Javadoc)
		 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
		 */
		@Override
		public int compare(ConnectionRoute o1, ConnectionRoute o2) {
			int i = 0;
			if (o1.isValid()) {
				if (o2.isValid()) {
					i = o1.getRank() - o2.getRank();
					if (i==0) {
						i = o1.collisions.size() - o2.collisions.size();
						if (i==0) {
							// TODO: figure out why connection crossing detection isn't working!
//							i = o1.crossings.size() - o2.crossings.size();
							if (i==0) {
								i = o1.getPoints().size() - o2.getPoints().size();
								if (i==0)
								{
									i = o1.getLength() - o2.getLength();
//									if (i==0) {
//										BoundaryAnchor ba1 = AnchorUtil.findNearestBoundaryAnchor(source, o1.get(0));
//										BoundaryAnchor ba2 = AnchorUtil.findNearestBoundaryAnchor(source, o2.get(0));
//
//										i = AnchorLocation.valueOf(ba1.locationType) - (int)ba2.locationType;
//									}
								}
							}
						}
					}
					return i;
				}
				return -1;
			}
			else if (!o2.isValid())
				return 0;
			return 1;
		}

		private boolean removeUnusedPoints() {
			boolean changed = false;

			Point p1 = getPoints().get(0);
			for (int i=1; i<getPoints().size()-1; ++i) {
				Point p2 = getPoints().get(i);
				if (i+1 < getPoints().size()) {
					// remove unnecessary bendpoints: two consecutive
					// horizontal or vertical line segments
					Point p3 = getPoints().get(i+1);
					int x1 = p1.getX();
					int x2 = p2.getX();
					int x3 = p3.getX();
					int y1 = p1.getY();
					int y2 = p2.getY();
					int y3 = p3.getY();
					if (
							(GraphicsUtil.isVertical(p1,p2) && GraphicsUtil.isVertical(p2,p3) && ((y1<y2 && y2<y3) || y1>y2 && y2>y3)) ||
							(GraphicsUtil.isHorizontal(p1,p2) && GraphicsUtil.isHorizontal(p2,p3) && ((x1<x2 && x2<x3) || x1>x2 && x2>x3))
					) {
						getPoints().remove(i);
						// look at these set of points again
						--i;
						changed = true;
					}
				}
				p1 = p2;
			}
			return changed;
		}
		
		private boolean removeUnusedSegments() {
			boolean changed = false;

			// remove unnecessary "U" shapes
			Point p1 = getPoints().get(1);
			for (int i=2; i<getPoints().size()-2; ++i) {
				Point p2 = getPoints().get(i);
				if (i+2 < getPoints().size()) {
					Point p3 = getPoints().get(i+1);
					Point p4 = getPoints().get(i+2);
					if (GraphicsUtil.isHorizontal(p1,p2) && GraphicsUtil.isVertical(p2,p3) && GraphicsUtil.isHorizontal(p3,p4)) {
						Point p = GraphicsUtil.createPoint(p1.getX(), p3.getY());
						if (router.getCollision(p1,p)==null) {
							getPoints().set(i+1, p);
							getPoints().remove(p2);
							getPoints().remove(p3);
							--i;
							changed = true;
						}

//						int x1 = p1.getX();
//						int x2 = p2.getX();
//						int x4 = p4.getX();
//						if ((x1 < x4 && x4 < x2) || (x1 > x4 && x4 > x2)) {
//							// this forms a horizontal "U" - remove if the new configuration does not cause a collision
//							Point p = GraphicsUtil.createPoint(x4, p2.getY());
//							if (router.getCollision(p,p4)==null) {
//								getPoints().set(i, p);
//								getPoints().remove(p3);
//								--i;
//								changed = true;
//							}
//						}
					}
					else if (GraphicsUtil.isVertical(p1,p2) && GraphicsUtil.isHorizontal(p2,p3) && GraphicsUtil.isVertical(p3,p4)) {
						Point p = GraphicsUtil.createPoint(p3.getX(), p1.getY());
						if (router.getCollision(p1,p)==null) {
							getPoints().set(i+1, p);
							getPoints().remove(p2);
							getPoints().remove(p3);
							--i;
							changed = true;
						}

//						int y1 = p1.getY();
//						int y2 = p2.getY();
//						int y4 = p4.getY();
//						if ((y1 < y4 && y4 < y2) || (y1 > y4 && y4 > y2)) {
//							// this forms a vertical "U"
//							p = GraphicsUtil.createPoint(p2.getX(), y4);
//							if (router.getCollision(p,p4)==null) {
//								getPoints().set(i, p);
//								getPoints().remove(p3);
//								--i;
//								changed = true;
//							}
//						}
					}
				}
				p1 = p2;
			}
			
			// remove "T" shapes
			p1 = getPoints().get(0);
			for (int i=1; i<getPoints().size()-1; ++i) {
				Point p2 = getPoints().get(i);
				if (i+1 < getPoints().size()) {
					Point p3 = getPoints().get(i+1);
					if (p1.getX() == p2.getX() && p2.getX() == p3.getX()) {
						if (	(p2.getY() < p1.getY() && p2.getY() < p3.getY()) ||
								(p2.getY() > p1.getY() && p2.getY() > p3.getY())
						) {
							getPoints().remove(p2);
							--i;
							changed = true;
						}
					}
					else if (p1.getY() == p2.getY() && p2.getY() == p3.getY()) {
						if (	(p2.getX() < p1.getX() && p2.getX() < p3.getX()) ||
								(p2.getX() > p1.getX() && p2.getX() > p3.getX())
						) {
							getPoints().remove(p2);
							--i;
							changed = true;
						}
					}
				}
				p1 = p2;
			}
			return changed;
		}
		
		/**
		 * Optimize.
		 *
		 * @return true, if successful
		 */
		public boolean optimize() {
			boolean changed = removeUnusedPoints();
			if (removeUnusedSegments()) {
				// this may cause some unused points to be left over
				removeUnusedPoints();
				changed = true;
			}
			return changed;
		}

		/**
		 * Gets the rank.
		 *
		 * @return the rank
		 */
		public int getRank() {
			return rank;
		}

		/**
		 * Sets the rank.
		 *
		 * @param rank the new rank
		 */
		public void setRank(int rank) {
			this.rank = rank;
		}

		/**
		 * Gets the points.
		 *
		 * @return the points
		 */
		public List<Point> getPoints() {
			return points;
		}

		/**
		 * Sets the points.
		 *
		 * @param points the new points
		 */
		public void setPoints(List<Point> points) {
			this.points = points;
		}
	}

Back to the top