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

« back to all changes in this revision

Viewing changes to src/sd/bv/transition.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 1996, 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 "transition.h"
 
23
#include "stgraph.h"
 
24
#include "inputfile.h"
 
25
#include "outputfile.h"
 
26
#include "stringlist.h"
 
27
#include "error.h"
 
28
 
 
29
Transition::Transition(STGraph *g, Subject *n1, Subject *n2): Edge(g, n1, n2) {
 
30
        SetDirected(True);
 
31
        event = "";
 
32
        actions = new List<string *>;
 
33
}
 
34
 
 
35
Transition::Transition(const Transition &t): Edge(t) {
 
36
        event = t.event;
 
37
        actions = new List<string *>;
 
38
        for (t.actions->first(); !t.actions->done(); t.actions->next()) {
 
39
                string *a = t.actions->cur();
 
40
                string *b = new string (*a);
 
41
                actions->add(b);
 
42
        }
 
43
}
 
44
 
 
45
Transition::~Transition() {
 
46
        actions->clear();
 
47
        delete actions;
 
48
}
 
49
 
 
50
Subject::NameErrType Transition::SetName(const string *) {
 
51
        error("transition label can't be edited\n");
 
52
        return Subject::WRONG_NAME;
 
53
}
 
54
 
 
55
Subject::NameErrType Transition::SetEvent(const string *s) {
 
56
        if (*s != "" && s->letters() == 0)
 
57
                return Subject::IMPOSSIBLE_NAME;
 
58
        // Check for double event strings between same pair of nodes.
 
59
        List<Subject *> edges;
 
60
        if (*s != "" && *s != event) {
 
61
                GetGraph()->GetEdges(&edges, GetSubject1(), 
 
62
                                        GetSubject2(), Code::TRANSITION);
 
63
                for (edges.first(); !edges.done(); edges.next()) {
 
64
                        Transition *t = (Transition *)edges.cur();
 
65
                        if (t != this && *t->GetEvent() == *s)
 
66
                                return Subject::DOUBLE_EDGE;
 
67
                }
 
68
        }
 
69
        event = *s;
 
70
        return Subject::OK;
 
71
}
 
72
 
 
73
Subject::NameErrType Transition::SetAction(const string *s, unsigned n, bool update) {
 
74
        if (*s != "" && s->letters() == 0)
 
75
                return Subject::IMPOSSIBLE_NAME;
 
76
        // Check for double actions.
 
77
        if (HasAction(s)) {
 
78
                if (n >= actions->count() || *(*actions)[n] != *s || !update)
 
79
                        return Subject::HAS_ACTION;
 
80
        }
 
81
        StringList::Insert(actions, s, n, update);
 
82
        return Subject::OK;
 
83
}
 
84
 
 
85
const string *Transition::GetAction(unsigned n) {
 
86
        if (n < actions->count()) {
 
87
                return (*actions)[n];
 
88
        }
 
89
        return 0;
 
90
}
 
91
 
 
92
bool Transition::HasAction(const string *s) {
 
93
        for (actions->first(); !actions->done(); actions->next()) {
 
94
                string *at = actions->cur();
 
95
                if (*at == *s)
 
96
                        return True;
 
97
        }
 
98
        return False;
 
99
}
 
100
 
 
101
void Transition::WriteMembers(OutputFile *ofile) {
 
102
        Edge::WriteMembers(ofile);
 
103
        (*ofile) << "\t{ Event " << '"' << event << '"' << " }\n";
 
104
 
 
105
        unsigned num = actions->count();
 
106
        (*ofile) << "\t{ Actions " << num << " }\n";
 
107
        for (unsigned i=0; i<num; i++) 
 
108
                (*ofile) << "\t{ Action " << '"' 
 
109
                        << *(*actions)[i] << '"' << " }\n";
 
110
}
 
111
 
 
112
bool Transition::ReadMembers(InputFile *ifile, double format) {
 
113
        if (!Edge::ReadMembers(ifile, format))
 
114
                return False;
 
115
        if (!ifile->ReadStringAttribute("Event", &event))
 
116
                return False;
 
117
        string val;
 
118
        if (!ifile->ReadAttribute("Actions", &val))
 
119
                return False;
 
120
        unsigned numItems = val.toint();
 
121
        for (unsigned i=0; i<numItems; i++) {
 
122
                string *a = new string;
 
123
                if (!ifile->ReadStringAttribute("Action", a)) {
 
124
                        delete a;
 
125
                        return False;
 
126
                }
 
127
                actions->add(a);
 
128
        }
 
129
        return True;
 
130
}