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

« back to all changes in this revision

Viewing changes to src/sd/bv/adsvaluation.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 "adsvaluation.h"
 
22
#include "adspropertyvaluation.h"
 
23
#include <string.h>
 
24
#include <ctype.h>
 
25
#include "outputfile.h"
 
26
#include "adshypergraph.h"
 
27
#include "adsclock.h"
 
28
#include "adsclockconstraint.h"
 
29
#include "code.h"
 
30
 
 
31
ADSValuation::ADSValuation(ADSCks *c, StateType s): Node(c){
 
32
  index=0;
 
33
  st=s;
 
34
  cm.SetInterval(c->GetADSHyperGraph()->GetNrTimeouts()+1);
 
35
  key =0;
 
36
}
 
37
 
 
38
ADSValuation::~ADSValuation(){
 
39
}
 
40
 
 
41
 
 
42
ADSValuation::ADSValuation(const ADSValuation &copy): Node (copy.GetGraph()) {
 
43
  cfg=copy.cfg;
 
44
  tlist =copy.tlist;
 
45
  plist = copy.plist;
 
46
  clist = copy.clist;
 
47
 
 
48
  /*
 
49
  qclist = copy.qclist;
 
50
  qtermlist=copy.qtermlist;
 
51
  queue = copy.queue;
 
52
  */
 
53
 
 
54
  cm = copy.cm;
 
55
  st = copy.st;
 
56
  index= 0;
 
57
}
 
58
 
 
59
ADSValuation::ADSValuation(const ADSValuation &copy, StateType s) : Node(copy.GetGraph()) {
 
60
  cfg=copy.cfg;
 
61
  tlist =copy.tlist;
 
62
  plist = copy.plist;
 
63
  clist = copy.clist;
 
64
 
 
65
 
 
66
  /*
 
67
  qclist = copy.qclist;
 
68
  qtermlist=copy.qtermlist;
 
69
  queue = copy.queue;
 
70
  */
 
71
 
 
72
  cm = copy.cm;
 
73
  st = s;
 
74
  index=0;
 
75
}
 
76
 
 
77
bool ADSValuation::operator==( ADSValuation &comp) {
 
78
  if (st!=comp.st) return False;
 
79
  if (cfg.length()!= comp.cfg.length()) return False;
 
80
 
 
81
  // compare the configurations
 
82
  if (!cfg.contains(comp.cfg)) return False;
 
83
  if (!comp.cfg.contains(cfg)) return False;
 
84
 
 
85
  // compare the terminated list nodes 
 
86
  if (tlist.length()!= comp.tlist.length()) return False;
 
87
  if (!tlist.contains(comp.tlist)) return False;
 
88
  if (!comp.tlist.contains(tlist)) return False;
 
89
 
 
90
  // compare the property valuations
 
91
  int plistlen=plist.count();
 
92
  int compplistlen=comp.plist.count();
 
93
  int i,j;
 
94
  for (i=0;i<plistlen;i++){
 
95
    bool found=False;
 
96
    for (j=0;j<compplistlen;j++){  
 
97
       if ( (* (plist[i])) == (* ( comp.plist[j])) ){
 
98
        found=True;
 
99
        break;
 
100
      }
 
101
    }
 
102
    if (!found) return False;
 
103
  }
 
104
  for (j=0;j<compplistlen;j++){   
 
105
    bool found=False;       
 
106
    for (i=0;i<plistlen;i++){
 
107
       if ( (* (plist[i])) == (* ( comp.plist[j])) ) {
 
108
        found=True;
 
109
        break;
 
110
      }
 
111
    }
 
112
    if (!found) return False;
 
113
  }
 
114
 
 
115
  // compare the clock constraints
 
116
  int clistlen=clist.count();
 
117
  int compclistlen=comp.clist.count();
 
118
  for (i=0;i<clistlen;i++){
 
119
    bool found=False;
 
120
    for (j=0;j<compclistlen;j++){
 
121
       if ( (* (clist[i] ) ) == (* ( comp.clist[j]) )) {
 
122
        found=True;
 
123
        break;
 
124
      }
 
125
    }
 
126
    if (!found) return False;
 
127
  }
 
128
  for (j=0;j<compclistlen;j++){
 
129
    bool found=False;
 
130
    for (i=0;i<clistlen;i++){
 
131
       if ( (* (clist[i] ) ) == (* ( comp.clist[j])) ){
 
132
        found=True;
 
133
        break;
 
134
      }
 
135
    }
 
136
    if (!found) return False;
 
137
  }
 
138
 
 
139
  // compare the clock manager
 
140
  if (!(cm==comp.cm)) return False; // check is needed to ensure that all clock constraints are eventually met
 
141
 
 
142
  // ADDD CHECKS FOR ILS!!!!!
 
143
 
 
144
  
 
145
  /*
 
146
  // compare the queue clock constraints
 
147
  int qclistlen=qclist.count();
 
148
  int compqclistlen=comp.qclist.count();
 
149
  for (i=0;i<qclistlen;i++){
 
150
    bool found=False;
 
151
    for (j=0;j<compqclistlen;j++){
 
152
       if ( (* (qclist[i] ) ) == (* ( comp.qclist[j]) )) {
 
153
        found=True;
 
154
        break;
 
155
      }
 
156
    }
 
157
    if (!found) return False;
 
158
  }
 
159
  for (j=0;j<compqclistlen;j++){
 
160
    bool found=False;
 
161
    for (i=0;i<qclistlen;i++){
 
162
       if ( (* (qclist[i] ) ) == (* ( comp.qclist[j])) ){
 
163
        found=True;
 
164
        break;
 
165
      }
 
166
    }
 
167
    if (!found) return False;
 
168
  }
 
169
 
 
170
  // compare the queue
 
171
  plistlen=queue.count();
 
172
  compplistlen=comp.queue.count();
 
173
  // int i,j;
 
174
  for (i=0;i<plistlen;i++){
 
175
    bool found=False;
 
176
    for (j=0;j<compplistlen;j++){  
 
177
       if ( (* (queue[i])) == (* ( comp.queue[j])) ){
 
178
        found=True;
 
179
        break;
 
180
      }
 
181
    }
 
182
    if (!found) return False;
 
183
  }
 
184
  for (j=0;j<compplistlen;j++){   
 
185
    bool found=False;       
 
186
    for (i=0;i<plistlen;i++){
 
187
       if ( (* (queue[i])) == (* ( comp.queue[j])) ) {
 
188
        found=True;
 
189
        break;
 
190
      }
 
191
    }
 
192
    if (!found) return False;
 
193
  }
 
194
 
 
195
  if (!qtermlist.contains(comp.qtermlist)) return False;
 
196
  if (!comp.qtermlist.contains(qtermlist)) return False;
 
197
 
 
198
  */  
 
199
  return True;
 
200
}
 
