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

« back to all changes in this revision

Viewing changes to src/sd/bv/initialstate.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 "initialstate.h"
 
23
#include "stgraph.h"
 
24
#include "lstring.h"
 
25
#include "stringlist.h"
 
26
#include "inputfile.h"
 
27
#include "outputfile.h"
 
28
 
 
29
InitialState::InitialState(STGraph *g): State(g) {
 
30
        controlProcess = "";
 
31
        actions = new List<string *>;
 
32
}
 
33
 
 
34
InitialState::InitialState(const InitialState &s): State(s) {
 
35
        actions = new List<string *>;
 
36
        for (s.actions->first(); !s.actions->done(); s.actions->next()) {
 
37
                string *a = s.actions->cur();
 
38
                string *b = new string(*a);
 
39
                actions->add(b);
 
40
        }
 
41
        controlProcess = s.controlProcess;
 
42
}
 
43
 
 
44
InitialState::~InitialState() {
 
45
        actions->clear();
 
46
        delete actions;
 
47
}
 
48
 
 
49
Subject::NameErrType InitialState::SetAction(const string *s, unsigned n, bool update) {
 
50
        if (*s != "" && s->letters()==0)
 
51
                return Subject::IMPOSSIBLE_NAME;
 
52
        // Check for double actions.
 
53
        if (HasAction(s)) {
 
54
                 if (n >= actions->count() || *(*actions)[n] != *s || !update)
 
55
                        return Subject::HAS_ACTION;
 
56
        }
 
57
        StringList::Insert(actions, s, n, update);
 
58
        return Subject::OK;
 
59
}
 
60
 
 
61
const string *InitialState::GetAction(unsigned n) {
 
62
        if (n < actions->count())
 
63
                return (*actions)[n];
 
64
        return 0;
 
65
}
 
66
 
 
67
bool InitialState::HasAction(const string *s) {
 
68
        for (actions->first(); !actions->done(); actions->next()) {
 
69
                string *at = actions->cur();
 
70
                if (*at == *s)
 
71
                        return True;
 
72
        }
 
73
        return False;
 
74
}
 
75
 
 
76
bool InitialState::ReadMembers(InputFile *ifile, double format) {
 
77
        if (!State::ReadMembers(ifile, format))
 
78
                return False;
 
79
        // read control process name.
 
80
        if (format >= 1.13) {
 
81
                if (!ifile->ReadStringAttribute("ControlProcess", &controlProcess))
 
82
                        return False;
 
83
        }
 
84
        // read actions
 
85
        string val;
 
86
        if (!ifile->ReadAttribute("Actions", &val))
 
87
                return False;
 
88
        unsigned numItems = val.toint();
 
89
        for (unsigned i=0; i<numItems; i++) {
 
90
                string *at = new string;
 
91
                if (!ifile->ReadStringAttribute("Action", at)) {
 
92
                        delete at;
 
93
                        return False;
 
94
                }
 
95
                actions->add(at);
 
96
        }
 
97
        return True;
 
98
}
 
99
 
 
100
void InitialState::WriteMembers(OutputFile *ofile) {
 
101
        State::WriteMembers(ofile);
 
102
        // write name of control process.
 
103
        (*ofile) << "\t{ ControlProcess " << '"' << controlProcess << '"' << " }\n";
 
104
        // write actions
 
105
        unsigned numItems = actions->count();
 
106
        (*ofile) << "\t{ Actions " << numItems << " }\n";
 
107
        for (unsigned i=0; i<numItems; i++) {
 
108
                (*ofile) << "\t{ Action " << '"' 
 
109
                        << *(*actions)[i] << '"' << " } \n";
 
110
        }
 
111
}