~ubuntu-branches/ubuntu/intrepid/tcm/intrepid

« back to all changes in this revision

Viewing changes to src/sd/bv/psdiagram.c

  • 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
#include "psgraph.h"
 
23
#include "psviewer.h"
 
24
#include "pswindow.h"
 
25
#include "pschecks.h"
 
26
#include "emptyedge.h"
 
27
#include "psprocess.h"
 
28
#include "urlabeledbox.h"
 
29
#include "textbox.h"
 
30
#include "comment.h"
 
31
#include "line.h"
 
32
#include "messagedialog.h"
 
33
#include "psdiagram.h"
 
34
 
 
35
PSDiagram::PSDiagram(Config *c, PSWindow *d, PSViewer *v, PSGraph *g): 
 
36
                Diagram(c,d,v,g) {
 
37
        UpdateNodeType(1);
 
38
        UpdateEdgeType(1);
 
39
        treeSequenceNr = 0;
 
40
        actionSequenceNr = 0;
 
41
        psChecks = new PSChecks(this,g);
 
42
}
 
43
 
 
44
PSDiagram::~PSDiagram() {
 
45
        delete psChecks;
 
46
}
 
47
 
 
48
Thing *PSDiagram::CreateThing(int classNr) {
 
49
        Grafport *g = GetDiagramViewer()->GetGrafport();
 
50
        ShapeView *v = GetDiagramViewer()->GetCurView();
 
51
        PSGraph *pg = (PSGraph *)GetGraph();
 
52
        Thing *thing = 0;
 
53
        // view
 
54
        if (classNr == Code::VIEW)
 
55
                thing = new ShapeView(GetDiagramViewer());
 
56
        // node shapes
 
57
        else if (classNr == Code::UR_LABELED_BOX)
 
58
                thing = new URLabeledBox(v, g, 0, 0);
 
59
        else if (classNr == Code::TEXT_BOX)
 
60
                thing = new TextBox(v, g, 0, 0);
 
61
        // lines
 
62
        else if (classNr == Code::LINE) {
 
63
                Line *line = new Line(v, g, 0, 0, 0);
 
64
                line->SetFixedName(True);
 
65
                thing = line;
 
66
        }
 
67
        // nodes
 
68
        else if (classNr == Code::PS_PROCESS)
 
69
                thing = new PSProcess(pg);
 
70
        else if (classNr == Code::COMMENT)
 
71
                thing = new Comment(pg);
 
72
        // edges
 
73
        else if (classNr == Code::EMPTY_EDGE)
 
74
                thing = new EmptyEdge(pg, 0, 0);
 
75
        else
 
76
                error("%s, line %d: impl error: "
 
77
                        "wrong class number %d in file\n", __FILE__, __LINE__, classNr);
 
78
        return thing;
 
79
}
 
80
 
 
81
Node *PSDiagram::CreateNode(){
 
82
        Node *node = 0;
 
83
        PSGraph *pg = (PSGraph *)GetGraph();
 
84
        if (GetNodeType() == Code::PS_PROCESS)
 
85
                node = new PSProcess(pg);
 
86
        else if (GetNodeType() == Code::COMMENT)
 
87
                node = new Comment(pg);
 
88
        else
 
89
                error("%s, line %d: impl error: "
 
90
                        "unknown node type\n", __FILE__, __LINE__);
 
91
        return node;
 
92
}
 
93
 
 
94
Edge *PSDiagram::CreateEdge(Subject *n1, Subject *n2){
 
95
        if (!CheckEdgeConstraints(n1, n2))
 
96
                return 0;
 
97
        Edge *edge = 0;
 
98
        PSGraph *pg = (PSGraph *)GetGraph();
 
99
        if (GetEdgeType() == Code::EMPTY_EDGE)
 
100
                edge = new EmptyEdge(pg, n1, n2);
 
101
        else
 
102
                error("%s, line %d: impl error: "
 
103
                        "unknown edge type\n", __FILE__, __LINE__);
 
104
        return edge;
 
105
}
 
106
 
 
107
NodeShape *PSDiagram::CreateNodeShape(Node *node, int x, int y) {
 
108
        NodeShape *shape = 0;
 
109
        Grafport *g = GetDiagramViewer()->GetGrafport();
 
110
        ShapeView *v = GetDiagramViewer()->GetCurView();
 
111
        if (GetNodeShapeType() == Code::TEXT_BOX)
 
112
                shape = new TextBox(v, g, x, y);
 
113
        else if (GetNodeShapeType() == Code::UR_LABELED_BOX)
 
114
                shape = new URLabeledBox(v, g, x, y);
 
115
        else
 
116
                error("%s, line %d: impl error: "
 
117
                        "node shape type does not exist\n", __FILE__, __LINE__);
 
118
 
 
119
        if (check(shape)) {
 
120
                shape->SetSubject(node);
 
121
                shape->SetTextShape();
 
122
        }
 
123
        return shape;
 
124
}
 
