-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathexpressbuild.cc
More file actions
393 lines (368 loc) · 14.4 KB
/
expressbuild.cc
File metadata and controls
393 lines (368 loc) · 14.4 KB
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*****************************************************************************
* expressbuild.cc *
* *
* Description: Contains all the functions relevant to building a Complex- *
* Collect out of an EXPRESS file. The complex structures are *
* built out of fedex structures. Subtype and supertype info *
* are processed, and multi-generation trees (e.g., subtypes of *
* subtypes) are built up from the fedex structures. *
* *
* Created by: David Rosenfeld *
* Date: 01/03/97 *
*****************************************************************************/
#include "complexSupport.h"
// Local function prototypes:
static void initEnts( Express );
static Entity findEnt( Entity, char * );
ComplexCollect::ComplexCollect( Express express )
/*
* Builds a ComplexCollect, a collection of ComplexLists, based on the
* entities contained in EXPRESS file express.
*/
{
DictionaryEntry de_sch, de_ent;
Schema schema;
ComplexList * cl, *prev = NULL;
// Some initializing:
clists = NULL;
count = 0;
// Set all ent->search_id's to 0. Since entities - even ones in different
// schemas - may be strongly connected, we must be sure not to process each
// one more than once.
initEnts( express );
// Next loop through all the entities, building ComplexLists:
DICTdo_type_init( express->symbol_table, &de_sch, OBJ_SCHEMA );
while( ( schema = ( Scope )DICTdo( &de_sch ) ) != 0 ) {
SCOPEdo_entities( schema, ent, de_ent )
if( ent->search_id == TRUE ) {
// we've hit this entity already
continue;
}
#ifdef COMPLEX_INFO
cout << "Processing entity " << ENTITYget_name( ent ) << endl;
#endif
if( ent->u.entity->subtypes != NULL ) {
cl = new ComplexList( ent, this );
// This constructor will not only create a ComplexList for
// the ent subtypes, but it will recurse for all of their
// subtypes. The entire hierarchy will become a single
// ComplexList and is now appended to the Collect ("this").
insert( cl );
}
SCOPEod
}
// Remove all Lists corresponding to entities which are subtypes of other
// entities. Such Lists should not exist on their own since their supers
// supercede them. (They were added in the first place to be available
// so that any supertype which accessed it would find it.)
cl = clists;
while( cl ) {
if( cl->Dependent() ) {
#ifdef COMPLEX_INFO
cout << "\nRemoving dependent entity " << cl->supertype() << endl;
#endif
remove( cl );
if( prev ) {
cl = prev->next;
// prev->next was automatically set to cl->next in remove()
// when cl was removed.
} else {
// prev may still = NULL (its starting value) if the first CList
// is dependent, and so we're the 1st time through this loop.
cl = clists;
}
} else {
prev = cl;
cl = cl->next;
}
}
}
static void initEnts( Express express )
/*
* Sets all the search_id's of all the entities to FALSE. The search_id's
* will be used to keep track of which entities we've build ComplexLists
* for already.
*/
{
DictionaryEntry de_sch, de_ent;
Schema schema;
DICTdo_type_init( express->symbol_table, &de_sch, OBJ_SCHEMA );
while( ( schema = ( Scope )DICTdo( &de_sch ) ) != 0 ) {
SCOPEdo_entities( schema, ent, de_ent )
ent->search_id = FALSE;
SCOPEod
}
}
ComplexList::ComplexList( Entity ent, ComplexCollect * col )
/*
* Builds a complex list from an entity which contains subtypes. (All our
* members are set here or in called functions except next which is set
* when this is inserted into owning ComplexCollect.)
*/
{
Expression exp;
ent->search_id = TRUE;
list = NULL;
next = NULL;
maxlevel = 0;
addSuper( ent );
if( ( exp = ent->u.entity->subtype_expression ) != NULL ) {
#ifdef COMPLEX_INFO
cout << " Has a sub expression\n";
#endif
head->processSubExp( exp, ent, col );
buildList();
}
// Check for any subtypes which were not a part of subtype_expr. Any
// subtype which was not in sub_exp but is included in subtypes is ANDORed
// with the rest of the List.
addImplicitSubs( ENTITYget_subtypes( ent ), col );
if( ENTITYget_supertypes( ent ) == NULL ) {
dependent = FALSE;
// Rebuild list in case implicit subs were added (we had to build the
// first time also so addImplicitSubs() would work).
buildList();
//maxlevel = head->setLevel( 0 );
head->setLevel( 0 );
} else {
// If this List has supertypes, we don't really need it as a List -
// it will ultimately be a part of its super(s)' List(s). We need it
// now so its supers will be able to find it. But mark that this
// does not stand on its own:
#ifdef COMPLEX_INFO
cout << " " << ENTITYget_name( ent ) << " is dependent\n";
#endif
dependent = TRUE;
}
}
void ComplexList::addSuper( Entity ent )
/*
* Sets our supertype. Assumes supertype was previously unset.
*/
{
head = new AndList;
head->supertype = TRUE;
// (Although this supertype may itself be a subtype of other supertypes,
// we call this a supertype. We only need this info during the list-
// creation stage (see MultList::processSubExp()).)
head->childList = new SimpleList( ENTITYget_name( ent ) );
head->numchildren = 1;
}
void MultList::processSubExp( Expression exp, Entity super,
ComplexCollect * col )
/*
* Recursive function which builds an EntList hierarchy from an entity's
* subtype expression. First called with this = the ComplexList->head and
* exp = entity->subtype_expression. Recurses with different arguments to
* process the subexpressions.
*/
{
struct Op_Subexpression * oe = &exp->e;
Entity ent;
MultList * mult;
int supertype = 0;
switch( TYPEis( exp->type ) ) {
case entity_:
ent = findEnt( super, exp->type->symbol.name );
#ifdef COMPLEX_INFO
cout << " Adding subtype " << ENTITYget_name( ent ) << endl;
#endif
addSimpleAndSubs( ent, col );
break;
case op_:
if( join == AND ) {
supertype = ( dynamic_cast< AndList * >(this) )->supertype;
}
if( ! supertype &&
( ( oe->op_code == OP_AND && join == AND )
|| ( oe->op_code == OP_ANDOR && join == ANDOR ) ) ) {
// If the subexp is of the same type as we, process its op's at
// the same level (add them on to our childList). 1st cond says
// we don't do this if this is the supertype. In that case, the
// supertype is AND'ed with its subtypes. The subtypes must be at
// a lower level. One reason for this is in case we find implicit
// subtypes, we'll want to ANDOR them with the rest of the subs.
// So we'll want the subs at a distinct lower level.
processSubExp( oe->op1, super, col );
processSubExp( oe->op2, super, col );
} else {
if( oe->op_code == OP_AND ) {
#ifdef COMPLEX_INFO
cout << " Processing AND\n";
#endif
mult = new AndList;
} else {
#ifdef COMPLEX_INFO
cout << " Processing ANDOR\n";
#endif
mult = new AndOrList;
}
appendList( mult );
mult->processSubExp( oe->op1, super, col );
mult->processSubExp( oe->op2, super, col );
}
break;
case oneof_:
#ifdef COMPLEX_INFO
cout << " Processing ONEOF\n";
#endif
mult = new OrList;
appendList( mult );
LISTdo( exp->u.list, arg, Expression )
mult->processSubExp( arg, super, col );
LISTod
break;
default:
// shouldn't happen
break;
}
}
static Entity findEnt( Entity ent0, char * name )
/*
* Returns an entity named name. The desired entity is likely to be in the
* same schema as ent0. findEnt first searches the schema which contains
* ent, and then searches the other schemas in the express file.
*/
{
Schema schema = ENTITYget_schema( ent0 ), sch;
DictionaryEntry de_ent, de_sch;
Express express;
// First look through the entities in the same schema as ent0:
SCOPEdo_entities( schema, ent, de_ent )
if( !strcmp( ENTITYget_name( ent ), name ) ) {
return ent;
}
SCOPEod
// If we still haven't found it, look through all the entities in the
// express file:
express = schema->superscope;
DICTdo_type_init( express->symbol_table, &de_sch, OBJ_SCHEMA );
while( ( sch = ( Scope )DICTdo( &de_sch ) ) != 0 ) {
if( sch == schema ) {
// Don't redo the schema which contains ent0 - we did it already.
continue;
}
SCOPEdo_entities( sch, ent, de_ent )
if( !strcmp( ENTITYget_name( ent ), name ) ) {
return ent;
}
SCOPEod
}
return NULL;
// Shouldn't happen - if the entity didn't exist, fedex would have
// complained already.
}
void ComplexList::addImplicitSubs( Linked_List subs, ComplexCollect * col )
/*
* Checks if there are any subtypes of entity this->supertype() which were
* not in the entity's subtype_expression. (subs is the entity's subtypes
* list.) If any are found they are ANDORed with the other subtypes.
*/
{
EntNode node( ( char * )"" );
// Temp var - used to check if this already contains certain values.
int none_yet = TRUE;
AndOrList * ao = 0;
LISTdo( subs, subEnt, Entity )
strncpy( node.name, ENTITYget_name( subEnt ), BUFSIZ );
if( !contains( &node ) ) {
// We've found an implicit subtype.
#ifdef COMPLEX_INFO
cout << " Adding implicit subtype " << ENTITYget_name( subEnt )
<< endl;
#endif
if( none_yet ) {
// If this is the first one, replace the previous subtype list
// with an ANDOR.
none_yet = FALSE;
ao = new AndOrList;
// Make the previous sub exp a child of ao:
ao->childList = head->childList->next;
if( ao->childList ) {
ao->childList->prev = NULL;
ao->numchildren = 1;
} else {
ao->numchildren = 0;
}
head->childList->next = ao;
ao->prev = head->childList;
head->numchildren = 2;
}
// Add the new entity to the end of ao. In case it has its own
// subtype list, call addSimpleAndSubs().
ao->addSimpleAndSubs( subEnt, col );
}
LISTod
}
void MultList::addSimpleAndSubs( Entity newEnt, ComplexCollect * col )
/*
* Called whenever we have a SimpleList (to be built from newEnt) to add
* to our ComplexList. The purpose of this function is to check if the
* Simple is itself a supertype of its own List. If so, we will build a
* ComplexList to be placed beneath this. If not, we will simply create a
* SimpleList corresponding to newEnt.
*/
{
ComplexList * sublist;
SimpleList * simple;
EntList * newlist;
OrList * olist;
// First get the easy case out of the way. If newEnt has no subtypes
// just create a corresponding SimpleList:
if( ENTITYget_subtypes( newEnt ) == NULL ) {
newEnt->search_id = TRUE;
simple = new SimpleList( ENTITYget_name( newEnt ) );
appendList( simple );
return;
}
// If newEnt has subtypes, find or build the corresponding ComplexList:
#ifdef COMPLEX_INFO
cout << " Subtype is a supertype ...\n";
#endif
if( newEnt->search_id == TRUE ) {
// We've processed child already, find its ComplexList in col:
#ifdef COMPLEX_INFO
cout << " was built already ... finding it\n";
#endif
sublist = col->find( ENTITYget_name( newEnt ) );
// Make a copy and append to this:
newlist = copyList( sublist->head );
} else {
// If this subtype has never been visited, we build a ComplexList out
// of it and add it to our ComplexCollect. Even though it only exists
// as a subtype of supertypes, we temporarily treat it as a List. We
// do this because it may be referenced again - by the current List
// in another way, or by another supertype. As a separate List and
// member of Collect, we'll be able to find it easily. After all the
// entities are processed, we'll remove this.
#ifdef COMPLEX_INFO
cout << " never built before ... building it now\n";
#endif
sublist = new ComplexList( newEnt, col );
col->insert( sublist );
// Since this is the first time we're creating this list, we don't need
// to copy it and append to this, as above. We'll use the same Lists
// again and also point to them from this.
appendList( sublist->head );
newlist = sublist->head;
}
// If the sub-list is not abstract, one more task:
if( ! newEnt->u.entity->abstract ) {
// Since the subtype is not abstract, it can be instantiated without
// its subtypes. Create an OrList OR'ing the supertype alone and its
// entire List:
olist = new OrList;
simple = new SimpleList( ( char * )sublist->supertype() );
olist->appendList( simple );
// We just added "newlist" to the end of this. We now replace it with
// our new or, and place it underneath the or. This OR's the new
// subtype alone with the subtype + its own subtypes - just what we
// want for a non-abstract subtype.
olist->appendList( newlist );
numchildren--;
// (Slightly ugly: Since we just grabbed newlist from this to or, we
// had the side effect of making this's numcount incorrect. I could
// have done this more elegantly, but was lazy.)
appendList( olist );
}
}