File: makebasic.c

package info (click to toggle)
mona 1.4-7-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,996 kB
  • ctags: 3,939
  • sloc: ansic: 14,363; cpp: 12,610; sh: 1,076; yacc: 493; lex: 358; makefile: 150; lisp: 53
file content (237 lines) | stat: -rw-r--r-- 6,008 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
 * MONA
 * Copyright (C) 1997-2002 BRICS.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the  Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
 * USA.
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "../Mem/mem.h"
#include "gta.h"

#define MAX_EXCEPTION 50
#define MAX_VARIABLES 10
/* #warning INTERNAL LIMITS */

typedef struct {
  unsigned value;
  char path[MAX_VARIABLES+1];
} Path;

static GTA *gta;
static unsigned numExceptions, nextException, exp;
static Path exception[MAX_EXCEPTION];
static State left, right; /* current left and right state */
static State defState;
static SsId s; /* current state space */
static unsigned sortedIndex[MAX_VARIABLES];
static int *offs;
static unsigned numOffs;
static char sortedPath[MAX_VARIABLES]; /* current exception path, sorted according to offsets */
static bdd_ptr bddPath[MAX_EXCEPTION];
static bdd_ptr def;

/*  USAGE:
 *
 *  gtaSetup(number of states in root state space)
 *  for each state space s do
 *    gtaSetupDelta(s, size of left successor, size of right successor,
 *                  offset array, size of offset array)
 *    for each pair (l,r) do
 *      gtaAllocExceptions(l, r, number of exceptions)
 *      gtaStoreException(state, path) [for each exception]
 *      gtaStoreDefault(state)
 *    gtaBuildDelta(initial state)
 *  gtaBuild(final state array) 
 */

/* auxiliary functions */

int offsCmp(const void *i1, const void *i2)
{
  unsigned o1 = offs[*((unsigned *) i1)], o2 = offs[*((unsigned *) i2)];

  if (o1 < o2)
    return -1;
  if (o1 > o2)
    return 1;
  return 0;
}

unsigned fn_unite(unsigned p, unsigned q)
{
  if (p == q || q == defState)
    return p;
  invariant(p == defState);
  return q;
}

/* make path ..... */
bdd_ptr makePath(bdd_manager *bddm, unsigned leafValue)
{
  int n;
  bdd_ptr p;
  
  /* insert leaf node */
  p = bdd_find_leaf_hashed_add_root(bddm, leafValue);

  /* build path bottom-up */
  for (n = numOffs - 1; n >= 0; n--)
    switch (sortedPath[n]) {
    case '0':
      p = bdd_find_node_hashed_add_root(bddm, p, def, 
					offs[sortedIndex[n]]);
      break;
    case '1':
      p = bdd_find_node_hashed_add_root(bddm, def, p, 
					offs[sortedIndex[n]]);
      break;
    case 'X': /* do nothing */
      break;
    }
  return p;
}

/* unite bddPaths */
bdd_ptr unitePaths(bdd_manager *bddm)
{
  int n;
  bdd_ptr p = bddPath[0];

  bdd_make_cache(bddm, 8, 4);
  for (n = 1; n < numExceptions; n++)
    p = bdd_apply2_hashed(bddm, p, bddm, bddPath[n],
			  bddm, &fn_unite);
  bdd_kill_cache(bddm);

  return p;
}

/* main functions */

/* prepare construction of a new GTA */
void gtaSetup(unsigned rootsize) 
{
  gta = gtaMake();
  gta->ss[0].size = rootsize;
}

void gtaSetupDelta(SsId d, unsigned lsize, unsigned rsize, 
		   int *offsets, unsigned numOffsets)
{
  int n;

  s = d;
  gta->ss[guide.muLeft[s]].size = lsize;
  gta->ss[guide.muRight[s]].size = rsize;

  /* prepare behaviour matrix and BDD manager */
  gta->ss[s].ls = lsize;
  gta->ss[s].rs = rsize;
  gta->ss[s].behaviour = (bdd_handle *) mem_alloc(sizeof(bdd_handle)*
						  gta->ss[s].ls*
						  gta->ss[s].rs);
  gta->ss[s].bddm = bdd_new_manager(8, 4);

  /* sort offsets */
  numOffs = numOffsets;
  offs = offsets;
  for (n = 0; n < numOffs; n++)
    sortedIndex[n] = n;
  qsort(sortedIndex, numOffs, sizeof(int), offsCmp);
}

/* prepare some exceptions from the (l,r) state pair 
   to the default result state */
void gtaAllocExceptions(SsId l, SsId r, unsigned n) 
{
  invariant(n <= MAX_EXCEPTION);
  numExceptions = n;
  nextException = 0;
  left = l;
  right = r;
}

/* store an exception to the default rule */
void gtaStoreException(unsigned value, char *path) 
{
  exception[nextException].value = value;
  invariant(strlen(path) <= MAX_VARIABLES);
  strcpy(exception[nextException++].path, path);
}

/* set default state and make BDD for current state pair */
void gtaStoreDefault(unsigned p) 
{
  bdd_ptr united;
  int n;
  bdd_manager *tmpBddm;

  invariant(numExceptions == nextException);
  defState = p;

  tmpBddm = bdd_new_manager(100, 10); /* large enough to avoid rehashing??? */
  /* COULD AN ASSERTION CHECK THAT REHASHING DOES NOT OCCUR???????? */

  /* insert default state as leaf */
  def = bdd_find_leaf_hashed_add_root(tmpBddm, defState);

  /* insert paths for exceptions */
  for (exp = 0; exp < numExceptions; exp++) {
    for (n = 0; n < numOffs; n++)
      sortedPath[n] = exception[exp].path[sortedIndex[n]];

    bddPath[exp] = makePath(tmpBddm, exception[exp].value);
  }

  /* unite path roots */
  if (numExceptions == 0)
    united = def;
  else if (numExceptions == 1)
    united = bddPath[0];
  else
    united = unitePaths(tmpBddm);

  /* insert into result BDD manager */
  bdd_prepare_apply1(tmpBddm);
  bdd_apply1(tmpBddm, united, gta->ss[s].bddm, &fn_identity);
  bdd_kill_manager(tmpBddm);
  
  /* set behaviour entry */
  BEH(gta->ss[s], left, right) = BDD_LAST_HANDLE(gta->ss[s].bddm);
}

void gtaBuildDelta(State initial)
{
  gta->ss[s].initial = initial; /* this could be moved to gtaSetupDelta!! */
}

GTA *gtaBuild(char *final)
{
  int n;

  invariant(strlen(final) == gta->ss[0].size);

  /* set final status */
  gta->final = (int *) mem_alloc(sizeof(int)*gta->ss[0].size);
  for (n = 0; n < gta->ss[0].size; n++)
    gta->final[n] = (final[n] == '-') ? -1 : (final[n] == '+' ? 1 : 0);

  return gtaReachable(gta);
}