125
 
 
126
Line *PSDiagram::CreateLine(
 
127
                Edge *edge, GShape *from, GShape *to, List<Point *> *l) {
 
128
        Line *line = 0;
 
129
        Grafport *g = GetDiagramViewer()->GetGrafport();
 
130
        ShapeView *v = GetDiagramViewer()->GetCurView();
 
131
        *((*l)[0]) = *(from->GetPosition());
 
132
        *((*l)[l->count()-1]) = *(to->GetPosition());
 
133
        if (GetLineType() == Code::LINE) {
 
134
                line = new Line(v, g, from, to, l, IsCurve());
 
135
                line->SetFixedName(True);
 
136
        }
 
137
        else {
 
138
                error("%s, line %d: impl error: "
 
139
                        "line type does not exist\n", __FILE__, __LINE__);
 
140
        }
 
141
        if (check(line)) {
 
142
                line->SetSubject(edge);
 
143
                line->SetTextShape();
 
144
        }
 
145
        return line;
 
146
}
 
147
 
 
148
void PSDiagram::UpdateNodeType(int num) {
 
149
        ((DiagramWindow *)GetMainWindow())->SetNodeName(num);
 
150
        switch (num) {
 
151
        case 1: SetNodeType(Code::PS_PROCESS);
 
152
                SetNodeShapeType(Code::UR_LABELED_BOX);
 
153
                break;
 
154
        case 2: SetNodeType(Code::COMMENT);
 
155
                SetNodeShapeType(Code::TEXT_BOX);
 
156
                break;
 
157
        default:
 
158
                error("%s, line %d: impl error: "
 
159
                "unknown node type selected\n", __FILE__, __LINE__);
 
160
        }
 
161
}
 
162
 
 
163
void PSDiagram::UpdateEdgeType(int num) {
 
164
        ((DiagramWindow *)GetMainWindow())->SetEdgeName(num);
 
165
        switch(num) {
 
166
        case 1: SetEdgeType(Code::EMPTY_EDGE);
 
167
                SetLineType(Code::LINE);
 
168
                break;
 
169
        default:
 
170
                error("%s, line %d: impl error: "
 
171
                "unknown edge type selected\n", __FILE__, __LINE__);
 
172
        }
 
173
}
 
174
 
 
175
bool PSDiagram::CheckEdgeConstraints(Subject *subj1, Subject *subj2) {
 
176
        if (!CheckConnection(subj1, subj2))
 
177
                return False;
 
178
        if ((subj1 == subj2) || GetGraph()->PathExists(subj1, subj2)) {
 
179
                ShowDialog(MessageDialog::ERROR, "Error",
 
180
                        "This connection violates the tree constraint");
 
181
                return False;
 
182
        }
 
183
        return True;
 
184
}
 
185
 
 
186
void PSDiagram::SaveEntries() {
 
187
        TraverseTree(False);
 
188
        Diagram::SaveEntries();
 
189
        ((PSViewer *)GetDiagramViewer())->UpdateShowSequences();
 
190
}
 
191
 
 
192
unsigned PSDiagram::TraverseTree(bool chec) {
 
193
        unsigned errors = 0;
 
194
        List<Subject *> all_nodes;
 
195
        treeSequenceNr = 0;
 
196
        actionSequenceNr = 0;
 
197
        GetGraph()->GetNodes(&all_nodes, Code::PS_PROCESS);
 
198
        PSProcess *root;
 
199
        unsigned nrRoots = FindRoot(&all_nodes, &root);
 
200
        List<Subject *> tree_nodes;
 
201
 
 
202
        if (nrRoots == 1) {
 
203
                root->SetRoot(True);
 
204
                tree_nodes.add(root);
 
205
                        if (chec)
 
206
                                errors += psChecks->CheckRootOperator(root, chkbuf);
 
207
                errors += TraverseChildren(root, &tree_nodes, chec);
 
208
        }
 
209
        else if (nrRoots == 0 && chec) {
 
210
                chkbuf +="* Error: there is no root process\n";
 
211
                errors++;
 
212
        }
 
213
        else if (nrRoots > 1 && chec) {
 
214
                chkbuf +="* Error: there is no unique root process\n";
 
215
                errors++;
 
216
        }
 
217
        // Check if there are remaining nodes not part of the tree:
 
218
        for (all_nodes.first(); !all_nodes.done(); all_nodes.next()) {
 
219
                Subject *node = all_nodes.cur();
 
220
                if (tree_nodes.find(node) == -1) {
 
221
                        // give remaining node unused sequence numbers.
 
222
                        ((PSProcess *)node)->SetTreeSequence(0);
 
223
                        ((PSProcess *)node)->SetActionSequence(0);
 
224
                        // give error if required.
 
225
                        if (chec && nrRoots == 1 && *node->GetName() != "") {
 
226
                                // empty names are noted elsewhere.
 
227
                                errors++;
 
228
                                chkbuf += "* Error: Process '";
 
229
                                chkbuf += *node->GetName();
 
230
                                chkbuf += "' is not part of the main tree\n";
 
231
                        }
 
232
                }
 
233
        }
 
234
        return errors;
 
235
}
 
