@canonical/anatomy-dsl 0.2.2 → 0.5.0

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.
package/dist/parse.js DELETED
@@ -1,72 +0,0 @@
1
- export function parseAnatomyYAML(raw) {
2
- const doc = raw;
3
- return {
4
- root: toNamedNode(doc.node),
5
- };
6
- }
7
- function toNamedNode(raw) {
8
- return {
9
- type: "named",
10
- uri: raw.uri,
11
- ...(raw.styles ? { styles: toStyles(raw.styles) } : {}),
12
- ...(raw.edges ? { edges: raw.edges.map(toEdge) } : {}),
13
- };
14
- }
15
- function toNode(raw) {
16
- if ("uri" in raw && raw.uri) {
17
- return toNamedNode(raw);
18
- }
19
- const anon = raw;
20
- return {
21
- type: "anonymous",
22
- role: anon.role,
23
- ...(anon.styles ? { styles: toStyles(anon.styles) } : {}),
24
- ...(anon.edges ? { edges: anon.edges.map(toEdge) } : {}),
25
- };
26
- }
27
- function toStyles(raw) {
28
- return Object.entries(raw).map(([key, value]) => ({
29
- key,
30
- value: String(value),
31
- }));
32
- }
33
- function toEdge(raw) {
34
- let target;
35
- if (raw.switch) {
36
- target = toSwitch(raw.switch);
37
- }
38
- else if (raw.node) {
39
- target = toNode(raw.node);
40
- }
41
- else if (raw.uri) {
42
- target = { type: "named", uri: raw.uri };
43
- }
44
- else {
45
- throw new Error("Edge must have node, uri, or switch");
46
- }
47
- return {
48
- target,
49
- relation: toRelation(raw.relation),
50
- };
51
- }
52
- function toSwitch(raw) {
53
- return {
54
- discriminator: raw.on,
55
- cases: raw.cases.map(toSwitchCase),
56
- };
57
- }
58
- function toSwitchCase(raw) {
59
- if (raw.uri !== undefined) {
60
- const node = { type: "named", uri: raw.uri };
61
- return { value: raw.uri, node };
62
- }
63
- const node = toNode(raw.node);
64
- const value = node.type === "named" ? node.uri : node.role;
65
- return { value, node };
66
- }
67
- function toRelation(raw) {
68
- return {
69
- cardinality: raw.cardinality,
70
- ...(raw.slotName ? { slotName: raw.slotName } : {}),
71
- };
72
- }
@@ -1,2 +0,0 @@
1
- import type { Specification } from "./types.js";
2
- export declare function anatomyToTTL(spec: Specification): string;
package/dist/transform.js DELETED
@@ -1,114 +0,0 @@
1
- const PREFIX = "@prefix : <http://anatomy-dsl.example.org/ontology#> .";
2
- const INDENT = " ";
3
- export function anatomyToTTL(spec) {
4
- const lines = [PREFIX, ""];
5
- lines.push("[] a :Specification ;");
6
- lines.push(`${INDENT}:rootNode [`);
7
- writeNode(lines, spec.root, 2);
8
- lines.push(`${INDENT}] .`);
9
- return `${lines.join("\n")}\n`;
10
- }
11
- function writeNode(lines, node, depth) {
12
- if (node.type === "named") {
13
- writeNamedNode(lines, node, depth);
14
- }
15
- else {
16
- writeAnonymousNode(lines, node, depth);
17
- }
18
- const styles = node.styles ?? [];
19
- const edges = node.edges ?? [];
20
- if (styles.length > 0) {
21
- writeStyles(lines, styles, depth, edges.length === 0);
22
- }
23
- if (edges.length > 0) {
24
- writeEdges(lines, edges, depth);
25
- }
26
- }
27
- function writeNamedNode(lines, node, depth) {
28
- const indent = INDENT.repeat(depth);
29
- lines.push(`${indent}a :NamedNode ;`);
30
- const hasMore = (node.styles && node.styles.length > 0) || (node.edges && node.edges.length > 0);
31
- lines.push(`${indent}:uri "${node.uri}"${hasMore ? " ;" : ""}`);
32
- }
33
- function writeAnonymousNode(lines, node, depth) {
34
- const indent = INDENT.repeat(depth);
35
- lines.push(`${indent}a :AnonymousNode ;`);
36
- const hasMore = (node.styles && node.styles.length > 0) || (node.edges && node.edges.length > 0);
37
- lines.push(`${indent}:role "${node.role}"${hasMore ? " ;" : ""}`);
38
- }
39
- function writeStyles(lines, styles, depth, isLast) {
40
- const indent = INDENT.repeat(depth);
41
- const innerIndent = INDENT.repeat(depth + 1);
42
- lines.push(`${indent}:hasStyle`);
43
- for (const [i, style] of styles.entries()) {
44
- const sep = i < styles.length - 1 ? " ," : isLast ? "" : " ;";
45
- lines.push(`${innerIndent}[ :styleKey "${style.key}" ; :styleValue "${style.value}" ]${sep}`);
46
- }
47
- }
48
- function writeEdges(lines, edges, depth) {
49
- const indent = INDENT.repeat(depth);
50
- for (const [i, edge] of edges.entries()) {
51
- if (i === 0) {
52
- lines.push(`${indent}:hasEdge [`);
53
- }
54
- else {
55
- lines.push(`${indent}] , [`);
56
- }
57
- writeEdge(lines, edge, depth + 1);
58
- }
59
- lines.push(`${indent}]`);
60
- }
61
- function writeEdge(lines, edge, depth) {
62
- const indent = INDENT.repeat(depth);
63
- lines.push(`${indent}a :Edge ;`);
64
- if (isSwitch(edge.target)) {
65
- lines.push(`${indent}:edgeSwitch [`);
66
- writeSwitch(lines, edge.target, depth + 1);
67
- lines.push(`${indent}] ;`);
68
- }
69
- else {
70
- lines.push(`${indent}:edgeTarget [`);
71
- writeNode(lines, edge.target, depth + 1);
72
- lines.push(`${indent}] ;`);
73
- }
74
- writeRelation(lines, edge, depth);
75
- }
76
- function writeSwitch(lines, sw, depth) {
77
- const indent = INDENT.repeat(depth);
78
- lines.push(`${indent}a :Switch ;`);
79
- lines.push(`${indent}:discriminator "${sw.discriminator}" ;`);
80
- for (const [i, sc] of sw.cases.entries()) {
81
- if (i === 0) {
82
- lines.push(`${indent}:hasCase [`);
83
- }
84
- else {
85
- lines.push(`${indent}] , [`);
86
- }
87
- writeSwitchCase(lines, sc, depth + 1);
88
- }
89
- lines.push(`${indent}]`);
90
- }
91
- function writeSwitchCase(lines, sc, depth) {
92
- const indent = INDENT.repeat(depth);
93
- lines.push(`${indent}a :SwitchCase ;`);
94
- lines.push(`${indent}:caseNode [`);
95
- writeNode(lines, sc.node, depth + 1);
96
- lines.push(`${indent}]`);
97
- }
98
- function writeRelation(lines, edge, depth) {
99
- const indent = INDENT.repeat(depth);
100
- lines.push(`${indent}:hasRelation [`);
101
- const inner = INDENT.repeat(depth + 1);
102
- lines.push(`${inner}a :Relation ;`);
103
- if (edge.relation.slotName) {
104
- lines.push(`${inner}:cardinality "${edge.relation.cardinality}" ;`);
105
- lines.push(`${inner}:slotName "${edge.relation.slotName}"`);
106
- }
107
- else {
108
- lines.push(`${inner}:cardinality "${edge.relation.cardinality}"`);
109
- }
110
- lines.push(`${indent}]`);
111
- }
112
- function isSwitch(target) {
113
- return "discriminator" in target;
114
- }
@@ -1,3 +0,0 @@
1
- export { parseAnatomyYAML } from "./parse.js";
2
- export { anatomyToTTL } from "./transform.js";
3
- export type { AnonymousNode, Edge, NamedNode, Node, Relation, Specification, Style, Switch, SwitchCase, } from "./types.js";
@@ -1,2 +0,0 @@
1
- import type { Specification } from "./types.js";
2
- export declare function parseAnatomyYAML(raw: unknown): Specification;
@@ -1,2 +0,0 @@
1
- import type { Specification } from "./types.js";
2
- export declare function anatomyToTTL(spec: Specification): string;
@@ -1,36 +0,0 @@
1
- export interface Specification {
2
- root: NamedNode;
3
- }
4
- export interface NamedNode {
5
- type: "named";
6
- uri: string;
7
- styles?: Style[];
8
- edges?: Edge[];
9
- }
10
- export interface AnonymousNode {
11
- type: "anonymous";
12
- role: string;
13
- styles?: Style[];
14
- edges?: Edge[];
15
- }
16
- export type Node = NamedNode | AnonymousNode;
17
- export interface Edge {
18
- target: Node | Switch;
19
- relation: Relation;
20
- }
21
- export interface Relation {
22
- cardinality: string;
23
- slotName?: string;
24
- }
25
- export interface Style {
26
- key: string;
27
- value: string;
28
- }
29
- export interface Switch {
30
- discriminator: "props" | "internal" | "override";
31
- cases: SwitchCase[];
32
- }
33
- export interface SwitchCase {
34
- value: string;
35
- node: Node;
36
- }
package/dist/types.d.ts DELETED
@@ -1,36 +0,0 @@
1
- export interface Specification {
2
- root: NamedNode;
3
- }
4
- export interface NamedNode {
5
- type: "named";
6
- uri: string;
7
- styles?: Style[];
8
- edges?: Edge[];
9
- }
10
- export interface AnonymousNode {
11
- type: "anonymous";
12
- role: string;
13
- styles?: Style[];
14
- edges?: Edge[];
15
- }
16
- export type Node = NamedNode | AnonymousNode;
17
- export interface Edge {
18
- target: Node | Switch;
19
- relation: Relation;
20
- }
21
- export interface Relation {
22
- cardinality: string;
23
- slotName?: string;
24
- }
25
- export interface Style {
26
- key: string;
27
- value: string;
28
- }
29
- export interface Switch {
30
- discriminator: "props" | "internal" | "override";
31
- cases: SwitchCase[];
32
- }
33
- export interface SwitchCase {
34
- value: string;
35
- node: Node;
36
- }
package/dist/types.js DELETED
@@ -1 +0,0 @@
1
- export {};