glossarist 0.4.36 → 0.4.37

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -19,21 +19,25 @@
19
19
 
20
20
  import { GlossaristModel } from './base.js';
21
21
  import { ConceptRef } from './concept-ref.js';
22
- import {
23
- PARTITIVE_PRESENCE,
24
- PARTITIVE_PRESENCE_VALUES,
25
- DEFAULT_PRESENCE,
26
- isValidPresence,
27
- } from './partitive-presence.js';
28
- import {
29
- PARTITIVE_COUNT_VALUES,
30
- DEFAULT_COUNT,
31
- isValidCount,
32
- } from './partitive-count.js';
22
+ import type { ConceptRefJson } from './concept-ref.js';
23
+ import { PARTITIVE_PRESENCE_VALUES, DEFAULT_PRESENCE, isValidPresence } from './partitive-presence.js';
24
+ import type { PartitivePresence } from './partitive-presence.js';
25
+ import { PARTITIVE_COUNT_VALUES, DEFAULT_COUNT, isValidCount } from './partitive-count.js';
26
+ import type { PartitiveCount } from './partitive-count.js';
33
27
  import { multiplicityFromPair } from './multiplicity.js';
34
28
 
35
- export class HyperedgeMember extends GlossaristModel {
36
- constructor(data = {}) {
29
+ export interface HyperedgeMemberJson {
30
+ ref: ConceptRefJson | ConceptRef;
31
+ presence?: PartitivePresence;
32
+ count?: PartitiveCount;
33
+ }
34
+
35
+ export abstract class HyperedgeMember extends GlossaristModel {
36
+ readonly ref: ConceptRef;
37
+ readonly presence: PartitivePresence;
38
+ readonly count: PartitiveCount;
39
+
40
+ constructor(data: HyperedgeMemberJson = { ref: {} }) {
37
41
  super();
38
42
  this.ref = _ensureRef(data.ref);
39
43
  this.presence = _resolvePresence(data.presence);
@@ -42,32 +46,40 @@ export class HyperedgeMember extends GlossaristModel {
42
46
  _assertValidPair(this.presence, this.count);
43
47
  }
44
48
 
45
- get isRequired() { return this.presence === PARTITIVE_PRESENCE.REQUIRED; }
46
- get isOptional() { return this.presence === PARTITIVE_PRESENCE.OPTIONAL; }
49
+ get isRequired(): boolean { return this.presence === 'required'; }
50
+ get isOptional(): boolean { return this.presence === 'optional'; }
47
51
 
48
- identity() {
52
+ override identity(): string {
49
53
  return HyperedgeMember.identityOf(this);
50
54
  }
51
55
 
52
- toJSON() {
53
- const obj = { ref: this.ref.toJSON() };
56
+ override toJSON(): HyperedgeMemberJson {
57
+ const obj: HyperedgeMemberJson = { ref: this.ref.toJSON() };
54
58
  if (this.presence !== DEFAULT_PRESENCE) obj.presence = this.presence;
55
59
  if (this.count !== DEFAULT_COUNT) obj.count = this.count;
56
60
  return obj;
57
61
  }
58
62
 
59
- static identityOf(value) {
63
+ static identityOf(
64
+ value: { ref?: { source?: string | null; id?: string | null } | null } | null | undefined,
65
+ ): string {
60
66
  const v = value ?? {};
61
67
  const ref = v.ref ?? {};
62
68
  return `${ref.source ?? ''}:${ref.id ?? ''}`;
63
69
  }
64
70
 
65
- static fromJSON(data) {
66
- return new HyperedgeMember(data);
71
+ static override fromJSON(_data: HyperedgeMemberJson): HyperedgeMember {
72
+ // Abstract base — subclasses MUST override. The base
73
+ // implementation is a typed marker that dispatches to the wrong
74
+ // class would surface as a runtime error rather than silently
75
+ // returning a mis-typed instance.
76
+ throw new Error(
77
+ 'HyperedgeMember.fromJSON must be overridden by a subclass',
78
+ );
67
79
  }
68
80
  }
69
81
 
70
- function _ensureRef(value) {
82
+ function _ensureRef(value: ConceptRefJson | ConceptRef | null | undefined): ConceptRef {
71
83
  const ref = value instanceof ConceptRef ? value : new ConceptRef(value ?? {});
72
84
  if (!ref.source && !ref.id && !ref.text) {
73
85
  throw new Error(
@@ -78,7 +90,7 @@ function _ensureRef(value) {
78
90
  return ref;
79
91
  }
80
92
 
81
- function _resolvePresence(value) {
93
+ function _resolvePresence(value: PartitivePresence | null | undefined): PartitivePresence {
82
94
  const p = value ?? DEFAULT_PRESENCE;
83
95
  if (!isValidPresence(p)) {
84
96
  throw new Error(
@@ -89,7 +101,7 @@ function _resolvePresence(value) {
89
101
  return p;
90
102
  }
91
103
 
92
- function _resolveCount(value) {
104
+ function _resolveCount(value: PartitiveCount | null | undefined): PartitiveCount {
93
105
  const c = value ?? DEFAULT_COUNT;
94
106
  if (!isValidCount(c)) {
95
107
  throw new Error(
@@ -100,9 +112,9 @@ function _resolveCount(value) {
100
112
  return c;
101
113
  }
102
114
 
103
- function _assertValidPair(presence, count) {
115
+ function _assertValidPair(presence: PartitivePresence, count: PartitiveCount): void {
104
116
  // Delegate to the multiplicity SSOT. The canonical error string
105
- // lives in one place: multiplicity.js. Calling multiplicityFromPair
117
+ // lives in one place: multiplicity.ts. Calling multiplicityFromPair
106
118
  // throws on the invalid combination; we don't need to duplicate the
107
119
  // error message here.
108
120
  multiplicityFromPair(presence, count);
@@ -21,18 +21,34 @@
21
21
  // The abstract base (AbstractHyperedge) has no wireKey and is skipped
22
22
  // by register() — it cannot be instantiated through the registry.
23
23
 
24
+ import type { AbstractHyperedge } from './abstract-hyperedge.js';
25
+
26
+ /**
27
+ * Shape a registered hyperedge class must satisfy. Every leaf class
28
+ * in src/models/ matches this shape (declared as static fields).
29
+ */
30
+ export interface HyperedgeClass {
31
+ readonly wireKey: string;
32
+ readonly typeTag: string;
33
+ readonly rdfType: string;
34
+ readonly memberClass: new (...args: never[]) => unknown;
35
+ readonly v1WireKeys?: ReadonlyArray<string>;
36
+ readonly kindLabel?: string;
37
+ readonly name: string;
38
+ }
39
+
24
40
  class HyperedgeRegistry {
25
- static _byWireKey = new Map();
26
- static _byTypeTag = new Map();
27
- static _byRdfType = new Map();
41
+ static _byWireKey: Map<string, HyperedgeClass> = new Map();
42
+ static _byTypeTag: Map<string, HyperedgeClass> = new Map();
43
+ static _byRdfType: Map<string, HyperedgeClass> = new Map();
28
44
 
29
- static register(cls) {
45
+ static register(cls: HyperedgeClass | undefined): boolean {
30
46
  if (!cls || !cls.wireKey || !cls.typeTag || !cls.rdfType) return false;
31
47
  if (this._byWireKey.has(cls.wireKey)) {
32
48
  throw new Error(
33
49
  `HyperedgeRegistry: duplicate wireKey '${cls.wireKey}' ` +
34
50
  `(trying to register ${cls.name}; already registered to ` +
35
- `${this._byWireKey.get(cls.wireKey).name})`,
51
+ `${this._byWireKey.get(cls.wireKey)?.name})`,
36
52
  );
37
53
  }
38
54
  if (this._byTypeTag.has(cls.typeTag)) {
@@ -53,9 +69,11 @@ class HyperedgeRegistry {
53
69
  return true;
54
70
  }
55
71
 
56
- // Test-only. Removes a class from all three indexes. Used by the OCP
57
- // spec to clean up mock types it adds during a test run.
58
- static unregister(cls) {
72
+ /**
73
+ * Test-only. Removes a class from all three indexes. Used by the OCP
74
+ * spec to clean up mock types it adds during a test run.
75
+ */
76
+ static unregister(cls: HyperedgeClass | undefined): boolean {
59
77
  if (!cls) return false;
60
78
  const r1 = this._byWireKey.delete(cls.wireKey);
61
79
  const r2 = this._byTypeTag.delete(cls.typeTag);
@@ -63,14 +81,20 @@ class HyperedgeRegistry {
63
81
  return r1 || r2 || r3;
64
82
  }
65
83
 
66
- static forWireKey(key) { return this._byWireKey.get(key) ?? null; }
67
- static forTypeTag(tag) { return this._byTypeTag.get(tag) ?? null; }
68
- static forRdfType(type) { return this._byRdfType.get(type) ?? null; }
84
+ static forWireKey(key: string): HyperedgeClass | null {
85
+ return this._byWireKey.get(key) ?? null;
86
+ }
87
+ static forTypeTag(tag: string): HyperedgeClass | null {
88
+ return this._byTypeTag.get(tag) ?? null;
89
+ }
90
+ static forRdfType(type: string): HyperedgeClass | null {
91
+ return this._byRdfType.get(type) ?? null;
92
+ }
69
93
 
70
- static allClasses() {
94
+ static allClasses(): HyperedgeClass[] {
71
95
  // Dedup in case the same class is reachable via multiple indexes.
72
- const seen = new Set();
73
- const out = [];
96
+ const seen = new Set<HyperedgeClass>();
97
+ const out: HyperedgeClass[] = [];
74
98
  for (const cls of this._byTypeTag.values()) {
75
99
  if (!seen.has(cls)) {
76
100
  seen.add(cls);
@@ -80,14 +104,16 @@ class HyperedgeRegistry {
80
104
  return out;
81
105
  }
82
106
 
83
- static allWireKeys() { return [...this._byWireKey.keys()]; }
84
- static allTypeTags() { return [...this._byTypeTag.keys()]; }
107
+ static allWireKeys(): string[] { return [...this._byWireKey.keys()]; }
108
+ static allTypeTags(): string[] { return [...this._byTypeTag.keys()]; }
85
109
 
86
- // Snapshot of every registered wire-shape identifier (v2 wireKey plus
87
- // any v1 legacy keys). Used by the parser to reserve STRUCTURAL_KEYS
88
- // and to know which top-level YAML keys to walk for hyperedge data.
89
- static allWireAndLegacyKeys() {
90
- const out = [];
110
+ /**
111
+ * Snapshot of every registered wire-shape identifier (v2 wireKey plus
112
+ * any v1 legacy keys). Used by the parser to reserve STRUCTURAL_KEYS
113
+ * and to know which top-level YAML keys to walk for hyperedge data.
114
+ */
115
+ static allWireAndLegacyKeys(): string[] {
116
+ const out: string[] = [];
91
117
  for (const cls of this.allClasses()) {
92
118
  out.push(cls.wireKey);
93
119
  for (const k of cls.v1WireKeys ?? []) out.push(k);
@@ -96,15 +122,19 @@ class HyperedgeRegistry {
96
122
  }
97
123
  }
98
124
 
99
- // Helper: partition a unified hyperedge array by wire key. Used by
100
- // Concept.toJSON, ConceptSerializer, and diff-patch to emit per-type
101
- // wire keys without per-type branching in those files. Adding a new
102
- // hyperedge type "just works" because the group is keyed by the
103
- // class's static wireKey.
104
- export function groupHyperedgesByWireKey(hyperedges) {
105
- const out = {};
125
+ /**
126
+ * Helper: partition a unified hyperedge array by wire key. Used by
127
+ * Concept.toJSON, ConceptSerializer, and diff-patch to emit per-type
128
+ * wire keys without per-type branching in those files. Adding a new
129
+ * hyperedge type "just works" because the group is keyed by the
130
+ * class's static wireKey.
131
+ */
132
+ export function groupHyperedgesByWireKey(
133
+ hyperedges: ReadonlyArray<AbstractHyperedge> | null | undefined,
134
+ ): Record<string, AbstractHyperedge[]> {
135
+ const out: Record<string, AbstractHyperedge[]> = {};
106
136
  for (const h of hyperedges ?? []) {
107
- const key = h?.constructor?.wireKey;
137
+ const key = (h?.constructor as unknown as HyperedgeClass | undefined)?.wireKey;
108
138
  if (!key) continue;
109
139
  if (!out[key]) out[key] = [];
110
140
  out[key].push(h);
@@ -0,0 +1,90 @@
1
+ // PartitiveHyperedge — an ISO 704:2022 / ISO 1087-1 / ISO 12620
2
+ // partitive relation connecting a comprehensive concept
3
+ // (superordinate concept partitive) to two or more partitive
4
+ // concepts (subordinate concepts partitive) which fitted together
5
+ // constitute the comprehensive.
6
+ //
7
+ // Extends AbstractHyperedge; the leaf-specific concern is the
8
+ // PartitiveMember type for `members`. The wire-level field name
9
+ // stays `partitives` for v1/v2 backward compat.
10
+
11
+ import { AbstractHyperedge } from './abstract-hyperedge.js';
12
+ import type { AbstractHyperedgeJson } from './abstract-hyperedge.js';
13
+ import { PartitiveMember } from './partitive-member.js';
14
+ import type { PartitiveMemberJson } from './partitive-member.js';
15
+ import { HyperedgeRegistry } from './hyperedge-registry.js';
16
+
17
+ export interface PartitiveHyperedgeJson extends AbstractHyperedgeJson {
18
+ members: ReadonlyArray<PartitiveMemberJson | PartitiveMember>;
19
+ /** PartitiveHyperedge wire alias of `members`. Both emitted for
20
+ * backward compat with consumers that read `partitives`. */
21
+ partitives?: ReadonlyArray<PartitiveMemberJson | PartitiveMember>;
22
+ }
23
+
24
+ export class PartitiveHyperedge extends AbstractHyperedge {
25
+ // Per-class metadata block — the OCP contract. External systems
26
+ // (parser, serializer, diff, RDF emitter, validators) read from
27
+ // these statics and the HyperedgeRegistry. Adding a new hyperedge
28
+ // type = declaring these fields on a new subclass + registering
29
+ // it. Nothing else in the codebase changes.
30
+ static wireKey = 'partitive_relations' as const;
31
+ static typeTag = 'partitive_relation' as const;
32
+ static rdfType = 'gloss:PartitiveRelation' as const;
33
+ static memberClass = PartitiveMember;
34
+ static v1WireKeys = ['partitive_hyperedges'];
35
+ static kindLabel = 'PART';
36
+
37
+ declare readonly members: ReadonlyArray<PartitiveMember>;
38
+
39
+ constructor(data: Partial<PartitiveHyperedgeJson> = {}) {
40
+ const memberSource = data.members ?? data.partitives;
41
+ const normalized: AbstractHyperedgeJson = {
42
+ comprehensive: data.comprehensive ?? {},
43
+ members: memberSource ?? [],
44
+ completeness: data.completeness ?? 'complete',
45
+ criterion: data.criterion,
46
+ sources: data.sources,
47
+ notes: data.notes,
48
+ status: data.status,
49
+ };
50
+ super(normalized);
51
+ this.members = (Array.isArray(memberSource) ? memberSource : []).map((m) =>
52
+ m instanceof PartitiveMember ? m : new PartitiveMember(m as PartitiveMemberJson),
53
+ );
54
+ }
55
+
56
+ get partitives(): ReadonlyArray<PartitiveMember> { return this.members; }
57
+
58
+ override toJSON(): PartitiveHyperedgeJson {
59
+ const base = super.toJSON() as Omit<PartitiveHyperedgeJson, 'partitives'>;
60
+ const { members, ...rest } = base;
61
+ return { ...rest, members: members as ReadonlyArray<PartitiveMemberJson | PartitiveMember>, partitives: members };
62
+ }
63
+
64
+ static identityOf(
65
+ value: {
66
+ comprehensive?: { source?: string | null; id?: string | null } | null;
67
+ partitives?: ReadonlyArray<{ ref?: { source?: string | null; id?: string | null } | null } | null>;
68
+ members?: ReadonlyArray<{ ref?: { source?: string | null; id?: string | null } | null } | null>;
69
+ } | null | undefined,
70
+ ): string {
71
+ const v = value ?? {};
72
+ const c = v.comprehensive ?? {};
73
+ const source = v.partitives ?? v.members ?? [];
74
+ const partKeys = source
75
+ .map((p) => {
76
+ const r = p?.ref ?? (p as { source?: string; id?: string } | null) ?? {};
77
+ return `${r.source ?? ''}:${r.id ?? ''}`;
78
+ })
79
+ .sort();
80
+ return `${c.source ?? ''}:${c.id ?? ''}|${partKeys.join('|')}`;
81
+ }
82
+
83
+ static override fromJSON(data: PartitiveHyperedgeJson): PartitiveHyperedge {
84
+ return new PartitiveHyperedge(data);
85
+ }
86
+ }
87
+
88
+ export default PartitiveHyperedge;
89
+
90
+ HyperedgeRegistry.register(PartitiveHyperedge);
@@ -13,36 +13,37 @@
13
13
  // HyperedgeMember, because it is partitive-specific. GenericMember
14
14
  // carries `delimitingCharacteristic` instead (the intension-difference
15
15
  // value per §5.5.4.2.1, which always applies to every generic member).
16
- //
17
- // Matches concept-model/models/concepts/PartitiveMember.lutaml exactly:
18
- // +ref — ConceptRef [1]
19
- // +presence — PartitivePresence [0..1] (default: required)
20
- // +count — PartitiveCount [0..1] (default: exactly_one)
21
- // +is_delimiting — Boolean [0..1] (default: false)
22
- //
23
- // The combination (optional, at_least_one) is invalid: if a part is
24
- // optional, the at-least-one constraint is vacuous and collapses to
25
- // (optional, multiple). Rejected at construction by HyperedgeMember's
26
- // delegate to multiplicityFromPair.
27
16
 
28
17
  import { HyperedgeMember } from './hyperedge-member.js';
18
+ import type { HyperedgeMemberJson } from './hyperedge-member.js';
19
+
20
+ export interface PartitiveMemberJson extends HyperedgeMemberJson {
21
+ is_delimiting?: boolean;
22
+ }
29
23
 
30
24
  export class PartitiveMember extends HyperedgeMember {
31
- constructor(data = {}) {
25
+ readonly is_delimiting: boolean;
26
+
27
+ constructor(data: PartitiveMemberJson = { ref: {} }) {
32
28
  super(data);
33
29
  this.is_delimiting = _resolveDelimiting(data.is_delimiting);
34
30
  }
35
31
 
36
- get isDelimiting() { return this.is_delimiting === true; }
32
+ get isDelimiting(): boolean { return this.is_delimiting === true; }
37
33
 
38
- toJSON() {
34
+ override toJSON(): PartitiveMemberJson {
39
35
  const base = super.toJSON();
40
- if (this.is_delimiting) base.is_delimiting = true;
41
- return base;
36
+ const out: PartitiveMemberJson = { ...base };
37
+ if (this.is_delimiting) out.is_delimiting = true;
38
+ return out;
39
+ }
40
+
41
+ static override fromJSON(data: PartitiveMemberJson): PartitiveMember {
42
+ return new PartitiveMember(data);
42
43
  }
43
44
  }
44
45
 
45
- function _resolveDelimiting(value) {
46
+ function _resolveDelimiting(value: boolean | null | undefined): boolean {
46
47
  if (value == null) return false;
47
48
  if (typeof value !== 'boolean') {
48
49
  throw new Error(
@@ -0,0 +1,70 @@
1
+ // SequentialHyperedge — an ISO 12620 A.6.3 / ISO 704:2022 §5.5.5
2
+ // sequential relation connecting a comprehensive concept to two or
3
+ // more subordinate concepts in an ORDERED sequence (temporal,
4
+ // spatial, causal, developmental).
5
+ //
6
+ // Mirror of PartitiveHyperedge and GenericHyperedge. Member array
7
+ // order IS significant — reversing the array reverses the sequence.
8
+ // Use cases: manufacturing process steps, biological life cycles,
9
+ // software release lifecycles, supply chain stages, geological eras.
10
+
11
+ import { AbstractHyperedge } from './abstract-hyperedge.js';
12
+ import type { AbstractHyperedgeJson } from './abstract-hyperedge.js';
13
+ import { SequentialMember } from './sequential-member.js';
14
+ import type { SequentialMemberJson } from './sequential-member.js';
15
+ import { HyperedgeRegistry } from './hyperedge-registry.js';
16
+
17
+ export interface SequentialHyperedgeJson extends AbstractHyperedgeJson {
18
+ members: ReadonlyArray<SequentialMemberJson | SequentialMember>;
19
+ }
20
+
21
+ export class SequentialHyperedge extends AbstractHyperedge {
22
+ // Per-class metadata block — the OCP contract.
23
+ static wireKey = 'sequential_relations' as const;
24
+ static typeTag = 'sequential_relation' as const;
25
+ static rdfType = 'gloss:SequentialRelation' as const;
26
+ static memberClass = SequentialMember;
27
+ static v1WireKeys: ReadonlyArray<string> = [];
28
+ static kindLabel = 'SEQ';
29
+
30
+ declare readonly members: ReadonlyArray<SequentialMember>;
31
+
32
+ constructor(data: Partial<SequentialHyperedgeJson> = {}) {
33
+ super({
34
+ comprehensive: data.comprehensive ?? {},
35
+ members: data.members ?? [],
36
+ completeness: data.completeness ?? 'complete',
37
+ criterion: data.criterion,
38
+ sources: data.sources,
39
+ notes: data.notes,
40
+ status: data.status,
41
+ });
42
+ this.members = (Array.isArray(data.members) ? data.members : []).map((m) =>
43
+ m instanceof SequentialMember ? m : new SequentialMember(m as SequentialMemberJson),
44
+ );
45
+ }
46
+
47
+ static identityOf(
48
+ value: {
49
+ comprehensive?: { source?: string | null; id?: string | null } | null;
50
+ members?: ReadonlyArray<{ ref?: { source?: string | null; id?: string | null } | null } | null>;
51
+ } | null | undefined,
52
+ ): string {
53
+ const v = value ?? {};
54
+ const c = v.comprehensive ?? {};
55
+ // For sequential, member order IS significant — do NOT sort.
56
+ const memberKeys = (v.members ?? []).map((m) => {
57
+ const r = m?.ref ?? (m as { source?: string; id?: string } | null) ?? {};
58
+ return `${r.source ?? ''}:${r.id ?? ''}`;
59
+ });
60
+ return `${c.source ?? ''}:${c.id ?? ''}|${memberKeys.join('|')}`;
61
+ }
62
+
63
+ static override fromJSON(data: SequentialHyperedgeJson): SequentialHyperedge {
64
+ return new SequentialHyperedge(data);
65
+ }
66
+ }
67
+
68
+ export default SequentialHyperedge;
69
+
70
+ HyperedgeRegistry.register(SequentialHyperedge);
@@ -1,13 +1,16 @@
1
1
  // SequentialMember — one member of a SequentialHyperedge. Inherits
2
2
  // the MECE shape from HyperedgeMember. Distinct leaf class for type
3
3
  // safety. Member array order IS significant for sequential hyperedges.
4
- //
5
- // See docs/design/abstract-nary-relation.md (concept-model repo).
6
4
 
7
5
  import { HyperedgeMember } from './hyperedge-member.js';
6
+ import type { HyperedgeMemberJson } from './hyperedge-member.js';
7
+
8
+ export type SequentialMemberJson = HyperedgeMemberJson;
8
9
 
9
10
  export class SequentialMember extends HyperedgeMember {
10
- // Inherits all attributes and validations from HyperedgeMember.
11
+ static override fromJSON(data: SequentialMemberJson): SequentialMember {
12
+ return new SequentialMember(data);
13
+ }
11
14
  }
12
15
 
13
16
  export default SequentialMember;
@@ -1,119 +0,0 @@
1
- // AbstractHyperedge — shared base for directed 1→N hyperedges
2
- // relations (PartitiveHyperedge, GenericHyperedge, future
3
- // SequentialRelation, AssociativeRelation).
4
- //
5
- // Carries the common shape:
6
- // - comprehensive: ConceptRef
7
- // - members: 2+ MECE dimensioned concept refs (≥2 per ISO 704)
8
- // - completeness: complete | partial
9
- // - criterion: localized subdivision criterion (ISO 12620)
10
- // - sources / notes / status: administrative metadata
11
- //
12
- // Concrete leaves add type-specific fields if needed (currently none).
13
- // See docs/design/abstract-hyperedge.md (concept-model repo).
14
-
15
- import { GlossaristModel } from './base.js';
16
- import { ConceptRef } from './concept-ref.js';
17
- import {
18
- COMPLETENESS,
19
- DEFAULT_COMPLETENESS,
20
- isValidCompleteness,
21
- } from './completeness.js';
22
-
23
- export class AbstractHyperedge extends GlossaristModel {
24
- constructor(data = {}) {
25
- super();
26
- this.comprehensive = _ensureComprehensive(data.comprehensive);
27
- this.members = _ensureMembers(data.members, this.constructor.name);
28
- this.completeness = _resolveCompleteness(data.completeness);
29
- this.criterion = _normalizeCriterion(data.criterion);
30
- this.sources = Array.isArray(data.sources) ? [...data.sources] : [];
31
- this.notes = data.notes ?? null;
32
- this.status = data.status ?? null;
33
-
34
- _assertNoSelfLoop(this.comprehensive, this.members);
35
- }
36
-
37
- get isComplete() { return this.completeness === COMPLETENESS.COMPLETE; }
38
- get isPartial() { return this.completeness === COMPLETENESS.PARTIAL; }
39
- get isCoordinate() { return this.members.length >= 2; }
40
-
41
- hasCriterion() { return this.criterion != null; }
42
-
43
- identity() {
44
- return this.constructor.identityOf(this);
45
- }
46
-
47
- toJSON() {
48
- const obj = {
49
- comprehensive: this.comprehensive.toJSON(),
50
- members: this.members.map(m => m.toJSON()),
51
- completeness: this.completeness,
52
- };
53
- if (this.criterion != null) obj.criterion = { ...this.criterion };
54
- if (this.sources.length > 0) obj.sources = [...this.sources];
55
- if (this.notes != null) obj.notes = { ...this.notes };
56
- if (this.status != null) obj.status = this.status;
57
- return obj;
58
- }
59
- }
60
-
61
- function _ensureComprehensive(value) {
62
- const ref = value instanceof ConceptRef ? value : new ConceptRef(value ?? {});
63
- if (!ref.source && !ref.id && !ref.text) {
64
- throw new Error(
65
- 'AbstractHyperedge.comprehensive must be a non-empty ConceptReference ' +
66
- '(source, id, or text required)',
67
- );
68
- }
69
- return ref;
70
- }
71
-
72
- function _ensureMembers(values, typeName) {
73
- if (!Array.isArray(values)) {
74
- throw new Error(
75
- `${typeName}.members must be an array (got ${typeof values})`,
76
- );
77
- }
78
- if (values.length < 2) {
79
- throw new Error(
80
- `${typeName} requires ≥2 partitives (ISO 704); got ${values.length}`,
81
- );
82
- }
83
- return values;
84
- }
85
-
86
- function _resolveCompleteness(value) {
87
- const c = value ?? DEFAULT_COMPLETENESS;
88
- if (!isValidCompleteness(c)) {
89
- throw new Error(
90
- `${c === value ? 'completeness' : 'completeness'} has invalid value ${JSON.stringify(value)}`,
91
- );
92
- }
93
- return c;
94
- }
95
-
96
- function _normalizeCriterion(value) {
97
- if (value == null) return null;
98
- if (typeof value === 'string') {
99
- return value.length > 0 ? { default: value } : null;
100
- }
101
- if (typeof value === 'object') {
102
- const entries = Object.entries(value).filter(([, v]) => typeof v === 'string');
103
- return entries.length > 0 ? Object.fromEntries(entries) : null;
104
- }
105
- return null;
106
- }
107
-
108
- function _assertNoSelfLoop(comprehensive, members) {
109
- const cKey = `${comprehensive.source ?? ''}:${comprehensive.id ?? ''}`;
110
- for (const m of members) {
111
- const ref = m.ref;
112
- const pKey = `${ref?.source ?? ''}:${ref?.id ?? ''}`;
113
- if (pKey === cKey && (ref?.source || ref?.id)) {
114
- throw new Error(
115
- 'Relation.members cannot include the comprehensive (no self-loops)',
116
- );
117
- }
118
- }
119
- }