201
 
 
202
 
 
203
 
 
204
 
 
205
 
 
206
 
 
207
 
 
208
 
 
209
 
 
210
 
 
211
void ADSValuation::GetConfig(Bag <Subject *> *b){
 
212
  b->join(cfg);
 
213
}
 
214
 
 
215
void ADSValuation::SetConfig(Bag <Subject *> c){
 
216
  List <Subject *> l;
 
217
  c.GetList(l);
 
218
  for (l.first();!l.done();l.next()){
 
219
    cfg.add(l.cur(),c.count(l.cur()));
 
220
  }
 
221
}
 
222
 
 
223
void ADSValuation::GetPropList(List <PropVal *> *pl){
 
224
  *pl=plist;
 
225
}
 
226
 
 
227
void ADSValuation::SetPropList(List <PropVal *> p){
 
228
  plist=p;
 
229
}
 
230
 
 
231
 
 
232
 
 
233
 
 
234
void ADSValuation::GetTerminated(Bag <ATDActionStateNode *> *tl){
 
235
  tl->join(tlist);
 
236
}
 
237
 
 
238
void ADSValuation::AddTerminated(Bag <ATDActionStateNode *>  t){
 
239
  List <ATDActionStateNode *> l;
 
240
  t.GetList(l);
 
241
  for (l.first();!l.done();l.next()){
 
242
    tlist.add(l.cur(),t.count(l.cur()));
 
243
  }
 
244
}
 
245
 
 
246
 
 
247
 
 
248
void ADSValuation::GetClockConstraints(List <ClockConstraint *> *cl){
 
249
  *cl=clist;
 
250
}
 
251
 
 
252
void ADSValuation::SetClockConstraints(List <ClockConstraint *> c){
 
253
  clist=c;
 
254
}
 
255
 
 
256
 
 
257
 
 
258
ClockManager ADSValuation::GetClockManager(){
 
259
       return cm;
 
260
}
 
261
 
 
262
void ADSValuation::SetClockManager(ClockManager &c){
 
263
  cm=c;
 
264
}
 
265
 
 
266
 
 
267
 
 
268
void ADSValuation::Write(OutputFile *ofile) {
 
269
  (*ofile) << "location ID:\t"<< GetId() <<"\t";
 
270
  if (st==stable) {(*ofile) << "stable\n";}
 
271
  else {(*ofile) << "unstable\n";}
 
272
  List <Subject *> l;
 
273
  cfg.GetList(l);
 
274
  for (l.first();!l.done();l.next()){
 
275
    (*ofile) << "cfg element =\n" ;  
 
276
    (*ofile) << (*(l.cur()->GetName()));
 
277
        (*ofile) << "\t:\t count = " << cfg.count(l.cur()) << "\n";
 
278
  }
 
279
  //
 
280
  
 
281
  (*ofile) << "TermList:\n";
 
282
  List <ATDActionStateNode *> l2;
 
283
  tlist.GetList(l2);
 
284
  for (l2.first();!l2.done();l2.next()){
 
285
    (*ofile) << "term el:" <<l2.cur()->GetId() <<":\t" << tlist.count(l2.cur()) <<"\n";
 
286
  }
 
287
  //
 
288
  (*ofile) << "PropValList:\n";
 
289
  for (plist.first();!plist.done();plist.next()){
 
290
        plist.cur()->Write(ofile);
 
291
  }
 
292
  (*ofile) <<"Time outs:\n";
 
293
  for (clist.first();!clist.done();clist.next()){
 
294
        clist.cur()->Write(ofile);
 
295
  }
 
296
  cm.Write(ofile);
 
297
 
 
298
  
 
299
  (*ofile) << "END of valuation\n";
 
300
}
 
