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

« back to all changes in this revision

Viewing changes to src/sd/bv/adsclockmanager.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
// This file is part of Toolkit for Conceptual Modeling (TCM).
 
3
// (c) copyright 2001, Universiteit Twente.
 
4
// Author: Rik Eshuis (eshuis@cs.utwente.nl).
 
5
//
 
6
// TCM is free software; you can redistribute it and/or modify
 
7
// it under the terms of the GNU General Public License as published by
 
8
// the Free Software Foundation; either version 2 of the License, or
 
9
// (at your option) any later version.
 
10
//
 
11
// TCM is distributed in the hope that it will be useful,
 
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
// GNU General Public License for more details.
 
15
//
 
16
// You should have received a copy of the GNU General Public License
 
17
// along with TCM; if not, write to the Free Software
 
18
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 
19
// 02111-1307, USA.
 
20
////////////////////////////////////////////////////////////////////////////////
 
21
#include "adsclockmanager.h"
 
22
#include "adsclockconstraint.h"
 
23
#include "outputfile.h"
 
24
#include "stdio.h"
 
25
#include <stdlib.h>
 
26
#include <ctype.h>
 
27
 
 
28
ClockManager::ClockManager(){
 
29
  int i;
 
30
  for (i=0;i<MAX;i++){
 
31
    integer_timespent[i]=0;
 
32
    fraction_timespent[i]=0;
 
33
  }
 
34
  interval=0;
 
35
}
 
36
 
 
37
 
 
38
ClockManager::~ClockManager(){
 
39
}
 
40
 
 
41
 
 
42
ClockManager::ClockManager(const ClockManager &c){
 
43
  cl=c.cl;
 
44
  int clistlen=cl.count();
 
45
  int i;
 
46
  for (i=0;i<clistlen   ;i++){
 
47
    integer_timespent[i]=c.integer_timespent[i];
 
48
    fraction_timespent[i]=c.fraction_timespent[i];
 
49
  }
 
50
  interval=c.interval;
 
51
}
 
52
 
 
53
 
 
54
 
 
55
bool ClockManager::operator==( ClockManager comp){
 
56
  int clistlen=cl.count();
 
57
  int compclistlen=comp.cl.count();
 
58
 
 
59
  int i,j;
 
60
  for (i=0;i<clistlen;i++){
 
61
    bool found=False;
 
62
    for (j=0;j<compclistlen;j++){
 
63
       if ( (cl[i]==comp.cl[j]) && (integer_timespent[i]==comp.integer_timespent[j])&& (fraction_timespent[i]==comp.fraction_timespent[j]) ){
 
64
        found=True;
 
65
        break;
 
66
      }
 
67
    }
 
68
    if (!found) return False;
 
69
  }
 
70
  for (j=0;j<compclistlen;j++){
 
71
    bool found=False;
 
72
    for (i=0;i<clistlen;i++){
 
73
       if ((cl[i] == comp.cl[j] ) && (integer_timespent[i]==comp.integer_timespent[j]) && (fraction_timespent[i]==comp.fraction_timespent[j]) )  {
 
74
        found=True;
 
75
        break;
 
76
      }
 
77
    }
 
78
    if (!found) return False;
 
79
  }
 
80
  return True;
 
81
}
 
82
 
 
83
 
 
84
 
 
85
bool ClockManager::ExistScheduled(){
 
86
  return (cl.count()>0);
 
87
}
 
88
 
 
89
 
 
90
bool ClockManager::ExistTimeouts(){
 
91
    int clistlen=cl.count();
 
92
    for (int i=0;i<clistlen;i++){
 
93
      if ((integer_timespent[i]==0) && (fraction_timespent[i]==0)){
 
94
        return True;
 
95
      }
 
96
    }  
 
97
    return False;
 
98
}
 
99
 
 
100
 
 
101
 
 
102
 
 
103
// Get all clockconstraints that must be responded to NOW
 
104
void ClockManager::GetTimeouts(List <ClockConstraint *> *l){
 
105
  if (!ExistScheduled()) {
 
106
    error("There are no timeouts scheduled!\n");
 
107
  }
 
108
  else{
 
109
    int i;
 
110
    int clistlen=cl.count();
 
111
    for (i=0;i<clistlen;i++){
 
112
      if ((integer_timespent[i]==0) && (fraction_timespent[i]==0)){
 
113
        l->add(cl[i]);
 
114
      }
 
115
    }
 
116
  }
 
117
}
 
