@linked.archi/kernel 0.1.5 → 0.1.7

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.
Files changed (35) hide show
  1. package/README.md +8 -0
  2. package/assets/archimate3/archimate3.2-metamodel.ttl +18 -1
  3. package/assets/archimate3/archimate3.2-principle-shapes.ttl +16 -22
  4. package/assets/archimate3/archimate3.2-questions.ttl +38 -38
  5. package/assets/archimate3/archimate3.2-viewpoint-shapes.ttl +1 -1
  6. package/assets/archimate4/archimate4-metamodel.ttl +18 -1
  7. package/assets/archimate4/archimate4-principle-shapes.ttl +38 -36
  8. package/assets/archimate4/archimate4-questions.ttl +38 -38
  9. package/assets/archimate4/archimate4-viewpoint-shapes.ttl +1 -1
  10. package/assets/catalog.ttl +776 -464
  11. package/assets/core/core-onto.ttl +12 -9
  12. package/assets/core/core-shapes.ttl +1 -1
  13. package/assets/core/core-vis-onto.ttl +99 -5
  14. package/assets/core/core-vis-shapes.ttl +275 -11
  15. package/package.json +5 -2
  16. package/src/model-manager/appearance.ts +131 -7
  17. package/src/model-manager/cascade.ts +5 -3
  18. package/src/model-manager/clipboard.ts +10 -2
  19. package/src/model-manager/concept-styles.ts +230 -0
  20. package/src/model-manager/interfaces.ts +19 -1
  21. package/src/model-manager/iri-conformance.ts +9 -2
  22. package/src/model-manager/placement-copy.ts +35 -8
  23. package/src/model-manager/questions.ts +6 -1
  24. package/src/model-manager/style-sets.ts +399 -0
  25. package/src/model-manager/style-values.ts +349 -0
  26. package/src/model-manager/view-manager.ts +74 -22
  27. package/src/model-manager/vocabulary.ts +10 -0
  28. package/src/rdf-engine/iri.ts +6 -3
  29. package/src/rdf-engine/native-load.ts +37 -0
  30. package/src/rdf-engine/observable-store.ts +98 -120
  31. package/src/rdf-engine/rdf-store.ts +14 -2
  32. package/src/rdf-engine/sparql-constraints.ts +15 -0
  33. package/src/rdf-engine/turtle-writer.ts +1 -0
  34. package/src/rdf-engine/validation-engine.ts +97 -32
  35. package/src/rdf-engine/validation-service.ts +3 -1
@@ -8,9 +8,14 @@
8
8
  * connector between the copied figures, a relationship between the copied placements of its ends, each with
9
9
  * its route, its looks and a name moved off its line. The source and the target may be different stores:
10
10
  * paste reads a copy's placements through a view manager over the copy and places them in the open view.
11
+ *
12
+ * A style based on another (core-vis 0.11) stays based on it where the target may be (`keepsBase`), and is
13
+ * detached otherwise — the base's looks for each presentation written on the copy's style for it — so that a copy
14
+ * looks as its original did either way, in every presentation.
11
15
  */
12
16
 
13
- import type { Bounds, FigureInfo, Point, ViewManager } from './interfaces.js';
17
+ import { detachedLooks, type LookSources } from './appearance.js';
18
+ import type { Bounds, FigureInfo, LinkInfo, Point, ViewManager } from './interfaces.js';
14
19
 
15
20
  /**
16
21
  * A figure as the target can place it. A reference opens a view of its own model: pasted into a model without
@@ -32,6 +37,13 @@ export interface PlacementCopyOptions {
32
37
  concept?: (iri: string) => string;
33
38
  /** Every position moved by this much. */
34
39
  offset?: Point;
40
+ /**
41
+ * Whether a copied style may stay based on `base` (core-vis 0.11's `arch-vis:basedOn`) where the copy draws
42
+ * `concept` in the target — none for a figure, a reference's view for a reference. Every base is kept when
43
+ * absent: in the same model, as *Duplicate view* copies. A style that may not is detached — the base's values
44
+ * written in where it sets none — so the copy looks as the original did.
45
+ */
46
+ keepsBase?: (base: string, concept: string | undefined) => boolean;
35
47
  }
36
48
 
37
49
  /** The placements made, by the source placement they copy. */
