File: DefaultGraphBundle.java

package info (click to toggle)
libgrinvin-core-java 1.2-1
  • links: PTS, VCS
  • area: contrib
  • in suites: squeeze
  • size: 3,904 kB
  • ctags: 5,009
  • sloc: java: 23,494; xml: 423; makefile: 15
file content (351 lines) | stat: -rw-r--r-- 11,292 bytes parent folder | download
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
/* DefaultGraphBundle.java
 * =========================================================================
 * This file is part of the GrInvIn project - http://www.grinvin.org
 * 
 * Copyright (C) 2005-2008 Universiteit Gent
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * 
 * A copy of the GNU General Public License can be found in the file
 * LICENSE.txt provided with the source distribution of this program (see
 * the META-INF directory in the source jar). This license can also be
 * found on the GNU website at http://www.gnu.org/licenses/gpl.html.
 * 
 * If you did not receive a copy of the GNU General Public License along
 * with this program, contact the lead developer, or write to the Free
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */

package org.grinvin.graphs;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.grinvin.gui.icons.DefaultGraphIconFactory;
import org.grinvin.gui.icons.GraphIconFactory;
import org.grinvin.invariants.Invariant;
import org.grinvin.invariants.InvariantComputer;
import org.grinvin.invariants.InvariantManager;
import org.grinvin.invariants.InvariantValue;
import org.grinvin.util.InternationalizedProperties;

/**
 * Default implementation of {@link GraphBundle}. Uses {@link DefaultGraph} and
 * {@link DefaultEmbedding} to represent the bundle.
 */
public class DefaultGraphBundle implements GraphBundle {
    
    //
    protected DefaultGraph graph;
    
    //
    protected List<DefaultEmbedding> embeddings;
    
    //
    protected List<DefaultAnnotation> annotations;
    
    //
    protected InternationalizedProperties properties;
    
    //TODO: might be better to use a special map here as InvariantValue contains Invariant as well
    protected Map<Invariant, InvariantValue> invariantValues;
    
    //
    protected List<GraphBundleListener> listeners;
    
    //
    protected GraphIconFactory graphIconFactory;

    //
    public GraphIconFactory getGraphIconFactory() {
        return graphIconFactory;
    }

    //
    public void setGraphIconFactory(GraphIconFactory graphIconFactory) {
        if (graphIconFactory == null)
            this.graphIconFactory = DefaultGraphIconFactory.getInstance();
        else
            this.graphIconFactory = graphIconFactory;
    }
    

    /**
     * Default constructor.
     */
    public DefaultGraphBundle() {
        this.graph = null;
        this.embeddings = new ArrayList<DefaultEmbedding>();
        this.annotations = new ArrayList<DefaultAnnotation>();
        invariantValues = new HashMap<Invariant, InvariantValue>();
        listeners = new ArrayList<GraphBundleListener>();
        this.graphIconFactory = DefaultGraphIconFactory.getInstance();
    }
    
    // implements GraphBundle
    public void setProperties(InternationalizedProperties properties) {
        this.properties = properties;
    }
    
    // implements GraphBundleView
    public DefaultEmbedding getEmbedding(int index) {
        return embeddings.get(index);
    }
    
    // implements GraphBundle
    public DefaultEmbedding createEmbedding() {
        DefaultEmbedding embedded = new DefaultEmbedding(graph, 0);
        embeddings.add(embedded);
        return embedded;
    }
    
    // implements GraphBundleView
    public int getEmbeddingCount() {
        return embeddings.size();
    }
    
    // implements GraphBundleView
    public DefaultEmbedding getEmbedding() {
        if (getEmbeddingCount() > 0)
            return getEmbedding(0);
        else
            return null;
    }
    
    // implements GraphBundle
    public DefaultGraph createGraph() {
        this.graph = new DefaultGraph();
        return graph;
    }
    
    // implements GraphBundle
    public DefaultAnnotation createAnnotation() {
        DefaultAnnotation annotation = new DefaultAnnotation(graph);
        annotations.add(annotation);
        return annotation;
    }

    // implements GraphBundleView
    public DefaultAnnotation getAnnotation() {
        if (getAnnotationCount() > 0)
            return getAnnotation(0);
        else
            return null;
    }
    
    // implements GraphBundleView
    public DefaultAnnotation getAnnotation(int index) {
        return annotations.get(index);
    }
    
    //implements GraphBundleView
    public int getAnnotationCount() {
        return annotations.size();
    }
    
    // implements GraphBundleView
    public InternationalizedProperties getProperties() {
        return properties;
    }
    
    // implements GraphBundleView
    public DefaultGraph getGraph() {
        return graph;
    }
    
    // implements GraphBundleView
    public String getName() {
        if (properties == null)
            return null;
        else
            return properties.getProperty("graph.name");
    }
    
    // implements GraphBundleView
    public String getDescription() {
        if (properties == null)
            return null;
        else
            return properties.getProperty("graph.description");
    }
    
