OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 17 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
17 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.sql.model.graph;
15
 
16
import org.openconcerto.sql.model.SQLName;
17
import org.openconcerto.sql.model.SQLTable;
18
 
19
import java.util.Iterator;
20
import java.util.Set;
21
 
61 ilm 22
import net.jcip.annotations.GuardedBy;
23
import net.jcip.annotations.ThreadSafe;
24
 
17 ilm 25
import org.jgrapht.DirectedGraph;
26
import org.jgrapht.Graph;
27
import org.jgrapht.UndirectedGraph;
28
import org.jgrapht.alg.ConnectivityInspector;
29
 
30
/**
31
 * Classe de base entre les graphes.
32
 *
33
 * @author ILM Informatique 22 décembre 2004
34
 */
61 ilm 35
@ThreadSafe
17 ilm 36
public abstract class BaseGraph {
37
 
61 ilm 38
    @GuardedBy("this")
39
    private final Graph<SQLTable, Link> graph;
17 ilm 40
 
41
    protected BaseGraph(DirectedGraph<SQLTable, Link> g) {
42
        this.graph = g;
43
    }
44
 
45
    protected BaseGraph(UndirectedGraph<SQLTable, Link> g) {
46
        this.graph = g;
47
    }
48
 
49
    /**
50
     * Retourne une chaine représentant ce graphe. D'abord les noeuds puis les liens:
51
     *
52
     * <pre>
53
     *        A
54
     *        B
55
     *        C
56
     *
61 ilm 57
     *        A B   label
17 ilm 58
     * </pre>
59
     *
60
     * Utile pour visualiser le graphe de la table dans JGraphPad.
61
     *
62
     * @return une représentation de ce graphe.
63
     */
61 ilm 64
    public synchronized String dump() {
17 ilm 65
        StringBuffer sb = new StringBuffer();
66
 
67
        Set vertices = this.graph.vertexSet();
68
        Iterator i = vertices.iterator();
69
        while (i.hasNext()) {
70
            SQLTable table = (SQLTable) i.next();
71
            sb.append(table.getSQLName()).append(table.hashCode()).append("\n");
72
        }
73
 
74
        sb.append("\n");
75
 
76
        Set edges = this.graph.edgeSet();
77
        i = edges.iterator();
78
        while (i.hasNext()) {
79
            Link l = (Link) i.next();
80
            final SQLName src = l.getSource().getSQLName();
81
            final SQLName dest = l.getTarget().getSQLName();
82
            sb.append(src).append("\t").append(dest).append("\t").append(l.getLabel().getName()).append("\n");
83
        }
84
 
85
        return sb.toString();
86
    }
87
 
88
    /**
89
     * Retourne toutes les tables qui sont liées de près ou de loin à la table passée.
90
     *
91
     * @param table la table source.
92
     * @return toutes les tables qui sont liées à la table passée.
93
     */
61 ilm 94
    public synchronized Set<SQLTable> getConnectedSet(SQLTable table) {
17 ilm 95
        final ConnectivityInspector<SQLTable, Link> insp;
96
        if (this.graph instanceof DirectedGraph)
97
            insp = new ConnectivityInspector<SQLTable, Link>((DirectedGraph<SQLTable, Link>) this.graph);
98
        else
99
            insp = new ConnectivityInspector<SQLTable, Link>((UndirectedGraph<SQLTable, Link>) this.graph);
100
        return insp.connectedSetOf(table);
101
    }
102
 
61 ilm 103
    public synchronized final Set<SQLTable> getAllTables() {
17 ilm 104
        return this.graph.vertexSet();
105
    }
106
 
61 ilm 107
    protected synchronized final Graph<SQLTable, Link> getGraph() {
17 ilm 108
        return this.graph;
109
    }
110
}