@@ -53,17 +65,28 @@ export function copyPlacements(
53
65
  const moved = (b: Bounds): Bounds => ({ ...b, x: b.x + dx, y: b.y + dy });
54
66
  const route = (points: Point[]): Point[] => points.map((p) => ({ x: p.x + dx, y: p.y + dy }));
55
67
 
68
+ const keepsBase = options.keepsBase ?? (() => true);
69
+ /**
70
+ * A copy's looks — for every presentation and for each — as the target can take them: still based on their bases,
71
+ * or with what a base gave written in (`detachedLooks`), its looks for each presentation too.
72
+ */
73
+ const copiedLooks = (sources: LookSources, drawn: string | undefined) => detachedLooks(sources, (base) => !keepsBase(base, drawn));
74
+
56
75
  const nodes = source.getNodes(sourceViewIri).filter((node) => !options.nodes || options.nodes.has(node.nodeIri));
57
76
  const copies = new Map<string, string>();
58
77
  for (const node of nodes) {
59
78
  const figure = placeable(node.figure, target);
79
+ // The concept the copy draws in the target: what a base of its concept's own may be (`keepsBase`).
80
+ const drawn = figure ? (figure.kind === 'reference' ? figure.view : undefined) : concept(node.elementIri);
81
+ // A figure's look for every presentation is in it: copied with it, based on what it was or detached.
82
+ const looks = copiedLooks({ ...node, appearance: figure ?? node.appearance }, drawn);
60
83
  const copied = figure
61
- ? target.addFigure!(targetViewIri, { ...figure, bounds: moved(node.bounds) })
84
+ ? target.addFigure!(targetViewIri, { ...figure, basedOn: undefined, ...looks.appearance, bounds: moved(node.bounds) })
62
85
  : target.addElementToView(targetViewIri, concept(node.elementIri), moved(node.bounds), { another: true });
63
86
  copies.set(node.nodeIri, copied);
64
87
  // Looking as it did: a figure's appearance is copied with it; its label saying what it said.
65
- if (node.appearance) target.setAppearance!(targetViewIri, copied, node.appearance);
66
- for (const [context, look] of Object.entries(node.contextAppearances ?? {})) target.setAppearance!(targetViewIri, copied, look, context);
88
+ if (!figure && looks.appearance) target.setAppearance!(targetViewIri, copied, looks.appearance);
89
+ for (const [context, look] of Object.entries(looks.contextAppearances ?? {})) target.setAppearance!(targetViewIri, copied, look, context);
67
90
  for (const label of node.labelExpressions ?? []) target.setLabelExpression!(targetViewIri, copied, label.context, label.expression);
68
91
  if (node.zOrder) target.setNodeZOrder?.(targetViewIri, copied, node.zOrder);
69
92
  }
@@ -73,6 +96,12 @@ export function copyPlacements(
73
96
  }
74
97
 
75
98
  const linkCopies = new Map<string, string>();
99
+ /** A line's looks — for every presentation and for each — on its copy, drawing `drawn` (none for a line from a note). */
100
+ const copyLinkLooks = (link: LinkInfo, copied: string, drawn: string | undefined) => {
101
+ const looks = copiedLooks(link, drawn);
102
+ if (looks.appearance) target.setAppearance!(targetViewIri, copied, looks.appearance);
103
+ for (const [context, look] of Object.entries(looks.contextAppearances ?? {})) target.setAppearance!(targetViewIri, copied, look, context);
104
+ };
76
105
  let pending = source.getLinks(sourceViewIri).filter((link) => !options.links || options.links.has(link.linkIri));
77
106
  for (;;) {
78
107
  const waiting = pending.filter((link) => {
@@ -85,8 +114,7 @@ export function copyPlacements(
85
114
  sourceNodeIri && targetNodeIri ? target.addConnector!(targetViewIri, { sourceNodeIri, targetNodeIri }, route(link.waypoints)) : undefined;
86
115
  if (!copied) return false;
87
116
  linkCopies.set(link.linkIri, copied);
88
- if (link.appearance) target.setAppearance!(targetViewIri, copied, link.appearance);
89
- for (const [context, look] of Object.entries(link.contextAppearances ?? {})) target.setAppearance!(targetViewIri, copied, look, context);
117
+ copyLinkLooks(link, copied, undefined);
90
118
  if (link.text) target.setConnectorText!(targetViewIri, copied, link.text);
91
119
  if (link.label) target.setLinkLabel!(targetViewIri, copied, moved(link.label));
92
120
  return false;
@@ -103,8 +131,7 @@ export function copyPlacements(
103
131
  );
104
132
  if (!copied) return true;
105
133
  linkCopies.set(link.linkIri, copied);
106
- if (link.appearance) target.setAppearance!(targetViewIri, copied, link.appearance);
107
- for (const [context, look] of Object.entries(link.contextAppearances ?? {})) target.setAppearance!(targetViewIri, copied, look, context);
134
+ copyLinkLooks(link, copied, concept(link.relationshipIri));
108
135
  // A name moved off the middle of its line stays where it was put.
109
136
  if (link.label) target.setLinkLabel!(targetViewIri, copied, moved(link.label));
110
137
  return false;
@@ -128,8 +128,13 @@ function anyGraph(pattern: string, g: string): string {
128
128
 
129
129
  type Row = Record<string, { value: string; language?: string } | undefined>;
130
130
 
131
+ /**
132
+ * A question set's statements, read where the store keeps the metamodels (`queryMetamodels`): listing the questions
133
+ * must not give the inner store a model just opened, which only SPARQL over the model needs (KD-28).
134
+ */
131
135
  function select(store: RdfStore, sparql: string): Row[] {
132
- const result = store.query(sparql);
136
+ const metamodels = (store as { queryMetamodels?(sparql: string): ReturnType<RdfStore['query']> }).queryMetamodels;
137
+ const result = typeof metamodels === 'function' ? metamodels.call(store, sparql) : store.query(sparql);
133
138
  return result.type === 'bindings' ? (result.results as Row[]) : [];
134
139
  }
135
140
 
@@ -0,0 +1,399 @@
1
+ /**
2
+ * Style sets (core-vis 0.11): named sets of styles to share — a team's or an organisation's standard looks, such
3
+ * as "To retire" or "Strategic". A model names the sets its diagrams use with `arch-vis:usesStyleSet`. A set's
4
+ * styles say they are in it (`arch-vis:inStyleSet`), have a name (`skos:prefLabel`) and may say what they are for
5
+ * (`skos:definition`); they are for every presentation context and based on no other style. A style's look for one
6
+ * context is another style of the set, for that context and based on it, at most one for each (core-vis-shapes
7
+ * 0.7.0): so the set is complete for each audience. A diagram element takes a named style by basing its own style on
8
+ * it (`arch-vis:basedOn`, written with `ViewManager.setAppearance`): drawn with the named style's values — for a
9
+ * context, its look for that context first — wherever its own style sets none, it follows the named style as it
10
+ * changes.
11
+ *
12
+ * la-gram keeps a set in the model — `{ns}styleset/{uuid}`, its styles `{set}/style/{uuid}`, a style's look for a
13
+ * context `{style}/{hash}` (DD-41) — and reads the sets its model uses that the model holds. A named style deleted,
14
+ * or a whole set, leaves what it gave with the styles based on it (detached, meta's DD-47), so that nothing drawn
15
+ * changes.
16
+ */
17
+
18
+ import { DataFactory } from 'n3';
19
+ import type { NamedNode, Quad, Term } from '@rdfjs/types';
20
+ import type { RdfStore } from '../rdf-engine/interfaces.js';
21
+ import { mintIri, partIri, uuid } from '../rdf-engine/iri.js';
22
+ import { selectLocalizedLiteral } from '../rdf-engine/localized-text.js';
23
+ import { APPEARANCE_KEYS, APPEARANCE_PREDICATES, type Appearance, type AppearanceChanges } from './appearance.js';
24
+ import { modelNamespace } from './model-namespace.js';
25
+ import { modelNameOf } from './model-queries.js';
26
+ import { shortHash } from './short-hash.js';
27
+ import { appearanceTerm, baseContextLooks, detachAllChange, lookOfStyle, styleValuesChange } from './style-values.js';
28
+ import { IRI, T, dedupe, humanize, langLiteral, localName, netChange, nn, triple } from './vocabulary.js';
29
+
30
+ /** The default graph: the model. */
31
+ const MODEL_GRAPH = DataFactory.defaultGraph() as unknown as NamedNode;
32
+
33
+ /** One of a style set's named styles. */
34
+ export interface NamedStyleInfo {
35
+ iri: string;
36
+ /** Its name, in the language asked for where it has one. */
37
+ name: string;
38
+ /** What it is for (`skos:definition`). */
39
+ definition?: string;
40
+ /** The set it is in. */
41
+ set: string;
42
+ /** Its values for every presentation context. */
43
+ look: Appearance;
44
+ /** Its looks for each presentation context it looks otherwise in, by the context's IRI: over {@link look} there. */
45
+ contextLooks?: Record<string, Appearance>;
46
+ }
47
+
48
+ /** A style set, with its styles. */
49
+ export interface StyleSetInfo {
50
+ iri: string;
51
+ name: string;
52
+ /** Its styles, by name. */
53
+ styles: NamedStyleInfo[];
54
+ }
55
+
56
+ /**
57
+ * The style sets `modelIri` uses (`arch-vis:usesStyleSet`) that the model holds, by name, each with its styles by
58
+ * name. A set named but not held — published apart, and not read with the model — is not listed: there is nothing
59
+ * of it to apply.
60
+ */
61
+ export function styleSetsOf(store: RdfStore, modelIri: string, language = 'en'): StyleSetInfo[] {
62
+ const sets: StyleSetInfo[] = [];
63
+ for (const q of store.getQuads(nn(modelIri), T.usesStyleSet, null, MODEL_GRAPH)) {
64
+ if (q.object.termType !== 'NamedNode' || store.getQuads(q.object, T.rdfType, T.StyleSet, MODEL_GRAPH).length === 0) continue;
65
+ const styles = store
66
+ .getQuads(null, T.inStyleSet, q.object, MODEL_GRAPH)
67
+ .map((s) => (s.subject.termType === 'NamedNode' ? namedStyleOf(store, s.subject.value, language) : undefined))
68
+ .filter((style): style is NamedStyleInfo => style !== undefined)
69
+ .sort(byName);
70
+ sets.push({ iri: q.object.value, name: nameOf(store, q.object.value, language), styles });
71
+ }
72
+ return sets.sort(byName);
73
+ }
74
+
75
+ /**
76
+ * A named style — one in a style set, for every presentation context — by its IRI, with its looks for each context;
77
+ * `undefined` for a style in no set, or for a style's look for one context, which is part of the style it is based on.
78
+ */
79
+ export function namedStyleOf(store: RdfStore, iri: string, language = 'en'): NamedStyleInfo | undefined {
80
+ const style = nn(iri);
81
+ const set = store.getQuads(style, T.inStyleSet, null, MODEL_GRAPH)[0]?.object;
82
+ if (!set || store.getQuads(style, T.inPresentationContext, null, MODEL_GRAPH).length > 0) return undefined;
83
+ const definition = selectLocalizedLiteral(
84
+ store.getQuads(style, T.definition, null, MODEL_GRAPH).map((q) => q.object),
85
+ language,
86
+ )?.value;
87
+ const contextLooks = baseContextLooks(store, style);
88
+ return {
89
+ iri,
90
+ name: nameOf(store, iri, language),
91
+ ...(definition ? { definition } : {}),
92
+ set: set.value,
93
+ look: lookOfStyle(store, style),
94
+ ...(contextLooks ? { contextLooks } : {}),
95
+ };
96
+ }
97
+
98
+ /** The IRI of a named style's look for `context`: `{style}/{hash}` (DD-41). */
99
+ export function namedStyleLookIri(styleIri: string, context: string): string {
100
+ return partIri(styleIri, shortHash(context));
101
+ }
102
+
103
+ /** What adds a new style set to the model, named, and used by it: `{ns}styleset/{uuid}`. */
104
+ export function newStyleSetChange(store: RdfStore, modelIri: string, name: string, language = 'en'): { iri: string; remove: Quad[]; add: Quad[] } {
105
+ const iri = mintIri(modelNamespace(store, modelIri), 'styleset');
106
+ const set = nn(iri);
107
+ return {
108
+ iri,
109
+ remove: [],
110
+ add: [triple(set, T.rdfType, T.StyleSet), triple(set, T.prefLabel, langLiteral(nameOrDefault(name, 'Styles'), language)), triple(nn(modelIri), T.usesStyleSet, set)],
111
+ };
112
+ }
113
+
114
+ /**
115
+ * What adds a named style to a set, with `look`'s values for every presentation context (not a style it is based
116
+ * on: a set's style is based on none) and, given, its looks for each context: `{set}/style/{uuid}`.
117
+ */
118
+ export function newNamedStyleChange(
119
+ setIri: string,
120
+ name: string,
121
+ look: Appearance,
122
+ options: { definition?: string; language?: string; contextLooks?: Record<string, Appearance> } = {},
123
+ ): { iri: string; remove: Quad[]; add: Quad[] } {
124
+ const language = options.language ?? 'en';
125
+ const iri = partIri(setIri, 'style', uuid());
126
+ const style = nn(iri);
127
+ const add: Quad[] = [
128
+ triple(style, T.rdfType, T.Style),
129
+ triple(style, T.inStyleSet, nn(setIri)),
130
+ triple(style, T.prefLabel, langLiteral(nameOrDefault(name, 'Style'), language)),
131
+ ];
132
+ const definition = options.definition?.trim();
133
+ if (definition) add.push(triple(style, T.definition, langLiteral(definition, language)));
134
+ add.push(...lookQuads(style, look));
135
+ for (const [context, contextLook] of Object.entries(options.contextLooks ?? {})) {
136
+ const values = lookQuads(nn(namedStyleLookIri(iri, context)), contextLook);
137
+ if (values.length > 0) add.push(...contextLookQuads(setIri, iri, context), ...values);
138
+ }
139
+ return { iri, remove: [], add };
140
+ }
141
+
142
+ /** What renames a set or a named style in `language`: its `skos:prefLabel` there replaced. An empty name changes nothing. */
143
+ export function renameChange(store: RdfStore, iri: string, name: string, language = 'en'): { remove: Quad[]; add: Quad[] } {
144
+ const text = name.trim();
145
+ if (!text) return { remove: [], add: [] };
146
+ return textChange(store, iri, IRI.prefLabel, text, language);
147
+ }
148
+
149
+ /** What says what a named style is for (`skos:definition`) in `language` — or, `null` or empty, says nothing. */
150
+ export function definitionChange(store: RdfStore, iri: string, definition: string | null, language = 'en'): { remove: Quad[]; add: Quad[] } {
151
+ return textChange(store, iri, IRI.definition, definition?.trim() ?? '', language);
152
+ }
153
+
154
+ /**
155
+ * What changes a named style's values — for every presentation context, or, given `context`, its look for that
156
+ * context — as `setAppearance` changes a diagram element's: a value sets it, `null` takes it away. Every diagram
157
+ * element based on it is drawn so. A look for a context is made where it has none, and emptied, goes: drawn there,
158
+ * the style's for every context. Never based on another style: a `basedOn` in `changes` is not written.
159
+ */
160
+ export function namedStyleChange(
161
+ store: RdfStore,
162
+ styleIri: string,
163
+ changes: AppearanceChanges,
164
+ context?: string,
165
+ ): { remove: Quad[]; add: Quad[] } {
166
+ const values: AppearanceChanges = { ...changes };
167
+ delete values.basedOn;
168
+ if (!context) return styleValuesChange(store, nn(styleIri), values);
169
+ const setIri = store.getQuads(nn(styleIri), T.inStyleSet, null, MODEL_GRAPH)[0]?.object.value;
170
+ if (!setIri) return { remove: [], add: [] };
171
+ const current = namedStyleLookOf(store, styleIri, context);
172
+ const look: Record<string, unknown> = { ...(current ? lookOfStyle(store, current) : {}) };
173
+ for (const key of APPEARANCE_KEYS) {
174
+ const value = values[key];
175
+ if (value === undefined) continue;
176
+ if (value === null) delete look[key];
177
+ else if (appearanceTerm(key, value as NonNullable<Appearance[typeof key]>)) look[key] = value;
178
+ }
179
+ const target = (current as NamedNode | undefined) ?? nn(namedStyleLookIri(styleIri, context));
180
+ const valueQuads = lookQuads(target, look as Appearance);
181
+ // Nothing left for that context: the look goes.
182
+ if (valueQuads.length === 0) return current ? removeNamedStyleLookChange(store, styleIri, context) : { remove: [], add: [] };
183
+ const remove = current ? store.getQuads(current as NamedNode, null, null, MODEL_GRAPH).filter((q) => APPEARANCE_PREDICATE_SET.has(q.predicate.value)) : [];
184
+ const add = [...(current ? [] : contextLookQuads(setIri, styleIri, context)), ...valueQuads];
185
+ return netChange({ remove, add });
186
+ }
187
+
188
+ /** What takes a named style's look for `context` away: what is based on the style is drawn there as for every context. */
189
+ export function removeNamedStyleLookChange(store: RdfStore, styleIri: string, context: string): { remove: Quad[]; add: Quad[] } {
190
+ const current = namedStyleLookOf(store, styleIri, context);
191
+ return current ? { remove: store.getQuads(current as NamedNode, null, null, MODEL_GRAPH), add: [] } : { remove: [], add: [] };
192
+ }
193
+
194
+ /** What deletes a named style, with its looks for each context: every style based on it keeps what it gave (detached), then it goes. */
195
+ export function deleteNamedStyleChange(store: RdfStore, styleIri: string): { remove: Quad[]; add: Quad[] } {
196
+ return deleteNamedStylesChange(store, [styleIri]);
197
+ }
198
+
199
+ /**
200
+ * What deletes a style set: its styles as {@link deleteNamedStyleChange} deletes them — what else is in it, too —
201
+ * the set, and every model's use of it.
202
+ */
203
+ export function deleteStyleSetChange(store: RdfStore, setIri: string): { remove: Quad[]; add: Quad[] } {
204
+ return deleteStyleSetsChange(store, [setIri]);
205
+ }
206
+
207
+ /**
208
+ * What deletes named styles together, with their looks for each context: the diagram elements based on any of them
209
+ * detached at once, each keeping what they gave it.
210
+ */
211
+ function deleteNamedStylesChange(store: RdfStore, styleIris: readonly string[]): { remove: Quad[]; add: Quad[] } {
212
+ const followers = detachAllChange(store, styleIris.map((iri) => nn(iri)));
213
+ const remove: Quad[] = [...followers.remove];
214
+ for (const iri of styleIris) {
215
+ remove.push(...store.getQuads(nn(iri), null, null, MODEL_GRAPH));
216
+ for (const look of contextLookTerms(store, iri)) remove.push(...store.getQuads(look as NamedNode, null, null, MODEL_GRAPH));
217
+ }
218
+ return netChange({ remove: dedupe(remove), add: followers.add });
219
+ }
220
+
221
+ /** What deletes style sets together: their styles at once, what else is in them, the sets, and every model's use of them. */
222
+ function deleteStyleSetsChange(store: RdfStore, setIris: readonly string[], alsoStyles: readonly string[] = []): { remove: Quad[]; add: Quad[] } {
223
+ const members = setIris.flatMap((setIri) => store.getQuads(null, T.inStyleSet, nn(setIri), MODEL_GRAPH).map((q) => q.subject));
224
+ const styles = members.filter((member) => namedStyleOf(store, member.value)).map((member) => member.value);
225
+ const change = deleteNamedStylesChange(store, [...new Set([...styles, ...alsoStyles])]);
226
+ const remove: Quad[] = [...change.remove];
227
+ for (const member of members) remove.push(...store.getQuads(member as NamedNode, null, null, MODEL_GRAPH));
228
+ for (const setIri of setIris) {
229
+ remove.push(...store.getQuads(nn(setIri), null, null, MODEL_GRAPH), ...store.getQuads(null, T.usesStyleSet, nn(setIri), MODEL_GRAPH));
230
+ }
231
+ return netChange({ remove: dedupe(remove), add: change.add });
232
+ }
233
+
234
+ /**
235
+ * Whether a diagram element of `modelIri` drawing `conceptIri` — none for a figure — may be based on `baseIri`
236
+ * (core-vis-shapes 0.7.0's `:BasedOnShape`): a style of a set the model uses, held in it, for every presentation
237
+ * context; or the concept's own look for every context. A paste keeps a `basedOn` only where this holds, and writes
238
+ * what the base gives in where it does not (`clipboard.ts`).
239
+ */
240
+ export function mayBaseOn(store: RdfStore, modelIri: string, conceptIri: string | undefined, baseIri: string): boolean {
241
+ const base = nn(baseIri);
242
+ if (store.getQuads(base, T.rdfType, T.Style, MODEL_GRAPH).length === 0) return false;
243
+ if (store.getQuads(base, T.inPresentationContext, null, MODEL_GRAPH).length > 0) return false;
244
+ const set = store.getQuads(base, T.inStyleSet, null, MODEL_GRAPH)[0]?.object;
245
+ if (set) return store.getQuads(nn(modelIri), T.usesStyleSet, set as NamedNode, MODEL_GRAPH).length > 0;
246
+ return !!conceptIri && store.getQuads(nn(conceptIri), T.conceptStyle, base, MODEL_GRAPH).length > 0;
247
+ }
248
+
249
+ // ── Settings › Styles: the model's sets as edited ───────────────────────
250
+
251
+ /** A named style as edited: its IRI when it is the model's already, its name, its looks for every context and for each. */
252
+ export interface StyleEdit {
253
+ iri?: string;
254
+ name: string;
255
+ definition?: string;
256
+ look: Appearance;
257
+ contextLooks?: Record<string, Appearance>;
258
+ }
259
+
260
+ /** A style set as edited: its IRI when it is the model's already, its name, and the styles it is to have. */
261
+ export interface StyleSetEdit {
262
+ iri?: string;
263
+ name: string;
264
+ styles: StyleEdit[];
265
+ }
266
+
267
+ /**
268
+ * What makes the style sets `modelIri` uses as `edited` says — one change, as la-gram's *File › Settings… › Styles*
269
+ * applies it. A set or a style of the model's that `edited` leaves out is deleted (what is based on it keeps what it
270
+ * gave: {@link deleteNamedStyleChange}); one without an IRI is made; the others are renamed, said what they are for,
271
+ * and given their looks — for every context and for each — as written, a value not written taken away. A set left
272
+ * with no style goes, or is not made: a set is its styles.
273
+ */
274
+ export function styleSetsEditChange(store: RdfStore, modelIri: string, edited: readonly StyleSetEdit[], language = 'en'): { remove: Quad[]; add: Quad[] } {
275
+ const remove: Quad[] = [];
276
+ const add: Quad[] = [];
277
+ const take = (change: { remove: Quad[]; add: Quad[] }) => {
278
+ remove.push(...change.remove);
279
+ add.push(...change.add);
280
+ };
281
+ const current = styleSetsOf(store, modelIri, language);
282
+ const kept = new Set(edited.filter((set) => set.iri && set.styles.length > 0).map((set) => set.iri!));
283
+ // What goes, at once: so that a diagram element based on two of them is detached from both together.
284
+ const goingSets = current.filter((set) => !kept.has(set.iri)).map((set) => set.iri);
285
+ const goingStyles = current
286
+ .filter((set) => kept.has(set.iri))
287
+ .flatMap((set) => {
288
+ const stays = new Set(edited.find((e) => e.iri === set.iri)!.styles.flatMap((s) => (s.iri ? [s.iri] : [])));
289
+ return set.styles.filter((style) => !stays.has(style.iri)).map((style) => style.iri);
290
+ });
291
+ if (goingSets.length > 0 || goingStyles.length > 0) take(deleteStyleSetsChange(store, goingSets, goingStyles));
292
+ for (const set of edited) {
293
+ if (set.styles.length === 0) continue;
294
+ const was = set.iri ? current.find((c) => c.iri === set.iri) : undefined;
295
+ let setIri = was?.iri;
296
+ if (!setIri) {
297
+ const made = newStyleSetChange(store, modelIri, set.name, language);
298
+ setIri = made.iri;
299
+ take(made);
300
+ } else if (set.name.trim() && set.name.trim() !== was!.name) take(renameChange(store, setIri, set.name, language));
301
+ for (const style of set.styles) {
302
+ const before = style.iri ? was?.styles.find((s) => s.iri === style.iri) : undefined;
303
+ if (!before) {
304
+ take(newNamedStyleChange(setIri, style.name, style.look, { language, ...(style.definition ? { definition: style.definition } : {}), ...(style.contextLooks ? { contextLooks: style.contextLooks } : {}) }));
305
+ continue;
306
+ }
307
+ if (style.name.trim() && style.name.trim() !== before.name) take(renameChange(store, before.iri, style.name, language));
308
+ if ((style.definition?.trim() ?? '') !== (before.definition ?? '')) take(definitionChange(store, before.iri, style.definition ?? null, language));
309
+ take(namedStyleChange(store, before.iri, replacing(before.look, style.look)));
310
+ const contexts = new Set([...Object.keys(before.contextLooks ?? {}), ...Object.keys(style.contextLooks ?? {})]);
311
+ for (const context of contexts) {
312
+ const look = style.contextLooks?.[context];
313
+ take(
314
+ look && APPEARANCE_KEYS.some((key) => look[key] !== undefined)
315
+ ? namedStyleChange(store, before.iri, replacing(before.contextLooks?.[context] ?? {}, look), context)
316
+ : removeNamedStyleLookChange(store, before.iri, context),
317
+ );
318
+ }
319
+ }
320
+ }
321
+ return netChange({ remove: dedupe(remove), add: dedupe(add) });
322
+ }
323
+
324
+ /** The changes that make a look `before` into `after`: each value of `after`, and `null` for what it no longer sets. */
325
+ function replacing(before: Appearance, after: Appearance): AppearanceChanges {
326
+ const changes: Record<string, unknown> = {};
327
+ for (const key of APPEARANCE_KEYS) {
328
+ if (after[key] !== undefined) {
329
+ if (after[key] !== before[key]) changes[key] = after[key];
330
+ } else if (before[key] !== undefined) changes[key] = null;
331
+ }
332
+ return changes as AppearanceChanges;
333
+ }
334
+
335
+ /** Each look's predicate, as a set. */
336
+ const APPEARANCE_PREDICATE_SET = new Set<string>(APPEARANCE_KEYS.map((key) => APPEARANCE_PREDICATES[key]));
337
+
338
+ /** `look`'s values on `style`, as quads. */
339
+ function lookQuads(style: NamedNode, look: Appearance): Quad[] {
340
+ return APPEARANCE_KEYS.flatMap((key) => {
341
+ const value = look[key];
342
+ const term = value === undefined ? undefined : appearanceTerm(key, value as NonNullable<Appearance[typeof key]>);
343
+ return term ? [triple(style, nn(APPEARANCE_PREDICATES[key]), term)] : [];
344
+ });
345
+ }
346
+
347
+ /** What a named style's look for `context` says it is: a style of the set, for that context, based on the style. */
348
+ function contextLookQuads(setIri: string, styleIri: string, context: string): Quad[] {
349
+ const look = nn(namedStyleLookIri(styleIri, context));
350
+ return [
351
+ triple(look, T.rdfType, T.Style),
352
+ triple(look, T.inStyleSet, nn(setIri)),
353
+ triple(look, T.inPresentationContext, nn(context)),
354
+ triple(look, T.basedOn, nn(styleIri)),
355
+ ];
356
+ }
357
+
358
+ /** A named style's looks for each context: the styles of its set based on it, each for a context. */
359
+ function contextLookTerms(store: RdfStore, styleIri: string): Term[] {
360
+ const set = store.getQuads(nn(styleIri), T.inStyleSet, null, MODEL_GRAPH)[0]?.object;
361
+ if (!set) return [];
362
+ return store
363
+ .getQuads(null, T.basedOn, nn(styleIri), MODEL_GRAPH)
364
+ .map((q) => q.subject)
365
+ .filter(
366
+ (look) =>
367
+ store.getQuads(look as NamedNode, T.inStyleSet, set as NamedNode, MODEL_GRAPH).length > 0 &&
368
+ store.getQuads(look as NamedNode, T.inPresentationContext, null, MODEL_GRAPH).length > 0,
369
+ );
370
+ }
371
+
372
+ /** A named style's look for `context`, `undefined` when it has none. */
373
+ function namedStyleLookOf(store: RdfStore, styleIri: string, context: string): Term | undefined {
374
+ return contextLookTerms(store, styleIri).find(
375
+ (look) => store.getQuads(look as NamedNode, T.inPresentationContext, null, MODEL_GRAPH)[0]?.object.value === context,
376
+ );
377
+ }
378
+
379
+ function nameOf(store: RdfStore, iri: string, language: string): string {
380
+ return modelNameOf(store, iri, language) ?? humanize(localName(iri));
381
+ }
382
+
383
+ function nameOrDefault(name: string, fallback: string): string {
384
+ return name.trim() || fallback;
385
+ }
386
+
387
+ function byName(a: { name: string; iri: string }, b: { name: string; iri: string }): number {
388
+ return a.name.localeCompare(b.name) || a.iri.localeCompare(b.iri);
389
+ }
390
+
391
+ /** What replaces `iri`'s `predicate` text in `language` — the others stay — or, empty, takes it away. */
392
+ function textChange(store: RdfStore, iri: string, predicate: string, text: string, language: string): { remove: Quad[]; add: Quad[] } {
393
+ const subject = nn(iri);
394
+ const remove = store
395
+ .getQuads(subject, nn(predicate), null, MODEL_GRAPH)
396
+ .filter((q) => q.object.termType === 'Literal' && ((q.object as { language?: string }).language ?? '') === language);
397
+ const add = text ? [triple(subject, nn(predicate), langLiteral(text, language))] : [];
398
+ return netChange({ remove, add });
399
+ }