@dxos/graph 0.7.5-main.ff8607b → 0.7.5-staging.2ff1350
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/README.md +9 -0
- package/dist/lib/browser/index.mjs +274 -107
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node/index.cjs +272 -101
- package/dist/lib/node/index.cjs.map +4 -4
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +274 -107
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/src/index.d.ts +2 -2
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/model.d.ts +107 -0
- package/dist/types/src/model.d.ts.map +1 -0
- package/dist/types/src/model.test.d.ts +2 -0
- package/dist/types/src/model.test.d.ts.map +1 -0
- package/dist/types/src/types.d.ts +28 -10
- package/dist/types/src/types.d.ts.map +1 -1
- package/dist/types/src/util.d.ts +16 -1
- package/dist/types/src/util.d.ts.map +1 -1
- package/package.json +14 -16
- package/src/index.ts +2 -2
- package/src/model.test.ts +120 -0
- package/src/model.ts +266 -0
- package/src/types.ts +31 -12
- package/src/util.ts +62 -10
- package/dist/types/src/buidler.d.ts +0 -16
- package/dist/types/src/buidler.d.ts.map +0 -1
- package/dist/types/src/graph.d.ts +0 -30
- package/dist/types/src/graph.d.ts.map +0 -1
- package/dist/types/src/graph.test.d.ts +0 -2
- package/dist/types/src/graph.test.d.ts.map +0 -1
- package/src/buidler.ts +0 -62
- package/src/graph.test.ts +0 -56
- package/src/graph.ts +0 -127
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { inspectCustom } from '@dxos/debug';
|
|
2
|
+
import { type MakeOptional } from '@dxos/util';
|
|
3
|
+
import { type Graph, type GraphNode, type GraphEdge, type BaseGraphNode, type BaseGraphEdge } from './types';
|
|
4
|
+
/**
|
|
5
|
+
* Wrapper class contains reactive nodes and edges.
|
|
6
|
+
*/
|
|
7
|
+
export declare class ReadonlyGraphModel<Node extends BaseGraphNode = BaseGraphNode, Edge extends BaseGraphEdge = BaseGraphEdge> {
|
|
8
|
+
protected readonly _graph: Graph;
|
|
9
|
+
/**
|
|
10
|
+
* NOTE: Pass in simple Graph or ReactiveObject.
|
|
11
|
+
*/
|
|
12
|
+
constructor(graph?: Graph);
|
|
13
|
+
[inspectCustom](): {
|
|
14
|
+
id: string | undefined;
|
|
15
|
+
nodes: ({
|
|
16
|
+
readonly id: string;
|
|
17
|
+
readonly type?: string | undefined;
|
|
18
|
+
readonly data?: any;
|
|
19
|
+
} & {
|
|
20
|
+
readonly [x: string]: any;
|
|
21
|
+
})[];
|
|
22
|
+
edges: {
|
|
23
|
+
readonly id: string;
|
|
24
|
+
readonly type?: string | undefined;
|
|
25
|
+
readonly data?: any;
|
|
26
|
+
readonly source: string;
|
|
27
|
+
readonly target: string;
|
|
28
|
+
}[];
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* Return stable sorted JSON representation of graph.
|
|
32
|
+
*/
|
|
33
|
+
toJSON(): {
|
|
34
|
+
id: string | undefined;
|
|
35
|
+
nodes: ({
|
|
36
|
+
readonly id: string;
|
|
37
|
+
readonly type?: string | undefined;
|
|
38
|
+
readonly data?: any;
|
|
39
|
+
} & {
|
|
40
|
+
readonly [x: string]: any;
|
|
41
|
+
})[];
|
|
42
|
+
edges: {
|
|
43
|
+
readonly id: string;
|
|
44
|
+
readonly type?: string | undefined;
|
|
45
|
+
readonly data?: any;
|
|
46
|
+
readonly source: string;
|
|
47
|
+
readonly target: string;
|
|
48
|
+
}[];
|
|
49
|
+
};
|
|
50
|
+
get graph(): Graph;
|
|
51
|
+
get nodes(): Node[];
|
|
52
|
+
get edges(): Edge[];
|
|
53
|
+
findNode(id: string): Node | undefined;
|
|
54
|
+
getNode(id: string): Node;
|
|
55
|
+
filterNodes({ type }?: Partial<GraphNode>): Node[];
|
|
56
|
+
findEdge(id: string): Edge | undefined;
|
|
57
|
+
getEdge(id: string): Edge;
|
|
58
|
+
filterEdges({ type, source, target }?: Partial<GraphEdge>): Edge[];
|
|
59
|
+
traverse(root: Node): Node[];
|
|
60
|
+
private _traverse;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Typed wrapper.
|
|
64
|
+
*/
|
|
65
|
+
export declare abstract class AbstractGraphModel<Node extends BaseGraphNode, Edge extends BaseGraphEdge, Model extends AbstractGraphModel<Node, Edge, Model, Builder>, Builder extends AbstractGraphBuilder<Node, Edge, Model>> extends ReadonlyGraphModel<Node, Edge> {
|
|
66
|
+
/**
|
|
67
|
+
* Allows chaining.
|
|
68
|
+
*/
|
|
69
|
+
abstract get builder(): Builder;
|
|
70
|
+
/**
|
|
71
|
+
* Shallow copy of provided graph.
|
|
72
|
+
*/
|
|
73
|
+
abstract copy(graph?: Partial<Graph>): Model;
|
|
74
|
+
clear(): this;
|
|
75
|
+
addGraph(graph: Model): this;
|
|
76
|
+
addGraphs(graphs: Model[]): this;
|
|
77
|
+
addNode(node: Node): Node;
|
|
78
|
+
addNodes(nodes: Node[]): Node[];
|
|
79
|
+
addEdge(edge: MakeOptional<Edge, 'id'>): Edge;
|
|
80
|
+
addEdges(edges: Edge[]): Edge[];
|
|
81
|
+
removeNode(id: string): Model;
|
|
82
|
+
removeNodes(ids: string[]): Model;
|
|
83
|
+
removeEdge(id: string): Model;
|
|
84
|
+
removeEdges(ids: string[]): Model;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Chainable wrapper
|
|
88
|
+
*/
|
|
89
|
+
export declare abstract class AbstractGraphBuilder<Node extends BaseGraphNode, Edge extends BaseGraphEdge, Model extends GraphModel<Node, Edge>> {
|
|
90
|
+
protected readonly _model: Model;
|
|
91
|
+
constructor(_model: Model);
|
|
92
|
+
get model(): Model;
|
|
93
|
+
call(cb: (builder: this) => void): this;
|
|
94
|
+
getNode(id: string): Node;
|
|
95
|
+
addNode(node: Node): this;
|
|
96
|
+
addEdge(edge: MakeOptional<Edge, 'id'>): this;
|
|
97
|
+
addNodes(nodes: Node[]): this;
|
|
98
|
+
addEdges(edges: Edge[]): this;
|
|
99
|
+
}
|
|
100
|
+
export declare class GraphModel<Node extends BaseGraphNode = BaseGraphNode, Edge extends BaseGraphEdge = BaseGraphEdge> extends AbstractGraphModel<Node, Edge, GraphModel<Node, Edge>, GraphBuilder<Node, Edge>> {
|
|
101
|
+
get builder(): GraphBuilder<Node, Edge>;
|
|
102
|
+
copy(graph?: Partial<Graph>): GraphModel<Node, Edge>;
|
|
103
|
+
}
|
|
104
|
+
export declare class GraphBuilder<Node extends BaseGraphNode = BaseGraphNode, Edge extends BaseGraphEdge = BaseGraphEdge> extends AbstractGraphBuilder<Node, Edge, GraphModel<Node, Edge>> {
|
|
105
|
+
call(cb: (builder: this) => void): this;
|
|
106
|
+
}
|
|
107
|
+
//# sourceMappingURL=model.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"model.d.ts","sourceRoot":"","sources":["../../../src/model.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAG5C,OAAO,EAAE,KAAK,YAAY,EAAwC,MAAM,YAAY,CAAC;AAErF,OAAO,EAAE,KAAK,KAAK,EAAE,KAAK,SAAS,EAAE,KAAK,SAAS,EAAE,KAAK,aAAa,EAAE,KAAK,aAAa,EAAE,MAAM,SAAS,CAAC;AAG7G;;GAEG;AACH,qBAAa,kBAAkB,CAC7B,IAAI,SAAS,aAAa,GAAG,aAAa,EAC1C,IAAI,SAAS,aAAa,GAAG,aAAa;IAE1C,SAAS,CAAC,QAAQ,CAAC,MAAM,EAAE,KAAK,CAAC;IAEjC;;OAEG;gBACS,KAAK,CAAC,EAAE,KAAK;IAIzB,CAAC,aAAa,CAAC;;;;;;;;;;;;;;;;;IAIf;;OAEG;IAEH,MAAM;;;;;;;;;;;;;;;;;IAON,IAAI,KAAK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Y,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;IACxF,IAAa,OAAO,6BAEnB;IAEQ,IAAI,CAAC,KAAK,CAAC,EAAE,OAAO,CAAC,KAAK,CAAC;CAGrC;AAED,qBAAa,YAAY,CACvB,IAAI,SAAS,aAAa,GAAG,aAAa,EAC1C,IAAI,SAAS,aAAa,GAAG,aAAa,CAC1C,SAAQ,oBAAoB,CAAC,IAAI,EAAE,IAAI,EAAE,UAAU,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;IACvD,IAAI,CAAC,EAAE,EAAE,CAAC,OAAO,EAAE,IAAI,KAAK,IAAI;CAI1C"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"model.test.d.ts","sourceRoot":"","sources":["../../../src/model.test.ts"],"names":[],"mappings":""}
|
|
@@ -1,41 +1,59 @@
|
|
|
1
1
|
import { S } from '@dxos/echo-schema';
|
|
2
|
+
import { type Specialize } from '@dxos/util';
|
|
2
3
|
export declare const BaseGraphNode: S.Struct<{
|
|
3
4
|
id: typeof S.String;
|
|
4
5
|
type: S.optional<typeof S.String>;
|
|
5
6
|
data: S.optional<typeof S.Any>;
|
|
6
7
|
}>;
|
|
8
|
+
/** Raw base type. */
|
|
7
9
|
export type BaseGraphNode = S.Schema.Type<typeof BaseGraphNode>;
|
|
8
|
-
|
|
10
|
+
/** Typed node data. */
|
|
11
|
+
export type GraphNode<Data = any, Optional extends boolean = false> = Specialize<BaseGraphNode, Optional extends true ? {
|
|
12
|
+
data?: Data;
|
|
13
|
+
} : {
|
|
9
14
|
data: Data;
|
|
10
|
-
}
|
|
15
|
+
}>;
|
|
16
|
+
export declare namespace GraphNode {
|
|
17
|
+
type Optional<T = any> = GraphNode<T, true>;
|
|
18
|
+
}
|
|
11
19
|
export declare const BaseGraphEdge: S.Struct<{
|
|
12
20
|
id: typeof S.String;
|
|
13
21
|
type: S.optional<typeof S.String>;
|
|
22
|
+
data: S.optional<typeof S.Any>;
|
|
14
23
|
source: typeof S.String;
|
|
15
24
|
target: typeof S.String;
|
|
16
|
-
data: S.optional<typeof S.Any>;
|
|
17
25
|
}>;
|
|
26
|
+
/** Raw base type. */
|
|
18
27
|
export type BaseGraphEdge = S.Schema.Type<typeof BaseGraphEdge>;
|
|
19
|
-
|
|
28
|
+
/** Typed edge data. */
|
|
29
|
+
export type GraphEdge<Data = any, Optional extends boolean = false> = Specialize<BaseGraphEdge, Optional extends true ? {
|
|
30
|
+
data?: Data;
|
|
31
|
+
} : {
|
|
20
32
|
data: Data;
|
|
21
|
-
}
|
|
33
|
+
}>;
|
|
34
|
+
export declare namespace GraphEdge {
|
|
35
|
+
type Optional<T = any> = GraphEdge<T, true>;
|
|
36
|
+
}
|
|
22
37
|
/**
|
|
23
|
-
* Generic graph
|
|
38
|
+
* Generic graph.
|
|
24
39
|
*/
|
|
25
40
|
export declare const Graph: S.Struct<{
|
|
26
|
-
|
|
41
|
+
id: S.optional<typeof S.String>;
|
|
42
|
+
nodes: S.mutable<S.Array$<S.extend<S.Struct<{
|
|
27
43
|
id: typeof S.String;
|
|
28
44
|
type: S.optional<typeof S.String>;
|
|
29
45
|
data: S.optional<typeof S.Any>;
|
|
30
|
-
}
|
|
46
|
+
}>, S.TypeLiteral<{}, readonly [{
|
|
47
|
+
readonly key: typeof S.String;
|
|
48
|
+
readonly value: typeof S.Any;
|
|
49
|
+
}]>>>>;
|
|
31
50
|
edges: S.mutable<S.Array$<S.Struct<{
|
|
32
51
|
id: typeof S.String;
|
|
33
52
|
type: S.optional<typeof S.String>;
|
|
53
|
+
data: S.optional<typeof S.Any>;
|
|
34
54
|
source: typeof S.String;
|
|
35
55
|
target: typeof S.String;
|
|
36
|
-
data: S.optional<typeof S.Any>;
|
|
37
56
|
}>>>;
|
|
38
57
|
}>;
|
|
39
58
|
export type Graph = S.Schema.Type<typeof Graph>;
|
|
40
|
-
export declare const emptyGraph: Graph;
|
|
41
59
|
//# sourceMappingURL=types.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/types.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,CAAC,EAAE,MAAM,mBAAmB,CAAC;
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/types.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,CAAC,EAAE,MAAM,mBAAmB,CAAC;AACtC,OAAO,EAAE,KAAK,UAAU,EAAE,MAAM,YAAY,CAAC;AAE7C,eAAO,MAAM,aAAa;;;;EAIxB,CAAC;AAEH,qBAAqB;AACrB,MAAM,MAAM,aAAa,GAAG,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,aAAa,CAAC,CAAC;AAChE,uBAAuB;AACvB,MAAM,MAAM,SAAS,CAAC,IAAI,GAAG,GAAG,EAAE,QAAQ,SAAS,OAAO,GAAG,KAAK,IAAI,UAAU,CAC9E,aAAa,EACb,QAAQ,SAAS,IAAI,GAAG;IAAE,IAAI,CAAC,EAAE,IAAI,CAAA;CAAE,GAAG;IAAE,IAAI,EAAE,IAAI,CAAA;CAAE,CACzD,CAAC;AAEF,MAAM,CAAC,OAAO,WAAW,SAAS,CAAC;IACjC,KAAY,QAAQ,CAAC,CAAC,GAAG,GAAG,IAAI,SAAS,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC;CACpD;AAED,eAAO,MAAM,aAAa;;;;;;EAMxB,CAAC;AAEH,qBAAqB;AACrB,MAAM,MAAM,aAAa,GAAG,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,aAAa,CAAC,CAAC;AAEhE,uBAAuB;AACvB,MAAM,MAAM,SAAS,CAAC,IAAI,GAAG,GAAG,EAAE,QAAQ,SAAS,OAAO,GAAG,KAAK,IAAI,UAAU,CAC9E,aAAa,EACb,QAAQ,SAAS,IAAI,GAAG;IAAE,IAAI,CAAC,EAAE,IAAI,CAAA;CAAE,GAAG;IAAE,IAAI,EAAE,IAAI,CAAA;CAAE,CACzD,CAAC;AAEF,MAAM,CAAC,OAAO,WAAW,SAAS,CAAC;IACjC,KAAY,QAAQ,CAAC,CAAC,GAAG,GAAG,IAAI,SAAS,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC;CACpD;AAOD;;GAEG;AACH,eAAO,MAAM,KAAK;;;;;;;;;;;;;;;;;EAIhB,CAAC;AAEH,MAAM,MAAM,KAAK,GAAG,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,KAAK,CAAC,CAAC"}
|
package/dist/types/src/util.d.ts
CHANGED
|
@@ -1,2 +1,17 @@
|
|
|
1
|
-
|
|
1
|
+
import { type ReactiveEchoObject } from '@dxos/echo-db';
|
|
2
|
+
import { GraphModel } from './model';
|
|
3
|
+
import { type GraphNode } from './types';
|
|
4
|
+
type EdgeMeta = {
|
|
5
|
+
source: string;
|
|
6
|
+
target: string;
|
|
7
|
+
relation?: string;
|
|
8
|
+
};
|
|
9
|
+
export declare const createEdgeId: ({ source, target, relation }: EdgeMeta) => string;
|
|
10
|
+
export declare const parseEdgeId: (id: string) => EdgeMeta;
|
|
11
|
+
/**
|
|
12
|
+
* Creates a new reactive graph from a set of ECHO objects.
|
|
13
|
+
* References are mapped onto graph edges.
|
|
14
|
+
*/
|
|
15
|
+
export declare const createGraph: (objects: ReactiveEchoObject<any>[]) => GraphModel<GraphNode<ReactiveEchoObject<any>>>;
|
|
16
|
+
export {};
|
|
2
17
|
//# sourceMappingURL=util.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"util.d.ts","sourceRoot":"","sources":["../../../src/util.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"util.d.ts","sourceRoot":"","sources":["../../../src/util.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,KAAK,kBAAkB,EAAE,MAAM,eAAe,CAAC;AAOxD,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AACrC,OAAO,EAAS,KAAK,SAAS,EAAE,MAAM,SAAS,CAAC;AAKhD,KAAK,QAAQ,GAAG;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,EAAE,MAAM,CAAA;CAAE,CAAC;AAEtE,eAAO,MAAM,YAAY,iCAAkC,QAAQ,WAIlE,CAAC;AAEF,eAAO,MAAM,WAAW,OAAQ,MAAM,KAAG,QAIxC,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,WAAW,YAAa,kBAAkB,CAAC,GAAG,CAAC,EAAE,KAAG,UAAU,CAAC,SAAS,CAAC,kBAAkB,CAAC,GAAG,CAAC,CAAC,CAiC7G,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/graph",
|
|
3
|
-
"version": "0.7.5-
|
|
3
|
+
"version": "0.7.5-staging.2ff1350",
|
|
4
4
|
"description": "Low-level graph API",
|
|
5
5
|
"homepage": "https://dxos.org",
|
|
6
6
|
"bugs": "https://github.com/dxos/dxos/issues",
|
|
7
7
|
"license": "MIT",
|
|
8
8
|
"author": "DXOS.org",
|
|
9
9
|
"sideEffects": true,
|
|
10
|
+
"type": "module",
|
|
10
11
|
"exports": {
|
|
11
12
|
".": {
|
|
12
13
|
"types": "./dist/types/src/index.d.ts",
|
|
@@ -32,27 +33,24 @@
|
|
|
32
33
|
"dependencies": {
|
|
33
34
|
"@effect/schema": "^0.75.5",
|
|
34
35
|
"@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
36
|
"lodash.defaultsdeep": "^4.6.1",
|
|
39
|
-
"@dxos/
|
|
40
|
-
"@dxos/
|
|
41
|
-
"@dxos/echo-db": "0.7.5-
|
|
42
|
-
"@dxos/echo-schema": "0.7.5-
|
|
43
|
-
"@dxos/live-object": "0.7.5-
|
|
44
|
-
"@dxos/log": "0.7.5-
|
|
45
|
-
"@dxos/
|
|
46
|
-
"@dxos/
|
|
47
|
-
"@dxos/util": "0.7.5-
|
|
37
|
+
"@dxos/async": "0.7.5-staging.2ff1350",
|
|
38
|
+
"@dxos/debug": "0.7.5-staging.2ff1350",
|
|
39
|
+
"@dxos/echo-db": "0.7.5-staging.2ff1350",
|
|
40
|
+
"@dxos/echo-schema": "0.7.5-staging.2ff1350",
|
|
41
|
+
"@dxos/live-object": "0.7.5-staging.2ff1350",
|
|
42
|
+
"@dxos/log": "0.7.5-staging.2ff1350",
|
|
43
|
+
"@dxos/schema": "0.7.5-staging.2ff1350",
|
|
44
|
+
"@dxos/invariant": "0.7.5-staging.2ff1350",
|
|
45
|
+
"@dxos/util": "0.7.5-staging.2ff1350"
|
|
48
46
|
},
|
|
49
47
|
"devDependencies": {
|
|
50
48
|
"@antv/graphlib": "^2.0.4",
|
|
51
49
|
"@antv/layout": "^1.2.13",
|
|
52
50
|
"@types/lodash.defaultsdeep": "^4.6.6",
|
|
53
|
-
"@dxos/echo-
|
|
54
|
-
"@dxos/
|
|
55
|
-
"@dxos/
|
|
51
|
+
"@dxos/echo-schema": "0.7.5-staging.2ff1350",
|
|
52
|
+
"@dxos/echo-db": "0.7.5-staging.2ff1350",
|
|
53
|
+
"@dxos/random": "0.7.5-staging.2ff1350"
|
|
56
54
|
},
|
|
57
55
|
"publishConfig": {
|
|
58
56
|
"access": "public"
|
package/src/index.ts
CHANGED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { describe, test } from 'vitest';
|
|
6
|
+
|
|
7
|
+
import { S } from '@dxos/echo-schema';
|
|
8
|
+
|
|
9
|
+
import { GraphModel } from './model';
|
|
10
|
+
import { BaseGraphNode, type GraphNode } from './types';
|
|
11
|
+
|
|
12
|
+
const TestNode = S.extend(
|
|
13
|
+
BaseGraphNode,
|
|
14
|
+
S.Struct({
|
|
15
|
+
value: S.String,
|
|
16
|
+
}),
|
|
17
|
+
);
|
|
18
|
+
|
|
19
|
+
type TestNode = S.Schema.Type<typeof TestNode>;
|
|
20
|
+
|
|
21
|
+
type TestData = { value: string };
|
|
22
|
+
|
|
23
|
+
describe('Graph', () => {
|
|
24
|
+
test('empty', ({ expect }) => {
|
|
25
|
+
const graph = new GraphModel();
|
|
26
|
+
expect(graph.nodes).to.have.length(0);
|
|
27
|
+
expect(graph.edges).to.have.length(0);
|
|
28
|
+
expect(graph.toJSON()).to.deep.eq({ nodes: [], edges: [] });
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
test('extended', ({ expect }) => {
|
|
32
|
+
const graph = new GraphModel<TestNode>();
|
|
33
|
+
const node = graph.addNode({ id: 'test', value: 'test' });
|
|
34
|
+
expect(node.value.length).to.eq(4);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
test('optional', ({ expect }) => {
|
|
38
|
+
{
|
|
39
|
+
const graph = new GraphModel<GraphNode<string>>();
|
|
40
|
+
const node = graph.addNode({ id: 'test', data: 'test' });
|
|
41
|
+
expect(node.data.length).to.eq(4);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
{
|
|
45
|
+
const graph = new GraphModel<GraphNode.Optional<string>>();
|
|
46
|
+
const node = graph.addNode({ id: 'test' });
|
|
47
|
+
expect(node.data?.length).to.be.undefined;
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
test('add and remove subgraphs', ({ expect }) => {
|
|
52
|
+
const graph = new GraphModel<GraphNode<TestData>>();
|
|
53
|
+
graph.builder
|
|
54
|
+
.addNode({ id: 'node1', data: { value: 'test' } })
|
|
55
|
+
.addNode({ id: 'node2', data: { value: 'test' } })
|
|
56
|
+
.addNode({ id: 'node3', data: { value: 'test' } })
|
|
57
|
+
.addEdge({ source: 'node1', target: 'node2' })
|
|
58
|
+
.addEdge({ source: 'node2', target: 'node3' });
|
|
59
|
+
expect(graph.nodes).to.have.length(3);
|
|
60
|
+
expect(graph.edges).to.have.length(2);
|
|
61
|
+
const pre = graph.toJSON();
|
|
62
|
+
|
|
63
|
+
const node = graph.findNode('node2');
|
|
64
|
+
expect(node).to.exist;
|
|
65
|
+
|
|
66
|
+
const removed = graph.removeNode('node2');
|
|
67
|
+
expect(removed.nodes).to.have.length(1);
|
|
68
|
+
expect(removed.edges).to.have.length(2);
|
|
69
|
+
expect(graph.nodes).to.have.length(2);
|
|
70
|
+
expect(graph.edges).to.have.length(0);
|
|
71
|
+
|
|
72
|
+
graph.addGraph(removed);
|
|
73
|
+
const post = graph.toJSON();
|
|
74
|
+
expect(pre).to.deep.eq(post);
|
|
75
|
+
|
|
76
|
+
graph.clear();
|
|
77
|
+
expect(graph.nodes).to.have.length(0);
|
|
78
|
+
expect(graph.edges).to.have.length(0);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
test('traverse', ({ expect }) => {
|
|
82
|
+
const graph = new GraphModel();
|
|
83
|
+
graph.builder
|
|
84
|
+
.addNode({ id: 'a' })
|
|
85
|
+
.addNode({ id: 'b' })
|
|
86
|
+
.addNode({ id: 'c' })
|
|
87
|
+
.addNode({ id: 'd' })
|
|
88
|
+
.addNode({ id: 'e' })
|
|
89
|
+
.addNode({ id: 'f' })
|
|
90
|
+
.addNode({ id: 'g' })
|
|
91
|
+
.addNode({ id: 'h' })
|
|
92
|
+
// Sub-graph 1.
|
|
93
|
+
.addEdge({ source: 'a', target: 'b' })
|
|
94
|
+
.addEdge({ source: 'a', target: 'c' })
|
|
95
|
+
.addEdge({ source: 'c', target: 'd' })
|
|
96
|
+
.addEdge({ source: 'd', target: 'e' })
|
|
97
|
+
.addEdge({ source: 'd', target: 'a' })
|
|
98
|
+
// Sub-graph 2.
|
|
99
|
+
.addEdge({ source: 'f', target: 'g' })
|
|
100
|
+
.addEdge({ source: 'g', target: 'h' });
|
|
101
|
+
|
|
102
|
+
const count = graph.nodes.length;
|
|
103
|
+
|
|
104
|
+
{
|
|
105
|
+
// Sub-graph 1.
|
|
106
|
+
const nodes = graph.traverse(graph.getNode('a'));
|
|
107
|
+
expect(nodes).to.have.length(5);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
{
|
|
111
|
+
// Sub-graph 2.
|
|
112
|
+
const nodes = graph.traverse(graph.getNode('f'));
|
|
113
|
+
expect(nodes).to.have.length(3);
|
|
114
|
+
|
|
115
|
+
// Remove sub-graph.
|
|
116
|
+
graph.removeNodes(nodes.map((node) => node.id));
|
|
117
|
+
expect(graph.nodes).to.have.length(count - 3);
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
});
|
package/src/model.ts
ADDED
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { inspectCustom } from '@dxos/debug';
|
|
6
|
+
import { failedInvariant, invariant } from '@dxos/invariant';
|
|
7
|
+
import { getSnapshot } from '@dxos/live-object';
|
|
8
|
+
import { type MakeOptional, isNotFalsy, removeBy, stripUndefined } from '@dxos/util';
|
|
9
|
+
|
|
10
|
+
import { type Graph, type GraphNode, type GraphEdge, type BaseGraphNode, type BaseGraphEdge } from './types';
|
|
11
|
+
import { createEdgeId } from './util';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Wrapper class contains reactive nodes and edges.
|
|
15
|
+
*/
|
|
16
|
+
export class ReadonlyGraphModel<
|
|
17
|
+
Node extends BaseGraphNode = BaseGraphNode,
|
|
18
|
+
Edge extends BaseGraphEdge = BaseGraphEdge,
|
|
19
|
+
> {
|
|
20
|
+
protected readonly _graph: Graph;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* NOTE: Pass in simple Graph or ReactiveObject.
|
|
24
|
+
*/
|
|
25
|
+
constructor(graph?: Graph) {
|
|
26
|
+
this._graph = graph ?? { nodes: [], edges: [] };
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
[inspectCustom]() {
|
|
30
|
+
return this.toJSON();
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Return stable sorted JSON representation of graph.
|
|
35
|
+
*/
|
|
36
|
+
// TODO(burdon): Create separate toJson method with computed signal.
|
|
37
|
+
toJSON() {
|
|
38
|
+
const { id, nodes, edges } = getSnapshot(this._graph);
|
|
39
|
+
nodes.sort(({ id: a }, { id: b }) => a.localeCompare(b));
|
|
40
|
+
edges.sort(({ id: a }, { id: b }) => a.localeCompare(b));
|
|
41
|
+
return stripUndefined({ id, nodes, edges });
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
get graph(): Graph {
|
|
45
|
+
return this._graph;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
get nodes(): Node[] {
|
|
49
|
+
return this._graph.nodes as Node[];
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
get edges(): Edge[] {
|
|
53
|
+
return this._graph.edges as Edge[];
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
//
|
|
57
|
+
// Nodes
|
|
58
|
+
//
|
|
59
|
+
|
|
60
|
+
findNode(id: string): Node | undefined {
|
|
61
|
+
return this.nodes.find((node) => node.id === id);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
getNode(id: string): Node {
|
|
65
|
+
return this.findNode(id) ?? failedInvariant();
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
filterNodes({ type }: Partial<GraphNode> = {}): Node[] {
|
|
69
|
+
return this.nodes.filter((node) => !type || type === node.type);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
//
|
|
73
|
+
// Edges
|
|
74
|
+
//
|
|
75
|
+
|
|
76
|
+
findEdge(id: string): Edge | undefined {
|
|
77
|
+
return this.edges.find((edge) => edge.id === id);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
getEdge(id: string): Edge {
|
|
81
|
+
return this.findEdge(id) ?? failedInvariant();
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
filterEdges({ type, source, target }: Partial<GraphEdge> = {}): Edge[] {
|
|
85
|
+
return this.edges.filter(
|
|
86
|
+
(edge) =>
|
|
87
|
+
(!type || type === edge.type) && (!source || source === edge.source) && (!target || target === edge.target),
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
//
|
|
92
|
+
// Traverse
|
|
93
|
+
//
|
|
94
|
+
|
|
95
|
+
traverse(root: Node): Node[] {
|
|
96
|
+
return this._traverse(root);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
private _traverse(root: Node, visited: Set<string> = new Set()): Node[] {
|
|
100
|
+
if (visited.has(root.id)) {
|
|
101
|
+
return [];
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
visited.add(root.id);
|
|
105
|
+
const targets = this.filterEdges({ source: root.id })
|
|
106
|
+
.map((edge) => this.getNode(edge.target))
|
|
107
|
+
.filter(isNotFalsy);
|
|
108
|
+
|
|
109
|
+
return [root, ...targets.flatMap((target) => this._traverse(target, visited))];
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Typed wrapper.
|
|
115
|
+
*/
|
|
116
|
+
export abstract class AbstractGraphModel<
|
|
117
|
+
Node extends BaseGraphNode,
|
|
118
|
+
Edge extends BaseGraphEdge,
|
|
119
|
+
Model extends AbstractGraphModel<Node, Edge, Model, Builder>,
|
|
120
|
+
Builder extends AbstractGraphBuilder<Node, Edge, Model>,
|
|
121
|
+
> extends ReadonlyGraphModel<Node, Edge> {
|
|
122
|
+
/**
|
|
123
|
+
* Allows chaining.
|
|
124
|
+
*/
|
|
125
|
+
abstract get builder(): Builder;
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Shallow copy of provided graph.
|
|
129
|
+
*/
|
|
130
|
+
abstract copy(graph?: Partial<Graph>): Model;
|
|
131
|
+
|
|
132
|
+
clear(): this {
|
|
133
|
+
this._graph.nodes.length = 0;
|
|
134
|
+
this._graph.edges.length = 0;
|
|
135
|
+
return this;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
addGraph(graph: Model): this {
|
|
139
|
+
this.addNodes(graph.nodes);
|
|
140
|
+
this.addEdges(graph.edges);
|
|
141
|
+
return this;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
addGraphs(graphs: Model[]): this {
|
|
145
|
+
graphs.forEach((graph) => {
|
|
146
|
+
this.addNodes(graph.nodes);
|
|
147
|
+
this.addEdges(graph.edges);
|
|
148
|
+
});
|
|
149
|
+
return this;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
addNode(node: Node): Node {
|
|
153
|
+
invariant(node.id, 'ID is required');
|
|
154
|
+
invariant(!this.findNode(node.id), `node already exists: ${node.id}`);
|
|
155
|
+
this._graph.nodes.push(node);
|
|
156
|
+
return node;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
addNodes(nodes: Node[]): Node[] {
|
|
160
|
+
return nodes.map((node) => this.addNode(node));
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
addEdge(edge: MakeOptional<Edge, 'id'>): Edge {
|
|
164
|
+
invariant(edge.source);
|
|
165
|
+
invariant(edge.target);
|
|
166
|
+
if (!edge.id) {
|
|
167
|
+
// TODO(burdon): Generate random id.
|
|
168
|
+
edge = { id: createEdgeId(edge), ...edge };
|
|
169
|
+
}
|
|
170
|
+
invariant(!this.findNode(edge.id!));
|
|
171
|
+
this._graph.edges.push(edge as Edge);
|
|
172
|
+
return edge as Edge;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
addEdges(edges: Edge[]): Edge[] {
|
|
176
|
+
return edges.map((edge) => this.addEdge(edge));
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
removeNode(id: string): Model {
|
|
180
|
+
const nodes = removeBy<Node>(this._graph.nodes as Node[], (node) => node.id === id);
|
|
181
|
+
const edges = removeBy<Edge>(this._graph.edges as Edge[], (edge) => edge.source === id || edge.target === id);
|
|
182
|
+
return this.copy({ nodes, edges });
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
removeNodes(ids: string[]): Model {
|
|
186
|
+
const graphs = ids.map((id) => this.removeNode(id));
|
|
187
|
+
return this.copy().addGraphs(graphs);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
removeEdge(id: string): Model {
|
|
191
|
+
const edges = removeBy<Edge>(this._graph.edges as Edge[], (edge) => edge.id === id);
|
|
192
|
+
return this.copy({ nodes: [], edges });
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
removeEdges(ids: string[]): Model {
|
|
196
|
+
const graphs = ids.map((id) => this.removeEdge(id));
|
|
197
|
+
return this.copy().addGraphs(graphs);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Chainable wrapper
|
|
203
|
+
*/
|
|
204
|
+
export abstract class AbstractGraphBuilder<
|
|
205
|
+
Node extends BaseGraphNode,
|
|
206
|
+
Edge extends BaseGraphEdge,
|
|
207
|
+
Model extends GraphModel<Node, Edge>,
|
|
208
|
+
> {
|
|
209
|
+
constructor(protected readonly _model: Model) {}
|
|
210
|
+
|
|
211
|
+
get model(): Model {
|
|
212
|
+
return this._model;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
call(cb: (builder: this) => void): this {
|
|
216
|
+
cb(this);
|
|
217
|
+
return this;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
getNode(id: string): Node {
|
|
221
|
+
return this.model.getNode(id);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
addNode(node: Node): this {
|
|
225
|
+
this._model.addNode(node);
|
|
226
|
+
return this;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
addEdge(edge: MakeOptional<Edge, 'id'>): this {
|
|
230
|
+
this._model.addEdge(edge);
|
|
231
|
+
return this;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
addNodes(nodes: Node[]): this {
|
|
235
|
+
this._model.addNodes(nodes);
|
|
236
|
+
return this;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
addEdges(edges: Edge[]): this {
|
|
240
|
+
this._model.addEdges(edges);
|
|
241
|
+
return this;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
export class GraphModel<
|
|
246
|
+
Node extends BaseGraphNode = BaseGraphNode,
|
|
247
|
+
Edge extends BaseGraphEdge = BaseGraphEdge,
|
|
248
|
+
> extends AbstractGraphModel<Node, Edge, GraphModel<Node, Edge>, GraphBuilder<Node, Edge>> {
|
|
249
|
+
override get builder() {
|
|
250
|
+
return new GraphBuilder<Node, Edge>(this);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
override copy(graph?: Partial<Graph>) {
|
|
254
|
+
return new GraphModel<Node, Edge>({ nodes: graph?.nodes ?? [], edges: graph?.edges ?? [] });
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
export class GraphBuilder<
|
|
259
|
+
Node extends BaseGraphNode = BaseGraphNode,
|
|
260
|
+
Edge extends BaseGraphEdge = BaseGraphEdge,
|
|
261
|
+
> extends AbstractGraphBuilder<Node, Edge, GraphModel<Node, Edge>> {
|
|
262
|
+
override call(cb: (builder: this) => void) {
|
|
263
|
+
cb(this);
|
|
264
|
+
return this;
|
|
265
|
+
}
|
|
266
|
+
}
|