summaryrefslogtreecommitdiffstats
path: root/sca-java-2.x/tags/2.0.1-RC1/modules/composite-diagram/src/main/java/org/apache/tuscany/sca/diagram/layout/LayoutBuilder.java
blob: d67dee1629944a9e44635b209649badd91f35c4d (plain)
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
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.    
 */

package org.apache.tuscany.sca.diagram.layout;

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

import org.apache.tuscany.sca.diagram.artifacts.Constant;

public class LayoutBuilder {

    private Entity[] elts;
    private int[][] conns;
    private int maxLevels = 8;

    private int totalLevel;
    private int totalLane;

    private int totalHeight;
    private int totalWidth;

    private int[][] graph;

    /**
     * Constructor which takes set of entities and their connection matrix
     *
     * @param entities
     * @param connections
     * @param maxLevels
     */
    public LayoutBuilder(Entity[] entities, int[][] connections, int maxLevels) {
        elts = entities;
        graph = connections; // Keep the original connections

        // Clone the connections
        int len = connections.length;
        conns = new int[len][];
        for (int i = 0; i < len; i++) {
            conns[i] = new int[len];
            for (int j = 0; j < len; j++) {
                conns[i][j] = connections[i][j];
            }
        }
        this.maxLevels = maxLevels;
    }

    /**
     * Layout Building Algorithm
     * ~~~~~~~~~~~~~~~~~~~~~~~~~
     *
     * Here we position (i.e. assigning a level and a lane) all Entities
     * in a unique cell of a grid.
     * 
     * 		 	lane0	  lane1	  lane2	  lane3 ....
     * 		 	 _______________________________
     * level0	|		|		|		|		|
     * 			|_______|_______|_______|_______|
     * level1	|		|		|		|		|
     * 			|_______|_______|_______|_______|
     * level2	|		|		|		|		|     
     * 
     * 1) Determining the Entity at level0, lane0 (starting entity)
     * 		-First Entity in the list of Entities which has one or more adjacent Entities
     * 		-If there is only one Entity it will eventually chosen
     * 
     * 2) Get connected Entities of starting Entity. 
     * 		* If there are connected entities;
     * 			*For each connected Entity; 
     * 				*We assign a corresponding level and a lane
     * 				*Then recurse the procedure for connections of the assigned Entity
     * 	
     * 
     */
    public Entity[] placeEntities() {

        sortEntities();

        // Build the grid for entities
        Entity[][] grid = new Entity[totalLane + 1][totalLevel + 1];
        int[] height = new int[totalLevel + 1];
        int[] width = new int[totalLane + 1];

        for (Entity e : elts) {
            grid[e.getLane()][e.getLevel()] = e;
            if (height[e.getLevel()] < e.getHeight() + Constant.COMPONENT_DEFAULT_HEIGHT) {
                height[e.getLevel()] = e.getHeight() + Constant.COMPONENT_DEFAULT_HEIGHT;
            }
            if (width[e.getLane()] < e.getWidth() + Constant.COMPONENT_DEFAULT_WIDTH) {
                width[e.getLane()] = e.getWidth() + Constant.COMPONENT_DEFAULT_WIDTH;
            }
        }

        for (int i = 1; i < totalLane + 1; i++) {
            width[i] += width[i - 1];
        }

        for (int j = 1; j < totalLevel + 1; j++) {
            height[j] += height[j - 1];
        }

        totalWidth = width[totalLane];
        totalHeight = height[totalLevel];

        for (int i = 0; i < totalLane + 1; i++) {
            for (int j = 0; j < totalLevel + 1; j++) {
                Entity ent = grid[i][j];
                if (ent != null) {
                    int w = ent.getLane() == 0 ? 0 : width[i - 1];
                    ent.setX(ent.getParent().getX() + ent.getStartPosition() + w);
                    int h = ent.getLevel() == 0 ? 0 : height[j - 1];
                    ent.setY(ent.getParent().getY() + ent.getStartPosition() / 2 + h);
                }
            }
        }

        return elts;

    }

