~ubuntu-branches/ubuntu/utopic/gridengine/utopic

« back to all changes in this revision

Viewing changes to source/libs/jgdi/cullconv/src/com/sun/grid/cull/CullDependencies.java

  • Committer: Bazaar Package Importer
  • Author(s): Mark Hymers
  • Date: 2008-06-25 22:36:13 UTC
  • Revision ID: james.westby@ubuntu.com-20080625223613-tvd9xlhuoct9kyhm
Tags: upstream-6.2~beta2
ImportĀ upstreamĀ versionĀ 6.2~beta2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*___INFO__MARK_BEGIN__*/
 
2
/*************************************************************************
 
3
 *
 
4
 *  The Contents of this file are made available subject to the terms of
 
5
 *  the Sun Industry Standards Source License Version 1.2
 
6
 *
 
7
 *  Sun Microsystems Inc., March, 2001
 
8
 *
 
9
 *
 
10
 *  Sun Industry Standards Source License Version 1.2
 
11
 *  =================================================
 
12
 *  The contents of this file are subject to the Sun Industry Standards
 
13
 *  Source License Version 1.2 (the "License"); You may not use this file
 
14
 *  except in compliance with the License. You may obtain a copy of the
 
15
 *  License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
 
16
 *
 
17
 *  Software provided under this License is provided on an "AS IS" basis,
 
18
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 
19
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 
20
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 
21
 *  See the License for the specific provisions governing your rights and
 
22
 *  obligations concerning the Software.
 
23
 *
 
24
 *   The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 
25
 *
 
26
 *   Copyright: 2001 by Sun Microsystems, Inc.
 
27
 *
 
28
 *   All Rights Reserved.
 
29
 *
 
30
 ************************************************************************/
 
31
/*___INFO__MARK_END__*/
 
32
package com.sun.grid.cull;
 
33
 
 
34
import java.util.ArrayList;
 
35
import java.util.HashMap;
 
36
import java.util.List;
 
37
import java.util.Map;
 
38
import java.util.Set;
 
39
import java.util.StringTokenizer;
 
40
import java.util.logging.Logger;
 
41
import javax.swing.tree.DefaultMutableTreeNode;
 
42
import org.apache.tools.ant.BuildException;
 
43
 
 
44
/**
 
45
 *
 
46
 */
 
