-
Notifications
You must be signed in to change notification settings - Fork 5
/
HNamespace.ts
1650 lines (1460 loc) · 38.9 KB
/
HNamespace.ts
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
/*
* Copyright (c) 2020, J2 Innovations. All Rights Reserved
*/
import { HGrid } from './HGrid'
import { HDict } from './HDict'
import { HStr } from './HStr'
import { HList } from './HList'
import { HSymbol } from './HSymbol'
import { Kind } from './Kind'
import { memoize } from '../util/memoize'
import { ZincReader } from './ZincReader'
import { valueIsKind, OptionalHVal } from './HVal'
import { HMarker } from './HMarker'
import { HRef } from './HRef'
import { IMPLIED_BY, makeDefaultValue } from './util'
import { LocalizedError } from '../util/LocalizedError'
export interface Defs {
[prop: string]: HDict
}
export interface NameToDefs {
[prop: string]: HDict[]
}
/**
* A unit dict.
*/
export interface Unit extends HDict {
quantity: HStr
name: HStr
symbol: HStr
}
/**
* The result of a reflection operation.
*/
export class Reflection {
/**
* The resultant defs.
*/
public readonly defs: readonly HDict[]
/**
* The source dict analysed.
*/
public readonly subject: HDict
/**
* The associated namespace.
*/
public readonly namespace: HNamespace
public constructor(defs: HDict[], subject: HDict, namespace: HNamespace) {
this.defs = defs
this.subject = subject
this.namespace = namespace
}
/**
* Return true if any of the subject's tags fit the given base def.
*
* @param base The base def.
* @returns True if the subject's tags fit the base.
*/
public fits(base: string | HSymbol): boolean {
for (const def of this.defs) {
if (this.namespace.fits(def.defName, base)) {
return true
}
}
return false
}
/**
* Return the reflection result as a grid.
*/
public toGrid(): HGrid {
return HGrid.make({ rows: this.defs as HDict[] })
}
/**
* Return the primary entity type of `dict` if no entity markers
* are implemented.
*/
@memoize()
public get type(): HDict {
const entity = this.namespace.byName('entity')
let type: HDict | undefined
if (entity) {
const typesWithInheritance = new Map<HDict, HDict[]>()
for (const def of this.defs) {
const inheritance = this.namespace.inheritance(def.defName)
if (inheritance.includes(entity)) {
typesWithInheritance.set(def, inheritance)
}
}
const allDefs = [...typesWithInheritance.keys()]
if (typesWithInheritance.size === 1) {
// Just get the first entity if only one has been found.
type = allDefs[0]
} else {
// If multiple entity tags have been found then we need to find which tag is the most specific.
// This can happen if a record has a tag like `ahu` and `equip`. The `ahu` tag extends `equip`.
// Therefore, we need to check all the inheritance to find the first tag that isn't any of the
// other tag's inheritance. This tag should be the most specific entity.
for (const def of allDefs) {
let included = false
for (const [
innerDef,
inheritance,
] of typesWithInheritance) {
if (innerDef !== def && inheritance.includes(def)) {
included = true
break
}
}
// If this def isn't in the inhertiance of any other tag then we should
// have the most specific entity.
if (!included) {
type = def
break
}
}
// If we really can't find anything then just fallback to the first entity we find.
if (!type) {
type = allDefs[0]
}
}
}
return type ?? (this.namespace.byName('dict') as HDict)
}
}
/**
* The default namespace for the environment.
*/
let defaultNamespace: HNamespace
/**
* Haystack namespace.
*
* A namespace holds a collection of normalized defs.
*/
export class HNamespace {
/**
* The underlying grid containing the defs.
*/
private readonly $grid: HGrid
/**
* Constructs a new namespace.
*
* @param defs A grid of normalized defined defs.
*/
public constructor(grid: HGrid) {
this.$grid = grid
}
/**
* @returns The default namespace for the environment.
*/
public static get defaultNamespace(): HNamespace {
return (
defaultNamespace ??
(defaultNamespace = new HNamespace(HGrid.make({})))
)
}
/**
* Set the default namespace for the environment.
*
* @param namespace The new default namespace.
*/
public static set defaultNamespace(namespace: HNamespace) {
defaultNamespace = namespace
}
/**
* Returns a defs lookup map.
*
* Since defs are keyed by a symbol this map provides fast look up.
*
* @returns A object with key to value defs.
*/
@memoize()
public get defs(): Defs {
const defs: Defs = {}
// Create a cache of symbol to defs so we can
// quickly look up information.
for (const dict of this.$grid) {
const name = dict.defName
if (name) {
defs[String(name)] = dict
}
}
return defs
}
/**
* Return a def via its name or undefined if it can't be found.
*
* @param name The name of the def to look up.
* @returns The def or undefined if it can't be found.
*/
public byName(name: string | HSymbol): HDict | undefined {
return this.defs[String(name)]
}
/**
* Return a def via its name or undefined if it can't be found.
*
* This method is a duplicate of `byName`.
*
* @param name The name of the def to look up.
* @returns The def or undefined if it can't be found.
*/
public get(name: string | HSymbol): HDict | undefined {
return this.byName(name)
}
/**
* Return true if the def exists in the namespace.
*
* @param name The name of the def to look up.
* @returns True if the def exists.
*/
public hasName(name: string | HSymbol): boolean {
return !!this.defs[String(name)]
}
/**
* Return true if the def exists in the namespace.
*
* This method is a duplicate of `hasName`.
*
* @param name The name of the def to look up.
* @returns True if the def exists.
*/
public has(name: string | HSymbol): boolean {
return this.hasName(name)
}
/**
* Return an array of dicts via their names. If a name can't
* be found then throw an error.
*
* @param names The names of the defs to look up.
* @returns A array of defs.
* @throws An error if the def can't be found.
*/
public byAllNames(
...names: (string | string[] | HSymbol | HSymbol[])[]
): HDict[] {
const nameList = names.reduce(
(
ns: string[],
name: string | string[] | HSymbol | HSymbol[]
): string[] =>
// Flatten the name list.
ns.concat(
Array.isArray(name)
? (name as (string | HSymbol)[]).map(
(nm: string | HSymbol): string => String(nm)
)
: [String(name)]
),
[]
)
const dicts: HDict[] = []
for (const name of nameList) {
const dict = this.byName(name)
if (dict) {
dicts.push(dict)
} else {
throw new Error(`Could not find ${name}`)
}
}
return dicts
}
/**
* @returns A list of all available conjunct defs.
*/
@memoize()
public get conjuncts(): readonly HDict[] {
const defs = this.defs
return Object.keys(defs)
.filter(HNamespace.isConjunct)
.map((name): HDict => defs[name])
}
/**
* Return true if the name is for a conjunct.
*
* @param name The name to test.
* @returns True if the name is for a conjunct.
*/
public static isConjunct(name: string | HSymbol): boolean {
return String(name).includes('-')
}
/**
* Split the conjunct name into its component parts.
*
* @param name The name to split.
* @returns The split marker tag names for the conjunct.
*/
public static splitConjunct(name: string | HSymbol): string[] {
return String(name).split('-')
}
/**
* Decomposes a conjunct into its respective defs and returns them.
*
* If the def is not a conjunct then it will just return itself.
*
* @param name The conjunct def to breakdown.
* @returns The defs the conjunct references.
* @throws An error if any of the defs are invalid.
*/
public conjunctDefs(name: string | HSymbol): HDict[] {
return this.byAllNames(HNamespace.splitConjunct(name))
}
/**
* Cache built to speed calculation of conjuncts.
*
* Example:
* ```
* {
* "ac": [
* ["elec"],
* ["elec", "meter"],
* ["freq"]
* ],
* "active": [
* ["power"]
* ],
* "air": [
* ["temp"]
* ],
* ...
* }
* ```
*/
@memoize()
private get $conjunctsKeyMap(): Record<string, string[][]> {
return this.conjuncts
.map((d) => HNamespace.splitConjunct(d.defName))
.reduce((a, c) => {
const key = c[0]
if (!a[key]) {
a[key] = []
}
a[key].push(c.slice(1))
return a
}, {} as Record<string, string[][]>)
}
/**
* Find all the conjuncts for the markers.
*
* @param markers The markers
* @param conjuncts The found conjuncts.
*/
private findConjuncts(markers: string[], conjuncts: HDict[]): void {
const map = this.$conjunctsKeyMap
for (const marker of markers) {
const match = map[marker]
if (match) {
match.forEach((m) => {
if (m.every((t) => markers.includes(t))) {
const def = this.byName([marker, ...m].join('-'))
if (def) {
conjuncts.push(def)
}
}
})
}
}
}
/**
* @returns A list of feature defs.
*/
@memoize()
public get features(): readonly HDict[] {
const defs = this.defs
return Object.keys(defs)
.filter(HNamespace.isFeature)
.map((name): HDict => defs[name])
}
/**
* Return true if the name is for a feature.
*
* @param name The name to test.
* @returns True if the name is for a feature.
*/
public static isFeature(name: string | HSymbol): boolean {
return String(name).includes(':')
}
/**
* Return the feature from the feature key. An empty
* string is returned if no feature can be found.
*
* For example, `lib:foo` would return `lib`.
*
* @param name The name to parse.
* @returns The feature.
*/
public static getFeature(name: string | HSymbol): string {
return HNamespace.isFeature(name) ? String(name).split(':')[0] : ''
}
/**
* Return the feature name from the feature key. An empty
* string is returned if no feature name can be found.
*
* For example, `lib:foo` would return `foo`.
*
* @param name The name to parse.
* @returns The feature name.
*/
public static getFeatureName(name: string | HSymbol): string {
return HNamespace.isFeature(name) ? String(name).split(':')[1] : ''
}
/**
* @returns A list of all the libs implemented by this namespace.
*/
@memoize()
public get libs(): HDict[] {
return this.subTypesOf('lib')
}
/**
* Returns the subtypes of the type.
*
* @param name The def name.
* @returns The subtypes.
*/
public subTypesOf(name: string | HSymbol): HDict[] {
return this.subTypes[String(name)] ?? []
}
/**
* Returns true if the def has subtypes.
*
* @param name The def name.
* @returns True if the def has subtypes.
*/
public hasSubTypes(name: string | HSymbol): boolean {
return !!this.subTypes[String(name)]
}
/**
* Returns a flattened list of all the subtypes.
*
* @param name The def name.
* @returns A list of subtypes.
*/
public allSubTypesOf(name: string | HSymbol): HDict[] {
const subTypes = new Set<HDict>()
this.findSubTypes(name, subTypes)
return [...subTypes]
}
/**
* Find all the specified subtypes.
*
* @param name The def name.
* @param subTypes The subtypes Set.
*/
private findSubTypes(name: string | HSymbol, subTypes: Set<HDict>): void {
const defs = this.subTypesOf(name)
for (const dict of defs) {
subTypes.add(dict)
this.findSubTypes(dict.defName, subTypes)
}
}
/**
* Returns an object with name to subtype defs.
*
* This enables quick look up of a type's subtypes.
*
* @returns A name to subtype def object.
*/
@memoize()
private get subTypes(): NameToDefs {
const subTypes: NameToDefs = {}
const defs = this.defs
for (const name in defs) {
const def = this.byName(name)
if (def) {
// Find the supertypes of the type.
const is = def.get<HList<HSymbol | null>>('is')
if (is) {
// Create an entry in the cache of type to subtypes.
for (const superName of is) {
if (superName) {
const subs =
subTypes[superName.value] ??
(subTypes[superName.value] = [])
subs.push(def)
}
}
}
}
}
return subTypes
}
/**
* Returns the supertypes of a def or an empty array if it can't be found.
*
* @param name The def name.
* @returns The supertype defs.
*/
public superTypesOf(name: string | HSymbol): HDict[] {
return this.has(name) ? this.doSuperTypesOf(name) : []
}
@memoize()
private doSuperTypesOf(name: string | HSymbol): HDict[] {
return (
this.byName(name)
?.get<HList<HSymbol | null>>('is')
?.reduce(this.reduceNameToDef, []) ?? []
)
}
/**
* Returns a flattened list of all the supertypes in the whole supertype chain.
*
* @param name The def name.
* @returns A list of supertypes.
*/
public allSuperTypesOf(name: string | HSymbol): HDict[] {
const superTypes = new Set<HDict>()
this.findSuperTypes(name, superTypes)
return [...superTypes]
}
/**
* Find all the specified supertypes.
*
* @param name The def name.
* @param superTypes The supertypes Set.
*/
private findSuperTypes(
name: string | HSymbol,
superTypes: Set<HDict>
): void {
const defs = this.superTypesOf(name)
for (const dict of defs) {
superTypes.add(dict)
this.findSuperTypes(dict.defName, superTypes)
}
}
/**
* Returns the choices for def.
*
* @param name The def name.
* @returns The choices for a def.
*/
public choicesFor(name: string | HSymbol): HDict[] {
let choices: HDict[] | undefined
const ofVal = this.byName(name)?.get('of')
if (ofVal) {
choices = this.subTypesOf(String(ofVal))
}
return choices ?? []
}
/**
* @returns An object containing names to subtypes for
* all defs that are choices.
*/
@memoize()
public get choices(): NameToDefs {
const choices: NameToDefs = {}
const defs = this.defs
for (const name in defs) {
if (this.byName(name)?.has('of')) {
choices[name] = this.choicesFor(name)
}
}
return choices
}
/**
* @returns The name of the available features.
*/
@memoize()
public get featureNames(): string[] {
const names = new Set<string>()
const defs = this.defs
for (const name in defs) {
if (HNamespace.isFeature(name)) {
const colon = name.indexOf(':')
if (colon > -1) {
const featureName = name.substring(0, colon)
if (featureName) {
names.add(featureName)
}
}
}
}
return [...names]
}
/**
* @returns A list of all the tagOn names.
*/
@memoize()
public get tagOnNames(): string[] {
const names = new Set<string>()
const defs = this.defs
for (const name in defs) {
const tagOn = this.byName(name)?.get<HList<HSymbol | null>>('tagOn')
if (tagOn) {
for (const tagName of tagOn) {
if (tagName) {
names.add(tagName.value)
}
}
}
}
return [...names]
}
/**
* @returns A object that maps def names to their respective tagOn defs.
*/
@memoize()
public get tagOnIndices(): NameToDefs {
return Object.keys(this.defs).reduce(
(obj: NameToDefs, name: string): NameToDefs => {
const defs = this.byName(name)
?.get<HList<HSymbol | null>>('tagOn')
?.reduce(this.reduceNameToDef, [])
if (defs && defs.length) {
obj[name] = defs
}
return obj
},
{}
)
}
/**
* Return the defs inheritance as a flattened array of defs.
*
* @param name The def name.
* @returns The def's inheritance.
*/
public inheritance(name: string | HSymbol): HDict[] {
return this.has(name) ? this.doInheritance(name) : []
}
@memoize()
private doInheritance(name: string | HSymbol): HDict[] {
const defs = new Set<HDict>()
const def = this.byName(name)
if (def) {
defs.add(def)
this.findSuperTypes(name, defs)
}
return [...defs]
}
/**
* Return an array of defs for the given association on the parent.
*
* @param parent The parent def.
* @param association The association.
* @returns An array of associated defs.
*/
public associations(
parent: string | HSymbol,
association: string | HSymbol
): HDict[] {
const assocDef = this.byName(association)
// Make sure the association exists and is an association.
if (
!assocDef
?.get<HList<HSymbol | null>>('is')
?.includes(HSymbol.make('association'))
) {
return []
}
// If the assocation isn't computed then just get the associated defs.
// For instance, this will return here if the association is 'tagOn'.
if (!assocDef.has('computed')) {
return (
this.byName(parent)
?.get<HList<HSymbol | null>>(String(association))
?.reduce(this.reduceNameToDef, []) ?? []
)
}
// Find the reciprocal def.
const reciprocalOf = assocDef.get<HSymbol>('reciprocalOf')
if (!reciprocalOf) {
return []
}
const recipDef = this.byName(reciprocalOf)
if (!recipDef) {
return []
}
// If searching for a computed assocation (i.e. tags) then more work is required.
// Search for all tagOns and match against the parent's inheritance.
return this.findReciprocalAssociations(parent, reciprocalOf)
}
/**
* Return the assocations for the parent for the reciprocal assocation.
*
* @param parent The parent def.
* @param reciprocalOf The reciprocal association.
* @returns An array of associated defs.
*/
@memoize()
private findReciprocalAssociations(
parent: string | HSymbol,
reciprocalOf: HSymbol
): HDict[] {
const inheritance = this.inheritance(parent)
const matches = new Set<HDict>()
const recipStr = String(reciprocalOf)
for (const name in this.defs) {
const def = this.byName(name)
const list = def?.get<HList<HSymbol | null>>(recipStr)
if (def && list) {
for (const symbol of list) {
if (symbol) {
const target = this.byName(symbol)
if (target && inheritance.includes(target)) {
matches.add(def)
}
}
}
}
}
return [...matches]
}
/**
* Return an array of defs for the `is` association on the parent.
*
* @param parent The parent def.
* @returns An array of associated defs.
*/
public is(parent: string | HSymbol): HDict[] {
return this.associations(parent, 'is')
}
/**
* Return an array of defs for the `tagOn` association on the parent.
*
* @param parent The parent def.
* @returns An array of associated defs.
*/
public tagOn(parent: string | HSymbol): HDict[] {
return this.associations(parent, 'tagOn')
}
/**
* Return an array of defs for the `tags` association on the parent.
*
* @param parent The parent def.
* @returns An array of associated defs.
*/
public tags(parent: string | HSymbol): HDict[] {
return this.associations(parent, 'tags')
}
/**
* Analyze the subject dict and return its implemented defs.
*
* @param subject The subject dict.
* @returns The reflected defs.
*/
public reflect(subject: HDict): Reflection {
const foundDefs: HDict[] = []
const markers: string[] = []
// 1. Check if the tag name maps to a tag def.
this.findDefs(subject, foundDefs, markers)
// 2. If the tag maps to a possible conjunct, then check if the dict
// has all the conjunct's tags.
this.findConjuncts(markers, foundDefs)
// 3. Infer the inheritance from all defs reflected from the previous steps.
const reflected = this.findSuperTypesFromDefs(foundDefs)
return new Reflection([...reflected], subject, this)
}
/**
* Match the most specific marker entity type for the specified dict.
*
* @param subject The subject dict.
* @returns The entity def or `dict` if one cannot be found.
*/
public defOfDict(subject: HDict): HDict {
return this.reflect(subject).type
}
/**
* Find the defs on the subject.
*
* @param subject The subject to search for defs.
* @param foundDefs The found defs.
* @param markers Any marker tags found.
*/
private findDefs(
subject: HDict,
foundDefs: HDict[],
markers: string[]
): void {
for (const name of subject.keys) {
const def = this.byName(name)
if (def) {
foundDefs.push(def)
// Note all marker tags so we can test for possible conjuncts.
if (subject.get(name)?.isKind(Kind.Marker)) {
markers.push(name)
}
}
}
}
/**
* Return all the super types from the specified defs.
*
* @param defs The defs to search.
* @returns The supertype defs.
*/
private findSuperTypesFromDefs(defs: HDict[]): HDict[] {
const reflected = new Set<HDict>()
for (const dict of defs) {
reflected.add(dict)
this.findSuperTypes(dict.defName, reflected)
}
return [...reflected]
}
/**
* Return true if the specified def `fits` the base def.
*
* If true this means that `def` is assignable to types of `base`.
* This is effectively the same as checking if `inheritance(def)` contains
* base.
*
* @param name The def name.
* @param base The name of the base def.
* @returns True if the def fits.
*/
public fits(name: string | HSymbol, base: string | HSymbol): boolean {
const baseDef = this.byName(base)
return !!(baseDef && this.inheritance(name).includes(baseDef))
}
/**
* Return true if the specified def is a marker.
*
* @param name The def name.
* @returns True if the def is a marker.
*/
public fitsMarker(name: string | HSymbol): boolean {
return this.fits(name, 'marker')
}
/**
* Return true if the specified def is a val.
*
* @param name The def name.
* @returns True if the def is a val.
*/
public fitsVal(name: string | HSymbol): boolean {
return this.fits(name, 'val')
}
/**
* Return true if the specified def is a choice.
*
* @param name The def name.
* @returns True if the def is a choice.
*/
public fitsChoice(name: string | HSymbol): boolean {
return this.fits(name, 'choice')
}
/**
* Return true if the specified def is a entity.
*
* @param name The def name.
* @returns True if the def is a entity.
*/
public fitsEntity(name: string | HSymbol): boolean {
return this.fits(name, 'entity')
}
/**
* Return the tags that should be added for implementation.
*
* @param name The def name.
* @returns An array of defs to be added.
*/
public implementation(name: string | HSymbol): HDict[] {
// 1.a Based on the tag name get the single def tag name.
// 1.b If this is a conjunct get each tag from it.
let dicts = this.conjunctDefs(name)
// 1.c Feature keys are never implemented.
dicts = dicts.filter(
(def: HDict): boolean => !HNamespace.isFeature(def.defName)
)
// 2. We walk the supertype tree of the def and apply any tag which is marked as mandatory.
const superTypes = new Set<HDict>()
for (const dict of dicts) {
this.findSuperTypes(dict.defName, superTypes)
}
for (const superType of superTypes) {
if (superType.get('mandatory')?.equals(HMarker.make())) {
dicts.push(superType)
}
}
// 3. Walk the tags and find any compulsory tags that need to be applied.
// Please note, `compulsory` is not part of the project haystack standard and
// should be considered an extra application level tag.
for (const tag of this.tags(name)) {
if (tag.get('compulsory')?.equals(HMarker.make())) {
dicts.push(tag)
}
}
return dicts
}
/**
* Return the Haystack kind value from the def. If the def is invalid or a kind
* cannot be ascertained then return undefined.
*
* Please note, `curVal` and `writeVal` are scalars but don't extend