forked from I-Sokolov/RDFGeomApi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenerator.cs
512 lines (439 loc) · 17.2 KB
/
Generator.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
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RDFWrappers
{
class Generator
{
/// <summary>
///
/// </summary>
const string KWD_PREPROC = "//##";
const string KWD_NAMESPACE = "NAMESPACE_NAME";
const string KWD_CLASS_NAME = "CLASS_NAME";
const string KWD_BASE_CLASS = "/*BASE CLASS*/Instance";
const string KWD_PROPERTIES_OF = "PROPERTIES_OF_CLASS";
const string KWD_PROPERTY_NAME = "PROPERTY_NAME";
const string KWD_DATA_TYPE = "double";
const string KWD_CARDINALITY_MIN = "CARDINALITY_MIN";
const string KWD_CARDINALITY_MAX = "CARDINALITY_MAX";
const string KWD_OBJECT_TYPE = "Instance";
const string KWD_asType = "asTYPE";
/// <summary>
///
/// </summary>
enum Template
{
None,
BeginFile,
ClassForwardDeclaration,
BeginAllClasses,
BeginWrapperClass,
StartPropertiesBlock,
SetDataProperty,
SetDataArrayProperty,
GetDataProperty,
GetDataArrayProperty,
SetObjectProperty,
SetObjectArrayProperty,
GetObjectProperty,
GetObjectArrayProperty,
GetObjectArrayPropertyInt64,
EndWrapperClass,
EndFile
}
/// <summary>
///
/// </summary>
bool m_cs; //cs or cpp
string m_TInt64;
string m_namespace;
Schema m_schema;
Dictionary<Template, string> m_template = new Dictionary<Template, string>();
Dictionary<string, string> m_replacements;
HashSet<string> m_generatedClasses;
HashSet<string> m_addedProperties;
/// <summary>
///
/// </summary>
/// <param name="schema"></param>
/// <param name="cs"></param>
public Generator (Schema schema, bool cs, string namespace_)
{
m_cs = cs;
m_TInt64 = m_cs ? "Int64" : "int64_t";
m_namespace = namespace_;
m_schema = schema;
foreach (var template in Enum.GetValues(Template.BeginFile.GetType ()))
{
m_template.Add((Template)template, "");
}
ParseTemplate();
}
/// <summary>
///
/// </summary>
/// <param name="outputFile"></param>
public void WriteWrapper(string outputFile)
{
m_generatedClasses = new HashSet<string>();
using (var writer = new StreamWriter(outputFile))
{
m_replacements = new Dictionary<string, string>();
m_replacements[KWD_NAMESPACE] = m_namespace;
WriteByTemplate(writer, Template.BeginFile);
//
// write forward declarationa
//
foreach (var cls in m_schema.m_classes)
{
m_replacements[KWD_CLASS_NAME] = cls.Key;
WriteByTemplate(writer, Template.ClassForwardDeclaration);
}
//
// write class definitions
//
writer.Write(m_template[Template.BeginAllClasses]);
foreach (var cls in m_schema.m_classes)
{
WriteClassWrapper(writer, cls.Key, cls.Value);
}
//
writer.Write(m_template[Template.EndFile]);
}
}
/// <summary>
///
/// </summary>
/// <param name="writer"></param>
/// <param name="clsName"></param>
/// <param name="cls"></param>
private void WriteClassWrapper (StreamWriter writer, string clsName, Schema.Class cls)
{
if (!m_generatedClasses.Add (clsName))
{
return;
}
if (!m_cs) //c++ requires base classes defined first
{
foreach (var parentId in cls.parents)
{
var parentName = m_schema.GetNameOfClass(parentId);
WriteClassWrapper(writer, parentName, m_schema.m_classes[parentName]);
}
}
clsName = ValidateIdentifier(clsName);
m_addedProperties = new HashSet<string>();
m_replacements = new Dictionary<string, string>();
//
//
m_replacements.Add(KWD_CLASS_NAME, clsName);
//
//
var baseClass = "Instance";
//first parent is the base class
var itParent = cls.parents.GetEnumerator();
if (itParent.MoveNext())
{
var parentId = itParent.Current;
baseClass = m_schema.GetNameOfClass(parentId);
baseClass = ValidateIdentifier(baseClass);
//gather base classes properties to avoid override here
CollectParentProperties(parentId);
}
m_replacements[KWD_BASE_CLASS] = baseClass;
//
//
WriteByTemplate(writer, Template.BeginWrapperClass);
//
//
WriteProperties(writer, clsName, cls.properties);
//
// only single inheritance is suppirted, dirrectly take properties from othe parents
while (itParent.MoveNext())
{
WriteParentProperties(writer, itParent.Current);
}
//
//
WriteByTemplate(writer,Template.EndWrapperClass);
}
/// <summary>
///
/// </summary>
/// <param name="parentId"></param>
private void CollectParentProperties(Int64 parentId)
{
string parentName = m_schema.GetNameOfClass(parentId);
var parentClass = m_schema.m_classes[parentName];
foreach (var cp in parentClass.properties)
{
m_addedProperties.Add(cp.name);
}
foreach (var nextParent in parentClass.parents)
{
CollectParentProperties(nextParent);
}
}
private void WriteByTemplate(StreamWriter writer, Template template)
{
string code = m_template[template];
foreach (var r in m_replacements)
{
if (r.Key.Equals(KWD_asType))
{
code = code.Replace(r.Key, r.Value, true, null); //ignore case
}
else
{
code = code.Replace(r.Key, r.Value);
}
}
if (m_cs)
{
code = code.Replace("string?", "string"); //warning CS8632: The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
}
else
{
code = code.Replace("const const", "const");
}
writer.Write(code);
}
/// <summary>
///
/// </summary>
/// <param name="parentClassId"></param>
private void WriteParentProperties (StreamWriter writer, Int64 parentClassId)
{
string parentName = m_schema.GetNameOfClass(parentClassId);
var parentClass = m_schema.m_classes[parentName];
WriteProperties(writer, parentName, parentClass.properties);
foreach (var nextParent in parentClass.parents)
{
WriteParentProperties(writer, nextParent);
}
}
/// <summary>
///
/// </summary>
/// <param name="properiesOfClass"></param>
/// <param name="properties"></param>
private void WriteProperties (StreamWriter writer, string properiesOfClass, List<Schema.ClassProperty> properties)
{
m_replacements[KWD_PROPERTIES_OF] = properiesOfClass;
bool first = true;
foreach (var prop in properties)
{
if (m_addedProperties.Add (prop.name))
{
if (first)
{
WriteByTemplate(writer, Template.StartPropertiesBlock);
first = false;
}
WritePropertyMethods(writer, prop);
}
}
}
/// <summary>
///
/// </summary>
/// <param name="writer"></param>
/// <param name="classProp"></param>
private void WritePropertyMethods (StreamWriter writer, Schema.ClassProperty classProp)
{
var prop = m_schema.m_properties[classProp.name];
m_replacements[KWD_PROPERTY_NAME] = classProp.name;
m_replacements[KWD_DATA_TYPE] = prop.DataType(m_cs);
m_replacements[KWD_CARDINALITY_MIN] = classProp.min.ToString();
m_replacements[KWD_CARDINALITY_MAX] = classProp.max.ToString();
m_replacements[KWD_asType] = "";
if (!prop.IsObject())
{
if (classProp.max == 1)
{
WriteByTemplate(writer, Template.SetDataProperty);
WriteByTemplate(writer, Template.GetDataProperty);
}
else
{
WriteByTemplate(writer, Template.SetDataArrayProperty);
WriteByTemplate(writer, Template.GetDataArrayProperty);
}
}
else
{
if (classProp.max == 1)
{
WriteSetObjectProperty(writer, prop, Template.SetObjectProperty);
//do we need this? we lose restrictions control! WriteAccessObjectProperty(writer, classProp.name, TInt64, "", Template.SetObjectProperty);
WriteGetObjectProperty(writer, prop, Template.GetObjectProperty);
}
else
{
WriteSetObjectProperty(writer, prop, Template.SetObjectArrayProperty);
WriteAccessObjectProperty(writer, m_TInt64, "", Template.SetObjectArrayProperty);
WriteGetObjectProperty(writer, prop, Template.GetObjectArrayProperty);
WriteAccessObjectProperty(writer, m_TInt64, "", Template.GetObjectArrayPropertyInt64);
}
}
}
/// <summary>
///
/// </summary>
/// <param name="writer"></param>
/// <param name="prop"></param>
/// <param name="template"></param>
private void WriteSetObjectProperty(StreamWriter writer, Schema.Property prop, Template template)
{
if (prop.resrtictions.Count > 0)
{
foreach (var restr in prop.resrtictions)
{
string instClass = m_schema.GetNameOfClass(restr);
WriteAccessObjectProperty(writer, instClass, "", template);
}
}
else
{
Verify(false, "This case was not tested yet: no restriction");
WriteAccessObjectProperty(writer, "Instance", "", template);
}
}
/// <summary>
///
/// </summary>
/// <param name="writer"></param>
/// <param name="prop"></param>
/// <param name="template"></param>
private void WriteGetObjectProperty(StreamWriter writer, Schema.Property prop, Template template)
{
if (prop.resrtictions.Count > 0)
{
bool first = true;
foreach (var restr in prop.resrtictions)
{
Verify(first, "This case was not tested yet: more then one restriction");
string instClass = m_schema.GetNameOfClass(restr);
string asType = first ? "" : instClass;
WriteAccessObjectProperty(writer, instClass, asType, template);
first = false;
}
}
else
{
Verify(false, "This case was not tested yet: no restriction");
WriteAccessObjectProperty(writer, "Instance", "", template);
}
}
/// <summary>
///
/// </summary>
/// <param name="writer"></param>
/// <param name="objectType"></param>
/// <param name="asType"></param>
/// <param name="template"></param>
private void WriteAccessObjectProperty(StreamWriter writer, string objectType, string asType, Template template)
{
m_replacements[KWD_OBJECT_TYPE] = objectType;
m_replacements[KWD_asType] = asType;
WriteByTemplate(writer, template);
}
/// <summary>
///
/// </summary>
private void ParseTemplate()
{
System.Reflection.Assembly assembly = System.Reflection.Assembly.GetCallingAssembly();
var templateName = m_cs ? "EngineEx_Template.cs" : "EngineEx_Template.h";
string resourceName = FormatResourceName(assembly, templateName);
using (Stream stream = assembly.GetManifestResourceStream(resourceName))
{
Verify(stream != null, "Failed get resource " + resourceName);
using (StreamReader reader = new StreamReader(stream)) //to read from file use new StreamReader(templateFile))
{
Verify(reader != null, "Failed create stream reader for " + templateName);
Template part = Template.BeginFile;
int nline = 0;
string line;
while ((line = reader.ReadLine()) != null)
{
nline++;
if (line.Contains(KWD_PREPROC))
{
part++;
Verify(line.Contains(part.ToString()), string.Format("Expected line {0} contains '{1}' substing while parsing template fle {2}", nline, part.ToString(), templateName));
}
else
{
string str = m_template[part];
str = str + line + "\r\n";
m_template[part] = str;
}
}
}
}
}
/// <summary>
///
/// </summary>
/// <param name="assembly"></param>
/// <param name="resourceName"></param>
/// <returns></returns>
private static string FormatResourceName(System.Reflection.Assembly assembly, string resourceName)
{
return assembly.GetName().Name + "." + resourceName.Replace(" ", "_").Replace("\\", ".").Replace("/", ".");
}
/// <summary>
///
/// </summary>
/// <param name="condition"></param>
/// <param name="exceptionMsg"></param>
public static void Verify (bool condition, string exceptionMsg)
{
System.Diagnostics.Trace.Assert(condition);
if (!condition)
{
throw new ApplicationException(exceptionMsg);
}
}
public static string ValidateIdentifier (string name)
{
using (var codeProvider = System.CodeDom.Compiler.CodeDomProvider.CreateProvider("C#"))
{
if (!codeProvider.IsValidIdentifier(name))
{
//does not work as expected, var id = codeProvider.CreateValidIdentifier(name);
bool first = true;
var builder = new System.Text.StringBuilder();
foreach (var ch in name)
{
if (first && char.IsLetter(ch) || !first && char.IsLetterOrDigit(ch))
{
//fits
builder.Append(ch);
}
else
{
//replace
builder.Append("_R");
builder.Append(((byte)ch).ToString());
builder.Append("R_");
}
first = false;
}
var id = builder.ToString();
Console.Write("!!! {0} is not a valid identifier, changed to {1}", name, id);
return id;
}
else
{
return name;
}
}
}
}
}