236
 
 
237
unsigned PSDiagram::FindRoot(List<Subject *> *nodes, PSProcess **root) {
 
238
        // find the root: the subject that has the shape with
 
239
        // the smallest y-coordinate.
 
240
        Point root_pos;
 
241
        unsigned card = 0;
 
242
        if (nodes->first()) {
 
243
                PSProcess *node = (PSProcess *)nodes->cur();
 
244
                Shape *nshape = GetDiagramViewer()->GetShape(node);
 
245
                root_pos = *nshape->GetPosition();
 
246
                *root = node;
 
247
                card = 1;
 
248
                while (nodes->next()) {
 
249
                        node = (PSProcess *)nodes->cur();
 
250
                        nshape = GetDiagramViewer()->GetShape(node);
 
251
                        const Point *pt = nshape->GetPosition();
 
252
                        if (root_pos.y > pt->y) {
 
253
                                root_pos = *pt;
 
254
                                *root = node;
 
255
                                card = 1;
 
256
                        }
 
257
                        else if (root_pos.y == pt->y) {
 
258
                                // more than one root ?
 
259
                                card++;
 
260
                        }
 
261
                }
 
262
        }
 
263
        return card;
 
264
}
 
265
 
 
266
void PSDiagram::FindChildren(PSProcess *parent, List<Subject *> *nodes, 
 
267
                        List<Subject *> *children) {
 
268
        List<Subject *> edges;
 
269
        List<Subject *> child_edges;
 
270
        // find all edges that connect the parent with its children.
 
271
        GetGraph()->CompleteSubject(&edges, parent);
 
272
        for (edges.first(); !edges.done(); edges.next()) {
 
273
                Edge *edge = (Edge *)edges.cur();
 
274
                Subject *n1 = edge->GetSubject1();
 
275
                Subject *n2 = edge->GetSubject2();
 
276
                if (n1 == parent && nodes->find(n2) == -1) {
 
277
                        child_edges.add(edge);
 
278
                }
 
279
                else if (n2 == parent && nodes->find(n1) == -1) {
 
280
                        child_edges.add(edge);
 
281
                }
 
282
        }
 
283
        children->empty();
 
284
        Edge *leftMost = 0;
 
285
        // look repeatedly for the 'leftMost' edge: i.e. the
 
286
        // edge whose line connects to the shape of the parent
 
287
        // with the smallest x-coordinate.
 
288
        while ((leftMost = FindLeftMostEdge(parent, &child_edges))) {
 
289
                if (leftMost->GetSubject2() == parent)
 
290
                        children->add(leftMost->GetSubject1());
 
291
                else
 
292
                        children->add(leftMost->GetSubject2());
 
293
                child_edges.remove(leftMost);
 
294
        }
 
295
}
 
296
 
 
297
Edge *PSDiagram::FindLeftMostEdge(PSProcess *parent, List<Subject *> *child_edges) {
 
298
        Edge *leftMost =0;
 
299
        int min_x;
 
300
        if (child_edges->first()) {
 
301
                Edge *edge = (Edge *)(*child_edges)[0];
 
302
                Line *line = (Line *)GetDiagramViewer()->GetShape(edge);
 
303
                Shape *nshape = GetDiagramViewer()->GetShape(parent);
 
304
                // we have to take care of the two different directions 
 
305
                // of the line.
 
306
                if (line->GetFromShape() == nshape)
 
307
                        min_x = (*line->GetLine())[0]->x;
 
308
                else
 
309
                        min_x = (*line->GetLine())[line->NrPoints()-1]->x;
 
310
                leftMost = edge;
 
311
                while (child_edges->next()) {
 
312
                        edge = (Edge *)child_edges->cur();
 
313
                        line = (Line *)GetDiagramViewer()->GetShape(edge);
 
314
                        Point *pt;
 
315
                        if (line->GetFromShape() == nshape)
 
316
                                pt = (*line->GetLine())[0];
 
317
                        else
 
318
                                pt = (*line->GetLine())[line->NrPoints()-1];
 
319
                        if (pt->x < min_x) {
 
320
                                min_x = pt->x; 
 
321
                                leftMost = edge;
 
322
                        }
 
323
                }
 
324
        }
 
325
        return leftMost;
 
326
}
 
