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

« back to all changes in this revision

Viewing changes to src/sd/dv/cbinteraction.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 2001, University of Twente.
 
5
// Author: Henk van de Zandschulp (henkz@cs.utwente.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 Foundation, Inc.,
 
19
// 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
20
//-----------------------------------------------------------------------------
 
21
#include "lstring.h"
 
22
#include "inputfile.h"
 
23
#include "outputfile.h"
 
24
#include "cbinteraction.h"
 
25
#include "cbgraph.h"
 
26
#include "cbdmessage.h"
 
27
#include "gshape.h"
 
28
#include "shapeview.h"
 
29
#include "cbviewer.h"
 
30
#include "diagramviewer.h"
 
31
#include "subject.h"
 
32
 
 
33
 
 
34
CBInteraction::CBInteraction(CBGraph *g, Subject *n1, Subject *n2): 
 
35
                BinaryRelationship(g, n1, n2) {
 
36
        messages = new List<CBDMessage *>;
 
37
 
 
38
        //add an initial message
 
39
        string *dummy_msg = new string("edit this >");
 
40
        SetMessage(dummy_msg, 0 , False);
 
41
}
 
42
 
 
43
CBInteraction::~CBInteraction() {
 
44
        messages->clear();
 
45
        delete messages;
 
46
}
 
47
 
 
48
 
 
49
bool CBInteraction::SetConstraint(string *cons, const string *nm) {
 
50
        // empty constraint is allowed
 
51
        string t = *nm;
 
52
        if (*nm == "" || True) {        //HZ_TMP 2B solved with bison...
 
53
                *cons = *nm;
 
54
                return True;
 
55
        }
 
56
        else
 
57
                return False;
 
58
}
 
59
 
 
60
 
 
61
Subject::NameErrType CBInteraction::SetMessage(const CBDMessage *m, unsigned n, bool update) {
 
62
        if (*m->GetString() != "" && m->GetString()->letters() == 0)
 
63
                return Subject::IMPOSSIBLE_NAME;
 
64
        // Check for double messages.
 
65
        if (HasMessage(m)) {
 
66
                if (n >= messages->count() || 
 
67
                        *(*messages)[n]->GetString() != *m->GetString() || !update)
 
68
                        return Subject::HAS_ACTION;
 
69
        }
 
70
 
 
71
 
 
72
        if (!update)    // add new msg
 
73
                messages->add((CBDMessage *)m);
 
74
        else {  //update
 
75
                CBDMessage *dummy = (*messages)[n];
 
76
                dummy->SetString(m->GetString());
 
77
        } // update
 
78
 
 
79
        return Subject::OK;
 
80
}
 
81
 
 
82
 
 
83
 
 
84
Subject::NameErrType CBInteraction::SetMessage(string *s, unsigned n, bool update) {
 
85
        string txt = *s;
 
86
        CBDMessage::DirectionType msgDir = CBDMessage::TOSHAPE; //init
 
87
 
 
88
        if (*s != "") {
 
89
                if (txt.endsWith(">") || txt.endsWith("v")) {
 
90
                        msgDir = CBDMessage::TOSHAPE;
 
91
                        txt.remove();           //last char
 
92
                }
 
93
                if (txt.endsWith("<") || txt.endsWith("^")) {
 
94
                        msgDir = CBDMessage::FROMSHAPE;
 
95
                        txt.remove();           //last char
 
96
                }
 
97
        }
 
98
 
 
99
        if (*s != "" && s->letters() == 0)
 
100
                return Subject::IMPOSSIBLE_NAME;
 
101
        // Check for double messages.
 
102
 
 
103
        if (!update) {          // add ??
 
104
                if (*s != "") { // add valid new msg
 
105
                        CBDMessage *msg = new CBDMessage(&txt,msgDir, CBDMessage::FLATFLOW);
 
106
                        messages->add(msg);
 
107
                } 
 
108
        } else {        //update
 
109
                if (*s != "") {
 
110
                        CBDMessage *dummy = (*messages)[n];
 
111
 
 
112
                        if ((dummy->GetDirection() != msgDir) &&
 
113
                                (msgDir != CBDMessage::NONE)) {
 
114
                                dummy->SetDirection(msgDir);
 
115
                        }
 
116
                        dummy->SetString(&txt);
 
117
                } else
 
118
                        messages->removei(n);
 
119
        } // update
 
120
        *s = txt;       // return stripped(?) string
 
121
        return Subject::OK;
 
122
}
 