    private Entity findEntity(int i) {

        for (Entity ent : elts) {
            if (ent.getId() == i) {
                return ent;
            }
        }
        return null;
    }

    private void setPosition(Entity ent, int level, int lane) {
        if (totalLane < lane) {
            totalLane = lane;
        }
        if (totalLevel < level) {
            totalLevel = level;
        }
        ent.setLevel(level);
        ent.setLane(lane);
        ent.setPositionSet(true);
    }

    /**
     * http://en.wikipedia.org/wiki/Coffman%E2%80%93Graham_algorithm#The_algorithm
     * @param sorted
     * @param e1
     * @param e2
     * @return
     */
    private int compareEntities(List<Integer> sorted, int e1, int e2) {
        List<Integer> neighbors1 = findIncomingNeighbors(e1);
        List<Integer> neighbors2 = findIncomingNeighbors(e2);

        if (neighbors1.isEmpty() && neighbors2.isEmpty()) {
            return 0;
        } else if (neighbors1.isEmpty()) {
            return -1;
        } else if (neighbors2.isEmpty()) {
            return 1;
        }

        int max = graph.length + 1;
        int n1 = max;
        int n2 = max;
        for (int i = sorted.size() - 1; i >= 0; i--) {
            if (neighbors1.contains(sorted.get(i))) {
                n1 = i;
            }
            if (neighbors2.contains(sorted.get(i))) {
                n2 = i;
            }
            if (n1 == n2) {
                // Need to try the 2nd most recently added incoming neighbor
                // Reset the indexes and continue
                n1 = max;
                n2 = max;
                continue;
            }
        }
        return n1 - n2;
    }

    private List<Integer> findIncomingNeighbors(int e1) {
        // Get all the inbound connections for a given entity 
        List<Integer> ins = new ArrayList<Integer>();
        for (int i = 0; i < graph.length; i++) {
            if (graph[i][e1] == 1) {
                ins.add(i);
            }
        }
        return ins;
    }

    /**
     * Perform a topological sort on the graph so that we can place the entities into level/lane grids
     * http://en.wikipedia.org/wiki/Coffman%E2%80%93Graham_algorithm#The_algorithm
     */
    List<Integer> sortEntities() {
        int lane = 0;
        final List<Integer> sorted = new ArrayList<Integer>();
        while (true) {
            List<Integer> ids = new ArrayList<Integer>();
            for (int i = 0; i < conns.length; i++) {
                Entity ent = findEntity(i);
                if (ent.isPositionSet()) {
                    continue;
                }
                boolean beingConnected = false;
                for (int j = 0; j < conns.length; j++) {
                    if (conns[j][i] == 1) {
                        beingConnected = true;
                        break;
                    }
                }
                if (!beingConnected) {
                    ids.add(i);
                }
            }

            if (ids.isEmpty()) {
                boolean end = true;
                // There might be circular dependencies
                for (Entity e : elts) {
                    if (!e.isPositionSet()) {
                        // Pick the first one 
                        ids.add(e.getId());
                        end = false;
                        break;
                    }
                }
                if (end) {
                    return sorted;
                }
            }
            int level = 0;
            Collections.sort(ids, new Comparator<Integer>() {

                @Override
                public int compare(Integer e1, Integer e2) {
                    return compareEntities(sorted, e1, e2);
                }
            });
            for (int i : ids) {
                sorted.add(i);

                if (maxLevels > 0 && level > 0 && (level % maxLevels == 0)) {
                    // Overflow to the next lane
                    level = 0;
                    lane++;
                }

                setPosition(findEntity(i), level++, lane);
                for (int j = 0; j < conns.length; j++) {
                    // Remove the connections from i
                    conns[i][j] = 0;
                }
            }
            lane++;
        }
    }

    public int getTotalLevel() {
        return totalLevel;
    }

    public int getTotalLane() {
        return totalLane;
    }

    public int getTotalHeight() {
        return totalHeight;
    }

    public int getTotalWidth() {
        return totalWidth;
    }

}