301
 
 
302
 
 
303
void ADSValuation:: WriteScreen() {
 
304
  
 
305
  (std::cout) << "location ID:\t"<< GetId() <<"\t";
 
306
  std::cout << "\t\tkey\t" << ComputeKey() << "\n";
 
307
  if (st==stable) {(std::cout) << "stable\n";}
 
308
  else {(std::cout) << "unstable\n";}
 
309
  List <Subject *> l;
 
310
  cfg.GetList(l);
 
311
  for (l.first();!l.done();l.next()){
 
312
    (std::cout) << "cfg element =\n" ;  
 
313
    (std::cout) << (*(l.cur()->GetName()));
 
314
        (std::cout) << "\t:\t count = " << cfg.count(l.cur()) << "\n";
 
315
  }
 
316
  //
 
317
  (std::cout) << "TermList:\n";
 
318
  List <ATDActionStateNode *> l2;
 
319
  tlist.GetList(l2);
 
320
  for (l2.first();!l2.done();l2.next()){
 
321
    (std::cout) << "term el:" <<l2.cur()->GetId() <<":\t" << tlist.count(l2.cur()) <<"\n";
 
322
  }
 
323
  //
 
324
  (std::cout) << "PropValList:\n";
 
325
  for (plist.first();!plist.done();plist.next()){
 
326
        plist.cur()->Write();
 
327
  }
 
328
  cm.Write();
 
329
  (std::cout) << "END of valuation\n";
 
330
}
 
331
 
 
332
 
 
333
 
 
334
void ADSValuation::SetNumber(int i){
 
335
  index=i;
 
336
}
 
337
 
 
338
int ADSValuation::GetNumber(){
 
339
  return index;
 
340
}
 
341
 
 
342
 
 
343
int ADSValuation::ComputeKey(){
 
344
  if (!key){
 
345
    key=1;
 
346
    List <Subject *> l;
 
347
    cfg.GetList(l);
 
348
    for (l.first();!l.done();l.next()){
 
349
      key = key * (7*(l.cur()->GetId()) * (cfg.count(l.cur())));
 
350
    }
 
351
    List <ATDActionStateNode *> l2;
 
352
    tlist.GetList(l2);
 
353
    for (l2.first();!l2.done();l2.next()){
 
354
      key = key + (1 * (l2.cur()->GetId()) * (tlist.count(l2.cur()) ))  ;
 
355
    }    
 
356
    for (plist.first();!plist.done();plist.next()){
 
357
      if (plist.cur()->GetBool())
 
358
                key=key + ((1) * (plist.cur()->GetProp()->GetId()));
 
359
    }   
 
360
    if (st!=stable) key=key+1;
 
361
  }
 
362
  return key;
 
363
}
 
364
 
 
365
bool ADSValuation::hassameconfig( ADSValuation &comp){
 
366
  Bag <Subject *> sb;
 
367
  sb.join(cfg);
 
368
  sb.join(comp.cfg);
 
369
  List <Subject *> sl;
 
370
  sb.GetList(sl);
 
371
  for (sl.first();!sl.done();sl.next()){
 
372
    if (cfg.count(sl.cur())!=comp.cfg.count(sl.cur()))
 
373
      return False;
 
374
     }   
 
375
  return true;
 
376
}
 
377
 
 
378
 
 
379
 
 
380
/*
 
381
 
 
382
 
 
383
void ADSValuation::GetQueue(List <PropVal *> *q){
 
384
  *q=queue;
 
385
}
 
386
 
 
387
void ADSValuation::SetQueue(List <PropVal *> q){
 
388
  queue=q;
 
389
}
 
390
 
 
391
void ADSValuation::GetQClockConstraints(List <ClockConstraint *> *cl){
 
392
  *cl=qclist;
 
393
}
 
394
 
 
395
void ADSValuation::SetQClockConstraints(List <ClockConstraint *> c){
 
396
  qclist=c;
 
397
}
 
398
 
 
399
void ADSValuation::GetQTerminated(Bag <ATDActionStateNode *> *tl){
 
400
  tl->join(qtermlist);
 
401
}
 
402
 
 
403
void ADSValuation::AddQTerminated(Bag <ATDActionStateNode *>  t){
 
404
  List <ATDActionStateNode *> l;
 
405
  t.GetList(l);
 
406
  for (l.first();!l.done();l.next()){
 
407
    qtermlist.add(l.cur(),t.count(l.cur()));
 
408
  }
 
409
}
 
410
*/
 
411
 
 
412