~ubuntu-branches/ubuntu/utopic/tcm/utopic

« back to all changes in this revision

Viewing changes to src/dg/graph.h

  • Committer: Bazaar Package Importer
  • Author(s): Otavio Salvador
  • Date: 2003-07-03 20:08:21 UTC
  • Revision ID: james.westby@ubuntu.com-20030703200821-se4xtqx25e5miczi
Tags: upstream-2.20
ImportĀ upstreamĀ versionĀ 2.20

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//------------------------------------------------------------------------------
 
2
// 
 
3
// This file is part of Toolkit for Conceptual Modeling (TCM).
 
4
// (c) copyright 1995, Vrije Universiteit Amsterdam.
 
5
// Author: Frank Dehne (frank@cs.vu.nl).
 
6
//
 
7
// TCM is free software; you can redistribute it and/or modify
 
8
// it under the terms of the GNU General Public License as published by
 
9
// the Free Software Foundation; either version 2 of the License, or
 
10
// (at your option) any later version.
 
11
//
 
12
// TCM is distributed in the hope that it will be useful,
 
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
// GNU General Public License for more details.
 
16
//
 
17
// You should have received a copy of the GNU General Public License
 
18
// along with TCM; if not, write to the Free Software
 
19
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 
20
// 02111-1307, USA.
 
21
//-----------------------------------------------------------------------------
 
22
#ifndef _GRAPH_H
 
23
#define _GRAPH_H
 
24
 
 
25
#include "llist.h"
 
26
#include "point.h"
 
27
#include "lstring.h"
 
28
class Node;
 
29
class Edge;
 
30
class Subject;
 
31
class OutputFile;
 
32
 
 
33
/// (abstract) graph class.
 
