Newer
Older
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
using JsonSubTypes;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Linq;
using System;
using UnityEngine;
using MoreLinq;
namespace REST_JSON_API
{
[JsonConverter(typeof(JsonSubtypes), "kind")]
[JsonSubtypes.KnownSubType(typeof(OMA), "OMA")]
[JsonSubtypes.KnownSubType(typeof(OMBINDC), "OMBINDC")]
[JsonSubtypes.KnownSubType(typeof(OMS), "OMS")]
[JsonSubtypes.KnownSubType(typeof(OMSTR), "OMSTR")]
[JsonSubtypes.KnownSubType(typeof(OMF), "OMF")]
[JsonSubtypes.KnownSubType(typeof(OMV), "OMV")]
[JsonSubtypes.KnownSubType(typeof(RAW), "RAW")]
//[JsonSubtypes.KnownSubType(typeof(OMC<T>), "OMC<" + typeof(T) + ">")]
[JsonSubtypes.KnownSubType(typeof(OMC<Vector3>), "OMC<UnityEngine.Vector3>")]
[JsonSubtypes.KnownSubType(typeof(OMC<float>), "OMC<System.Single>")]
[JsonSubtypes.KnownSubType(typeof(OMC<double>), "OMC<System.Double>")]
abstract public partial class SOMDoc
{
public string kind;
protected SOMDoc() { kind = this.GetType().Name; }
public SOMDoc TypeOf()
=> TypeOf(new SOMDoc[0]);
protected internal abstract SOMDoc TypeOf(SOMDoc[] args);
public static bool Equivalent(SOMDoc sd1, SOMDoc sd2)
=> sd1 != null && sd2 != null
&& sd1.Equivalent(sd2);
public abstract bool Equivalent(SOMDoc sd2);
public LambdaExpression PartialInvokeCastingLambdaExpression(out Type[] signature_args, object[] callArgs = null, bool[] useArgs = null)
{
LambdaExpression lambda_orig = GetLambdaExpression();
signature_args = new Type[lambda_orig.Parameters.Count + 1];
ParameterExpression object_arr = Expression.Parameter(typeof(object[]), "PARAMS_Arr");
Expression[] cast_new_to_signature = new Expression[lambda_orig.Parameters.Count];
int n_params = 0;
for (int i = 0; i < lambda_orig.Parameters.Count; i++)
{
if (callArgs != null && callArgs.Length < i
&& (useArgs == null || (useArgs.Length < i && useArgs[i])))
{
cast_new_to_signature[i] =
Expression.Constant(callArgs[i], lambda_orig.Parameters[i].Type);
continue;
}
signature_args[n_params++] = lambda_orig.Parameters[i].Type;
cast_new_to_signature[i] =
Expression.Convert(
Expression.ArrayIndex(
object_arr,
Expression.Constant(i)
),
lambda_orig.Parameters[i].Type
);
}
signature_args[n_params] = lambda_orig.ReturnType;
signature_args = signature_args.Slice(0, n_params + 1).ToArray();
return Expression.Lambda(
typeof(Array).IsAssignableFrom(lambda_orig.ReturnType)
? Expression.Convert(
Expression.Invoke(lambda_orig, cast_new_to_signature),
typeof(object[]))
: Expression.NewArrayInit(
typeof(object),
new Expression[] { Expression.Convert(Expression.Invoke(lambda_orig, cast_new_to_signature), typeof(object)) }),
object_arr
);
}
public LambdaExpression GetLambdaExpression()
=> GetLambdaExpression(new LambdaExpression[0], new LambdaExpression[0], new ParameterExpression[0]);
protected internal abstract LambdaExpression GetLambdaExpression(LambdaExpression[] lambda_applicant, LambdaExpression[] lambda_arguments, ParameterExpression[] bound_params);
public abstract override string ToString();
public abstract SOMDoc MapURIs(Dictionary<string, string> old_to_new);
#region MakeMMT_OMS_URItoSOMDoc
public static OMA MakeTupel(Vector3 vec)
=> MakeTupel(new[] {
new OMF(vec.x),
new OMF(vec.y),
new OMF(vec.z),
});
public static OMA MakeTupel(SOMDoc[] args)
=> new(
new OMS(MMTConstants.Tuple),
args
);
public static OMA MakeShallowList(SOMDoc[] args)
=> new(
new OMS(MMTConstants.ListApplicant),
args
);
public static OMA MakeDeepList(SOMDoc[] args, SOMDoc typeURI)
{
SOMDoc[] end_of_list = new SOMDoc[] {
new OMA(
new OMS(MMTConstants.ListEnd),
new[] {
typeURI,
}
),
args.Length == 0
? null
: args[^1]
};
if (args.Length == 0)
end_of_list = end_of_list[..^0];
SOMDoc defines = new OMA(
new OMS(MMTConstants.ListLiteral),
end_of_list
);
for (int i = args.Length - 2; i >= 0; i--)
{
defines = new OMA(
new OMS(MMTConstants.ListLiteral),
new[] {
defines,
args[i],
});
}
SOMDoc type = new OMA(
new OMS(MMTConstants.ListApplicant),
new[] { typeURI }
);
return new OMA(type, new[] { defines });
}
#endregion MakeMMT_OMS_URItoSOMDoc
}
public abstract class SOMDocCRTP<T> : SOMDoc where T : SOMDocCRTP<T>
{
protected SOMDocCRTP() : base() { }
public override bool Equivalent(SOMDoc sd2)
=> this.GetType() == sd2.GetType() && (this as T).EquivalentWrapped(sd2 as T);
protected abstract bool EquivalentWrapped(T sd2);
public override SOMDoc MapURIs(Dictionary<string, string> old_to_new)
=> MapURIsWrapped(old_to_new);
protected abstract T MapURIsWrapped(Dictionary<string, string> old_to_new);
}
public class OMBINDC : SOMDocCRTP<OMBINDC>
{
public new string kind = "OMBINDC";
public string name;
public SOMDoc lambdabody;
/// <summary>@Type of this variable</summary>
[JsonIgnore]
public Type type;
/// <summary>Enables (especially <see cref="JsonConverter"/>) to read and set <see cref="type"/> by its <c>string</c> representation.</summary>
public string typeString
{
get => MMTConstants.TYPE_TO_OMS[type];
set => type = MMTConstants.OMS_TO_TYPE[value];
}
[JsonConstructor]
public OMBINDC(string name, string typeString, SOMDoc lambdabody) : base()
{
this.name = name;
this.typeString = typeString;
this.lambdabody = lambdabody;
}
protected internal override SOMDoc TypeOf(SOMDoc[] args)
=> throw new NotImplementedException();
public OMBINDC(string name, Type type, SOMDoc lambdabody) : base()
{
this.name = name;
this.type = type;
this.lambdabody = lambdabody;
}
protected override bool EquivalentWrapped(OMBINDC sd2)
=> this.type == sd2.type
&& this.name.Equals(sd2.name)
&& this.lambdabody.Equivalent(sd2.lambdabody);
protected internal override LambdaExpression GetLambdaExpression(LambdaExpression[] lambda_applicant, LambdaExpression[] lambda_arguments, ParameterExpression[] bound_params)
{
ParameterExpression[] bind_me = bound_params.ShallowCloneAppend(
new[] { Expression.Parameter(type, name) }
);
return lambdabody.GetLambdaExpression(lambda_applicant, lambda_arguments, bind_me);
}
public override string ToString()
=> "OMBINDC(" + name + ", " + typeString + ")->";
protected override OMBINDC MapURIsWrapped(Dictionary<string, string> old_to_new)
=> new(name, type, lambdabody.MapURIs(old_to_new));
}
public class OMA : SOMDocCRTP<OMA>
{
public new string kind = "OMA";
public SOMDoc applicant;
public SOMDoc[] arguments;
[JsonConstructor]
public OMA(SOMDoc applicant, SOMDoc[] arguments) : base()
{
this.applicant = applicant;
this.arguments = arguments;
}
protected internal override SOMDoc TypeOf(SOMDoc[] args)
=> applicant.TypeOf(arguments.Select(a => a.TypeOf()).ToArray());
protected override bool EquivalentWrapped(OMA sd2)
=> Equivalent(this.applicant, sd2.applicant)
&& this.arguments
.Zip(sd2.arguments, (arg1, arg2) => Equivalent(arg1, arg2))
.All(b => b);
protected internal override LambdaExpression GetLambdaExpression(LambdaExpression[] lambda_applicant_paps, LambdaExpression[] lambda_applicant_grands, ParameterExpression[] bound_params)
=> applicant.GetLambdaExpression(
arguments.Select(arg => arg.GetLambdaExpression(new LambdaExpression[0], new LambdaExpression[0], bound_params)).ToArray(),
lambda_applicant_paps,
bound_params
);
public override string ToString()
=> applicant.ToString() + "(" + string.Join(", ", arguments.Select(a => a.ToString())) + ")";
protected override OMA MapURIsWrapped(Dictionary<string, string> old_to_new)
=> new OMA(
applicant.MapURIs(old_to_new),
arguments.Select(arg => arg.MapURIs(old_to_new)).ToArray()
);
}
public class OMS : SOMDocCRTP<OMS>
{
public new string kind = "OMS";
public string uri;
[JsonConstructor]
public OMS(string uri) : base()
{
this.uri = uri;
}
protected internal override SOMDoc TypeOf(SOMDoc[] args)
{
if (FactOrganizer.AllFacts.TryGetValue(uri, out Fact found))
return new OMS(MMTConstants.TYPE_TO_OMS[found.GetType()]);
if (MMTConstants.HeterogenApplication_TO_TypeOF.TryGetValue(uri, out string type))
return new OMA(
new OMS(type),
args
);
if (MMTConstants.HomogenApplication_TO_TypeOF.TryGetValue(uri, out type))
return new OMA(
new OMS(type),
new[] { args[0] }
);
if (MMTConstants.URI_TO_TypeOF.TryGetValue(uri, out type))
return new OMS(type);
throw new NotImplementedException();
}
protected override bool EquivalentWrapped(OMS sd2)
=> this.uri == sd2.uri;
protected internal override LambdaExpression GetLambdaExpression(LambdaExpression[] lambda_applicant, LambdaExpression[] lambda_arguments, ParameterExpression[] bound_params)
=> SOMDocToLambdaExpression<float>.MakeLambdaExpression(uri, lambda_applicant, lambda_arguments, bound_params);
public override string ToString()
=> uri;
protected override OMS MapURIsWrapped(Dictionary<string, string> old_to_new)
{
if (!old_to_new.TryGetValue(uri, out string new_uri))
new_uri = uri;
return new OMS(new_uri);
}
}
public class OMSTR : SOMDocCRTP<OMSTR>
{
public new string kind = "OMSTR";
[JsonProperty("float")]
public string s;
[JsonConstructor]
public OMSTR(string s) : base()
{
this.s = s;
}
protected internal override SOMDoc TypeOf(SOMDoc[] args)
=> throw new NotImplementedException();
protected override bool EquivalentWrapped(OMSTR sd2)
=> this.s == sd2.s;
protected internal override LambdaExpression GetLambdaExpression(LambdaExpression[] lambda_applicant, LambdaExpression[] lambda_arguments, ParameterExpression[] bound_params)
=> Expression.Lambda(Expression.Constant(s, typeof(string)), null);
public override string ToString()
=> s;
protected override OMSTR MapURIsWrapped(Dictionary<string, string> old_to_new)
=> (OMSTR)this.MemberwiseClone();
}
public class OMF : SOMDocCRTP<OMF>
{
public new string kind = "OMF";
[JsonProperty("float")]
public float @float;
[JsonConstructor]
public OMF(float f) : base()
{
this.@float = f;
}
protected internal override SOMDoc TypeOf(SOMDoc[] args)
=> new OMS(MMTConstants.RealLit);
protected override bool EquivalentWrapped(OMF sd2)
=> Mathf.Approximately(@float, sd2.@float);
protected internal override LambdaExpression GetLambdaExpression(LambdaExpression[] lambda_applicant, LambdaExpression[] lambda_arguments, ParameterExpression[] bound_params)
=> Expression.Lambda(Expression.Constant(@float, typeof(float)), null);
public override string ToString()
=> @float.ToString();
protected override OMF MapURIsWrapped(Dictionary<string, string> old_to_new)
=> (OMF)this.MemberwiseClone();
}
public class OMC<T> : SOMDocCRTP<OMC<T>>
{
public new string kind = "OMC<" + typeof(T) + ">";
public T value;
[JsonConstructor]
public OMC(T value) : base()
{
this.value = value;
}
protected internal override SOMDoc TypeOf(SOMDoc[] args)
=> new OMS(MMTConstants.TYPE_TO_OMS[typeof(T)]);
protected override bool EquivalentWrapped(OMC<T> sd2)
{
Debug.LogWarning("Cannot check Equivalency for " + this.GetType() + "; only whether it's exact!");
return this.value.Equals(value);
}
protected internal override LambdaExpression GetLambdaExpression(LambdaExpression[] lambda_applicant, LambdaExpression[] lambda_arguments, ParameterExpression[] bound_params)
=> Expression.Lambda(Expression.Constant(value, typeof(T)), null);
public override string ToString()
=> "C_" + typeof(T) + "(" + value.ToString() + ")";
protected override OMC<T> MapURIsWrapped(Dictionary<string, string> old_to_new)
=> (OMC<T>)this.MemberwiseClone();
}
public class OMV : SOMDocCRTP<OMV>
{
public new string kind = "OMV";
public string name;
[JsonConstructor]
public OMV(string name) : base()
{
this.name = name;
}
protected internal override SOMDoc TypeOf(SOMDoc[] args)
=> throw new NotImplementedException();
protected override bool EquivalentWrapped(OMV sd2)
=> this.name == sd2.name;
protected internal override LambdaExpression GetLambdaExpression(LambdaExpression[] lambda_applicant, LambdaExpression[] lambda_arguments, ParameterExpression[] bound_params)
{
ParameterExpression v = bound_params.FirstOrDefault(param => param.Name.Equals(name));
if (v == null)
{
Debug.LogErrorFormat("Unable to find {0} for {1} with name: {2}", nameof(OMBINDC), nameof(OMV), name);
return Expression.Lambda(Expression.Empty(), null);
}
else
return Expression.Lambda(v, new[] { v });
}
public override string ToString()
=> "Variable_" + "(" + name + ")";
protected override OMV MapURIsWrapped(Dictionary<string, string> old_to_new)
=> (OMV)this.MemberwiseClone();
}
public class RAW : SOMDocCRTP<RAW>
{
public new string kind = "RAW";
public string xml;
[JsonConstructor]
public RAW(string xml) : base()
{
this.xml = xml;
}
protected internal override SOMDoc TypeOf(SOMDoc[] args)
=> throw new NotImplementedException();
protected override RAW MapURIsWrapped(Dictionary<string, string> old_to_new)
{
string copy = xml;
foreach (KeyValuePair<string, string> KeyVal in old_to_new)
copy = copy.Replace(KeyVal.Key, KeyVal.Value);
return new RAW(copy);
}
public override string ToString()
=> xml;
protected override bool EquivalentWrapped(RAW sd2)
=> throw new NotImplementedException(); //xml == sd2.xml; // only exact
protected internal override LambdaExpression GetLambdaExpression(LambdaExpression[] lambda_applicant, LambdaExpression[] lambda_arguments, ParameterExpression[] bound_params)
{
throw new NotImplementedException();
}
}
}