123
 
 
124
 
 
125
 
 
126
Subject::NameErrType CBInteraction::SetMessage(string *s, unsigned n, bool update, bool toggle) {
 
127
        string txt = *s;
 
128
        CBDMessage::DirectionType msgDir = CBDMessage::TOSHAPE; //init
 
129
 
 
130
        if (update && n>0)      {       //existing msg : reset init
 
131
                        msgDir = (*messages)[n]->GetDirection();
 
132
        }       //existing msg : reset init
 
133
 
 
134
        if (*s != "") {
 
135
                if (txt.endsWith(">") || txt.endsWith("v")) {
 
136
                        msgDir = CBDMessage::TOSHAPE;
 
137
                        txt.remove();           //last char
 
138
                }
 
139
                if (txt.endsWith("<") || txt.endsWith("^")) {
 
140
                        msgDir = CBDMessage::FROMSHAPE;
 
141
                        txt.remove();           //last char
 
142
                }
 
143
        }
 
144
 
 
145
        if (*s != "") {
 
146
                if (txt.endsWith(">") || txt.endsWith("v")) {
 
147
                        msgDir = (!toggle ? CBDMessage::TOSHAPE : CBDMessage::FROMSHAPE);
 
148
                        txt.remove();           //last char
 
149
                }
 
150
                if (txt.endsWith("<") || txt.endsWith("^")) {
 
151
                        msgDir = (!toggle ? CBDMessage::FROMSHAPE : CBDMessage::TOSHAPE);
 
152
                        txt.remove();           //last char
 
153
                }
 
154
        }
 
155
 
 
156
        if (*s != "" && s->letters() == 0)
 
157
                return Subject::IMPOSSIBLE_NAME;
 
158
        // Check for double messages.
 
159
 
 
160
        if (!update) {          // add ??
 
161
                if (*s != "") { // add valid new msg
 
162
                        CBDMessage *msg = new CBDMessage(&txt, msgDir, CBDMessage::FLATFLOW);
 
163
                        messages->add(msg);
 
164
                } 
 
165
        } else {        //update
 
166
                if (*s != "") {
 
167
                        CBDMessage *dummy = (*messages)[n];
 
168
                        if ((dummy->GetDirection() != msgDir) &&
 
169
                                (msgDir != CBDMessage::NONE)) {
 
170
                                dummy->SetDirection(msgDir);
 
171
                        }
 
172
                        dummy->SetString(&txt);
 
173
                } else
 
174
                        messages->removei(n);
 
175
        } // update
 
176
 
 
177
        *s = txt;       // return stripped(?) string
 
178
        return Subject::OK;
 
179
}
 
180
 
 
181
 
 
182
const CBDMessage *CBInteraction::GetMessage(unsigned n) {
 
183
        if (n < messages->count()) {
 
184
                return (*messages)[n];
 
185
        }
 
186
        return 0;
 
187
}
 
188
 
 
189
 
 
190
bool CBInteraction::HasMessage(const CBDMessage *m) {
 
191
        for (messages->first(); !messages->done(); messages->next()) {
 
192
                CBDMessage *at = messages->cur();
 
193
                if (*at->GetString() == *m->GetString())
 
194
                        return True;
 
195
        }
 
196
        return False;
 
197
}
 
198
 
 
199
 
 
200
void CBInteraction::WriteMembers(OutputFile *ofile) {
 
201
        BinaryRelationship::WriteMembers(ofile);
 
202
 
 
203
        unsigned num = messages->count();
 
204
        string x;
 
205
        (*ofile) << "\t{ Messages " << num << " }\n";
 
206
 
 
207
        for (unsigned i=0; i<num; i++) {
 
208
                (*ofile) << "\t{ Message " << '"' 
 
209
                        << *(*messages)[i]->GetString() << '"' << " }\n";
 
210
                CBDMessage::DirectionType2String((*messages)[i]->GetDirection(), &x);
 
211
                (*ofile) << "\t{ Direction " << x << " }\n";
 
212
                CBDMessage::ControlFlowType2String((*messages)[i]->GetFlow(), &x);
 
213
                (*ofile) << "\t{ Flow " << x << " }\n";
 
214
        }
 
215
}
 
216
 
 
217
bool CBInteraction::ReadMembers(InputFile *ifile, double format) {
 
218
        if (!BinaryRelationship::ReadMembers(ifile, format))
 
219
                return False;
 
220
        string val;
 
221
        if (!ifile->ReadAttribute("Messages", &val))
 
222
                return False;
 
223
        unsigned numItems = val.toint();
 
224
 
 
225
        string *a = new string;
 
226
        CBDMessage::DirectionType direction;
 
227
        CBDMessage::ControlFlowType flow;
 
228
 
 
229
        // Read messages data
 
230
        for (int i=0; i<(int) numItems; i++) {
 
231
                if (!ifile->ReadStringAttribute("Message", a)) {
 
232
                        delete a;
 
233
                        return False;
 
234
                }
 
235
                if (!ifile->ReadAttribute("Direction", &val))
 
236
                        return False;
 
237
                direction = CBDMessage::String2DirectionType(&val);
 
238
                if (!ifile->ReadAttribute("Flow", &val))
 
239
                        return False;
 
240
                flow = CBDMessage::String2ControlFlowType(&val);
 
241
                if (i == 0)             //overrule dummy "edit this" message
 
242
                        messages->clear();
 
243
                messages->add(new CBDMessage(a, direction, flow));
 
244
        }
 
245
        return True;
 
246
}