34
class Graph {
 
35
/*@Doc: {\large {\bf scope:} diagram} */
 
36
public:
 
37
        ///
 
38
        Graph(); 
 
39
        ///
 
40
        virtual ~Graph();
 
41
        ///
 
42
        void AddNode(Node *node);
 
43
        ///
 
44
        void AddEdge(Edge *edge);
 
45
        ///
 
46
        void RemoveNode(Node *node) {nodes->remove(node);}
 
47
        ///
 
48
        void RemoveEdge(Edge *edge) {edges->remove(edge);}
 
49
        ///
 
50
        bool HasNode(Node *node) {return nodes->find(node) != -1;}
 
51
        ///
 
52
        bool HasEdge(Edge *edge) {return edges->find(edge) != -1;}
 
53
        ///
 
54
        void ClearNodes() {nodes->clear();}
 
55
        ///
 
56
        void ClearEdges() {edges->clear();}
 
57
 
 
58
        /// checks if edge type may connect subject types 1 and 2.
 
59
        bool CheckConnection(int stype1, int stype2, int edgetype);
 
60
 
 
61
        /// checks if there is an edge in the graph that connects s1 and s2.
 
62
        bool IsConnected(Subject *s1, Subject *s2);
 
63
 
 
64
        /// same as above, but edge should also have name n.
 
65
        bool IsConnected(Subject *s1, Subject *s2, const string *n);
 
66
 
 
67
        /// here edge should also be of type t.
 
68
        bool IsConnected(Subject *s1, Subject *s2, int t);
 
69
 
 
70
        /// combination of the two members above.
 
71
        bool IsConnected(Subject *s1, Subject *s2, const string *n, int t);
 
72
 
 
73
        // the "Get" and "Complete" functions return the number of
 
74
        // elements added to the given output list.
 
75
 
 
76
        /// add to l all edges which connect the subjects.
 
77
        int CompleteSubjects(List<Subject *> *l);
 
78
 
 
79
        /// add to l all edges of which subject s is part.
 
80
        int CompleteSubject(List<Subject *> *l, Subject *s);
 
81
 
 
82
        /// add to l all edges which connect subjects s1 and s2.
 
83
        int CompleteSubject(List<Subject *> *l, Subject *s1, Subject *s2);
 
84
 
 
85
        /// add to l all subjects which are connected by edges already in l.
 
86
        int CompleteEdges(List<Subject *> *l);
 
87
 
 
88
        /// add to l the subjects that are directly connected to subject s.
 
89
        int GetConnected(List<Subject *> *l, Subject *s);
 
90
 
 
91
        /// add to l all nodes in the graph.
 
92
        int GetNodes(List<Subject *> *l);
 
93
 
 
94
        /// add to l all nodes of type t in the graph.
 
95
        int GetNodes(List<Subject *> *l, int t);
 
96
 
 
97
        /// add to l all nodes having name n in the graph.
 
98
        int GetNodes(List<Subject *> *l, const string *n);
 
99
 
 
100
        /// combination of two above members.
 
101
        int GetNodes(List<Subject *> *l, const string *n, int t);
 
102
 
 
103
        /// add to l all edges in the graph.
 
104
        int GetEdges(List<Subject *> *l);
 
105
 
 
106
        /// add to l all edges of type t in the graph.
 
107
        int GetEdges(List<Subject *> *l, int t);
 
108
 
 
109
        /// add to l all edges having name n in the graph.
 
110
        int GetEdges(List<Subject *> *l, const string *n);
 
111
 
 
112
        /// combination of two above members.
 
113
        int GetEdges(List<Subject *> *l, const string *n, int t);
 
114
 
 
115
        /// add to l all edges departing from subject s in the graph.
 
116
        int GetEdgesFrom(List<Subject *> *l, Subject *s);
 
117
 
 
118
        /// add to l all edges of type t departing from subject s.
 
119
        int GetEdgesFrom(List<Subject *> *l, Subject *s, int t);
 
120
 
 
121
        /// add to l all edges having name n departing from subject s.
 
122
        int GetEdgesFrom(List<Subject *> *l, Subject *s, const string *n);
 
123
 
 
124
        /// combination of two above members.
 
125
        int GetEdgesFrom(List<Subject *> *l, Subject *s, const string *n, int t);
 
126
 
 
127
        /// add to l all edges going to subject s.
 
128
        int GetEdgesTo(List<Subject *> *l, Subject *s);
 
129
 
 
130
        /// add to l all edges of type t going to subject s.
 
131
        int GetEdgesTo(List<Subject *> *l, Subject *s, int t);
 
132
 
 
133
        /// add to l all edges having name n going to subject s.
 
134
        int GetEdgesTo(List<Subject *> *l, Subject *s, const string *n);
 
135
 
 
136
        /// combination of the two above members.
 
137
        int GetEdgesTo(List<Subject *> *l, Subject *s, const string *n, int t);
 
138
 
 
139
        /// add to l all edges between subjects from and to
 
140
        int GetEdges(List<Subject *> *l, Subject *from, Subject *to);
 
141
 
 
142
        /// add to l all edges of type t between subjects from and to
 
143
        int GetEdges(List<Subject *> *l, Subject *from, Subject *to, int t);
 
144
 
 
145
        /// combination of the two above members.
 
146
        int GetEdges(List<Subject *> *l, Subject *from, Subject *to, const string *n);
 
147
 
 
148
        /// combination of two above members.
 
149
        int GetEdges(List<Subject *> *l, Subject *from, Subject *to, const string *n, int t);
 
150
 
 
151
        /// return number of nodes in the graph
 
152
        int CountNodes();
 
153
 
 
154
        /// return number of nodes of type t in the graph
 
155
        int CountNodes(int t);
 
156
 
 
157
        /// return number of nodes having name n in the graph
 
158
        int CountNodes(const string *n);
 
159
 
 
160
        /// return nr. of nodes of type t, having name n in the graph
 
161
        int CountNodes(const string *n, int t);
 
162
 
 
163
        /// return number of edges in the graph
 
164
        int CountEdges();
 
165
 
 
166
        /// return number of edges of type t in the graph
 
167
        int CountEdges(int t);
 
168
 
 
169
        /// return number of edges having name n in the graph
 
170
        int CountEdges(const string *n);
 
171
 
 
172
        /// return nr. of edges of type t, having name n in the graph
 
173
        int CountEdges(const string *n, int t);
 
174
 
 
175
        /// return nr. of edges departing from subject s
 
176
        int CountEdgesFrom(Subject *s);
 
177
 
 
178
        /// return nr. of edges of type t departing from subject s
 
179
        int CountEdgesFrom(Subject *s, int t);
 
180
 
 
181
        /// return number of edges having name n departing from subject s
 
182
        int CountEdgesFrom(Subject *s, const string *n);
 
183
 
 
184
        /// return number of edges of type t, having name n, departing from subject s
 
185
        int CountEdgesFrom(Subject *s, const string *n, int t);
 
186
 
 
187
        /// return nr. of edges going to subject s
 
188
        int CountEdgesTo(Subject *s);
 
189
 
 
190
        /// return nr. of edges of type t going to subject s
 
191
        int CountEdgesTo(Subject *s, int t);
 
192
 
 
193
        /// return number of edges having name n going to subject s
 
194
        int CountEdgesTo(Subject *s, const string *n);
 
195
 
 
196
        /// return number of edges of type t, having name n going to subject s
 
197
        int CountEdgesTo(Subject *s, const string *n, int t);
 
198
 
 
199
        /// return number of edges between subjects s1 and s2
 
200
        int CountEdges(Subject *s1, Subject *s2);
 
201
 
 
202
        /// return number of edges of type t between subjects s1 and s2
 
203
        int CountEdges(Subject *s1, Subject *s2, int t);
 
204
 
 
205
        /// return number of edges having name n between subjects s1 and s2
 
206
        int CountEdges(Subject *s1, Subject *s2, const string *n);
 
207
 
 
208
        /// return number of edges of type t, having name n between subjects s1 and s2
 
209
        int CountEdges(Subject *s1, Subject *s2, const string *n, int t);
 
210
 
 
211
 
 
212
        /// returns if there is some connected path of graph edges from s1 to s2.
 
213
        bool PathExists(Subject *s1, Subject *s2);
 
214
 
 
215
        /// returns if there is some conn. path of graph edges of type t from s1 to s2.
 
216
        bool PathExists(Subject *s1, Subject *s2, int t);
 
217
 
 
218
        // returns list of edges visited. takes edge direction into account.
 
219
        // not implemented due to different implementation method
 
220
        // by David Jansen <dnjansen@cs.utwente.nl>
 
221
        // bool PathExists(Subject *s1, Subject *s2, List<Edge *> *path);
 
222
 
 
223
        /// return if there's path from s1 to s2. irregardless edge direction.
 
224
        bool UndirectedPathExists(Subject *s1, Subject *s2);
 
225
 
 
226
        /// Writes all nodes and edges to outputfile f.
 
227
        void WriteSubjects(OutputFile *f); 
 
228
 
 
229
        /// initializes the table containing all allowed connections.
 
230
        virtual void InitConnections()=0;
 
231
 
 
232
        /// some counter can be used
 
233
        void SetCounter(int n) {counter = n;}
 
234
        ///
 
235
        int GetCounter() {return counter;}
 
236
 
 
237
        /// some string can be used as index prefix.
 
238
        void SetIndexPrefix(const char *s) {indexPrefix = s;}
 
239
        ///
 
240
        void SetIndexPrefix(const string *s) {indexPrefix = *s;}
 
241
        ///
 
242
        const string *GetIndexPrefix() const {return &indexPrefix;}
 
243
 
 
244
        /// index string := prefix + counter.
 
245
        void GetIndex(string *s);
 
246
 
 
247
        /// assign to index a new unique index
 
248
        virtual void GetNextIndex(string *index);
 
249
 
 
250
        /// return number of nodes in the graph having this index.
 
251
        virtual int CountIndexes(const string *index);
 
252
 
 
253
protected:
 
254
        /// Allowed 'connected' types in graph (depends on type of editor).
 
255
        int *nodeTypes;
 
256
 
 
257
        /// Allowed edges type in graph (depends on type of editor).
 
258
        int *edgeTypes;
 
259
 
 
260
        /// Max. number of different connected types and edge types.
 
261
        enum {MAX_TYPES=14};
 
262
 
 
263
        /// matrix to store what types can be connected by what edge types.
 
264
        int connections[MAX_TYPES][MAX_TYPES][MAX_TYPES]; 
 
265
 
 
266
        /// the graph nodes.
 
267
        List<Node *> *nodes;
 
268
 
 
269
        /// the graph edges.
 
270
        List<Edge *> *edges;
 
271
 
 
272
        // bool PathExists(Subject *s1, Subject *s2, List<Edge *> *path, 
 
273
        //              int edgetype, bool Directed);
 
274
        /// used by other PathExists.
 
275
        bool PathExists(Subject *s1, Subject *s2,
 
276
                        int edgetype, bool Directed);
 
277
 
 
278
        /// return number of nodes in l having this index 
 
279
        int CountIndex(const string *index, List<Subject *> *l);
 
280
 
 
281
private:
 
282
        /// current index prefix
 
283
        string indexPrefix;
 
284
 
 
285
        /// current counter
 
286
        int counter;
 
287
};
 
288
#endif