327
 
 
328
unsigned PSDiagram::TraverseChildren(PSProcess *parent, List<Subject *> *nodes, bool chec) {
 
329
        unsigned errors = 0;
 
330
        List<Subject *> children;
 
331
        FindChildren(parent, nodes, &children);
 
332
        // list name of parent and of their children.
 
333
        // Call this function recursively for all the children.
 
334
        for (children.first(); !children.done(); children.next()) {
 
335
                PSProcess *child = (PSProcess *)children.cur();
 
336
                child->SetRoot(False);
 
337
                nodes->add(children.cur());
 
338
        }
 
339
        if (chec) {
 
340
                errors += psChecks->CheckChildPositions(
 
341
                        GetDiagramViewer(), parent, &children, chkbuf);
 
342
                errors += psChecks->CheckChildOperators(parent, &children, chkbuf);
 
343
        }
 
344
        for (children.first(); !children.done(); children.next())
 
345
                errors += TraverseChildren((PSProcess *)children.cur(), nodes, chec);
 
346
        if (children.count() == 0) {
 
347
                char op = parent->GetOperator();
 
348
                if (op != '?' && op != '!')
 
349
                        parent->SetAction(True);
 
350
                else
 
351
                        parent->SetAction(False);
 
352
        }
 
353
        else if (children.count() == 1) {
 
354
                // parent of a '!' process is also considered as a action.
 
355
                PSProcess *p = (PSProcess *)children[0];
 
356
                if (p->GetOperator() == '!')
 
357
                        parent->SetAction(True);
 
358
                else
 
359
                        parent->SetAction(False);
 
360
        }
 
361
        else
 
362
                parent->SetAction(False);
 
363
        parent->SetTreeSequence(++treeSequenceNr);
 
364
        if (parent->IsAction())
 
365
                parent->SetActionSequence(++actionSequenceNr);
 
366
        else
 
367
                parent->SetActionSequence(0);
 
368
 
 
369
        return errors;
 
370
}
 
371
 
 
372
void PSDiagram::CheckDocument() {
 
373
        chkbuf = "";
 
374
        unsigned errors = 0;
 
375
        errors += TraverseTree(True);
 
376
        ((PSViewer *)GetDiagramViewer())->UpdateShowSequences();
 
377
        errors += psChecks->CheckNamelessNodes(Code::PS_PROCESS, chkbuf);
 
378
        errors += psChecks->CheckDoubleProcessNames(chkbuf);
 
379
        ReportCheck(errors, &chkbuf);
 
380
}
 
381
 
 
382
bool PSDiagram::SetOperator(PSProcess *proc, const string *s) {
 
383
        List<GShape *> shapes;
 
384
        GetDiagramViewer()->GetShapes(proc, &shapes);
 
385
        // empty string means sequence (space).
 
386
        char op;
 
387
        if (s->length() == 0)
 
388
                op = ' ';
 
389
        else
 
390
                op = (*s)[0];
 
391
        if (proc->SetOperator(op)) {
 
392
                if (check(shapes.first()))
 
393
                        do
 
394
                                ((URLabeledBox *)shapes.cur())->UpdateURLabel(s);
 
395
                        while (shapes.next());
 
396
                else 
 
397
                        return False;
 
398
        }
 
399
        else {
 
400
                string txt = *s + " is not a valid process operator";
 
401
                ShowDialog(MessageDialog::ERROR, "Error", &txt);
 
402
                return False;
 
403
        }
 
404
        return True;
 
405
}
 
406
 
 
407
bool PSDiagram::SetText(TextShape *t, const string *s) {
 
408
        const string *description = t->GetDescription();
 
409
        Subject *subj = t->GetParent()->GetSubject();
 
410
        if (*description == "Process Operator" &&
 
411
                    (subj->GetClassType()==Code::PS_PROCESS))
 
412
                return SetOperator((PSProcess *)subj, s);
 
413
        else
 
414
                return Diagram::SetText(t, s);
 
415
}