    // implements getInvariantValue
    public InvariantValue getInvariantValue(Invariant invariant) {
        InvariantValue result = invariantValues.get(invariant);
        if (result == null || isOldInvariantValue(result)) {
            InvariantComputer v = InvariantManager.getInstance().getInvariantComputerFor(invariant);
            try {
                result = v.compute(this);
                addInvariantValue(result);
            } catch (Exception ex) {
                // makes sure Grinvin does not go down with defunct invariant computers
                Logger logger = Logger.getLogger("org.grinvin.invariants.computers");
                logger.log(Level.WARNING, "InvariantComputer " + v.getId() + " failed to compute invariant " + v.getInvariantId() + " for graph " + this.getName(), ex);
            }
        }
        return result;
    }
    
    // implements getCachedInvariantValue
    public InvariantValue getCachedInvariantValue(Invariant invariant) {
        InvariantValue result = invariantValues.get(invariant);
        if (result != null && !isOldInvariantValue(result)) {
            return result;
        } else {
            return null;
        }
    }
    
    private boolean isOldInvariantValue(InvariantValue value) {
        List<InvariantComputer> computers = InvariantManager.getInstance().getInvariantComputersFor(value.getInvariant());
        for (InvariantComputer computer : computers) {
            if (computer.getId().equals(value.getComputerId()) && computer.getVersion().compareTo(value.getComputerVersion()) > 0) {
                return true;
            } else if (isStandardInvariantComputer(computer) && getOldInvariantComputerId(computer).equals(value.getComputerId()) &&
                    computer.getVersion().compareTo(value.getComputerVersion()) > 0) {
                return true;
            }
        }
        return false;
    }
    
    private String getOldInvariantComputerId(InvariantComputer computer) {
        if (isStandardInvariantComputer(computer)) {
            String computerId = computer.getId();
            int pos = computerId.lastIndexOf('.');
            return "org.grinvin.invariants" + computerId.substring(pos);
        } else {
            return computer.getId();
        }
    }
    
    private boolean isStandardInvariantComputer(InvariantComputer computer) {
        return computer.getId().startsWith("org.grinvin.invariants.");
    }
    
    // implements GraphBundleView
    public Collection<InvariantValue> getInvariantValues() {
        return invariantValues.values();
    }
    
    // implements GraphBundleView
    public Set<Invariant> getInvariants() {
        return invariantValues.keySet();
    }
    
    //implements addInvariantValue
    public void addInvariantValue(InvariantValue value) {
        invariantValues.put(value.getInvariant(), value);
        fireGraphBundleChanged();
    }
    
    //
    public void addGraphBundleListener(GraphBundleListener listener) {
        listeners.add(listener);
    }
    
    //
    public void removeGraphBundleListener(GraphBundleListener listener) {
        listeners.remove(listener);
    }
    
    //
    public void fireGraphBundleChanged() {
        for (GraphBundleListener listener : listeners)
            listener.graphBundleChanged(this);
    }
    
    /* ============================================================
     * CACHED VALUES
     * ============================================================ */
    
    //
    private int cachedModCount;
    
    //
    private boolean [][] booleanAdjacencyMatrix;
    
    //
    public boolean[][] booleanAdjacencyMatrix() {
        if (booleanAdjacencyMatrix == null || graph.getModCount() != cachedModCount) {
            invalidateCache();
            booleanAdjacencyMatrix = Graphs.booleanAdjacencyMatrix(graph);
            cachedModCount = graph.getModCount();
        }
        return booleanAdjacencyMatrix;
    }
    
    //
    private int[][] adjacencyList;
    
    //
    public int[][] adjacencyList() {
        if (adjacencyList == null || graph.getModCount() != cachedModCount) {
            invalidateCache();
            adjacencyList = Graphs.adjacencyList(graph);
            cachedModCount = graph.getModCount();
        }
        return adjacencyList;
    }
    
    //
    private int[][] distanceMatrix;
    
    //
    public int[][] distanceMatrix() {
        if (distanceMatrix == null || graph.getModCount() != cachedModCount) {
            invalidateCache();
            distanceMatrix = Graphs.distanceMatrix(graph);
            cachedModCount = graph.getModCount();
        }
        return distanceMatrix;
    }

    //
    private int[] eccentricityList;
    
    //
    public int[] eccentricityList() {
        if (eccentricityList == null || graph.getModCount() != cachedModCount) {
            invalidateCache();
            eccentricityList = Graphs.eccentricityList(graph);
            cachedModCount = graph.getModCount();
        }
        return eccentricityList;
    }
    
    // clear the cached values
    private void invalidateCache() {
        booleanAdjacencyMatrix = null;
        adjacencyList = null;
        distanceMatrix = null;
        eccentricityList = null;
    }
    
    // clear the cached values and removed the cached invariantvalues
    public void invalidate() {
        invalidateCache();
        invariantValues.clear();
        fireGraphBundleChanged();
    }

    public void invalidate(InvariantValue value) {
        invariantValues.remove(value.getInvariant());
        fireGraphBundleChanged();
    }
}