-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCriticalPathsDemo.java
338 lines (286 loc) · 10.8 KB
/
CriticalPathsDemo.java
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
/****************************************************************************
**
** This demo file is part of yFiles for JavaFX 3.6.
**
** Copyright (c) 2000-2023 by yWorks GmbH, Vor dem Kreuzberg 28,
** 72070 Tuebingen, Germany. All rights reserved.
**
** yFiles demo files exhibit yFiles for JavaFX functionalities. Any redistribution
** of demo files in source code or binary form, with or without
** modification, is not permitted.
**
** Owners of a valid software license for a yFiles for JavaFX version that this
** demo is shipped with are allowed to use the demo source code as basis
** for their own yFiles for JavaFX powered applications. Use of such programs is
** governed by the rights and conditions as set out in the yFiles for JavaFX
** license agreement.
**
** THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY EXPRESS OR IMPLIED
** WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
** MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
** NO EVENT SHALL yWorks BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
** TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
** PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
** LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
** NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**
***************************************************************************/
package layout.criticalpaths;
import com.yworks.yfiles.graph.GraphItemTypes;
import com.yworks.yfiles.graph.IEdge;
import com.yworks.yfiles.graph.IGraph;
import com.yworks.yfiles.graph.INode;
import com.yworks.yfiles.graph.styles.IEdgeStyle;
import com.yworks.yfiles.graph.styles.PolylineEdgeStyle;
import com.yworks.yfiles.graph.styles.RectangleNodeStyle;
import com.yworks.yfiles.layout.hierarchic.EdgeLayoutDescriptor;
import com.yworks.yfiles.layout.hierarchic.HierarchicLayout;
import com.yworks.yfiles.layout.hierarchic.HierarchicLayoutData;
import com.yworks.yfiles.layout.hierarchic.SimplexNodePlacer;
import com.yworks.yfiles.layout.tree.LayeredNodePlacer;
import com.yworks.yfiles.layout.tree.TreeLayout;
import com.yworks.yfiles.layout.tree.TreeLayoutData;
import com.yworks.yfiles.utils.IListEnumerable;
import com.yworks.yfiles.view.GraphControl;
import com.yworks.yfiles.view.Pen;
import com.yworks.yfiles.view.input.GraphViewerInputMode;
import javafx.scene.paint.Color;
import toolkit.DemoApplication;
import toolkit.DemoStyles;
import toolkit.Themes;
import toolkit.WebViewUtils;
import javafx.scene.control.Tooltip;
import javafx.scene.web.WebView;
import java.io.IOException;
import java.time.Duration;
import java.util.ArrayList;
/**
* Shows how to emphazise important, or 'critical', paths with hierarchic and
* tree layout algorithms.
*/
public class CriticalPathsDemo extends DemoApplication {
public WebView helpView;
public GraphControl graphControl;
/**
* Specifies the layout algorithm used to demonstrate the effect of critical
* path priorities.
* Currently, only {@link HierarchicLayout} and {@link TreeLayout} support
* critical paths.
*/
private static final Algorithm LAYOUT_ALGORITHM = Algorithm.Hierarchic;
/**
* Styles for visualizing the critical path priority that may be associated
* to an edge.
*/
private static final IEdgeStyle[] PRIORITY_STYLES = createPriorityStyles();
/**
* Creates styles for visualizing the critical path priority that may be
* associated to an edge.
*/
private static IEdgeStyle[] createPriorityStyles() {
Pen[] pens = {
new Pen(Color.rgb(0xC7, 0xC7, 0xA6), 1), // priority 0 (least)
new Pen(Color.rgb(0x33, 0x66, 0x99), 3), // priority 1
new Pen(Color.rgb(0x56, 0x92, 0x6E), 3), // priority 2
new Pen(Color.rgb(0xF0, 0xC8, 0x08), 3), // priority 3
new Pen(Color.rgb(0xFF, 0x6C, 0x00), 3), // priority 4
new Pen(Color.rgb(0xDB, 0x3A, 0x34), 3), // priority 5 (highest)
};
IEdgeStyle[] styles = new IEdgeStyle[pens.length];
for (int i = 0; i < styles.length; ++i) {
PolylineEdgeStyle style = new PolylineEdgeStyle();
style.setPen(pens[i]);
styles[i] = style;
}
return styles;
}
/**
* Initializes the demo.
*/
public void initialize() {
WebViewUtils.initHelp(helpView, this);
// configure user interaction, namely the the edge tool tip
initializeInputMode();
// load a sample graph
loadGraph();
// set colors for edges according to their associated critical path priority
updateStyles(graphControl.getGraph());
// run the chosen layout algorithm
runLayout();
}
/**
* Configures user interaction.
* Adds priority tooltips to edges and context menus for edges and nodes.
*/
private void initializeInputMode() {
GraphViewerInputMode gvim = new GraphViewerInputMode();
gvim.setSelectableItems(GraphItemTypes.NONE);
gvim.setFocusableItems(GraphItemTypes.NONE);
gvim.setToolTipItems(GraphItemTypes.EDGE);
gvim.addQueryItemToolTipListener(( sender, args ) -> {
if (!args.isHandled()) {
args.setToolTip(new Tooltip("Priority: " + getPriority((IEdge) args.getItem())));
}
});
graphControl.setInputMode(gvim);
}
/**
* Colors each edge in the given graph according to the critical path
* priority of each edge.
*/
private static void updateStyles( IGraph graph ) {
for (IEdge edge : graph.getEdges()) {
graph.setStyle(edge, PRIORITY_STYLES[getPriority(edge)]);
}
}
/**
* Clears critical path priorities and updates the displayed graph
* accordingly.
*/
public void clearPriorities() {
IGraph graph = graphControl.getGraph();
clearPrioritiesCore(graph);
updateStyles(graph);
runLayout();
}
/**
* Clears all critical path priorities in the given graph.
*/
private static void clearPrioritiesCore( IGraph graph ) {
for (IEdge edge : graph.getEdges()) {
edge.setTag(Integer.valueOf(0));
}
}
/**
* Assigns random critical path priorities and updates the displayed graph
* accordingly.
*/
public void randomPriorities() {
IGraph graph = graphControl.getGraph();
randomPrioritiesCore(graph);
updateStyles(graph);
runLayout();
}
/**
* Marks random upstream paths from leaf nodes to generate random long paths.
*/
private static void randomPrioritiesCore( IGraph graph ) {
//Get all leafs in the graph and save them
ArrayList<INode> leafs = new ArrayList<>();
for (INode node : graph.getNodes()) {
if (graph.outEdgesAt(node).size() == 0) {
leafs.add(node);
}
}
clearPrioritiesCore(graph);
// mark the upstream path of random leaf nodes
for (int i = 0, n = Math.min(10, leafs.size()); i < n; ++i) {
int rndNodeIndex = (int) Math.floor(Math.random() * leafs.size());
int rndPriority = (int) (Math.floor(Math.random() * 5) + 1);
markPredecessorsPath(graph, leafs.get(rndNodeIndex), rndPriority);
}
}
/**
* Marks the upstream path from a given node with the given priority
*/
private static void markPredecessorsPath( IGraph graph, INode node, int priority ) {
for (IEdge edge = firstInEdge(graph, node);
edge != null;
edge = firstInEdge(graph, edge.getSourceNode())) {
if (getPriority(edge) > priority) {
//stop upstream path when a higher priority is found
break;
}
edge.setTag(priority);
}
}
/**
* Returns the first ingoing edge of the given node or {@code null} if
* the node has no ingoing edges.
*/
private static IEdge firstInEdge( IGraph graph, INode node ) {
IListEnumerable<IEdge> edges = graph.inEdgesAt(node);
return edges.size() > 0 ? edges.first() : null;
}
/**
* Returns the critical path priority stored in the given edge.
* More specifically, returns the integral value of the edge's tag or
* {@code 0} if the tag is not a number.
*/
private static int getPriority( IEdge edge ) {
Object tag = edge.getTag();
return tag instanceof Number ? ((Number) tag).intValue() : 0;
}
/**
* Runs the in {@literal LAYOUT_ALGORITHM} chosen layout, either the hierarchic layout or the tree layout. Furthermore,
* the edges get colored according to their priority.
*/
private void runLayout() {
if (LAYOUT_ALGORITHM == Algorithm.Hierarchic) {
runHierarchicLayout();
} else {
runTreeLayout();
}
}
/**
* Creates the hierarchic layout, applies settings to it and then runs the layout.
*/
private void runHierarchicLayout() {
EdgeLayoutDescriptor layoutDescriptor = new EdgeLayoutDescriptor();
layoutDescriptor.setMinimumFirstSegmentLength(30);
layoutDescriptor.setMinimumLastSegmentLength(30);
SimplexNodePlacer nodePlacer = new SimplexNodePlacer();
nodePlacer.setBarycenterModeEnabled(true);
HierarchicLayout layout = new HierarchicLayout();
layout.setOrthogonalRoutingEnabled(true);
layout.setEdgeLayoutDescriptor(layoutDescriptor);
layout.setNodePlacer(nodePlacer);
HierarchicLayoutData layoutData = new HierarchicLayoutData();
layoutData.setCriticalEdgePriorities(CriticalPathsDemo::getPriority);
// set edge crossing costs to reduce the probability of different critical
// paths crossing each other
layoutData.setEdgeCrossingCosts(edge -> getPriority(edge) + 1.0);
graphControl.morphLayout(layout, Duration.ofMillis(700), layoutData);
}
/**
* Creates the tree layout, applies settings to it and then runs the layout.
*/
private void runTreeLayout() {
LayeredNodePlacer nodePlacer = new LayeredNodePlacer();
nodePlacer.setLayerSpacing(60);
nodePlacer.setSpacing(30);
TreeLayout layout = new TreeLayout();
layout.setDefaultNodePlacer(nodePlacer);
TreeLayoutData layoutData = new TreeLayoutData();
layoutData.setCriticalEdgePriorities(CriticalPathsDemo::getPriority);
graphControl.morphLayout(layout, Duration.ofMillis(700), layoutData);
}
/**
* Loads a sample graph appropriate for the chosen
* {@link #LAYOUT_ALGORITHM layout algorithm}.
*/
private void loadGraph() {
String path = LAYOUT_ALGORITHM == Algorithm.Tree
? "resources/tree.graphml"
: "resources/hierarchic.graphml";
RectangleNodeStyle nodeStyle = DemoStyles.createDemoNodeStyle(Themes.PALETTE44);
graphControl.getGraph().getNodeDefaults().setStyle(nodeStyle);
try {
graphControl.importFromGraphML(getClass().getResource(path));
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
/**
* Lists the layout algorithms that support critical paths.
*/
private enum Algorithm {
Tree,
Hierarchic
}
}