1 |
/* |
2 |
* Logical Relation Module |
3 |
* by Vicente Rico-Ramirez |
4 |
* Created: 09/96 |
5 |
* Version: $Revision: 1.8 $ |
6 |
* Version control file: $RCSfile: logrel.c,v $ |
7 |
* Date last modified: $Date: 1998/01/29 00:42:26 $ |
8 |
* Last modified by: $Author: ballan $ |
9 |
* |
10 |
* This file is part of the SLV solver. |
11 |
* |
12 |
* The SLV solver is free software; you can redistribute |
13 |
* it and/or modify it under the terms of the GNU General Public License as |
14 |
* published by the Free Software Foundation; either version 2 of the |
15 |
* License, or (at your option) any later version. |
16 |
* |
17 |
* The SLV solver is distributed in hope that it will be |
18 |
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of |
19 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
20 |
* General Public License for more details. |
21 |
* |
22 |
* You should have received a copy of the GNU General Public License |
23 |
* along with the program; if not, write to the Free Software Foundation, |
24 |
* Inc., 675 Mass Ave, Cambridge, MA 02139 USA. Check the file named |
25 |
* COPYING. COPYING is found in ../compiler. |
26 |
* |
27 |
*/ |
28 |
|
29 |
#include <math.h> |
30 |
#include <utilities/ascConfig.h> |
31 |
#include <utilities/ascMalloc.h> |
32 |
#include <utilities/mem.h> |
33 |
#include <general/list.h> |
34 |
#include <general/dstring.h> |
35 |
#include <compiler/fractions.h> |
36 |
#include <compiler/instance_enum.h> |
37 |
#include <compiler/compiler.h> |
38 |
#include <compiler/symtab.h> |
39 |
#include <compiler/extfunc.h> |
40 |
#include <compiler/extcall.h> |
41 |
#include <compiler/functype.h> |
42 |
#include <compiler/safe.h> |
43 |
#include <compiler/dimen.h> |
44 |
#include <compiler/expr_types.h> |
45 |
#include <compiler/find.h> |
46 |
#include <compiler/atomvalue.h> |
47 |
#include <compiler/instquery.h> |
48 |
#include <compiler/mathinst.h> |
49 |
#include <compiler/parentchild.h> |
50 |
#include <compiler/instance_io.h> |
51 |
#include <compiler/logical_relation.h> |
52 |
#include <compiler/logrelation.h> |
53 |
#include <compiler/logrel_util.h> |
54 |
#include <compiler/packages.h> |
55 |
#define _SLV_SERVER_C_SEEN_ /* for the extrel stuff in header */ |
56 |
#include "mtx.h" |
57 |
#include "slv_types.h" |
58 |
#include "var.h" |
59 |
#include "rel.h" |
60 |
#include "discrete.h" |
61 |
#include "conditional.h" |
62 |
#include "logrel.h" |
63 |
#include "bnd.h" |
64 |
#include "slv_server.h" |
65 |
|
66 |
#ifndef IPTR |
67 |
#define IPTR(i) ((struct Instance *)(i)) |
68 |
#endif |
69 |
#define LOGREL_DEBUG FALSE |
70 |
|
71 |
/* define symchar names needed */ |
72 |
static symchar *g_strings[1]; |
73 |
#define INCLUDED_R g_strings[0] |
74 |
|
75 |
|
76 |
static const struct logrel_relation g_logrel_defaults = { |
77 |
NULL, /* instance */ |
78 |
NULL, /* incidence */ |
79 |
0, /* n_incidences */ |
80 |
-1, /* mindex */ |
81 |
-1, /* sindex */ |
82 |
-1, /* model index */ |
83 |
(LOGREL_INCLUDED) /* flags */ |
84 |
}; |
85 |
|
86 |
/* |
87 |
* Don't forget to update the |
88 |
* initialization when the structure |
89 |
* is modified. |
90 |
*/ |
91 |
|
92 |
|
93 |
static struct logrel_relation |
94 |
*logrel_copy(const struct logrel_relation *logrel) |
95 |
{ |
96 |
struct logrel_relation *newlogrel; |
97 |
newlogrel = ASC_NEW(struct logrel_relation); |
98 |
*newlogrel = *logrel; |
99 |
return(newlogrel); |
100 |
} |
101 |
|
102 |
|
103 |
struct logrel_relation *logrel_create(SlvBackendToken instance, |
104 |
struct logrel_relation *newlogrel) |
105 |
{ |
106 |
if (newlogrel==NULL) { |
107 |
newlogrel = logrel_copy(&g_logrel_defaults); /* malloc the logrelation */ |
108 |
} else { |
109 |
*newlogrel = g_logrel_defaults; /* init the space we've been sent */ |
110 |
} |
111 |
assert(newlogrel!=NULL); |
112 |
newlogrel->instance = instance; |
113 |
return(newlogrel); |
114 |
} |
115 |
|
116 |
|
117 |
|
118 |
SlvBackendToken logrel_instance(struct logrel_relation *logrel) |
119 |
{ |
120 |
if (logrel==NULL) return NULL; |
121 |
return (SlvBackendToken) logrel->instance; |
122 |
} |
123 |
|
124 |
void logrel_write_name(slv_system_t sys,struct logrel_relation *logrel, |
125 |
FILE *fp) |
126 |
{ |
127 |
if (logrel == NULL || fp==NULL) return; |
128 |
if (sys!=NULL) { |
129 |
WriteInstanceName(fp,logrel_instance(logrel),slv_instance(sys)); |
130 |
} else { |
131 |
WriteInstanceName(fp,logrel_instance(logrel),NULL); |
132 |
} |
133 |
} |
134 |
|
135 |
|
136 |
void logrel_destroy(struct logrel_relation *logrel) |
137 |
{ |
138 |
struct Instance *inst; |
139 |
|
140 |
ascfree((POINTER)logrel->incidence); |
141 |
inst = IPTR(logrel->instance); |
142 |
if (inst) { |
143 |
if (GetInterfacePtr(inst)==logrel) { |
144 |
SetInterfacePtr(inst,NULL); |
145 |
} |
146 |
} |
147 |
ascfree((POINTER)logrel); |
148 |
} |
149 |
|
150 |
|
151 |
uint32 logrel_flags( struct logrel_relation *logrel) |
152 |
{ |
153 |
return logrel->flags; |
154 |
} |
155 |
|
156 |
void logrel_set_flags(struct logrel_relation *logrel, unsigned int flags) |
157 |
{ |
158 |
logrel->flags = flags; |
159 |
} |
160 |
|
161 |
|
162 |
uint32 logrel_flagbit(struct logrel_relation *logrel, uint32 one) |
163 |
{ |
164 |
if (logrel==NULL || logrel->instance == NULL) { |
165 |
FPRINTF(stderr,"ERROR: logrel_flagbit called with bad logrel.\n"); |
166 |
return 0; |
167 |
} |
168 |
return (logrel->flags & one); |
169 |
} |
170 |
|
171 |
|
172 |
void logrel_set_flagbit(struct logrel_relation *logrel, uint32 field, |
173 |
uint32 one) |
174 |
{ |
175 |
if (one) { |
176 |
logrel->flags |= field; |
177 |
} else { |
178 |
logrel->flags &= ~field; |
179 |
} |
180 |
} |
181 |
|
182 |
|
183 |
boolean logrel_not_equal(struct logrel_relation *logrel) |
184 |
{ |
185 |
switch( LogRelRelop(GetInstanceLogRelOnly(IPTR(logrel->instance))) ) { |
186 |
case e_boolean_neq: |
187 |
return(TRUE); |
188 |
default: |
189 |
return(FALSE); |
190 |
} |
191 |
} |
192 |
|
193 |
|
194 |
boolean logrel_equal( struct logrel_relation *logrel) |
195 |
{ |
196 |
switch( LogRelRelop(GetInstanceLogRelOnly(IPTR(logrel->instance))) ) { |
197 |
case e_boolean_eq: |
198 |
return(TRUE); |
199 |
default: |
200 |
return(FALSE); |
201 |
} |
202 |
} |
203 |
|
204 |
|
205 |
static enum logrel_enum compenum2logrelenum(enum Expr_enum t) |
206 |
{ |
207 |
switch (t) { |
208 |
case e_boolean_eq: |
209 |
return e_logrel_equal; |
210 |
case e_boolean_neq: |
211 |
return e_logrel_not_equal; |
212 |
default: |
213 |
FPRINTF(ASCERR,"ERROR (logrel.c): compenum2logrelenum miscalled.\n"); |
214 |
return e_logrel_equal; |
215 |
} |
216 |
} |
217 |
|
218 |
|
219 |
enum logrel_enum logrel_relop( struct logrel_relation *logrel) |
220 |
{ |
221 |
return |
222 |
compenum2logrelenum(LogRelRelop( |
223 |
GetInstanceLogRelOnly(IPTR(logrel->instance)))); |
224 |
} |
225 |
|
226 |
|
227 |
char *logrel_make_name(slv_system_t sys,struct logrel_relation *logrel) |
228 |
{ |
229 |
return WriteInstanceNameString(IPTR(logrel->instance), |
230 |
IPTR(slv_instance(sys))); |
231 |
} |
232 |
|
233 |
|
234 |
int32 logrel_mindex( struct logrel_relation *logrel) |
235 |
{ |
236 |
return( logrel->mindex ); |
237 |
} |
238 |
|
239 |
|
240 |
void logrel_set_mindex( struct logrel_relation *logrel, int32 index) |
241 |
{ |
242 |
logrel->mindex = index; |
243 |
} |
244 |
|
245 |
|
246 |
int32 logrel_sindex( struct logrel_relation *logrel) |
247 |
{ |
248 |
return( logrel->sindex ); |
249 |
} |
250 |
|
251 |
void logrel_set_sindex( struct logrel_relation *logrel, int32 index) |
252 |
{ |
253 |
logrel->sindex = index; |
254 |
} |
255 |
|
256 |
|
257 |
int32 logrel_model(const struct logrel_relation *logrel) |
258 |
{ |
259 |
return((const int32) logrel->model ); |
260 |
} |
261 |
|
262 |
|
263 |
void logrel_set_model( struct logrel_relation *logrel, int32 index) |
264 |
{ |
265 |
logrel->model = index; |
266 |
} |
267 |
|
268 |
|
269 |
int32 logrel_residual(struct logrel_relation *logrel) |
270 |
{ |
271 |
return( LogRelResidual(GetInstanceLogRelOnly(IPTR(logrel->instance)))); |
272 |
} |
273 |
|
274 |
|
275 |
void logrel_set_residual( struct logrel_relation *logrel, int32 residual) |
276 |
{ |
277 |
struct logrelation *logreln; |
278 |
logreln = (struct logrelation *) |
279 |
GetInstanceLogRelOnly(IPTR(logrel->instance)); |
280 |
SetLogRelResidual(logreln,residual); |
281 |
} |
282 |
|
283 |
|
284 |
|
285 |
int32 logrel_nominal( struct logrel_relation *logrel) |
286 |
{ |
287 |
return( LogRelNominal(GetInstanceLogRelOnly(IPTR(logrel->instance)))); |
288 |
} |
289 |
|
290 |
|
291 |
int32 logrel_n_incidencesF(struct logrel_relation *logrel) |
292 |
{ |
293 |
if (logrel!=NULL) return logrel->n_incidences; |
294 |
FPRINTF(stderr,"logrel_n_incidences miscalled with NULL\n"); |
295 |
return 0; |
296 |
} |
297 |
|
298 |
|
299 |
void logrel_set_incidencesF(struct logrel_relation *logrel,int n, |
300 |
struct dis_discrete **bv) |
301 |
{ |
302 |
if(logrel!=NULL && n >=0) { |
303 |
if (n && bv==NULL) { |
304 |
FPRINTF(stderr,"logrel_set_incidence miscalled with NULL list\n"); |
305 |
} |
306 |
logrel->n_incidences = n; |
307 |
logrel->incidence = bv; |
308 |
return; |
309 |
} |
310 |
FPRINTF(stderr,"logrel_set_incidence miscalled with NULL or n < 0\n"); |
311 |
} |
312 |
|
313 |
|
314 |
const struct dis_discrete |
315 |
**logrel_incidence_list( struct logrel_relation *logrel) |
316 |
{ |
317 |
if (logrel==NULL) return NULL; |
318 |
return( (const struct dis_discrete **)logrel->incidence ); |
319 |
} |
320 |
|
321 |
|
322 |
struct dis_discrete |
323 |
**logrel_incidence_list_to_modify( struct logrel_relation *logrel) |
324 |
{ |
325 |
if (logrel==NULL) return NULL; |
326 |
return( (struct dis_discrete **)logrel->incidence ); |
327 |
} |
328 |
|
329 |
|
330 |
int logrel_apply_filter( struct logrel_relation *logrel, |
331 |
logrel_filter_t *filter) |
332 |
{ |
333 |
if (logrel==NULL || filter==NULL) { |
334 |
FPRINTF(stderr,"logrel_apply_filter miscalled with NULL\n"); |
335 |
return FALSE; |
336 |
} |
337 |
/* AND to mask off irrelevant bits in flags and match value, |
338 |
then compare */ |
339 |
return ( (filter->matchbits & logrel->flags) == |
340 |
(filter->matchbits & filter->matchvalue) ); |
341 |
} |
342 |
|
343 |
/* too bad there's no entry point that rel must call before being used |
344 |
* generally, like the FindType checking stuff in var.c |
345 |
*/ |
346 |
static void check_included_flag(void){ |
347 |
if (INCLUDED_R == NULL || AscFindSymbol(INCLUDED_R) == NULL) { |
348 |
INCLUDED_R = AddSymbolL("included",8); |
349 |
} |
350 |
} |
351 |
|
352 |
uint32 logrel_included( struct logrel_relation *logrel) |
353 |
{ |
354 |
struct Instance *c; |
355 |
check_included_flag(); |
356 |
c = ChildByChar(IPTR(logrel->instance),INCLUDED_R); |
357 |
if( c == NULL ) { |
358 |
FPRINTF(stderr,"ERROR: (logrel) logrel_included\n"); |
359 |
FPRINTF(stderr," No 'included' field in logrelation.\n"); |
360 |
WriteInstance(stderr,IPTR(logrel->instance)); |
361 |
return FALSE; |
362 |
} |
363 |
logrel_set_flagbit(logrel,LOGREL_INCLUDED,GetBooleanAtomValue(c)); |
364 |
return( GetBooleanAtomValue(c) ); |
365 |
} |
366 |
|
367 |
|
368 |
void logrel_set_included( struct logrel_relation *logrel, uint32 included) |
369 |
{ |
370 |
struct Instance *c; |
371 |
check_included_flag(); |
372 |
c = ChildByChar(IPTR(logrel->instance),INCLUDED_R); |
373 |
if( c == NULL ) { |
374 |
FPRINTF(stderr,"ERROR: (logrel) logrel_set_included\n"); |
375 |
FPRINTF(stderr," No 'included' field in logrelation.\n"); |
376 |
WriteInstance(stderr,IPTR(logrel->instance)); |
377 |
return; |
378 |
} |
379 |
SetBooleanAtomValue(c,included,(unsigned)0); |
380 |
logrel_set_flagbit(logrel,LOGREL_INCLUDED,included); |
381 |
} |