-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathFormatEXP.cs
685 lines (592 loc) · 26.3 KB
/
FormatEXP.cs
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
// Name: FormatEXP.cs
// Description: EXPRESS file importer/exporter
// Author: Tim Chipman
// Origination: Work performed for BuildingSmart by Constructivity.com LLC.
// Copyright: (c) 2012 BuildingSmart International Ltd.
// License: http://www.buildingsmart-tech.org/legal
using System;
using System.Collections.Generic;
using System.Text;
using IfcDoc.Schema.DOC;
namespace IfcDoc.Format.EXP
{
internal class FormatEXP :
IDisposable,
IComparer<string>
{
string m_filename;
DocProject m_project;
DocModelView[] m_views;
Dictionary<DocObject, bool> m_included;
bool m_oneof = true; // always use ONEOF, even if only one subtype -- was False for IFC2x3 and IFC4 final; True for IFC4 TC1.
bool m_longform = true; // convert all type references within rules into "IFC4" for longform schema -- was False for IFC2x3 and IFC4 final; True for IFC4 TC1.
public FormatEXP(string filename)
{
this.m_filename = filename;
}
public DocProject Instance
{
get
{
return this.m_project;
}
set
{
this.m_project = value;
}
}
/// <summary>
/// Optional model views for filtering definitions.
/// </summary>
public DocModelView[] ModelViews
{
get
{
return this.m_views;
}
set
{
this.m_views = value;
this.m_included = null;
if (this.m_views != null)
{
this.m_included = new Dictionary<DocObject, bool>();
foreach (DocModelView docView in this.m_views)
{
this.m_project.RegisterObjectsInScope(docView, this.m_included);
}
}
}
}
/// <summary>
/// Replaces references to schemas with longform schema name, e.g. IFC4.
/// </summary>
/// <param name="expression"></param>
/// <returns></returns>
private string MakeLongFormExpression(string expression)
{
if (!this.m_longform)
return expression;
string replace = "'IFC4.";
foreach (DocSection docSection in this.m_project.Sections)
{
foreach (DocSchema docSchema in docSection.Schemas)
{
string find = "'" + docSchema.Name.ToUpper() + '.';
if (expression.Contains(find))
{
expression = expression.Replace(find, replace);
}
}
}
return expression.Trim();
}
public void Save()
{
SortedList<string, DocDefined> mapDefined = new SortedList<string, DocDefined>(this);
SortedList<string, DocEnumeration> mapEnum = new SortedList<string, DocEnumeration>(this);
SortedList<string, DocSelect> mapSelect = new SortedList<string, DocSelect>(this);
SortedList<string, DocEntity> mapEntity = new SortedList<string, DocEntity>(this);
SortedList<string, DocFunction> mapFunction = new SortedList<string, DocFunction>(this);
SortedList<string, DocGlobalRule> mapRule = new SortedList<string, DocGlobalRule>(this);
SortedList<string, DocObject> mapGeneral = new SortedList<string,DocObject>();
foreach (DocSection docSection in this.m_project.Sections)
{
foreach (DocSchema docSchema in docSection.Schemas)
{
if (this.m_included == null || this.m_included.ContainsKey(docSchema))
{
foreach (DocType docType in docSchema.Types)
{
if (this.m_included == null || this.m_included.ContainsKey(docType))
{
if (docType is DocDefined)
{
mapDefined.Add(docType.Name, (DocDefined)docType);
}
else if (docType is DocEnumeration)
{
mapEnum.Add(docType.Name, (DocEnumeration)docType);
}
else if (docType is DocSelect)
{
mapSelect.Add(docType.Name, (DocSelect)docType);
}
mapGeneral.Add(docType.Name, docType);
}
}
foreach (DocEntity docEnt in docSchema.Entities)
{
if (this.m_included == null || this.m_included.ContainsKey(docEnt))
{
mapEntity.Add(docEnt.Name, docEnt);
mapGeneral.Add(docEnt.Name, docEnt);
}
}
foreach (DocFunction docFunc in docSchema.Functions)
{
if ((this.m_included == null || this.m_included.ContainsKey(docFunc)) && !mapFunction.ContainsKey(docFunc.Name))
{
mapFunction.Add(docFunc.Name, docFunc);
}
}
foreach (DocGlobalRule docRule in docSchema.GlobalRules)
{
if (this.m_included == null || this.m_included.ContainsKey(docRule))
{
mapRule.Add(docRule.Name, docRule);
}
}
}
}
}
string dirpath = System.IO.Path.GetDirectoryName(this.m_filename);
if (!System.IO.Directory.Exists(dirpath))
{
System.IO.Directory.CreateDirectory(dirpath);
}
using (System.IO.StreamWriter writer = new System.IO.StreamWriter(this.m_filename))
{
string schemaid = "IFC4";
string org = "buildingSMART International Limited";
writer.Write("" +
"(*\r\n" +
"Copyright by:\r\n" +
org + ", " + DateTime.UtcNow.Year + "\r\n" +
"\r\n" +
"Any technical documentation made available by " + org + "\r\n" +
"is the copyrighted work of " + org + " and is owned by the \r\n" +
org + ". It may be photocopied, used in software development, \r\n" +
"or translated into another computer language without prior written consent from \r\n" +
org + " provided that full attribution is given. \r\n" +
"Prior written consent is required if changes are made to the technical specification.\r\n" +
"\r\n" +
"This material is delivered to you as is and " + org + " makes \r\n" +
"no warranty of any kind with regard to it, including, but not limited to, the implied \r\n" +
"warranties as to its accuracy or fitness for a particular purpose. Any use of the \r\n" +
"technical documentation or the information contained therein is at the risk of the user. \r\n" +
"Documentation may include technical or other inaccuracies or typographical errors. \r\n" +
org + " shall not be liable for errors contained therein or \r\n" +
"for incidental consequential damages in connection with the furnishing, performance or use \r\n" +
"of the material. The information contained in this document is subject to change without notice.\r\n" +
"\r\n" +
"Issue date:\r\n" +
DateTime.Today.ToLongDateString() + "\r\n" + //"December 27, 2012\r\n" +
"\r\n" +
"*)\r\n" +
"\r\n");
writer.WriteLine("SCHEMA " + schemaid + ";");
writer.WriteLine();
// stripped optional applicable if MVD is used
if (this.m_included != null)
{
writer.WriteLine("TYPE IfcStrippedOptional = BOOLEAN;");
writer.WriteLine("END_TYPE;");
writer.WriteLine();
}
// defined types
foreach (DocDefined docDef in mapDefined.Values)
{
writer.Write("TYPE ");
writer.Write(docDef.Name);
writer.Write(" = ");
if(docDef.Aggregation != null)
{
WriteExpressAggregation(writer, docDef.Aggregation);
}
writer.Write(docDef.DefinedType);
string length = "";
if (docDef.Length > 0)
{
length = "(" + docDef.Length.ToString() + ")";
}
else if (docDef.Length < 0)
{
int len = -docDef.Length;
length = "(" + len.ToString() + ") FIXED";
}
writer.Write(length);
writer.WriteLine(";");
// where rules -- excluded from any MVD
if (this.m_included == null && docDef.WhereRules != null && docDef.WhereRules.Count > 0)
{
writer.WriteLine(" WHERE");
foreach (DocWhereRule where in docDef.WhereRules)
{
writer.Write("\t");
writer.Write(where.Name);
writer.Write(" : ");
writer.Write(MakeLongFormExpression(where.Expression));
writer.WriteLine(";");
}
}
writer.WriteLine("END_TYPE;");
writer.WriteLine();
}
// enumerations
foreach (DocEnumeration docEnum in mapEnum.Values)
{
writer.Write("TYPE ");
writer.Write(docEnum.Name);
writer.Write(" = ENUMERATION OF");
writer.WriteLine();
for(int i = 0; i < docEnum.Constants.Count; i++)
{
DocConstant docConst = docEnum.Constants[i];
if (i == 0)
{
writer.Write("\t(");
}
else
{
writer.Write("\t,");
}
writer.Write(docConst.Name);
if (i == docEnum.Constants.Count - 1)
{
writer.WriteLine(");");
}
else
{
writer.WriteLine();
}
}
writer.WriteLine("END_TYPE;");
writer.WriteLine();
}
// selects
foreach (DocSelect docSelect in mapSelect.Values)
{
writer.Write("TYPE ");
writer.Write(docSelect.Name);
writer.Write(" = SELECT");
writer.WriteLine();
SortedList<string, DocSelectItem> sortSelect = new SortedList<string, DocSelectItem>(this);
foreach (DocSelectItem docSelectItem in docSelect.Selects)
{
sortSelect.Add(docSelectItem.Name, docSelectItem);
}
int nSelect = 0;
for (int i = 0; i < sortSelect.Keys.Count; i++)
{
DocSelectItem docConst = sortSelect.Values[i];
DocObject docRefEnt = null;
if (mapGeneral.TryGetValue(docConst.Name, out docRefEnt))
{
if (this.m_included == null || this.m_included.ContainsKey(docRefEnt))
{
if (nSelect == 0)
{
writer.Write("\t(");
}
else
{
writer.WriteLine();
writer.Write("\t,");
}
nSelect++;
writer.Write(docConst.Name);
}
}
}
writer.WriteLine(");");
writer.WriteLine("END_TYPE;");
writer.WriteLine();
}
// entities
foreach (DocEntity docEntity in mapEntity.Values)
{
writer.Write("ENTITY ");
writer.Write(docEntity.Name);
if ((docEntity.IsAbstract()))
{
writer.WriteLine();
writer.Write(" ABSTRACT");
}
// build up list of subtypes from other schemas
SortedList<string, DocEntity> subtypes = new SortedList<string, DocEntity>(this); // sort to match Visual Express
foreach (DocEntity eachent in mapEntity.Values)
{
if (eachent.BaseDefinition != null && eachent.BaseDefinition.Equals(docEntity.Name))
{
subtypes.Add(eachent.Name, eachent);
}
}
if (subtypes.Count > 0)
{
StringBuilder sb = new StringBuilder();
// Capture all subtypes, not just those within schema
int countsub = 0;
foreach (string ds in subtypes.Keys)
{
DocEntity refent = subtypes[ds];
if (this.m_included == null || this.m_included.ContainsKey(refent))
{
countsub++;
if (sb.Length != 0)
{
sb.Append("\r\n ,");
}
sb.Append(ds);
}
}
if (!docEntity.IsAbstract())
{
writer.WriteLine();
}
if (countsub > 1 || this.m_oneof)
{
writer.Write(" SUPERTYPE OF (ONEOF\r\n (" + sb.ToString() + "))");
}
else if (countsub == 1)
{
writer.Write(" SUPERTYPE OF (" + sb.ToString() + ")");
}
}
if (docEntity.BaseDefinition != null)
{
writer.WriteLine();
writer.Write(" SUBTYPE OF (");
writer.Write(docEntity.BaseDefinition);
writer.Write(")");
}
writer.WriteLine(";");
// direct attributes
bool hasinverse = false;
bool hasderived = false;
foreach (DocAttribute attr in docEntity.Attributes)
{
if (attr.Inverse == null && attr.Derived == null)
{
writer.Write("\t");
writer.Write(attr.Name);
writer.Write(" : ");
if (attr.IsOptional())
{
writer.Write("OPTIONAL ");
}
WriteExpressAggregation(writer, attr);
if (this.m_included == null || this.m_included.ContainsKey(attr))
{
writer.Write(attr.DefinedType);
}
else
{
writer.Write("IfcStrippedOptional");
}
writer.WriteLine(";");
}
else if (attr.Inverse != null && attr.Derived == null)
{
DocObject docref = null;
if (mapGeneral.TryGetValue(attr.DefinedType, out docref))
{
if (this.m_included == null || this.m_included.ContainsKey(docref))
{
hasinverse = true;
}
}
}
else if (attr.Derived != null)
{
hasderived = true;
}
}
// derived attributes
if (hasderived)
{
writer.WriteLine(" DERIVE");
foreach (DocAttribute attr in docEntity.Attributes)
{
if (attr.Derived != null)
{
// determine the superclass having the attribute
DocEntity found = null;
DocEntity super = docEntity;
while (super != null && found == null && super.BaseDefinition != null)
{
super = mapEntity[super.BaseDefinition] as DocEntity;
if (super != null)
{
foreach (DocAttribute docattr in super.Attributes)
{
if (docattr.Name.Equals(attr.Name))
{
// found class
found = super;
break;
}
}
}
}
writer.Write("\t");
if (found != null)
{
// overridden attribute
writer.Write("SELF\\");
writer.Write(found.Name);
writer.Write(".");
}
writer.Write(attr.Name);
writer.Write(" : ");
WriteExpressAggregation(writer, attr);
writer.Write(attr.DefinedType);
writer.Write(" := ");
writer.Write(attr.Derived);
writer.WriteLine(";");
}
}
}
// inverse attributes
if (hasinverse)
{
writer.WriteLine(" INVERSE");
foreach (DocAttribute attr in docEntity.Attributes)
{
if (attr.Inverse != null && attr.Derived == null)
{
DocObject docref = null;
if (mapGeneral.TryGetValue(attr.DefinedType, out docref))
{
if (this.m_included == null || this.m_included.ContainsKey(docref))
{
writer.Write("\t");
writer.Write(attr.Name);
writer.Write(" : ");
WriteExpressAggregation(writer, attr);
writer.Write(attr.DefinedType);
writer.Write(" FOR ");
writer.Write(attr.Inverse);
writer.WriteLine(";");
}
}
}
}
}
// unique rules
if(this.m_included == null && docEntity.UniqueRules != null && docEntity.UniqueRules.Count > 0)
{
writer.WriteLine(" UNIQUE");
foreach (DocUniqueRule where in docEntity.UniqueRules)
{
writer.Write("\t");
writer.Write(where.Name);
writer.Write(" : ");
foreach (DocUniqueRuleItem ruleitem in where.Items)
{
if (ruleitem != where.Items[0])
{
writer.Write(", ");
}
writer.Write(ruleitem.Name);
}
writer.WriteLine(";");
}
}
// where rules
if (this.m_included == null && docEntity.WhereRules != null && docEntity.WhereRules.Count > 0)
{
writer.WriteLine(" WHERE");
foreach (DocWhereRule where in docEntity.WhereRules)
{
writer.Write("\t");
writer.Write(where.Name);
writer.Write(" : ");
writer.Write(MakeLongFormExpression(where.Expression));
writer.WriteLine(";");
}
}
writer.WriteLine("END_ENTITY;");
writer.WriteLine();
}
// functions
foreach (DocFunction docFunction in mapFunction.Values)
{
writer.Write("FUNCTION ");
writer.WriteLine(docFunction.Name);
writer.WriteLine(MakeLongFormExpression(docFunction.Expression));
writer.WriteLine("END_FUNCTION;");
writer.WriteLine();
}
// rules
foreach (DocGlobalRule docRule in mapRule.Values)
{
writer.Write("RULE ");
writer.Write(docRule.Name);
writer.WriteLine(" FOR");
writer.Write("\t(");
writer.Write(docRule.ApplicableEntity);
writer.WriteLine(");");
writer.WriteLine(docRule.Expression);
// where
writer.WriteLine(" WHERE");
foreach (DocWhereRule docWhere in docRule.WhereRules)
{
writer.Write(" ");
writer.Write(docWhere.Name);
writer.Write(" : ");
writer.Write(MakeLongFormExpression(docWhere.Expression));
writer.WriteLine(";");
}
writer.WriteLine("END_RULE;");
writer.WriteLine();
}
writer.WriteLine("END_SCHEMA;");
}
}
private static void WriteExpressAggregation(System.IO.StreamWriter writer, DocAttribute attr)
{
if (attr.AggregationType == 0)
return;
DocAttribute docAggregation = attr;
while (docAggregation != null)
{
switch (docAggregation.AggregationType)
{
case 1:
writer.Write("LIST ");
break;
case 2:
writer.Write("ARRAY ");
break;
case 3:
writer.Write("SET ");
break;
case 4:
writer.Write("BAG ");
break;
}
if (docAggregation.AggregationUpper != null && docAggregation.AggregationUpper != "0")
{
writer.Write("[" + docAggregation.AggregationLower + ":" + docAggregation.AggregationUpper + "] OF ");
}
else if (docAggregation.AggregationLower != null)
{
writer.Write("[" + docAggregation.AggregationLower + ":?] OF ");
}
else
{
writer.Write("[0:?] OF ");
}
if ((docAggregation.AggregationFlag & 2) != 0)
{
// unique
writer.Write("UNIQUE ");
}
// drill in
docAggregation = docAggregation.AggregationAttribute;
}
}
#region IDisposable Members
public void Dispose()
{
}
#endregion
#region IComparer Members
public int Compare(string x, string y)
{
return String.CompareOrdinal((string)x, (string)y);
}
#endregion
}
}