FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
modelscript/languages/sysml2/src/language.ts at main · modelscript/modelscript · GitHub
modelscript/languages/sysml2/src/language.ts at main · modelscript/modelscript · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Uh oh!
There was an error while loading.
Please reload this page
.
modelscript
/
modelscript
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
3
Star
12
Code
Issues
1
Pull requests
5
Discussions
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
modelscript
/
languages
/
sysml2
/
src
/
language.ts
Copy path
More file actions
More file actions
Latest commit
History
History
History
5157 lines (4669 loc) · 177 KB
Breadcrumbs
modelscript
/
languages
/
sysml2
/
src
/
language.ts
Copy path
File metadata and controls
5157 lines (4669 loc) · 177 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* eslint-disable */
/**
* examples/sysml2/language.ts — SysML v2 language definition
*
* Port of the SysML2 Xtext grammar into metascript combinators.
* Inherits KerML expression rules inline (flattened AST).
*
*
@license
LGPL-3.0-or-later
*/
import
{
emitAxioms
}
from
"../reasoner-bridge.js"
;
import
{
choice
,
def
,
error
,
field
,
info
,
language
,
optional
,
prec
,
ref
,
repeat
,
repeat1
,
seq
,
token
,
warning
,
}
from
"@modelscript/language"
;
import
type
{
QueryDB
,
SymbolEntry
}
from
"@modelscript/language/compiler"
;
// ---------------------------------------------------------------------------
// Precedence constants
// ---------------------------------------------------------------------------
const
PREC
=
{
CONDITIONAL
:
1
,
NULL_COALESCING
:
2
,
IMPLIES
:
3
,
OR
:
4
,
XOR
:
5
,
AND
:
6
,
EQUALITY
:
7
,
CLASSIFICATION
:
8
,
RELATIONAL
:
9
,
RANGE
:
10
,
ADDITIVE
:
11
,
MULTIPLICATIVE
:
12
,
EXPONENTIATION
:
13
,
UNARY
:
14
,
EXTENT
:
15
,
PRIMARY
:
16
,
}
as
const
;
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
// ---- shared attribute extractors ----
const
meta
=
(
self
:
SymbolEntry
)
=>
self
.
metadata
as
Record
<
string
,
unknown
>
|
null
;
const
metaStr
=
(
self
:
SymbolEntry
,
key
:
string
)
:
string
|
null
=>
(
meta
(
self
)
?.
[
key
]
as
string
)
??
null
;
// ---- shared symbol helpers ----
/** Shared symbol lambda for all Definition rules.
* Captures modifier fields as attributes from CST nodes. */
const
defAttrs
=
(
_defKind
:
string
)
=>
(
self
:
any
)
=>
(
{
kind
:
"Definition"
as
const
,
name
:
self
.
declaredName
,
exports
:
[
"body"
]
as
any
[
]
,
attributes
:
{
isAbstract
:
self
.
isAbstract
,
isVariation
:
self
.
isVariation
,
}
,
}
)
;
/** Shared symbol lambda for all Usage rules.
* Captures all modifier fields from _usage_modifier as CST attributes. */
const
usageAttrs
=
(
_usageKind
:
string
)
=>
(
self
:
any
)
=>
(
{
kind
:
"Usage"
as
const
,
name
:
self
.
declaredName
,
attributes
:
{
direction
:
self
.
direction
,
isAbstract
:
self
.
isAbstract
,
isVariation
:
self
.
isVariation
,
isDerived
:
self
.
isDerived
,
isEnd
:
self
.
isEnd
,
isRef
:
self
.
isRef
,
isOrdered
:
self
.
isOrdered
,
isNonunique
:
self
.
isNonunique
,
isConstant
:
self
.
isConstant
,
multiplicityLower
:
self
.
ownedMultiplicity
.
ownedRelatedElement
.
lowerBound
,
multiplicityUpper
:
self
.
ownedMultiplicity
.
ownedRelatedElement
.
upperBound
,
}
,
}
)
;
// ---- shared queries ----
/** Shared queries for all Definition and Package rules */
const
namespaceQueries
=
{
members
:
(
db
:
QueryDB
,
self
:
SymbolEntry
)
=>
db
.
childrenOf
(
self
.
id
)
,
ownedDefinitions
:
(
db
:
QueryDB
,
self
:
SymbolEntry
)
=>
db
.
childrenOf
(
self
.
id
)
.
filter
(
(
c
)
=>
c
.
kind
===
"Definition"
||
c
.
kind
===
"Enumeration"
)
,
ownedUsages
:
(
db
:
QueryDB
,
self
:
SymbolEntry
)
=>
db
.
childrenOf
(
self
.
id
)
.
filter
(
(
c
)
=>
c
.
kind
===
"Usage"
)
,
imports
:
(
db
:
QueryDB
,
self
:
SymbolEntry
)
=>
db
.
childrenOf
(
self
.
id
)
.
filter
(
(
c
)
=>
c
.
kind
===
"Import"
)
,
}
;
/** Structural queries for definitions — filter children by usageKind metadata */
const
definitionStructuralQueries
=
{
...
namespaceQueries
,
inferredSuperClasses
:
(
db
:
QueryDB
,
self
:
SymbolEntry
)
=>
packageTraceabilityQueries
.
inferredSuperClasses
(
db
,
self
)
,
ownedParts
:
(
db
:
QueryDB
,
self
:
SymbolEntry
)
=>
db
.
childrenOf
(
self
.
id
)
.
filter
(
(
c
)
=>
c
.
ruleName
===
"PartUsage"
)
,
ownedAttributes
:
(
db
:
QueryDB
,
self
:
SymbolEntry
)
=>
db
.
childrenOf
(
self
.
id
)
.
filter
(
(
c
)
=>
c
.
ruleName
===
"AttributeUsage"
)
,
ownedPorts
:
(
db
:
QueryDB
,
self
:
SymbolEntry
)
=>
db
.
childrenOf
(
self
.
id
)
.
filter
(
(
c
)
=>
c
.
ruleName
===
"PortUsage"
)
,
ownedActions
:
(
db
:
QueryDB
,
self
:
SymbolEntry
)
=>
db
.
childrenOf
(
self
.
id
)
.
filter
(
(
c
)
=>
c
.
ruleName
===
"ActionUsage"
)
,
ownedConstraints
:
(
db
:
QueryDB
,
self
:
SymbolEntry
)
=>
db
.
childrenOf
(
self
.
id
)
.
filter
(
(
c
)
=>
c
.
ruleName
===
"ConstraintUsage"
)
,
ownedConnections
:
(
db
:
QueryDB
,
self
:
SymbolEntry
)
=>
db
.
childrenOf
(
self
.
id
)
.
filter
(
(
c
)
=>
c
.
ruleName
===
"ConnectionUsage"
)
,
superclassifiers
:
(
db
:
QueryDB
,
self
:
SymbolEntry
)
=>
{
// Find child OwnedSubclassification ref entries and resolve them
const
results
:
SymbolEntry
[
]
=
[
]
;
for
(
const
child
of
db
.
childrenOf
(
self
.
id
)
)
{
if
(
child
.
ruleName
===
"OwnedSubclassification"
&&
child
.
name
)
{
const
targets
=
db
.
byName
(
child
.
name
)
;
for
(
const
t
of
targets
)
if
(
t
.
kind
===
"Definition"
)
results
.
push
(
t
)
;
}
}
return
results
;
}
,
extractTopology
:
(
db
:
QueryDB
,
self
:
SymbolEntry
)
=>
{
// Basic extraction to stub out Phase 3 logic
const
rootIds
=
[
self
.
id
]
;
const
nodes
=
new
Map
<
number
,
import
(
"@modelscript/language/compiler"
)
.
TopologyNode
>
(
)
;
const
edges
:
import
(
"@modelscript/language/compiler"
)
.
TopologyEdge
[
]
=
[
]
;
const
walk
=
(
entryId
:
number
,
parentId
:
number
|
null
,
pathPrefix
:
string
)
=>
{
const
entry
=
db
.
symbol
(
entryId
)
;
if
(
!
entry
)
return
;
const
path
=
pathPrefix
?
`
${
pathPrefix
}
.
${
entry
.
name
}
`
:
entry
.
name
||
""
;
const
node
:
import
(
"@modelscript/language/compiler"
)
.
TopologyNode
=
{
usageId
:
entry
.
id
,
path
,
targetClassId
:
null
,
typeName
:
""
,
children
:
[
]
,
parentId
,
}
;
nodes
.
set
(
entry
.
id
,
node
)
;
// Check allocation
const
allocations
=
db
.
childrenOf
(
entry
.
id
)
.
filter
(
(
c
)
=>
c
.
ruleName
===
"AllocationUsage"
)
;
for
(
const
alloc
of
allocations
)
{
const
target
=
db
.
query
<
SymbolEntry
|
null
>
(
"resolvedTarget"
,
alloc
.
id
)
;
if
(
target
)
{
node
.
targetClassId
=
target
.
id
;
}
}
// Check implements in SysML itself (or modelica index)
let
implementsTarget
=
(
entry
.
metadata
as
any
)
?.
implementsTarget
as
string
|
undefined
;
const
ann
=
(
entry
.
metadata
as
any
)
?.
annotationClause
as
string
|
undefined
;
if
(
!
implementsTarget
&&
ann
)
{
const
match
=
ann
.
match
(
/
S
y
s
M
L
\s
*
\(
\s
*
i
m
p
l
e
m
e
n
t
s
\s
*
=
\s
*
"
(
[
^
"
]
+
)
"
\s
*
\)
/
)
;
if
(
match
)
implementsTarget
=
match
[
1
]
;
}
if
(
implementsTarget
)
{
const
res
=
db
.
byName
(
implementsTarget
)
;
if
(
res
&&
res
.
length
>
0
)
node
.
targetClassId
=
res
[
0
]
.
id
;
}
const
parts
=
db
.
childrenOf
(
entry
.
id
)
.
filter
(
(
c
)
=>
c
.
ruleName
===
"PartUsage"
||
c
.
ruleName
===
"SubjectUsage"
||
c
.
ruleName
===
"ObjectiveRequirementUsage"
,
)
;
for
(
const
part
of
parts
)
{
if
(
part
.
ruleName
===
"ObjectiveRequirementUsage"
&&
!
node
.
targetClassId
)
{
// Find subject inside objective
const
subjects
=
db
.
childrenOf
(
part
.
id
)
.
filter
(
(
c
)
=>
c
.
ruleName
===
"SubjectUsage"
)
;
for
(
const
subj
of
subjects
)
{
const
typeNode
=
usageQueries
.
resolvedType
(
db
,
subj
)
;
if
(
typeNode
)
node
.
targetClassId
=
typeNode
.
id
;
}
}
const
childNode
=
walk
(
part
.
id
,
entry
.
id
,
path
)
;
if
(
childNode
)
node
.
children
.
push
(
childNode
)
;
// If this part is a SubjectUsage and its type maps to a Class or Definition, optionally adopt its targetClassId
// to bootstrap the root definition (useful if the subject IS the model to simulate)
if
(
part
.
ruleName
===
"SubjectUsage"
&&
!
node
.
targetClassId
)
{
const
typeNode
=
usageQueries
.
resolvedType
(
db
,
part
)
;
if
(
typeNode
)
node
.
targetClassId
=
typeNode
.
id
;
}
}
const
connects
=
db
.
childrenOf
(
entry
.
id
)
.
filter
(
(
c
)
=>
c
.
ruleName
===
"ConnectionUsage"
)
;
for
(
const
conn
of
connects
)
{
// extract endpoints
const
refs
=
db
.
childrenOf
(
conn
.
id
)
.
filter
(
(
c
)
=>
c
.
kind
===
"Reference"
)
;
if
(
refs
.
length
>=
2
&&
refs
[
0
]
.
name
&&
refs
[
1
]
.
name
)
{
const
src
=
resolveFeatureInScope
(
db
,
conn
.
parentId
,
refs
[
0
]
.
name
)
;
const
tgt
=
resolveFeatureInScope
(
db
,
conn
.
parentId
,
refs
[
1
]
.
name
)
;
if
(
src
&&
tgt
)
{
edges
.
push
(
{
sourceId
:
src
.
id
,
targetId
:
tgt
.
id
,
connectionId
:
conn
.
id
,
}
)
;
}
}
}
return
node
;
}
;
walk
(
self
.
id
,
null
,
""
)
;
// Build explicit sysmlPath → simVarName mapping.
// The root node's path segment is stripped because Modelica (and most simulators)
// flatten under the top-level model without repeating its name.
const
variableMap
=
new
Map
<
string
,
string
>
(
)
;
const
rootPath
=
self
.
name
||
""
;
const
rootPrefix
=
rootPath
?
rootPath
+
"."
:
""
;
const
buildVarMap
=
(
node
:
import
(
"@modelscript/language/compiler"
)
.
TopologyNode
)
=>
{
if
(
node
.
path
&&
node
.
path
!==
rootPath
)
{
// Strip root prefix: "circuit.C.v" → "C.v"
const
simPath
=
node
.
path
.
startsWith
(
rootPrefix
)
?
node
.
path
.
substring
(
rootPrefix
.
length
)
:
node
.
path
;
variableMap
.
set
(
node
.
path
,
simPath
)
;
}
// Also map attribute children of each part (e.g., part.voltage → part.v)
const
usageEntry
=
db
.
symbol
(
node
.
usageId
)
;
if
(
usageEntry
)
{
for
(
const
attr
of
db
.
childrenOf
(
usageEntry
.
id
)
)
{
if
(
attr
.
ruleName
===
"AttributeUsage"
&&
attr
.
name
)
{
const
sysmlAttrPath
=
node
.
path
?
`
${
node
.
path
}
.
${
attr
.
name
}
`
:
attr
.
name
;
const
simAttrPath
=
node
.
path
&&
node
.
path
.
startsWith
(
rootPrefix
)
?
`
${
node
.
path
.
substring
(
rootPrefix
.
length
)
}
.
${
attr
.
name
}
`
:
sysmlAttrPath
;
variableMap
.
set
(
sysmlAttrPath
,
simAttrPath
)
;
}
}
}
for
(
const
child
of
node
.
children
)
{
buildVarMap
(
child
)
;
}
}
;
for
(
const
rootId
of
rootIds
)
{
const
rootNode
=
nodes
.
get
(
rootId
)
;
if
(
rootNode
)
buildVarMap
(
rootNode
)
;
}
return
{
rootIds
,
nodes
,
edges
,
variableMap
}
;
}
,
}
;
/** Queries for all Usage rules */
const
usageQueries
=
{
inferredSuperClasses
:
(
db
:
QueryDB
,
self
:
SymbolEntry
)
=>
packageTraceabilityQueries
.
inferredSuperClasses
(
db
,
self
)
,
ownedFeatures
:
(
db
:
QueryDB
,
self
:
SymbolEntry
)
=>
db
.
childrenOf
(
self
.
id
)
.
filter
(
(
c
)
=>
c
.
kind
===
"Usage"
)
,
resolvedType
:
(
db
:
QueryDB
,
self
:
SymbolEntry
)
=>
{
// Find child OwnedFeatureTyping ref entries and resolve the type name
for
(
const
child
of
db
.
childrenOf
(
self
.
id
)
)
{
if
(
child
.
ruleName
===
"OwnedFeatureTyping"
&&
child
.
name
)
{
const
targets
=
db
.
byName
(
child
.
name
)
;
for
(
const
t
of
targets
)
{
if
(
t
.
kind
===
"Definition"
||
t
.
kind
===
"Enumeration"
||
t
.
kind
===
"Class"
)
return
t
;
}
}
}
return
null
;
}
,
typeSpecifier
:
(
db
:
QueryDB
,
self
:
SymbolEntry
)
=>
{
// Resolve type from child OwnedFeatureTyping ref
const
typeChild
=
db
.
childrenOf
(
self
.
id
)
.
find
(
(
c
)
=>
c
.
ruleName
===
"OwnedFeatureTyping"
)
;
let
typeName
=
typeChild
?.
name
??
null
;
if
(
typeName
===
"KerML::Real"
||
typeName
===
"ISQ::Real"
)
typeName
=
"Real"
;
if
(
typeName
===
"KerML::Integer"
)
typeName
=
"Integer"
;
if
(
typeName
===
"KerML::Boolean"
)
typeName
=
"Boolean"
;
return
typeName
;
}
,
redefinedFeatures
:
(
db
:
QueryDB
,
self
:
SymbolEntry
)
=>
db
.
childrenOf
(
self
.
id
)
.
filter
(
(
c
)
=>
c
.
ruleName
===
"OwnedRedefinition"
)
,
subsettedFeatures
:
(
db
:
QueryDB
,
self
:
SymbolEntry
)
=>
db
.
childrenOf
(
self
.
id
)
.
filter
(
(
c
)
=>
c
.
ruleName
===
"OwnedSubsetting"
)
,
}
;
// ---- shared model configs ----
/** Shared model config for all Definition rules */
const
definitionModel
=
{
name
:
"DefinitionNode"
as
const
,
visitable
:
true
,
specializable
:
true
,
properties
:
{
defKind
:
"string"
as
const
,
isAbstract
:
"boolean"
as
const
,
isVariation
:
"boolean"
as
const
,
}
,
queryTypes
:
{
members
:
"SemanticNode[]"
as
const
,
ownedUsages
:
"UsageNode[]"
as
const
,
ownedDefinitions
:
"DefinitionNode[]"
as
const
,
imports
:
"SemanticNode[]"
as
const
,
ownedParts
:
"UsageNode[]"
as
const
,
ownedAttributes
:
"UsageNode[]"
as
const
,
ownedPorts
:
"UsageNode[]"
as
const
,
ownedActions
:
"UsageNode[]"
as
const
,
ownedConstraints
:
"UsageNode[]"
as
const
,
ownedConnections
:
"UsageNode[]"
as
const
,
superclassifiers
:
"DefinitionNode[]"
as
const
,
}
,
}
;
/** Shared model config for all Usage rules */
const
usageModel
=
{
name
:
"UsageNode"
as
const
,
visitable
:
true
,
specializable
:
true
,
properties
:
{
usageKind
:
"string"
as
const
,
isRef
:
"boolean"
as
const
,
isAbstract
:
"boolean"
as
const
,
isVariation
:
"boolean"
as
const
,
isDerived
:
"boolean"
as
const
,
isEnd
:
"boolean"
as
const
,
isOrdered
:
"boolean"
as
const
,
isNonunique
:
"boolean"
as
const
,
isConstant
:
"boolean"
as
const
,
direction
:
"string | null"
as
const
,
}
,
queryTypes
:
{
resolvedType
:
"DefinitionNode | null"
as
const
,
ownedFeatures
:
"UsageNode[]"
as
const
,
redefinedFeatures
:
"SemanticNode[]"
as
const
,
subsettedFeatures
:
"SemanticNode[]"
as
const
,
}
,
}
;
/** Shared model config for Package rules */
const
packageModel
=
{
name
:
"PackageNode"
as
const
,
visitable
:
true
,
properties
:
{
isLibrary
:
"boolean"
as
const
,
isStandard
:
"boolean"
as
const
}
,
queryTypes
:
{
members
:
"SemanticNode[]"
as
const
,
ownedDefinitions
:
"DefinitionNode[]"
as
const
,
ownedUsages
:
"UsageNode[]"
as
const
,
imports
:
"SemanticNode[]"
as
const
,
}
,
}
;
// ---------------------------------------------------------------------------
// KerML Expression Evaluator — CST-based tree-walk interpreter
// ---------------------------------------------------------------------------
/** Result of evaluating an expression. undefined = "cannot evaluate". */
type
EvalResult
=
number
|
boolean
|
string
|
null
|
undefined
;
/** CSTNode type alias for expression evaluation */
type
CSTNode
=
import
(
"@modelscript/language/compiler"
)
.
CSTNode
;
/**
* Resolve a feature reference name within a scope.
* Searches: (1) children of the enclosing definition, (2) global byName.
*/
const
resolveFeatureInScope
=
(
db
:
QueryDB
,
scopeId
:
number
|
null
,
name
:
string
)
:
SymbolEntry
|
null
=>
{
// Search enclosing scope's children first
if
(
scopeId
!==
null
)
{
let
current
:
number
|
null
=
scopeId
;
while
(
current
!==
null
)
{
const
parent
=
db
.
symbol
(
current
)
;
if
(
!
parent
)
break
;
for
(
const
child
of
db
.
childrenOf
(
parent
.
id
)
)
{
if
(
child
.
name
===
name
)
return
child
;
}
current
=
parent
.
parentId
;
}
}
// Fallback: global name lookup
const
globals
=
db
.
byName
(
name
)
;
return
globals
?.
[
0
]
??
null
;
}
;
/**
* Resolve the type entry for a feature usage.
* Reads the OwnedFeatureTyping child's name and resolves via db.byName().
*/
const
resolveTypeOf
=
(
db
:
QueryDB
,
entry
:
SymbolEntry
)
:
SymbolEntry
|
null
=>
{
// Look for OwnedFeatureTyping children to get the type name
for
(
const
child
of
db
.
childrenOf
(
entry
.
id
)
)
{
if
(
child
.
ruleName
===
"OwnedFeatureTyping"
&&
child
.
name
)
{
const
typeEntries
=
db
.
byName
(
child
.
name
)
;
if
(
typeEntries
.
length
>
0
)
return
typeEntries
[
0
]
;
}
}
return
null
;
}
;
/**
* Get the bound value of a feature (from modification, default value, etc.).
* Returns undefined if no value is bound.
*/
const
getFeatureValue
=
(
db
:
QueryDB
,
entry
:
SymbolEntry
)
:
EvalResult
=>
{
// Try to get the result expression (for calculations)
// or read the CST default value
const
cst
=
db
.
cstNode
(
entry
.
id
)
as
CSTNode
|
null
;
if
(
!
cst
)
return
undefined
;
// Look for a ValuePart → OwnedExpression default value
for
(
const
child
of
cst
.
children
)
{
if
(
child
.
type
===
"OwnedExpression"
||
child
.
type
===
"ResultExpressionMember"
)
{
const
exprNode
=
child
.
type
===
"ResultExpressionMember"
?
child
.
childForFieldName
(
"ownedRelatedElement"
)
:
child
;
if
(
exprNode
)
return
evaluateExpressionNode
(
db
,
exprNode
,
entry
.
parentId
)
;
}
}
// Look for default value in modification syntax (= expr)
const
text
=
db
.
cstText
(
entry
.
startByte
,
entry
.
endByte
)
;
if
(
text
)
{
const
eqMatch
=
text
.
match
(
/
=
\s
*
(
[
0
-
9
]
+
(?:
\.
[
0
-
9
]
+
)
?
)
\s
*
;
?
\s
*
$
/
)
;
if
(
eqMatch
)
{
const
num
=
parseFloat
(
eqMatch
[
1
]
)
;
if
(
!
isNaN
(
num
)
)
return
num
;
}
}
return
undefined
;
}
;
/**
* Resolve a dot-path like `vehicle.mass`:
* 1. Resolve `vehicle` in scope → find its type definition
* 2. Look up `mass` in that type's children
* 3. Return the final feature's value
*/
const
resolveFeaturePath
=
(
db
:
QueryDB
,
names
:
string
[
]
,
scopeId
:
number
|
null
)
:
EvalResult
=>
{
if
(
names
.
length
===
0
)
return
undefined
;
// Resolve the base name
let
current
=
resolveFeatureInScope
(
db
,
scopeId
,
names
[
0
]
)
;
if
(
!
current
)
return
undefined
;
// If single name, return its value directly
if
(
names
.
length
===
1
)
{
return
getFeatureValue
(
db
,
current
)
;
}
// Walk the chain: for each step, resolve the type and find the member
for
(
let
i
=
1
;
i
<
names
.
length
;
i
++
)
{
// Get the type of the current feature
const
typeDef
=
resolveTypeOf
(
db
,
current
)
;
if
(
!
typeDef
)
return
undefined
;
// Find the named member in the type's children
const
memberName
=
names
[
i
]
;
let
found
:
SymbolEntry
|
null
=
null
;
for
(
const
child
of
db
.
childrenOf
(
typeDef
.
id
)
)
{
if
(
child
.
name
===
memberName
)
{
found
=
child
;
break
;
}
}
// Cross-Language Resolution: If the member is not found in the SysML type,
// search across the UnifiedWorkspace for any implementation targets (e.g. Modelica classes)
if
(
!
found
)
{
for
(
const
sym
of
db
.
allEntries
(
)
)
{
const
meta
=
sym
.
metadata
as
Record
<
string
,
unknown
>
|
undefined
;
let
implementsTarget
:
string
|
undefined
=
undefined
;
// Fast path for native SysML items
if
(
meta
?.
implementsTarget
===
typeDef
.
name
)
{
implementsTarget
=
typeDef
.
name
;
}
// Fallback for external languages like Modelica
else
if
(
meta
?.
annotationClause
&&
typeof
meta
.
annotationClause
===
"string"
)
{
const
match
=
meta
.
annotationClause
.
match
(
/
S
y
s
M
L
\s
*
\(
\s
*
i
m
p
l
e
m
e
n
t
s
\s
*
=
\s
*
"
(
[
^
"
]
+
)
"
\s
*
\)
/
)
;
if
(
match
&&
match
[
1
]
===
typeDef
.
name
)
implementsTarget
=
match
[
1
]
;
}
if
(
implementsTarget
)
{
for
(
const
child
of
db
.
childrenOf
(
sym
.
id
)
)
{
if
(
child
.
name
===
memberName
)
{
found
=
child
;
break
;
// Found in Modelica/external class
}
}
}
if
(
found
)
break
;
}
}
if
(
!
found
)
return
undefined
;
// If this is the last step, return the value
if
(
i
===
names
.
length
-
1
)
{
return
getFeatureValue
(
db
,
found
)
;
}
// Otherwise, continue traversal
current
=
found
;
}
return
undefined
;
}
;
/**
* Extract the feature name chain from a FeatureReferenceExpression or
* PrimaryExpression with featureChain.
*/
const
extractNameChain
=
(
node
:
CSTNode
)
:
string
[
]
=>
{
const
names
:
string
[
]
=
[
]
;
const
extractQualifiedName
=
(
qn
:
CSTNode
)
:
string
|
null
=>
{
// QualifiedName → name: Name → ID
const
nameNode
=
qn
.
childForFieldName
(
"name"
)
;
if
(
nameNode
)
{
// Get the last segment of a qualified name
const
id
=
nameNode
.
children
.
find
(
(
c
)
=>
c
.
type
===
"ID"
)
;
if
(
id
)
return
id
.
text
;
return
nameNode
.
text
;
}
return
qn
.
text
;
}
;
// Walk the node to extract names
if
(
node
.
type
===
"FeatureReferenceExpression"
)
{
const
rel
=
node
.
childForFieldName
(
"ownedRelationship"
)
||
node
.
children
.
find
(
(
c
:
any
)
=>
c
.
type
===
"FeatureReferenceMember"
)
||
node
.
children
[
0
]
;
if
(
rel
)
{
const
memberElement
=
rel
.
childForFieldName
(
"memberElement"
)
||
rel
.
children
.
find
(
(
c
:
any
)
=>
c
.
type
===
"QualifiedName"
)
||
rel
.
children
[
0
]
;
if
(
memberElement
)
{
const
name
=
extractQualifiedName
(
memberElement
)
||
memberElement
.
text
.
trim
(
)
;
if
(
name
)
names
.
push
(
name
.
trim
(
)
)
;
}
}
}
else
if
(
node
.
type
===
"PrimaryExpression"
)
{
// base: _BaseExpression, featureChain: FeatureChainMember
const
base
=
node
.
childForFieldName
(
"base"
)
;
if
(
base
)
{
const
baseNames
=
extractNameChain
(
base
)
;
names
.
push
(
...
baseNames
)
;
}
// Collect all featureChain members
for
(
const
child
of
node
.
children
)
{
if
(
child
.
type
===
"FeatureChainMember"
)
{
const
memberElement
=
child
.
childForFieldName
(
"memberElement"
)
;
if
(
memberElement
)
{
const
name
=
extractQualifiedName
(
memberElement
)
;
if
(
name
)
names
.
push
(
name
)
;
}
}
}
}
return
names
;
}
;
/**
* Evaluate a KerML expression CST node.
*
* Walks the expression AST and computes values for:
* - Literals (integer, real, boolean, string, null, infinity)
* - Arithmetic (+, -, *, /, %, **, ^)
* - Relational (<, >, <=, >=)
* - Equality (==, !=)
* - Logical (and, or, xor, not, implies)
* - Conditional (if ? else)
* - Feature references with full dot-path resolution (vehicle.mass)
*
* Returns undefined for expressions that cannot be statically evaluated.
*/
const
evaluateExpressionNode
=
(
db
:
QueryDB
,
node
:
CSTNode
,
scopeId
:
number
|
null
)
:
EvalResult
=>
{
switch
(
node
.
type
)
{
// --- Literals ---
case
"LiteralInteger"
:
{
const
val
=
node
.
childForFieldName
(
"value"
)
;
return
val
?
parseInt
(
val
.
text
,
10
)
:
parseInt
(
node
.
text
.
trim
(
)
,
10
)
;
}
case
"LiteralReal"
:
{
const
val
=
node
.
childForFieldName
(
"value"
)
;
return
val
?
parseFloat
(
val
.
text
)
:
parseFloat
(
node
.
text
.
trim
(
)
)
;
}
case
"LiteralBoolean"
:
{
const
val
=
node
.
childForFieldName
(
"value"
)
;
const
text
=
val
?
val
.
text
:
node
.
text
.
trim
(
)
;
if
(
text
===
"true"
)
return
true
;
if
(
text
===
"false"
)
return
false
;
return
undefined
;
}
case
"LiteralString"
:
{
const
val
=
node
.
childForFieldName
(
"value"
)
;
const
text
=
val
?
val
.
text
:
node
.
text
.
trim
(
)
;
return
text
.
replace
(
/
^
[
"
'
]
|
[
"
'
]
$
/
g
,
""
)
;
}
case
"NullExpression"
:
return
null
;
case
"LiteralInfinity"
:
return
Infinity
;
// --- Arithmetic ---
case
"AdditiveExpression"
:
case
"MultiplicativeExpression"
:
case
"ExponentiationExpression"
:
{
const
operands
:
CSTNode
[
]
=
[
]
;
const
operators
:
string
[
]
=
[
]
;
for
(
const
child
of
node
.
children
)
{
const
fn
=
node
.
fieldNameForChild
?.
(
node
.
children
.
indexOf
(
child
)
)
;
if
(
fn
===
"operand"
)
operands
.
push
(
child
)
;
else
if
(
fn
===
"operator"
)
operators
.
push
(
child
.
text
)
;
}
if
(
operands
.
length
<
2
||
operators
.
length
===
0
)
return
undefined
;
let
result
=
evaluateExpressionNode
(
db
,
operands
[
0
]
,
scopeId
)
;
if
(
typeof
result
!==
"number"
)
return
undefined
;
for
(
let
i
=
0
;
i
<
operators
.
length
;
i
++
)
{
const
right
=
evaluateExpressionNode
(
db
,
operands
[
i
+
1
]
,
scopeId
)
;
if
(
typeof
right
!==
"number"
)
return
undefined
;
switch
(
operators
[
i
]
)
{
case
"+"
:
result
=
(
result
as
number
)
+
right
;
break
;
case
"-"
:
result
=
(
result
as
number
)
-
right
;
break
;
case
"*"
:
result
=
(
result
as
number
)
*
right
;
break
;
case
"/"
:
result
=
right
!==
0
?
(
result
as
number
)
/
right
:
undefined
;
break
;
case
"%"
:
result
=
right
!==
0
?
(
result
as
number
)
%
right
:
undefined
;
break
;
case
"**"
:
case
"^"
:
result
=
Math
.
pow
(
result
as
number
,
right
)
;
break
;
default
:
return
undefined
;
}
if
(
result
===
undefined
)
return
undefined
;
}
return
result
;
}
// --- Unary ---
case
"UnaryExpression"
:
{
const
op
=
node
.
childForFieldName
(
"operator"
)
;
const
operand
=
node
.
childForFieldName
(
"operand"
)
;
if
(
!
op
||
!
operand
)
return
undefined
;
const
val
=
evaluateExpressionNode
(
db
,
operand
,
scopeId
)
;
switch
(
op
.
text
)
{
case
"+"
:
return
typeof
val
===
"number"
?
val
:
undefined
;
case
"-"
:
return
typeof
val
===
"number"
?
-
val
:
undefined
;
case
"not"
:
return
typeof
val
===
"boolean"
?
!
val
:
undefined
;
case
"~"
:
return
typeof
val
===
"number"
?
~
val
:
undefined
;
default
:
return
undefined
;
}
}
// --- Relational ---
case
"RelationalExpression"
:
{
let
operands
:
CSTNode
[
]
=
[
]
;
let
operators
:
string
[
]
=
[
]
;
for
(
const
child
of
node
.
children
)
{
const
fn
=
node
.
fieldNameForChild
?.
(
node
.
children
.
indexOf
(
child
)
)
;
if
(
fn
===
"operand"
)
operands
.
push
(
child
)
;
else
if
(
fn
===
"operator"
)
operators
.
push
(
child
.
text
??
child
.
children
?.
[
0
]
?.
text
??
""
)
;
}
if
(
operands
.
length
<
2
&&
node
.
children
.
length
===
3
)
{
operands
=
[
node
.
children
[
0
]
,
node
.
children
[
2
]
]
;
operators
=
[
(
node
.
children
[
1
]
.
text
??
""
)
.
trim
(
)
]
;
}
if
(
operands
.
length
<
2
||
operators
.
length
===
0
)
return
undefined
;
const
left
=
evaluateExpressionNode
(
db
,
operands
[
0
]
,
scopeId
)
;
const
right
=
evaluateExpressionNode
(
db
,
operands
[
1
]
,
scopeId
)
;
if
(
typeof
left
!==
"number"
||
typeof
right
!==
"number"
)
return
undefined
;
const
opText
=
operators
[
0
]
;
switch
(
opText
)
{
case
"<"
:
return
left
<
right
;
case
">"
:
return
left
>
right
;
case
"<="
:
return
left
<=
right
;
case
">="
:
return
left
>=
right
;
default
:
return
undefined
;
}
}
// --- Equality ---
case
"EqualityExpression"
:
{
const
operands
:
CSTNode
[
]
=
[
]
;
const
operators
:
string
[
]
=
[
]
;
for
(
const
child
of
node
.
children
)
{
const
fn
=
node
.
fieldNameForChild
?.
(
node
.
children
.
indexOf
(
child
)
)
;
if
(
fn
===
"operand"
)
operands
.
push
(
child
)
;
else
if
(
fn
===
"operator"
)
operators
.
push
(
child
.
text
??
child
.
children
?.
[
0
]
?.
text
??
""
)
;
}
if
(
operands
.
length
<
2
||
operators
.
length
===
0
)
return
undefined
;
const
left
=
evaluateExpressionNode
(
db
,
operands
[
0
]
,
scopeId
)
;
const
right
=
evaluateExpressionNode
(
db
,
operands
[
1
]
,
scopeId
)
;
if
(
left
===
undefined
||
right
===
undefined
)
return
undefined
;
const
opText
=
operators
[
0
]
;
switch
(
opText
)
{
case
"=="
:
return
left
===
right
;
case
"!="
:
return
left
!==
right
;
case
"==="
:
return
left
===
right
;
case
"!=="
:
return
left
!==
right
;
default
:
return
undefined
;
}
}
// --- Logical ---
case
"AndExpression"
:
{
const
operands
:
CSTNode
[
]
=
[
]
;
for
(
const
child
of
node
.
children
)
{
const
fn
=
node
.
fieldNameForChild
?.
(
node
.
children
.
indexOf
(
child
)
)
;
if
(
fn
===
"operand"
)
operands
.
push
(
child
)
;
}
if
(
operands
.
length
<
2
)
return
undefined
;
let
result
=
evaluateExpressionNode
(
db
,
operands
[
0
]
,
scopeId
)
;
if
(
typeof
result
!==
"boolean"
)
return
undefined
;
for
(
let
i
=
1
;
i
<
operands
.
length
;
i
++
)
{
const
right
=
evaluateExpressionNode
(
db
,
operands
[
i
]
,
scopeId
)
;
if
(
typeof
right
!==
"boolean"
)
return
undefined
;
result
=
(
result
as
boolean
)
&&
right
;
}
return
result
;
}
case
"OrExpression"
:
{
const
operands
:
CSTNode
[
]
=
[
]
;
for
(
const
child
of
node
.
children
)
{
const
fn
=
node
.
fieldNameForChild
?.
(
node
.
children
.
indexOf
(
child
)
)
;
if
(
fn
===
"operand"
)
operands
.
push
(
child
)
;
}
if
(
operands
.
length
<
2
)
return
undefined
;
let
result
=
evaluateExpressionNode
(
db
,
operands
[
0
]
,
scopeId
)
;
if
(
typeof
result
!==
"boolean"
)
return
undefined
;
for
(
let
i
=
1
;
i
<
operands
.
length
;
i
++
)
{
const
right
=
evaluateExpressionNode
(
db
,
operands
[
i
]
,
scopeId
)
;
if
(
typeof
right
!==
"boolean"
)
return
undefined
;
result
=
(
result
as
boolean
)
||
right
;
}
return
result
;
}
case
"XorExpression"
:
{
const
operands
:
CSTNode
[
]
=
[
]
;
for
(
const
child
of
node
.
children
)
{
const
fn
=
node
.
fieldNameForChild
?.
(
node
.
children
.
indexOf
(
child
)
)
;
if
(
fn
===
"operand"
)
operands
.
push
(
child
)
;
}
if
(
operands
.
length
<
2
)
return
undefined
;
let
result
=
evaluateExpressionNode
(
db
,
operands
[
0
]
,
scopeId
)
;
if
(
typeof
result
!==
"boolean"
)
return
undefined
;
for
(
let
i
=
1
;
i
<
operands
.
length
;
i
++
)
{
const
right
=
evaluateExpressionNode
(
db
,
operands
[
i
]
,
scopeId
)
;
if
(
typeof
right
!==
"boolean"
)
return
undefined
;
result
=
(
result
as
boolean
)
!==
right
;
}
return
result
;
}
case
"ImpliesExpression"
:
{
const
operands
:
CSTNode
[
]
=
[
]
;
for
(
const
child
of
node
.
children
)
{
const
fn
=
node
.
fieldNameForChild
?.
(
node
.
children
.
indexOf
(
child
)
)
;
if
(
fn
===
"operand"
)
operands
.
push
(
child
)
;
}
if
(
operands
.
length
<
2
)
return
undefined
;
const
left
=
evaluateExpressionNode
(
db
,
operands
[
0
]
,
scopeId
)
;
// For implies references, we need to unwrap the ImpliesExpressionReference
let
rightNode
=
operands
[
1
]
;
if
(
rightNode
.
type
===
"ImpliesExpressionReference"
)
{
const
inner
=
rightNode
.
childForFieldName
(
"ownedRelationship"
)
;
if
(
inner
)
{
const
elem
=
inner
.
childForFieldName
(
"ownedRelatedElement"
)
;
if
(
elem
)
rightNode
=
elem
;
}
}
const
right
=
evaluateExpressionNode
(
db
,
rightNode
,
scopeId
)
;
if
(
typeof
left
!==
"boolean"
||
typeof
right
!==
"boolean"
)
return
undefined
;
return
!
left
||
right
;
// p implies q ≡ ¬p ∨ q
}
// --- Conditional ---
case
"ConditionalExpression"
:
{
const
operand
=
node
.
childForFieldName
(
"operand"
)
;
const
thenRef
=
node
.
childForFieldName
(
"thenOperand"
)
;
const
elseRef
=
node
.
childForFieldName
(
"elseOperand"
)
;
if
(
!
operand
||
!
thenRef
||
!
elseRef
)
return
undefined
;
const
cond
=
evaluateExpressionNode
(
db
,
operand
,
scopeId
)
;
if
(
typeof
cond
!==
"boolean"
)
return
undefined
;
// Unwrap OwnedExpressionReference → OwnedExpressionMember → OwnedExpression
const
unwrap
=
(
ref
:
CSTNode
)
:
CSTNode
=>
{
const
rel
=
ref
.
childForFieldName
(
"ownedRelationship"
)
;
if
(
rel
)
{
const
elem
=
rel
.
childForFieldName
(
"ownedRelatedElement"
)
;
if
(
elem
)
return
elem
;
}
return
ref
;
}
;
return
cond
?
evaluateExpressionNode
(
db
,
unwrap
(
thenRef
)
,
scopeId
)
:
evaluateExpressionNode
(
db
,
unwrap
(
elseRef
)
,
scopeId
)
;
}
// --- Feature references (with dot-path resolution) ---
case
"FeatureReferenceExpression"
:
{
const
names
=
extractNameChain
(
node
)
;
if
(
names
.
length
===
0
)
return
undefined
;
return
resolveFeaturePath
(
db
,
names
,
scopeId
)
;
}
case
"PrimaryExpression"
:
{
// Check for parenthesized expression: ( expr )
const
expr
=
node
.
childForFieldName
(
"operand"
)
;
if
(
expr
)
return
evaluateExpressionNode
(
db
,
expr
,
scopeId
)
;
// Check for dot-path feature chain
const
names
=
extractNameChain
(
node
)
;
if
(
names
.
length
>
0
)
{
return
resolveFeaturePath
(
db
,
names
,
scopeId
)
;
}
// Check children
for
(
const
child
of
node
.
children
)
{
const
val
=
evaluateExpressionNode
(
db
,
child
,
scopeId
)
;
if
(
val
!==
undefined
)
return
val
;
}
return
undefined
;
}
// --- Wrapped expressions ---
case
"OwnedExpression"
:
case
"ExpressionBody"
:
{
for
(
const
child
of
node
.
children
)
{
const
val
=
evaluateExpressionNode
(
db
,
child
,
scopeId
)
;
if
(
val
!==
undefined
)
return
val
;
}
return
undefined
;
}
default
:
{
// For wrapper nodes, try to evaluate the first recognized child
for
(
const
child
of
node
.
children
)
{
const
val
=
evaluateExpressionNode
(
db
,
child
,
scopeId
)
;
if
(
val
!==
undefined
)
return
val
;
}
return
undefined
;
}
}
}
;
/**
* Evaluate the body of a CalculationDefinition or CalculationUsage.
* Finds the ResultExpressionMember in the CST and evaluates it.
*/
const
evaluateResultExpression
=
(
db
:
QueryDB
,
self
:
SymbolEntry
,
evalScopeId
?:
number
|
null
)
:
EvalResult
=>
{
const
cst
=
db
.
cstNode
(
self
.
id
)
as
CSTNode
|
null
;
if
(
!
cst
)
return
undefined
;
// Find ResultExpressionMember in the calculation body
const
findResultExpr
=
(
node
:
CSTNode
)
:
CSTNode
|
null
=>
{
if
(
node
.
type
===
"ResultExpressionMember"
)
return
node
;
for
(
const
child
of
node
.
children
)
{
const
found
=
findResultExpr
(
child
)
;
if
(
found
)
return
found
;
}
return
null
;
}
;
const
resultMember
=
findResultExpr
(
cst
)
;
if
(
!
resultMember
)
return
undefined
;
// Navigate to the OwnedExpression inside
const
ownedRelElem
=
resultMember
.
childForFieldName
(
"ownedRelatedElement"
)
||
resultMember
.
children
.
find
(
(
c
:
any
)
=>
c
.
type
===
"OwnedExpression"
)
||
resultMember
.
children
[
0
]
;
if
(
!
ownedRelElem
)
return
undefined
;
return
evaluateExpressionNode
(
db
,
ownedRelElem
,
evalScopeId
??
self
.
parentId
)
;
}
;
// ---------------------------------------------------------------------------
// Calculation Definition/Usage — dedicated models and queries
// ---------------------------------------------------------------------------
/** Model config for CalculationDefinition */
const
calculationDefinitionModel
=
{
name
:
"CalculationDefinitionNode"
as
const
,
visitable
:
true
,
specializable
:
true
,
properties
:
{
...
definitionModel
.
properties
,
}
,
queryTypes
:
{
...
definitionModel
.
queryTypes
,
parameters
:
"UsageNode[]"
as
const
,
returnParameter
:
"UsageNode | null"
as
const
,
resultExpression
:
"unknown"
as
const
,
}
,
}
;
/** Model config for CalculationUsage */
const
calculationUsageModel
=
{
name
:
"CalculationUsageNode"
as
const
,
visitable
:
true
,
specializable
:
true
,
properties
:
{
...
usageModel
.
properties
,
}
,
queryTypes
:
{
...
usageModel
.
queryTypes
,
parameters
:
"UsageNode[]"
as
const
,
returnParameter
:
"UsageNode | null"
as
const
,
resultExpression
:
"unknown"
as
const
,
}
,
}
;
/** Calculation Definition queries */
const
calculationDefinitionQueries
=
{
...
definitionStructuralQueries
,
parameters
:
(
db
:
QueryDB
,
self
:
SymbolEntry
)
=>
db
.
childrenOf
(
self
.
id
)
.
filter
(
(
c
)
=>
c
.
kind
===
"Usage"
&&
(
metaStr
(
c
,
"direction"
)
===
"in"
||
metaStr
(
c
,
"direction"
)
===
"inout"
)
)
,
returnParameter
:
(
db
:
QueryDB
,
self
:
SymbolEntry
)
=>
{
const
outputs
=
db
.
childrenOf
(
self
.
id
)
.
filter
(
(
c
)
=>
c
.
kind
===
"Usage"
&&
metaStr
(
c
,
"direction"
)
===
"out"
)
;
return
outputs
[
0
]
??
null
;
}
,
resultExpression
:
(
db
:
QueryDB
,
self
:
SymbolEntry
)
=>
evaluateResultExpression
(
db
,
self
)
,
}
;
/** Calculation Usage queries */
const
calculationUsageQueries
=
{
...
usageQueries
,
parameters
:
(
db
:
QueryDB
,
self
:
SymbolEntry
)
=>
db
.
childrenOf
(
self
.
id
)
.
filter
(
(
c
)
=>
c
.
kind
===
"Usage"
&&
(
metaStr
(
c
,
"direction"
)
===
"in"
||
metaStr
(
c
,
"direction"
)
===
"inout"
)
)
,
returnParameter
:
(
db
:
QueryDB
,
self
:
SymbolEntry
)
=>
{
const
outputs
=
db
.
childrenOf
(
self
.
id
)
.
filter
(
(
c
)
=>
c
.
kind
===
"Usage"
&&
metaStr
(
c
,
"direction"
)
===
"out"
)
;
return
outputs
[
0
]
??
null
;
}
,
resultExpression
:
(
db
:
QueryDB
,
self
:
SymbolEntry
)
=>
evaluateResultExpression
(
db
,
self
)
,
}
;
const
calculationQueries
=
calculationDefinitionQueries
;
const
calculationModel
=
calculationDefinitionModel
;
// ---------------------------------------------------------------------------
// Constraint Evaluation — computed fields
// ---------------------------------------------------------------------------
/**
* Evaluate the body of a ConstraintDefinition or ConstraintUsage.
* The constraint body is a _CalculationBody containing a ResultExpressionMember.
* Returns true/false/null (null = indeterminate).
*/
const
evaluateConstraintBody
=
(
db
:
QueryDB
,
self
:
SymbolEntry
,
evalScopeId
?:
number
|
null
)
:
boolean
|
null
=>
{
const
result
=
evaluateResultExpression
(
db
,
self
,
evalScopeId
)
;
if
(
typeof
result
===
"boolean"
)
return
result
;
if
(
result
===
undefined
)
return
null
;
// indeterminate
// Coerce numbers: 0 = false, nonzero = true
if
(
typeof
result
===
"number"
)
return
result
!==
0
;
return
null
;
}
;
/** Constraint-specific queries */
View remainder of file in raw view
Footer
© 2026 GitHub, Inc.
Footer navigation
Terms
Privacy
Security
Status
Community
Docs
Contact
You can’t perform that action at this time.
Back
|
FazBrowse Home
|
New Git URL