@dxos/graph 0.7.5-main.499c70c
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/LICENSE +8 -0
- package/README.md +3 -0
- package/dist/lib/browser/index.mjs +212 -0
- package/dist/lib/browser/index.mjs.map +7 -0
- package/dist/lib/browser/meta.json +1 -0
- package/dist/lib/node/index.cjs +233 -0
- package/dist/lib/node/index.cjs.map +7 -0
- package/dist/lib/node/meta.json +1 -0
- package/dist/lib/node-esm/index.mjs +214 -0
- package/dist/lib/node-esm/index.mjs.map +7 -0
- package/dist/lib/node-esm/meta.json +1 -0
- package/dist/types/src/buidler.d.ts +16 -0
- package/dist/types/src/buidler.d.ts.map +1 -0
- package/dist/types/src/graph.d.ts +30 -0
- package/dist/types/src/graph.d.ts.map +1 -0
- package/dist/types/src/graph.test.d.ts +2 -0
- package/dist/types/src/graph.test.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +4 -0
- package/dist/types/src/index.d.ts.map +1 -0
- package/dist/types/src/types.d.ts +41 -0
- package/dist/types/src/types.d.ts.map +1 -0
- package/dist/types/src/util.d.ts +2 -0
- package/dist/types/src/util.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -0
- package/package.json +60 -0
- package/src/buidler.ts +62 -0
- package/src/graph.test.ts +56 -0
- package/src/graph.ts +127 -0
- package/src/index.ts +7 -0
- package/src/types.ts +42 -0
- package/src/util.ts +17 -0
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
import { createRequire } from 'node:module';const require = createRequire(import.meta.url);
|
|
2
|
+
|
|
3
|
+
// packages/common/graph/src/buidler.ts
|
|
4
|
+
import { FormatEnum } from "@dxos/echo-schema";
|
|
5
|
+
import { invariant } from "@dxos/invariant";
|
|
6
|
+
import { getSchema } from "@dxos/live-object";
|
|
7
|
+
import { log } from "@dxos/log";
|
|
8
|
+
import { getSchemaProperties } from "@dxos/schema";
|
|
9
|
+
|
|
10
|
+
// packages/common/graph/src/graph.ts
|
|
11
|
+
import { create } from "@dxos/live-object";
|
|
12
|
+
|
|
13
|
+
// packages/common/graph/src/types.ts
|
|
14
|
+
import { S } from "@dxos/echo-schema";
|
|
15
|
+
var BaseGraphNode = S.Struct({
|
|
16
|
+
id: S.String,
|
|
17
|
+
type: S.optional(S.String),
|
|
18
|
+
data: S.optional(S.Any)
|
|
19
|
+
});
|
|
20
|
+
var BaseGraphEdge = S.Struct({
|
|
21
|
+
id: S.String,
|
|
22
|
+
type: S.optional(S.String),
|
|
23
|
+
source: S.String,
|
|
24
|
+
target: S.String,
|
|
25
|
+
data: S.optional(S.Any)
|
|
26
|
+
});
|
|
27
|
+
var Graph = S.Struct({
|
|
28
|
+
nodes: S.mutable(S.Array(BaseGraphNode)),
|
|
29
|
+
edges: S.mutable(S.Array(BaseGraphEdge))
|
|
30
|
+
});
|
|
31
|
+
var emptyGraph = {
|
|
32
|
+
nodes: [],
|
|
33
|
+
edges: []
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
// packages/common/graph/src/util.ts
|
|
37
|
+
var removeElements = (array, predicate) => {
|
|
38
|
+
const deleted = [];
|
|
39
|
+
for (let i = array.length - 1; i >= 0; i--) {
|
|
40
|
+
if (predicate(array[i], i)) {
|
|
41
|
+
deleted.push(...array.splice(i, 1));
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return deleted;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
// packages/common/graph/src/graph.ts
|
|
48
|
+
var ReadonlyGraphModel = class {
|
|
49
|
+
constructor({ nodes = [], edges = [] } = {}) {
|
|
50
|
+
this._graph = create(Graph, {
|
|
51
|
+
nodes,
|
|
52
|
+
edges
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
toJSON() {
|
|
56
|
+
this._graph.nodes.sort(({ id: a }, { id: b }) => a.localeCompare(b));
|
|
57
|
+
this._graph.edges.sort(({ id: a }, { id: b }) => a.localeCompare(b));
|
|
58
|
+
return JSON.parse(JSON.stringify(this._graph));
|
|
59
|
+
}
|
|
60
|
+
get graph() {
|
|
61
|
+
return this._graph;
|
|
62
|
+
}
|
|
63
|
+
get nodes() {
|
|
64
|
+
return this._graph.nodes;
|
|
65
|
+
}
|
|
66
|
+
get edges() {
|
|
67
|
+
return this._graph.edges;
|
|
68
|
+
}
|
|
69
|
+
getNode(id) {
|
|
70
|
+
return this.nodes.find((node) => node.id === id);
|
|
71
|
+
}
|
|
72
|
+
getNodes({ type }) {
|
|
73
|
+
return this.nodes.filter((node) => !type || type === node.type);
|
|
74
|
+
}
|
|
75
|
+
getEdge(id) {
|
|
76
|
+
return this.edges.find((edge) => edge.id === id);
|
|
77
|
+
}
|
|
78
|
+
getEdges({ type, source, target }) {
|
|
79
|
+
return this.edges.filter((edge) => (!type || type === edge.type) && (!source || source === edge.source) && (!target || target === edge.target));
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
var GraphModel = class _GraphModel extends ReadonlyGraphModel {
|
|
83
|
+
clear() {
|
|
84
|
+
this._graph.nodes.length = 0;
|
|
85
|
+
this._graph.edges.length = 0;
|
|
86
|
+
return this;
|
|
87
|
+
}
|
|
88
|
+
addGraph(graph) {
|
|
89
|
+
this.addNodes(graph.nodes);
|
|
90
|
+
this.addEdges(graph.edges);
|
|
91
|
+
return this;
|
|
92
|
+
}
|
|
93
|
+
addGraphs(graphs) {
|
|
94
|
+
graphs.forEach((graph) => {
|
|
95
|
+
this.addNodes(graph.nodes);
|
|
96
|
+
this.addEdges(graph.edges);
|
|
97
|
+
});
|
|
98
|
+
return this;
|
|
99
|
+
}
|
|
100
|
+
addNode(node) {
|
|
101
|
+
this._graph.nodes.push(node);
|
|
102
|
+
return this;
|
|
103
|
+
}
|
|
104
|
+
addNodes(nodes) {
|
|
105
|
+
nodes.forEach((node) => this.addNode(node));
|
|
106
|
+
return this;
|
|
107
|
+
}
|
|
108
|
+
addEdge(edge) {
|
|
109
|
+
this._graph.edges.push(edge);
|
|
110
|
+
return this;
|
|
111
|
+
}
|
|
112
|
+
addEdges(edges) {
|
|
113
|
+
edges.forEach((edge) => this.addEdge(edge));
|
|
114
|
+
return this;
|
|
115
|
+
}
|
|
116
|
+
// TODO(burdon): Return graph.
|
|
117
|
+
removeNode(id) {
|
|
118
|
+
const nodes = removeElements(this._graph.nodes, (node) => node.id === id);
|
|
119
|
+
const edges = removeElements(this._graph.edges, (edge) => edge.source === id || edge.target === id);
|
|
120
|
+
return new _GraphModel({
|
|
121
|
+
nodes,
|
|
122
|
+
edges
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
removeNodes(ids) {
|
|
126
|
+
const graphs = ids.map((id) => this.removeNode(id));
|
|
127
|
+
return new _GraphModel().addGraphs(graphs);
|
|
128
|
+
}
|
|
129
|
+
removeEdge(id) {
|
|
130
|
+
const edges = removeElements(this._graph.edges, (edge) => edge.id === id);
|
|
131
|
+
return new _GraphModel({
|
|
132
|
+
edges
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
removeEdges(ids) {
|
|
136
|
+
const graphs = ids.map((id) => this.removeEdge(id));
|
|
137
|
+
return new _GraphModel().addGraphs(graphs);
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
// packages/common/graph/src/buidler.ts
|
|
142
|
+
var __dxlog_file = "/home/runner/work/dxos/dxos/packages/common/graph/src/buidler.ts";
|
|
143
|
+
var createEdgeId = ({ source, target, relation }) => `${source}-${relation}-${target}`;
|
|
144
|
+
var parseEdgeId = (id) => {
|
|
145
|
+
const [source, relation, target] = id.split("-");
|
|
146
|
+
invariant(source && target && relation, void 0, {
|
|
147
|
+
F: __dxlog_file,
|
|
148
|
+
L: 22,
|
|
149
|
+
S: void 0,
|
|
150
|
+
A: [
|
|
151
|
+
"source && target && relation",
|
|
152
|
+
""
|
|
153
|
+
]
|
|
154
|
+
});
|
|
155
|
+
return {
|
|
156
|
+
source,
|
|
157
|
+
relation,
|
|
158
|
+
target
|
|
159
|
+
};
|
|
160
|
+
};
|
|
161
|
+
var createGraph = (objects) => {
|
|
162
|
+
const graph = new GraphModel();
|
|
163
|
+
objects.forEach((object) => {
|
|
164
|
+
graph.addNode({
|
|
165
|
+
id: object.id,
|
|
166
|
+
type: object.typename,
|
|
167
|
+
data: object
|
|
168
|
+
});
|
|
169
|
+
});
|
|
170
|
+
objects.forEach((object) => {
|
|
171
|
+
const schema = getSchema(object);
|
|
172
|
+
if (!schema) {
|
|
173
|
+
log.info("no schema for object", {
|
|
174
|
+
id: object.id.slice(0, 8)
|
|
175
|
+
}, {
|
|
176
|
+
F: __dxlog_file,
|
|
177
|
+
L: 41,
|
|
178
|
+
S: void 0,
|
|
179
|
+
C: (f, a) => f(...a)
|
|
180
|
+
});
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
for (const prop of getSchemaProperties(schema.ast, object)) {
|
|
184
|
+
if (prop.format === FormatEnum.Ref) {
|
|
185
|
+
const source = object;
|
|
186
|
+
const target = object[prop.name]?.target;
|
|
187
|
+
if (target) {
|
|
188
|
+
graph.addEdge({
|
|
189
|
+
id: createEdgeId({
|
|
190
|
+
source: source.id,
|
|
191
|
+
target: target.id,
|
|
192
|
+
relation: String(prop.name)
|
|
193
|
+
}),
|
|
194
|
+
source: source.id,
|
|
195
|
+
target: target.id
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
});
|
|
201
|
+
return graph;
|
|
202
|
+
};
|
|
203
|
+
export {
|
|
204
|
+
BaseGraphEdge,
|
|
205
|
+
BaseGraphNode,
|
|
206
|
+
Graph,
|
|
207
|
+
GraphModel,
|
|
208
|
+
ReadonlyGraphModel,
|
|
209
|
+
createEdgeId,
|
|
210
|
+
createGraph,
|
|
211
|
+
emptyGraph,
|
|
212
|
+
parseEdgeId
|
|
213
|
+
};
|
|
214
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../src/buidler.ts", "../../../src/graph.ts", "../../../src/types.ts", "../../../src/util.ts"],
|
|
4
|
+
"sourcesContent": ["//\n// Copyright 2023 DXOS.org\n//\n\nimport { type ReactiveEchoObject } from '@dxos/echo-db';\nimport { FormatEnum } from '@dxos/echo-schema';\nimport { invariant } from '@dxos/invariant';\nimport { getSchema } from '@dxos/live-object';\nimport { log } from '@dxos/log';\nimport { getSchemaProperties } from '@dxos/schema';\n\nimport { GraphModel } from './graph';\nimport { type GraphNode } from './types';\n\n// NOTE: The `relation` is different from the `type`.\ntype Edge = { source: string; target: string; relation: string };\n\nexport const createEdgeId = ({ source, target, relation }: Edge) => `${source}-${relation}-${target}`;\n\nexport const parseEdgeId = (id: string): Edge => {\n const [source, relation, target] = id.split('-');\n invariant(source && target && relation);\n return { source, relation, target };\n};\n\n/**\n * Maps an ECHO object graph onto a layout graph.\n */\nexport const createGraph = (objects: ReactiveEchoObject<any>[]): GraphModel<GraphNode<ReactiveEchoObject<any>>> => {\n const graph = new GraphModel<GraphNode<ReactiveEchoObject<any>>>();\n\n // Map objects.\n objects.forEach((object) => {\n graph.addNode({ id: object.id, type: object.typename, data: object });\n });\n\n // Find references.\n objects.forEach((object) => {\n const schema = getSchema(object);\n if (!schema) {\n log.info('no schema for object', { id: object.id.slice(0, 8) });\n return;\n }\n\n // Parse schema to follow referenced objects.\n for (const prop of getSchemaProperties(schema.ast, object)) {\n if (prop.format === FormatEnum.Ref) {\n const source = object;\n const target = object[prop.name]?.target;\n if (target) {\n graph.addEdge({\n id: createEdgeId({ source: source.id, target: target.id, relation: String(prop.name) }),\n source: source.id,\n target: target.id,\n });\n }\n }\n }\n });\n\n return graph;\n};\n", "//\n// Copyright 2024 DXOS.org\n//\n\nimport { create } from '@dxos/live-object';\n\nimport { Graph, type GraphNode, type GraphEdge } from './types';\nimport { removeElements } from './util';\n\n// TODO(burdon): Traversal/visitor.\n\n/**\n * Typed reactive object graph.\n */\nexport class ReadonlyGraphModel<Node extends GraphNode = any, Edge extends GraphEdge = any> {\n protected readonly _graph: Graph;\n\n constructor({ nodes = [], edges = [] }: Partial<Graph> = {}) {\n this._graph = create(Graph, { nodes, edges });\n }\n\n toJSON() {\n // Sort to enable stable comparisons.\n this._graph.nodes.sort(({ id: a }, { id: b }) => a.localeCompare(b));\n this._graph.edges.sort(({ id: a }, { id: b }) => a.localeCompare(b));\n return JSON.parse(JSON.stringify(this._graph));\n }\n\n get graph(): Graph {\n return this._graph;\n }\n\n get nodes(): Node[] {\n return this._graph.nodes as Node[];\n }\n\n get edges(): Edge[] {\n return this._graph.edges as Edge[];\n }\n\n getNode(id: string): Node | undefined {\n return this.nodes.find((node) => node.id === id);\n }\n\n getNodes({ type }: Partial<Node>): Node[] {\n return this.nodes.filter((node) => !type || type === node.type);\n }\n\n getEdge(id: string): Edge | undefined {\n return this.edges.find((edge) => edge.id === id);\n }\n\n getEdges({ type, source, target }: Partial<Edge>): Edge[] {\n return this.edges.filter(\n (edge) =>\n (!type || type === edge.type) && (!source || source === edge.source) && (!target || target === edge.target),\n );\n }\n}\n\nexport class GraphModel<Node extends GraphNode = any, Edge extends GraphEdge = any> extends ReadonlyGraphModel<\n Node,\n Edge\n> {\n clear(): this {\n this._graph.nodes.length = 0;\n this._graph.edges.length = 0;\n return this;\n }\n\n addGraph(graph: GraphModel<Node, Edge>): this {\n this.addNodes(graph.nodes);\n this.addEdges(graph.edges);\n return this;\n }\n\n addGraphs(graphs: GraphModel<Node, Edge>[]): this {\n graphs.forEach((graph) => {\n this.addNodes(graph.nodes);\n this.addEdges(graph.edges);\n });\n return this;\n }\n\n addNode(node: Node): this {\n this._graph.nodes.push(node);\n return this;\n }\n\n addNodes(nodes: Node[]): this {\n nodes.forEach((node) => this.addNode(node));\n return this;\n }\n\n addEdge(edge: Edge): this {\n this._graph.edges.push(edge);\n return this;\n }\n\n addEdges(edges: Edge[]): this {\n edges.forEach((edge) => this.addEdge(edge));\n return this;\n }\n\n // TODO(burdon): Return graph.\n\n removeNode(id: string): GraphModel<Node, Edge> {\n const nodes = removeElements(this._graph.nodes, (node) => node.id === id);\n const edges = removeElements(this._graph.edges, (edge) => edge.source === id || edge.target === id);\n return new GraphModel<Node, Edge>({ nodes, edges });\n }\n\n removeNodes(ids: string[]): GraphModel<Node, Edge> {\n const graphs = ids.map((id) => this.removeNode(id));\n return new GraphModel<Node, Edge>().addGraphs(graphs);\n }\n\n removeEdge(id: string): GraphModel<Node, Edge> {\n const edges = removeElements(this._graph.edges, (edge) => edge.id === id);\n return new GraphModel<Node, Edge>({ edges });\n }\n\n removeEdges(ids: string[]): GraphModel<Node, Edge> {\n const graphs = ids.map((id) => this.removeEdge(id));\n return new GraphModel<Node, Edge>().addGraphs(graphs);\n }\n}\n", "//\n// Copyright 2024 DXOS.org\n//\n\nimport { S } from '@dxos/echo-schema';\n\n// Prior art:\n// - https://graphology.github.io (TS, tree-shakable, multiple packages for extensions)\n// - https://github.com/dagrejs/graphlib (mature, extensive)\n// - https://github.com/avoidwork/tiny-graph\n\nexport const BaseGraphNode = S.Struct({\n id: S.String,\n type: S.optional(S.String),\n data: S.optional(S.Any),\n});\n\nexport type BaseGraphNode = S.Schema.Type<typeof BaseGraphNode>;\nexport type GraphNode<Data extends object | void = void> = BaseGraphNode & { data: Data };\n\nexport const BaseGraphEdge = S.Struct({\n id: S.String,\n type: S.optional(S.String),\n source: S.String,\n target: S.String,\n data: S.optional(S.Any),\n});\n\nexport type BaseGraphEdge = S.Schema.Type<typeof BaseGraphEdge>;\nexport type GraphEdge<Data extends object | void = void> = BaseGraphEdge & { data: Data };\n\n/**\n * Generic graph abstraction.\n */\nexport const Graph = S.Struct({\n nodes: S.mutable(S.Array(BaseGraphNode)),\n edges: S.mutable(S.Array(BaseGraphEdge)),\n});\n\nexport type Graph = S.Schema.Type<typeof Graph>;\n\nexport const emptyGraph: Graph = { nodes: [], edges: [] };\n", "//\n// Copyright 2024 DXOS.org\n//\n\n// TODO(burdon): GraphBuilder.\n\n// TODO(burdon): Move to @dxos/live-object?\nexport const removeElements = <T>(array: T[], predicate: (element: T, index: number) => boolean): T[] => {\n const deleted: T[] = [];\n for (let i = array.length - 1; i >= 0; i--) {\n if (predicate(array[i], i)) {\n deleted.push(...array.splice(i, 1));\n }\n }\n\n return deleted;\n};\n"],
|
|
5
|
+
"mappings": ";;;AAKA,SAASA,kBAAkB;AAC3B,SAASC,iBAAiB;AAC1B,SAASC,iBAAiB;AAC1B,SAASC,WAAW;AACpB,SAASC,2BAA2B;;;ACLpC,SAASC,cAAc;;;ACAvB,SAASC,SAAS;AAOX,IAAMC,gBAAgBC,EAAEC,OAAO;EACpCC,IAAIF,EAAEG;EACNC,MAAMJ,EAAEK,SAASL,EAAEG,MAAM;EACzBG,MAAMN,EAAEK,SAASL,EAAEO,GAAG;AACxB,CAAA;AAKO,IAAMC,gBAAgBR,EAAEC,OAAO;EACpCC,IAAIF,EAAEG;EACNC,MAAMJ,EAAEK,SAASL,EAAEG,MAAM;EACzBM,QAAQT,EAAEG;EACVO,QAAQV,EAAEG;EACVG,MAAMN,EAAEK,SAASL,EAAEO,GAAG;AACxB,CAAA;AAQO,IAAMI,QAAQX,EAAEC,OAAO;EAC5BW,OAAOZ,EAAEa,QAAQb,EAAEc,MAAMf,aAAAA,CAAAA;EACzBgB,OAAOf,EAAEa,QAAQb,EAAEc,MAAMN,aAAAA,CAAAA;AAC3B,CAAA;AAIO,IAAMQ,aAAoB;EAAEJ,OAAO,CAAA;EAAIG,OAAO,CAAA;AAAG;;;AClCjD,IAAME,iBAAiB,CAAIC,OAAYC,cAAAA;AAC5C,QAAMC,UAAe,CAAA;AACrB,WAASC,IAAIH,MAAMI,SAAS,GAAGD,KAAK,GAAGA,KAAK;AAC1C,QAAIF,UAAUD,MAAMG,CAAAA,GAAIA,CAAAA,GAAI;AAC1BD,cAAQG,KAAI,GAAIL,MAAMM,OAAOH,GAAG,CAAA,CAAA;IAClC;EACF;AAEA,SAAOD;AACT;;;AFFO,IAAMK,qBA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,QAAQA,OAAOG;cAAIF,UAAU4B,OAAOR,KAAKM,IAAI;YAAE,CAAA;YACrF5B,QAAQA,OAAOI;YACfH,QAAQA,OAAOG;UACjB,CAAA;QACF;MACF;IACF;EACF,CAAA;AAEA,SAAOK;AACT;",
|
|
6
|
+
"names": ["FormatEnum", "invariant", "getSchema", "log", "getSchemaProperties", "create", "S", "BaseGraphNode", "S", "Struct", "id", "String", "type", "optional", "data", "Any", "BaseGraphEdge", "source", "target", "Graph", "nodes", "mutable", "Array", "edges", "emptyGraph", "removeElements", "array", "predicate", "deleted", "i", "length", "push", "splice", "ReadonlyGraphModel", "constructor", "nodes", "edges", "_graph", "create", "Graph", "toJSON", "sort", "id", "a", "b", "localeCompare", "JSON", "parse", "stringify", "graph", "getNode", "find", "node", "getNodes", "type", "filter", "getEdge", "edge", "getEdges", "source", "target", "GraphModel", "clear", "length", "addGraph", "addNodes", "addEdges", "addGraphs", "graphs", "forEach", "addNode", "push", "addEdge", "removeNode", "removeElements", "removeNodes", "ids", "map", "removeEdge", "removeEdges", "createEdgeId", "source", "target", "relation", "parseEdgeId", "id", "split", "invariant", "createGraph", "objects", "graph", "GraphModel", "forEach", "object", "addNode", "type", "typename", "data", "schema", "getSchema", "log", "info", "slice", "prop", "getSchemaProperties", "ast", "format", "FormatEnum", "Ref", "name", "addEdge", "String"]
|
|
7
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"inputs":{"packages/common/graph/src/types.ts":{"bytes":3825,"imports":[{"path":"@dxos/echo-schema","kind":"import-statement","external":true}],"format":"esm"},"packages/common/graph/src/util.ts":{"bytes":1705,"imports":[],"format":"esm"},"packages/common/graph/src/graph.ts":{"bytes":12508,"imports":[{"path":"@dxos/live-object","kind":"import-statement","external":true},{"path":"packages/common/graph/src/types.ts","kind":"import-statement","original":"./types"},{"path":"packages/common/graph/src/util.ts","kind":"import-statement","original":"./util"}],"format":"esm"},"packages/common/graph/src/buidler.ts":{"bytes":7346,"imports":[{"path":"@dxos/echo-schema","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"@dxos/live-object","kind":"import-statement","external":true},{"path":"@dxos/log","kind":"import-statement","external":true},{"path":"@dxos/schema","kind":"import-statement","external":true},{"path":"packages/common/graph/src/graph.ts","kind":"import-statement","original":"./graph"}],"format":"esm"},"packages/common/graph/src/index.ts":{"bytes":654,"imports":[{"path":"packages/common/graph/src/buidler.ts","kind":"import-statement","original":"./buidler"},{"path":"packages/common/graph/src/graph.ts","kind":"import-statement","original":"./graph"},{"path":"packages/common/graph/src/types.ts","kind":"import-statement","original":"./types"}],"format":"esm"}},"outputs":{"packages/common/graph/dist/lib/node-esm/index.mjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":12613},"packages/common/graph/dist/lib/node-esm/index.mjs":{"imports":[{"path":"@dxos/echo-schema","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"@dxos/live-object","kind":"import-statement","external":true},{"path":"@dxos/log","kind":"import-statement","external":true},{"path":"@dxos/schema","kind":"import-statement","external":true},{"path":"@dxos/live-object","kind":"import-statement","external":true},{"path":"@dxos/echo-schema","kind":"import-statement","external":true}],"exports":["BaseGraphEdge","BaseGraphNode","Graph","GraphModel","ReadonlyGraphModel","createEdgeId","createGraph","emptyGraph","parseEdgeId"],"entryPoint":"packages/common/graph/src/index.ts","inputs":{"packages/common/graph/src/buidler.ts":{"bytesInOutput":1716},"packages/common/graph/src/graph.ts":{"bytesInOutput":2460},"packages/common/graph/src/types.ts":{"bytesInOutput":454},"packages/common/graph/src/util.ts":{"bytesInOutput":223},"packages/common/graph/src/index.ts":{"bytesInOutput":0}},"bytes":5367}}}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { type ReactiveEchoObject } from '@dxos/echo-db';
|
|
2
|
+
import { GraphModel } from './graph';
|
|
3
|
+
import { type GraphNode } from './types';
|
|
4
|
+
type Edge = {
|
|
5
|
+
source: string;
|
|
6
|
+
target: string;
|
|
7
|
+
relation: string;
|
|
8
|
+
};
|
|
9
|
+
export declare const createEdgeId: ({ source, target, relation }: Edge) => string;
|
|
10
|
+
export declare const parseEdgeId: (id: string) => Edge;
|
|
11
|
+
/**
|
|
12
|
+
* Maps an ECHO object graph onto a layout graph.
|
|
13
|
+
*/
|
|
14
|
+
export declare const createGraph: (objects: ReactiveEchoObject<any>[]) => GraphModel<GraphNode<ReactiveEchoObject<any>>>;
|
|
15
|
+
export {};
|
|
16
|
+
//# sourceMappingURL=buidler.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"buidler.d.ts","sourceRoot":"","sources":["../../../src/buidler.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,KAAK,kBAAkB,EAAE,MAAM,eAAe,CAAC;AAOxD,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AACrC,OAAO,EAAE,KAAK,SAAS,EAAE,MAAM,SAAS,CAAC;AAGzC,KAAK,IAAI,GAAG;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAA;CAAE,CAAC;AAEjE,eAAO,MAAM,YAAY,iCAAkC,IAAI,WAAsC,CAAC;AAEtG,eAAO,MAAM,WAAW,OAAQ,MAAM,KAAG,IAIxC,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,WAAW,YAAa,kBAAkB,CAAC,GAAG,CAAC,EAAE,KAAG,UAAU,CAAC,SAAS,CAAC,kBAAkB,CAAC,GAAG,CAAC,CAAC,CAiC7G,CAAC"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { Graph, type GraphNode, type GraphEdge } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Typed reactive object graph.
|
|
4
|
+
*/
|
|
5
|
+
export declare class ReadonlyGraphModel<Node extends GraphNode = any, Edge extends GraphEdge = any> {
|
|
6
|
+
protected readonly _graph: Graph;
|
|
7
|
+
constructor({ nodes, edges }?: Partial<Graph>);
|
|
8
|
+
toJSON(): any;
|
|
9
|
+
get graph(): Graph;
|
|
10
|
+
get nodes(): Node[];
|
|
11
|
+
get edges(): Edge[];
|
|
12
|
+
getNode(id: string): Node | undefined;
|
|
13
|
+
getNodes({ type }: Partial<Node>): Node[];
|
|
14
|
+
getEdge(id: string): Edge | undefined;
|
|
15
|
+
getEdges({ type, source, target }: Partial<Edge>): Edge[];
|
|
16
|
+
}
|
|
17
|
+
export declare class GraphModel<Node extends GraphNode = any, Edge extends GraphEdge = any> extends ReadonlyGraphModel<Node, Edge> {
|
|
18
|
+
clear(): this;
|
|
19
|
+
addGraph(graph: GraphModel<Node, Edge>): this;
|
|
20
|
+
addGraphs(graphs: GraphModel<Node, Edge>[]): this;
|
|
21
|
+
addNode(node: Node): this;
|
|
22
|
+
addNodes(nodes: Node[]): this;
|
|
23
|
+
addEdge(edge: Edge): this;
|
|
24
|
+
addEdges(edges: Edge[]): this;
|
|
25
|
+
removeNode(id: string): GraphModel<Node, Edge>;
|
|
26
|
+
removeNodes(ids: string[]): GraphModel<Node, Edge>;
|
|
27
|
+
removeEdge(id: string): GraphModel<Node, Edge>;
|
|
28
|
+
removeEdges(ids: string[]): GraphModel<Node, Edge>;
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=graph.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"graph.d.ts","sourceRoot":"","sources":["../../../src/graph.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,KAAK,EAAE,KAAK,SAAS,EAAE,KAAK,SAAS,EAAE,MAAM,SAAS,CAAC;AAKhE;;GAEG;AACH,qBAAa,kBAAkB,CAAC,IAAI,SAAS,SAAS,GAAG,GAAG,EAAE,IAAI,SAAS,SAAS,GAAG,GAAG;IACxF,SAAS,CAAC,QAAQ,CAAC,MAAM,EAAE,KAAK,CAAC;gBAErB,EAAE,KAAU,EAAE,KAAU,EAAE,GAAE,OAAO,CAAC,KAAK,CAAM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AAI,CAAC;IAK9C,WAAW,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,UAAU,CAAC,IAAI,EAAE,IAAI,CAAC;CAInD"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"graph.test.d.ts","sourceRoot":"","sources":["../../../src/graph.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AAIA,cAAc,WAAW,CAAC;AAC1B,cAAc,SAAS,CAAC;AACxB,cAAc,SAAS,CAAC"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { S } from '@dxos/echo-schema';
|
|
2
|
+
export declare const BaseGraphNode: S.Struct<{
|
|
3
|
+
id: typeof S.String;
|
|
4
|
+
type: S.optional<typeof S.String>;
|
|
5
|
+
data: S.optional<typeof S.Any>;
|
|
6
|
+
}>;
|
|
7
|
+
export type BaseGraphNode = S.Schema.Type<typeof BaseGraphNode>;
|
|
8
|
+
export type GraphNode<Data extends object | void = void> = BaseGraphNode & {
|
|
9
|
+
data: Data;
|
|
10
|
+
};
|
|
11
|
+
export declare const BaseGraphEdge: S.Struct<{
|
|
12
|
+
id: typeof S.String;
|
|
13
|
+
type: S.optional<typeof S.String>;
|
|
14
|
+
source: typeof S.String;
|
|
15
|
+
target: typeof S.String;
|
|
16
|
+
data: S.optional<typeof S.Any>;
|
|
17
|
+
}>;
|
|
18
|
+
export type BaseGraphEdge = S.Schema.Type<typeof BaseGraphEdge>;
|
|
19
|
+
export type GraphEdge<Data extends object | void = void> = BaseGraphEdge & {
|
|
20
|
+
data: Data;
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* Generic graph abstraction.
|
|
24
|
+
*/
|
|
25
|
+
export declare const Graph: S.Struct<{
|
|
26
|
+
nodes: S.mutable<S.Array$<S.Struct<{
|
|
27
|
+
id: typeof S.String;
|
|
28
|
+
type: S.optional<typeof S.String>;
|
|
29
|
+
data: S.optional<typeof S.Any>;
|
|
30
|
+
}>>>;
|
|
31
|
+
edges: S.mutable<S.Array$<S.Struct<{
|
|
32
|
+
id: typeof S.String;
|
|
33
|
+
type: S.optional<typeof S.String>;
|
|
34
|
+
source: typeof S.String;
|
|
35
|
+
target: typeof S.String;
|
|
36
|
+
data: S.optional<typeof S.Any>;
|
|
37
|
+
}>>>;
|
|
38
|
+
}>;
|
|
39
|
+
export type Graph = S.Schema.Type<typeof Graph>;
|
|
40
|
+
export declare const emptyGraph: Graph;
|
|
41
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/types.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,CAAC,EAAE,MAAM,mBAAmB,CAAC;AAOtC,eAAO,MAAM,aAAa;;;;EAIxB,CAAC;AAEH,MAAM,MAAM,aAAa,GAAG,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,aAAa,CAAC,CAAC;AAChE,MAAM,MAAM,SAAS,CAAC,IAAI,SAAS,MAAM,GAAG,IAAI,GAAG,IAAI,IAAI,aAAa,GAAG;IAAE,IAAI,EAAE,IAAI,CAAA;CAAE,CAAC;AAE1F,eAAO,MAAM,aAAa;;;;;;EAMxB,CAAC;AAEH,MAAM,MAAM,aAAa,GAAG,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,aAAa,CAAC,CAAC;AAChE,MAAM,MAAM,SAAS,CAAC,IAAI,SAAS,MAAM,GAAG,IAAI,GAAG,IAAI,IAAI,aAAa,GAAG;IAAE,IAAI,EAAE,IAAI,CAAA;CAAE,CAAC;AAE1F;;GAEG;AACH,eAAO,MAAM,KAAK;;;;;;;;;;;;;EAGhB,CAAC;AAEH,MAAM,MAAM,KAAK,GAAG,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,KAAK,CAAC,CAAC;AAEhD,eAAO,MAAM,UAAU,EAAE,KAAgC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"util.d.ts","sourceRoot":"","sources":["../../../src/util.ts"],"names":[],"mappings":"AAOA,eAAO,MAAM,cAAc,GAAI,CAAC,SAAS,CAAC,EAAE,aAAa,CAAC,OAAO,EAAE,CAAC,EAAE,KAAK,EAAE,MAAM,KAAK,OAAO,KAAG,CAAC,EASlG,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":"5.7.2"}
|
package/package.json
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@dxos/graph",
|
|
3
|
+
"version": "0.7.5-main.499c70c",
|
|
4
|
+
"description": "Low-level graph API",
|
|
5
|
+
"homepage": "https://dxos.org",
|
|
6
|
+
"bugs": "https://github.com/dxos/dxos/issues",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"author": "DXOS.org",
|
|
9
|
+
"sideEffects": true,
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./dist/types/src/index.d.ts",
|
|
13
|
+
"browser": "./dist/lib/browser/index.mjs",
|
|
14
|
+
"node": "./dist/lib/node-esm/index.mjs"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"types": "dist/types/src/index.d.ts",
|
|
18
|
+
"typesVersions": {
|
|
19
|
+
"*": {
|
|
20
|
+
"meta": [
|
|
21
|
+
"dist/types/src/meta.d.ts"
|
|
22
|
+
],
|
|
23
|
+
"types": [
|
|
24
|
+
"dist/types/src/types/index.d.ts"
|
|
25
|
+
]
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
"files": [
|
|
29
|
+
"dist",
|
|
30
|
+
"src"
|
|
31
|
+
],
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"@effect/schema": "^0.75.5",
|
|
34
|
+
"@preact/signals-core": "^1.6.0",
|
|
35
|
+
"graphology": "^0.25.4",
|
|
36
|
+
"graphology-shortest-path": "^2.1.0",
|
|
37
|
+
"graphology-types": "^0.24.8",
|
|
38
|
+
"lodash.defaultsdeep": "^4.6.1",
|
|
39
|
+
"@dxos/async": "0.7.5-main.499c70c",
|
|
40
|
+
"@dxos/echo-schema": "0.7.5-main.499c70c",
|
|
41
|
+
"@dxos/debug": "0.7.5-main.499c70c",
|
|
42
|
+
"@dxos/invariant": "0.7.5-main.499c70c",
|
|
43
|
+
"@dxos/live-object": "0.7.5-main.499c70c",
|
|
44
|
+
"@dxos/echo-db": "0.7.5-main.499c70c",
|
|
45
|
+
"@dxos/log": "0.7.5-main.499c70c",
|
|
46
|
+
"@dxos/schema": "0.7.5-main.499c70c",
|
|
47
|
+
"@dxos/util": "0.7.5-main.499c70c"
|
|
48
|
+
},
|
|
49
|
+
"devDependencies": {
|
|
50
|
+
"@antv/graphlib": "^2.0.4",
|
|
51
|
+
"@antv/layout": "^1.2.13",
|
|
52
|
+
"@types/lodash.defaultsdeep": "^4.6.6",
|
|
53
|
+
"@dxos/echo-db": "0.7.5-main.499c70c",
|
|
54
|
+
"@dxos/echo-schema": "0.7.5-main.499c70c",
|
|
55
|
+
"@dxos/random": "0.7.5-main.499c70c"
|
|
56
|
+
},
|
|
57
|
+
"publishConfig": {
|
|
58
|
+
"access": "public"
|
|
59
|
+
}
|
|
60
|
+
}
|
package/src/buidler.ts
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2023 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { type ReactiveEchoObject } from '@dxos/echo-db';
|
|
6
|
+
import { FormatEnum } from '@dxos/echo-schema';
|
|
7
|
+
import { invariant } from '@dxos/invariant';
|
|
8
|
+
import { getSchema } from '@dxos/live-object';
|
|
9
|
+
import { log } from '@dxos/log';
|
|
10
|
+
import { getSchemaProperties } from '@dxos/schema';
|
|
11
|
+
|
|
12
|
+
import { GraphModel } from './graph';
|
|
13
|
+
import { type GraphNode } from './types';
|
|
14
|
+
|
|
15
|
+
// NOTE: The `relation` is different from the `type`.
|
|
16
|
+
type Edge = { source: string; target: string; relation: string };
|
|
17
|
+
|
|
18
|
+
export const createEdgeId = ({ source, target, relation }: Edge) => `${source}-${relation}-${target}`;
|
|
19
|
+
|
|
20
|
+
export const parseEdgeId = (id: string): Edge => {
|
|
21
|
+
const [source, relation, target] = id.split('-');
|
|
22
|
+
invariant(source && target && relation);
|
|
23
|
+
return { source, relation, target };
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Maps an ECHO object graph onto a layout graph.
|
|
28
|
+
*/
|
|
29
|
+
export const createGraph = (objects: ReactiveEchoObject<any>[]): GraphModel<GraphNode<ReactiveEchoObject<any>>> => {
|
|
30
|
+
const graph = new GraphModel<GraphNode<ReactiveEchoObject<any>>>();
|
|
31
|
+
|
|
32
|
+
// Map objects.
|
|
33
|
+
objects.forEach((object) => {
|
|
34
|
+
graph.addNode({ id: object.id, type: object.typename, data: object });
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
// Find references.
|
|
38
|
+
objects.forEach((object) => {
|
|
39
|
+
const schema = getSchema(object);
|
|
40
|
+
if (!schema) {
|
|
41
|
+
log.info('no schema for object', { id: object.id.slice(0, 8) });
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Parse schema to follow referenced objects.
|
|
46
|
+
for (const prop of getSchemaProperties(schema.ast, object)) {
|
|
47
|
+
if (prop.format === FormatEnum.Ref) {
|
|
48
|
+
const source = object;
|
|
49
|
+
const target = object[prop.name]?.target;
|
|
50
|
+
if (target) {
|
|
51
|
+
graph.addEdge({
|
|
52
|
+
id: createEdgeId({ source: source.id, target: target.id, relation: String(prop.name) }),
|
|
53
|
+
source: source.id,
|
|
54
|
+
target: target.id,
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
return graph;
|
|
62
|
+
};
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { describe, test } from 'vitest';
|
|
6
|
+
|
|
7
|
+
import { createEdgeId } from './buidler';
|
|
8
|
+
import { GraphModel } from './graph';
|
|
9
|
+
import { type GraphNode } from './types';
|
|
10
|
+
|
|
11
|
+
type TestData = { value: string };
|
|
12
|
+
|
|
13
|
+
describe('Graph', () => {
|
|
14
|
+
test('empty', ({ expect }) => {
|
|
15
|
+
const { graph } = new GraphModel();
|
|
16
|
+
expect(graph.nodes).to.have.length(0);
|
|
17
|
+
expect(graph.edges).to.have.length(0);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
test('add and remove subgraphs', ({ expect }) => {
|
|
21
|
+
const graph = new GraphModel<GraphNode<TestData>>()
|
|
22
|
+
.addNode({ id: 'node-1', data: { value: 'test' } })
|
|
23
|
+
.addNode({ id: 'node-2', data: { value: 'test' } })
|
|
24
|
+
.addNode({ id: 'node-3', data: { value: 'test' } })
|
|
25
|
+
.addEdge({
|
|
26
|
+
id: createEdgeId({ source: 'node-1', target: 'node-2', relation: 'test' }),
|
|
27
|
+
source: 'node-1',
|
|
28
|
+
target: 'node-2',
|
|
29
|
+
})
|
|
30
|
+
.addEdge({
|
|
31
|
+
id: createEdgeId({ source: 'node-2', target: 'node-3', relation: 'test' }),
|
|
32
|
+
source: 'node-2',
|
|
33
|
+
target: 'node-3',
|
|
34
|
+
});
|
|
35
|
+
expect(graph.nodes).to.have.length(3);
|
|
36
|
+
expect(graph.edges).to.have.length(2);
|
|
37
|
+
const pre = graph.toJSON();
|
|
38
|
+
|
|
39
|
+
const node: GraphNode<TestData> = graph.getNode('node-2')!;
|
|
40
|
+
expect(node).to.exist;
|
|
41
|
+
|
|
42
|
+
const removed = graph.removeNode('node-2');
|
|
43
|
+
expect(removed.nodes).to.have.length(1);
|
|
44
|
+
expect(removed.edges).to.have.length(2);
|
|
45
|
+
expect(graph.nodes).to.have.length(2);
|
|
46
|
+
expect(graph.edges).to.have.length(0);
|
|
47
|
+
|
|
48
|
+
graph.addGraph(removed);
|
|
49
|
+
const post = graph.toJSON();
|
|
50
|
+
expect(pre).to.deep.eq(post);
|
|
51
|
+
|
|
52
|
+
graph.clear();
|
|
53
|
+
expect(graph.nodes).to.have.length(0);
|
|
54
|
+
expect(graph.edges).to.have.length(0);
|
|
55
|
+
});
|
|
56
|
+
});
|