118
 
 
119
 
 
120
 
 
121
 
 
122
bool ClockManager::Update(List <ClockConstraint *> *zero, 
 
123
                          List <ClockConstraint *> *off){
 
124
 
 
125
  // reset off timers 
 
126
 
 
127
  // locate timers in cl
 
128
  for (off->first();!off->done();off->next()){
 
129
    int index=cl.find(off->cur());  // must exists due to assumption
 
130
    if (index==-1){ 
 
131
      error("I cannot turn off a clock constraint that is not turned on!\n"); 
 
132
      return False;  
 
133
    }
 
134
    integer_timespent[index]=-1; // used below to update array integer_timespent
 
135
  }
 
136
  int nrtimer=cl.count();
 
137
 
 
138
  // remove timers from cl
 
139
  for (off->first();!off->done();off->next()){
 
140
    cl.remove(off->cur());  // off->cur() must exist due to assumption
 
141
  }
 
142
 
 
143
 
 
144
 
 
145
  // remove off timers from array timespent
 
146
  int i;
 
147
  for (i=0;i<nrtimer;i++){
 
148
    if (integer_timespent[i]==-1){
 
149
 
 
150
      int j;
 
151
   
 
152
      // remove from timespent
 
153
      for (j=i;j<nrtimer-1;j++){
 
154
        integer_timespent[j]=integer_timespent[j+1];
 
155
        fraction_timespent[j]=fraction_timespent[j+1];
 
156
      }
 
157
      
 
158
    }
 
159
  }
 
160
 
 
161
 
 
162
  for (zero->first();!zero->done();zero->next()){
 
163
    //zero->cur()->Write();
 
164
    if (cl.find(zero->cur())>-1){
 
165
        error("Warning: hyperedge ");
 
166
        string str= cl[cl.find(zero->cur())]->GetLimit();
 
167
        error(str.getstr());
 
168
        error(" multiple times enabled!\n");
 
169
        return False;
 
170
    }
 
171
    else{
 
172
      cl.add(zero->cur());
 
173
      integer_timespent[cl.count()-1]=zero->cur()->GetLimit();
 
174
      fraction_timespent[cl.count()-1]=0;
 
175
    }    
 
176
  }  
 
177
  return True;
 
178
}
 
179
 
 
180
 
 
181
 
 
182
 
 
183
 
 
184
 
 
185
bool ClockManager::Tick(){
 
186
  int i;
 
187
  int clen=cl.count();
 
188
  for (i=0;i<clen;i++){
 
189
    if (fraction_timespent[i]==0){
 
190
      if (integer_timespent[i]>0){ 
 
191
        integer_timespent[i]--;
 
192
      }
 
193
      else{ // (integer_timespent[i]==0)
 
194
        error("A timeout is not responded to in time!\n");
 
195
        return False;
 
196
      }  
 
197
      fraction_timespent[i]=interval-1;
 
198
    }
 
199
    else{ // fraction_timespent[i]>0
 
200
      fraction_timespent[i]--;  
 
201
    }
 
202
  }
 
203
  return True;
 
204
}
 
205
 
 
206
 
 
207
 
 
208
 
 
209
 
 
210
 
 
211
void ClockManager::Write(OutputFile *ofile){
 
212
  (*ofile) << "Status of clock manager:\n";
 
213
  for (cl.first();!cl.done();cl.next()){
 
214
       cl.cur()->Write(ofile);
 
215
  }
 
216
  unsigned i;
 
217
  for (i=0;i<cl.count();i++){
 
218
     (*ofile) << "Time spent " << integer_timespent[i] <<"\t"<< 
 
219
                fraction_timespent[i] << "/"<< interval  << "!\n";
 
220
  }
 
221
}
 
222
 
 
223
 
 
224
void ClockManager::Write(){
 
225
  std::cout << "Status of clock manager:\n";
 
226
  for (cl.first();!cl.done();cl.next()){
 
227
       cl.cur()->Write();
 
228
  }
 
229
  unsigned i;
 
230
  for (i=0;i<cl.count();i++){
 
231
     std::cout << "Time spent " << integer_timespent[i] <<"\t"<< 
 
232
                fraction_timespent[i] << "/"<< interval  << "!\n";
 
233
  }
 
234
}