47
public class CullDependencies {
 
48
 
 
49
    private Logger logger = Logger.getLogger("cullconv");
 
50
 
 
51
    Map<String, Node> nodeMap = new HashMap<String, Node>();
 
52
 
 
53
    public static final String FILTER_OBJECTS = "objects";
 
54
    public static final String FILTER_PRIMITIVE_OBJECTS = "primitives";
 
55
    public static final String FILTER_ROOT_OBJECTS = "root";
 
56
    public static final String FILTER_PRIMITIVE_ROOT_OBJECTS = "primitive_root";
 
57
    public static final String FILTER_MAPPED_OBJECTS = "mapped";
 
58
    public static final String FILTER_MAP_OBJECTS = "map";
 
59
    public static final String FILTER_DEPEND_OBJECTS = "depend";
 
60
    public static final String FILTER_EVENT_OBJECTS = "event";
 
61
 
 
62
 
 
63
    public static final int INCLUDE_ROOT_OBJECTS = 1;
 
64
    public static final int INCLUDE_OBJECTS = 2;
 
65
    public static final int INCLUDE_PRIMITIVE_OBJECTS = 4;
 
66
    public static final int INCLUDE_PRIMITIVE_ROOT_OBJECTS = 8;
 
67
    public static final int INCLUDE_MAPPED_OBJECTS = 16;
 
68
    public static final int INCLUDE_MAP_OBJECTS = 32;
 
69
    public static final int INCLUDE_DEPEND_OBJECTS = 64;
 
70
    public static final int INCLUDE_EVENT_OBJECTS = 128;
 
71
    public static final int INCLUDE_ALL = 65535;
 
72
    private int includeMask;
 
73
 
 
74
    /** Creates a new instance of CullDependencies */
 
75
    public CullDependencies(CullDefinition culldef, Set<String> nameSet, String objectFilter) throws BuildException {
 
76
 
 
77
        includeMask = 0;
 
78
 
 
79
        if (objectFilter == null) {
 
80
            includeMask = INCLUDE_ALL;
 
81
        } else {
 
82
            StringTokenizer st = new StringTokenizer(objectFilter);
 
83
 
 
84
            while (st.hasMoreTokens()) {
 
85
                String token = st.nextToken();
 
86
                if (FILTER_OBJECTS.equals(token)) {
 
87
                    includeMask |= INCLUDE_OBJECTS;
 
88
                } else if (FILTER_PRIMITIVE_OBJECTS.equals(token)) {
 
89
                    includeMask |= INCLUDE_PRIMITIVE_OBJECTS;
 
90
                } else if (FILTER_ROOT_OBJECTS.equals(token)) {
 
91
                    includeMask |= INCLUDE_ROOT_OBJECTS;
 
92
                } else if (FILTER_EVENT_OBJECTS.equals(token)) {
 
93
                    includeMask |= INCLUDE_EVENT_OBJECTS;
 
94
                } else if (FILTER_DEPEND_OBJECTS.equals(token)) {
 
95
                    includeMask |= INCLUDE_DEPEND_OBJECTS;
 
96
                } else if (FILTER_MAPPED_OBJECTS.equals(token)) {
 
97
                    includeMask |= INCLUDE_MAPPED_OBJECTS;
 
98
                } else if (FILTER_MAP_OBJECTS.equals(token)) {
 
99
                    includeMask |= INCLUDE_MAP_OBJECTS;
 
100
                } else if (FILTER_PRIMITIVE_ROOT_OBJECTS.equals(token)) {
 
101
                    includeMask |= INCLUDE_PRIMITIVE_ROOT_OBJECTS;
 
102
                } else {
 
103
                    throw new BuildException("Invalid object filter '" + token + "'");
 
104
                }
 
105
            }
 
106
        }
 
107
 
 
108
        for (String name : nameSet) {
 
109
            CullObject obj = culldef.getCullObject(name);
 
110
            Node node = nodeMap.get(name);
 
111
            if (node == null) {
 
112
                node = new Node(obj);
 
113
                nodeMap.put(name, node);
 
114
            }
 
115
 
 
116
            for (int i = 0; i < obj.getAttrCount(); i++) {
 
117
                CullAttr attr = obj.getAttr(i);
 
118
                String type = attr.getType();
 
119
 
 
120
                CullObject subobj = culldef.getCullObject(type);
 
121
                if (subobj != null) {
 
122
                    node = (Node) nodeMap.get(type);
 
123
                    if (node == null) {
 
124
                        node = new Node(subobj);
 
125
                        nodeMap.put(type, node);
 
126
                    }
 
127
                    node.addDepend(obj);
 
128
                }
 
129
            }
 
130
 
 
131
            if (obj.getParentObject() != null) {
 
132
                CullObject subobj = obj.getParentObject();
 
133
                if (subobj != null) {
 
134
                    node = (Node) nodeMap.get(subobj.getName());
 
135
                    if (node == null) {
 
136
                        node = new Node(subobj);
 
137
                        nodeMap.put(subobj.getName(), node);
 
138
                    }
 
139
                    node.addDepend(obj);
 
140
                }
 
141
            }
 
142
        }
 
143
    }
 
144
 
 
145
    public boolean includeRootObjects() {
 
146
        return (includeMask & INCLUDE_ROOT_OBJECTS) == INCLUDE_ROOT_OBJECTS;
 
147
    }
 
148
 
 
149
    public boolean includeEventObjects() {
 
150
        return (includeMask & INCLUDE_EVENT_OBJECTS) == INCLUDE_EVENT_OBJECTS;
 
151
    }
 
152
 
 
153
    public boolean includeObjects() {
 
154
        return (includeMask & INCLUDE_OBJECTS) == INCLUDE_OBJECTS;
 
155
    }
 
156
 
 
157
    public boolean includePrimitveObjects() {
 
158
        return (includeMask & INCLUDE_PRIMITIVE_OBJECTS) == INCLUDE_PRIMITIVE_OBJECTS;
 
159
    }
 
160
 
 
161
    public boolean includeMappedObjects() {
 
162
        return (includeMask & INCLUDE_MAPPED_OBJECTS) == INCLUDE_MAPPED_OBJECTS;
 
163
    }
 
164
 
 
165
    public boolean includePrimitveRootObjects() {
 
166
        return (includeMask & INCLUDE_PRIMITIVE_ROOT_OBJECTS) == INCLUDE_PRIMITIVE_ROOT_OBJECTS;
 
167
    }
 
168
 
 
169
    public boolean includeMapObjects() {
 
170
        return (includeMask & INCLUDE_MAP_OBJECTS) == INCLUDE_MAP_OBJECTS;
 
171
    }
 
172
 
 
173
    public boolean includeDependObjects() {
 
174
        return (includeMask & INCLUDE_DEPEND_OBJECTS) == INCLUDE_DEPEND_OBJECTS;
 
175
    }
 
176
 
 
177
    public Node getNode(String name) {
 
178
        return (Node) nodeMap.get(name);
 
179
    }
 
180
 
 
181
    public class Node {
 
182
 
 
183
        private CullObject obj;
 
184
        private List<CullObject> dependSet = new ArrayList<CullObject>();
 
185
 
 
186
        public Node(CullObject obj) {
 
187
            this.obj = obj;
 
188
        }
 
189
 
 
190
        public void addDepend(CullObject obj) {
 
191
            dependSet.add(obj);
 
192
            needed = null;
 
193
        }
 
194
 
 
195
        public int getDependCount() {
 
196
            return dependSet.size();
 
197
        }
 
198
 
 
199
        private Boolean needed = null;
 
200
        private boolean isNeededArmed = false;
 
201
 
 
202
        public boolean isNeeded() {
 
203
            if (needed == null) {
 
204
                if (isNeededArmed) {
 
205
                    needed = Boolean.FALSE;
 
206
                } else {
 
207
                    isNeededArmed = true;
 
208
                    if (obj.hasEvents() && includeEventObjects()) {
 
209
                        needed = Boolean.TRUE;
 
210
                    } else if (obj.getType() == CullObject.TYPE_PRIMITIVE && includePrimitveObjects()) {
 
211
                        needed = Boolean.TRUE;
 
212
                    } else if (obj.getType() == CullObject.TYPE_MAPPED && includeMappedObjects()) {
 
213
                        needed = Boolean.TRUE;
 
214
                    } else if (obj.getType() == CullObject.TYPE_MAP && includeMapObjects()) {
 
215
                        needed = Boolean.TRUE;
 
216
                    } else if (obj.isRootObject()) {
 
217
                        if (obj.getType() == CullObject.TYPE_PRIMITIVE) {
 
218
                            if (includePrimitveRootObjects()) {
 
219
                                needed = Boolean.TRUE;
 
220
                            }
 
221
                        } else {
 
222
                            if (includeRootObjects()) {
 
223
                                needed = Boolean.TRUE;
 
224
                            }
 
225
                        }
 
226
                    } else if (includeObjects()) {
 
227
                        needed = Boolean.TRUE;
 
228
                    }
 
229
 
 
230
                    if (needed == null) {
 
231
                        for (CullObject dependObj : dependSet) {
 
232
                            Node node = getNode(dependObj.getName());
 
233
                            if (node != null) {
 
234
                                if (node.isNeeded()) {
 
235
                                    switch (obj.getType()) {
 
236
                                        case CullObject.TYPE_PRIMITIVE:
 
237
                                            {
 
238
                                                if (includePrimitveObjects()) {
 
239
                                                    needed = Boolean.TRUE;
 
240
                                                } else {
 
241
                                                    needed = Boolean.FALSE;
 
242
                                                }
 
243
                                                break;
 
244
                                            }
 
245
                                        case CullObject.TYPE_MAPPED:
 
246
                                            {
 
247
                                                if (includeMappedObjects()) {
 
248
                                                    needed = Boolean.TRUE;
 
249
                                                } else {
 
250
                                                    needed = Boolean.FALSE;
 
251
                                                }
 
252
                                                break;
 
253
                                            }
 
254
                                        case CullObject.TYPE_MAP:
 
255
                                            {
 
256
                                                if (includeMapObjects()) {
 
257
                                                    needed = Boolean.TRUE;
 
258
                                                } else {
 
259
                                                    needed = Boolean.FALSE;
 
260
                                                }
 
261
                                                break;
 
262
                                            }
 
263
                                        default:
 
264
                                            {
 
265
                                                if (includeDependObjects()) {
 
266
                                                    needed = Boolean.TRUE;
 
267
                                                }
 
268
                                            }
 
269
                                    }
 
270
                                }
 
271
                            }
 
272
                        }
 
273
                        if (needed == null) {
 
274
                            needed = Boolean.FALSE;
 
275
                        }
 
276
                    }
 
277
                    isNeededArmed = false;
 
278
                }
 
279
            }
 
280
            return needed.booleanValue();
 
281
        }
 
282
 
 
283
        boolean toStringArmed = false;
 
284
 
 
285
        public String toString() {
 
286
            StringBuffer ret = new StringBuffer();
 
287
 
 
288
            ret.append(obj.getName());
 
289
            ret.append("(");
 
290
            if (obj.isRootObject()) {
 
291
                ret.append("]");
 
292
            }
 
293
            CullObject testObj = obj;
 
294
            if (obj.getParentObject() != null) {
 
295
                testObj = obj.getParentObject();
 
296
            }
 
297
 
 
298
            if (testObj.getType() == CullObject.TYPE_PRIMITIVE) {
 
299
                ret.append("P");
 
300
            }
 
301
            if (testObj.getType() == CullObject.TYPE_MAPPED) {
 
302
                ret.append("M");
 
303
            }
 
304
 
 
305
            ret.append(")");
 
306
            if (!toStringArmed) {
 
307
                toStringArmed = true;
 
308
                if (isNeeded()) {
 
309
                    ret.append("*");
 
310
                }
 
311
                if (!dependSet.isEmpty()) {
 
312
                    boolean first = true;
 
313
                    ret.append(" --> [");
 
314
                    for (CullObject obj : dependSet) {
 
315
                        Node node = getNode(obj.getName());
 
316
                        if (first) {
 
317
                            first = false;
 
318
                        } else {    
 
319
                            ret.append(", ");
 
320
                        }
 
321
                        ret.append(node.toString());
 
322
                    }
 
323
                    ret.append("]");
 
324
                }
 
325
                toStringArmed = false;
 
326
            }
 
327
            return ret.toString();
 
328
        }
 
329
    }
 
330
 
 
331
 
 
332
 
 
333
    private static DefaultMutableTreeNode findNode(DefaultMutableTreeNode node, CullObject obj) {
 
334
 
 
335
        if (node.getUserObject().equals(obj)) {
 
336
            return node;
 
337
        }
 
338
 
 
339
        DefaultMutableTreeNode ret = null;
 
340
        for (DefaultMutableTreeNode child = (DefaultMutableTreeNode) node.getFirstChild(); child != null; child = (DefaultMutableTreeNode) child.getNextSibling()) {
 
341
 
 
342
            ret = findNode(child, obj);
 
343
            if (ret != null) {
 
344
                break;
 
345
            }
 
346
        }
 
347
        return ret;
 
348
    }
 
349
}