1
/*___INFO__MARK_BEGIN__*/
2
/*************************************************************************
4
* The Contents of this file are made available subject to the terms of
5
* the Sun Industry Standards Source License Version 1.2
7
* Sun Microsystems Inc., March, 2001
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
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.
24
* The Initial Developer of the Original Code is: Sun Microsystems, Inc.
26
* Copyright: 2001 by Sun Microsystems, Inc.
28
* All Rights Reserved.
30
************************************************************************/
31
/*___INFO__MARK_END__*/
32
package com.sun.grid.cull;
34
import java.util.ArrayList;
35
import java.util.HashMap;
36
import java.util.List;
39
import java.util.StringTokenizer;
40
import java.util.logging.Logger;
41
import javax.swing.tree.DefaultMutableTreeNode;
42
import org.apache.tools.ant.BuildException;
47
public class CullDependencies {
49
private Logger logger = Logger.getLogger("cullconv");
51
Map<String, Node> nodeMap = new HashMap<String, Node>();
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";
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;
74
/** Creates a new instance of CullDependencies */
75
public CullDependencies(CullDefinition culldef, Set<String> nameSet, String objectFilter) throws BuildException {
79
if (objectFilter == null) {
80
includeMask = INCLUDE_ALL;
82
StringTokenizer st = new StringTokenizer(objectFilter);
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;
103
throw new BuildException("Invalid object filter '" + token + "'");
108
for (String name : nameSet) {
109
CullObject obj = culldef.getCullObject(name);
110
Node node = nodeMap.get(name);
112
node = new Node(obj);
113
nodeMap.put(name, node);
116
for (int i = 0; i < obj.getAttrCount(); i++) {
117
CullAttr attr = obj.getAttr(i);
118
String type = attr.getType();
120
CullObject subobj = culldef.getCullObject(type);
121
if (subobj != null) {
122
node = (Node) nodeMap.get(type);
124
node = new Node(subobj);
125
nodeMap.put(type, node);
131
if (obj.getParentObject() != null) {
132
CullObject subobj = obj.getParentObject();
133
if (subobj != null) {
134
node = (Node) nodeMap.get(subobj.getName());
136
node = new Node(subobj);
137
nodeMap.put(subobj.getName(), node);
145
public boolean includeRootObjects() {
146
return (includeMask & INCLUDE_ROOT_OBJECTS) == INCLUDE_ROOT_OBJECTS;
149
public boolean includeEventObjects() {
150
return (includeMask & INCLUDE_EVENT_OBJECTS) == INCLUDE_EVENT_OBJECTS;
153
public boolean includeObjects() {
154
return (includeMask & INCLUDE_OBJECTS) == INCLUDE_OBJECTS;
157
public boolean includePrimitveObjects() {
158
return (includeMask & INCLUDE_PRIMITIVE_OBJECTS) == INCLUDE_PRIMITIVE_OBJECTS;
161
public boolean includeMappedObjects() {
162
return (includeMask & INCLUDE_MAPPED_OBJECTS) == INCLUDE_MAPPED_OBJECTS;
165
public boolean includePrimitveRootObjects() {
166
return (includeMask & INCLUDE_PRIMITIVE_ROOT_OBJECTS) == INCLUDE_PRIMITIVE_ROOT_OBJECTS;
169
public boolean includeMapObjects() {
170
return (includeMask & INCLUDE_MAP_OBJECTS) == INCLUDE_MAP_OBJECTS;
173
public boolean includeDependObjects() {
174
return (includeMask & INCLUDE_DEPEND_OBJECTS) == INCLUDE_DEPEND_OBJECTS;
177
public Node getNode(String name) {
178
return (Node) nodeMap.get(name);
183
private CullObject obj;
184
private List<CullObject> dependSet = new ArrayList<CullObject>();
186
public Node(CullObject obj) {
190
public void addDepend(CullObject obj) {
195
public int getDependCount() {
196
return dependSet.size();
199
private Boolean needed = null;
200
private boolean isNeededArmed = false;
202
public boolean isNeeded() {
203
if (needed == null) {
205
needed = Boolean.FALSE;
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;
222
if (includeRootObjects()) {
223
needed = Boolean.TRUE;
226
} else if (includeObjects()) {
227
needed = Boolean.TRUE;
230
if (needed == null) {
231
for (CullObject dependObj : dependSet) {
232
Node node = getNode(dependObj.getName());
234
if (node.isNeeded()) {
235
switch (obj.getType()) {
236
case CullObject.TYPE_PRIMITIVE:
238
if (includePrimitveObjects()) {
239
needed = Boolean.TRUE;
241
needed = Boolean.FALSE;
245
case CullObject.TYPE_MAPPED:
247
if (includeMappedObjects()) {
248
needed = Boolean.TRUE;
250
needed = Boolean.FALSE;
254
case CullObject.TYPE_MAP:
256
if (includeMapObjects()) {
257
needed = Boolean.TRUE;
259
needed = Boolean.FALSE;
265
if (includeDependObjects()) {
266
needed = Boolean.TRUE;
273
if (needed == null) {
274
needed = Boolean.FALSE;
277
isNeededArmed = false;
280
return needed.booleanValue();
283
boolean toStringArmed = false;
285
public String toString() {
286
StringBuffer ret = new StringBuffer();
288
ret.append(obj.getName());
290
if (obj.isRootObject()) {
293
CullObject testObj = obj;
294
if (obj.getParentObject() != null) {
295
testObj = obj.getParentObject();
298
if (testObj.getType() == CullObject.TYPE_PRIMITIVE) {
301
if (testObj.getType() == CullObject.TYPE_MAPPED) {
306
if (!toStringArmed) {
307
toStringArmed = true;
311
if (!dependSet.isEmpty()) {
312
boolean first = true;
313
ret.append(" --> [");
314
for (CullObject obj : dependSet) {
315
Node node = getNode(obj.getName());
321
ret.append(node.toString());
325
toStringArmed = false;
327
return ret.toString();
333
private static DefaultMutableTreeNode findNode(DefaultMutableTreeNode node, CullObject obj) {
335
if (node.getUserObject().equals(obj)) {
339
DefaultMutableTreeNode ret = null;
340
for (DefaultMutableTreeNode child = (DefaultMutableTreeNode) node.getFirstChild(); child != null; child = (DefaultMutableTreeNode) child.getNextSibling()) {
342
ret = findNode(child, obj);