@ts-graphviz/core 0.0.0-pr956-20240225073457 → 0.0.0-pr956-20240225091623
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/CHANGELOG.md +3 -3
- package/package.json +39 -16
- package/src/attribute.ts +0 -18
- package/src/core.ts +0 -5
- package/src/from-dot.ts +0 -73
- package/src/model-factory/index.ts +0 -2
- package/src/model-factory/model-factory-builder.test.ts +0 -79
- package/src/model-factory/model-factory-builder.ts +0 -55
- package/src/model-factory/model-factory.test.ts +0 -61
- package/src/model-factory/model-factory.ts +0 -40
- package/src/model-factory/types.ts +0 -46
- package/src/models/AttributeList.spec.ts +0 -58
- package/src/models/AttributeList.ts +0 -32
- package/src/models/AttributesBase.spec.ts +0 -79
- package/src/models/AttributesBase.ts +0 -62
- package/src/models/AttributesGroup.spec.ts +0 -18
- package/src/models/AttributesGroup.ts +0 -13
- package/src/models/Digraph.spec.ts +0 -17
- package/src/models/Digraph.ts +0 -11
- package/src/models/DotObject.ts +0 -5
- package/src/models/Edge.spec.ts +0 -48
- package/src/models/Edge.ts +0 -40
- package/src/models/Graph.spec.ts +0 -18
- package/src/models/Graph.ts +0 -11
- package/src/models/GraphBase.spec.ts +0 -364
- package/src/models/GraphBase.ts +0 -263
- package/src/models/Node.spec.ts +0 -25
- package/src/models/Node.ts +0 -37
- package/src/models/RootGraph.spec.ts +0 -69
- package/src/models/RootGraph.ts +0 -48
- package/src/models/Subgraph.spec.ts +0 -196
- package/src/models/Subgraph.ts +0 -44
- package/src/models/index.ts +0 -15
- package/src/models/registerModelContext.ts +0 -14
- package/src/to-dot.ts +0 -36
- package/tsconfig.json +0 -8
- package/typedoc.json +0 -4
- package/vite.config.ts +0 -22
package/src/models/GraphBase.ts
DELETED
|
@@ -1,263 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
AttributeKey,
|
|
3
|
-
ClusterSubgraphAttributeKey,
|
|
4
|
-
EdgeAttributeKey,
|
|
5
|
-
EdgeAttributesObject,
|
|
6
|
-
EdgeModel,
|
|
7
|
-
EdgeTargetLikeTuple,
|
|
8
|
-
EdgeTargetTuple,
|
|
9
|
-
GraphBaseModel,
|
|
10
|
-
GraphCommonAttributes,
|
|
11
|
-
ModelsContext,
|
|
12
|
-
NodeAttributeKey,
|
|
13
|
-
NodeAttributesObject,
|
|
14
|
-
NodeModel,
|
|
15
|
-
RootModelsContext,
|
|
16
|
-
SubgraphAttributeKey,
|
|
17
|
-
SubgraphAttributesObject,
|
|
18
|
-
SubgraphModel,
|
|
19
|
-
createModelsContext,
|
|
20
|
-
isNodeRefGroupLike,
|
|
21
|
-
toNodeRef,
|
|
22
|
-
toNodeRefGroup,
|
|
23
|
-
} from '@ts-graphviz/common';
|
|
24
|
-
import { AttributeList } from './AttributeList.js';
|
|
25
|
-
import { AttributesBase } from './AttributesBase.js';
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Base class for Graph objects.
|
|
29
|
-
* @group Models
|
|
30
|
-
*/
|
|
31
|
-
export abstract class GraphBase<T extends AttributeKey>
|
|
32
|
-
extends AttributesBase<T>
|
|
33
|
-
implements GraphBaseModel<T>
|
|
34
|
-
{
|
|
35
|
-
/** @hidden */
|
|
36
|
-
#models = RootModelsContext;
|
|
37
|
-
|
|
38
|
-
public readonly id?: string;
|
|
39
|
-
|
|
40
|
-
public comment?: string;
|
|
41
|
-
|
|
42
|
-
public readonly attributes: Readonly<GraphCommonAttributes> = Object.freeze({
|
|
43
|
-
graph: new AttributeList<
|
|
44
|
-
'Graph',
|
|
45
|
-
SubgraphAttributeKey | ClusterSubgraphAttributeKey
|
|
46
|
-
>('Graph'),
|
|
47
|
-
edge: new AttributeList<'Edge', EdgeAttributeKey>('Edge'),
|
|
48
|
-
node: new AttributeList<'Node', NodeAttributeKey>('Node'),
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
get nodes(): ReadonlyArray<NodeModel> {
|
|
52
|
-
return Array.from(this.#objects.nodes.values());
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
get edges(): ReadonlyArray<EdgeModel> {
|
|
56
|
-
return Array.from(this.#objects.edges.values());
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
get subgraphs(): ReadonlyArray<SubgraphModel> {
|
|
60
|
-
return Array.from(this.#objects.subgraphs.values());
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/** @hidden */
|
|
64
|
-
#objects: Readonly<{
|
|
65
|
-
nodes: Map<string, NodeModel>;
|
|
66
|
-
edges: Set<EdgeModel>;
|
|
67
|
-
subgraphs: Set<SubgraphModel>;
|
|
68
|
-
}> = {
|
|
69
|
-
nodes: new Map(),
|
|
70
|
-
edges: new Set(),
|
|
71
|
-
subgraphs: new Set(),
|
|
72
|
-
};
|
|
73
|
-
|
|
74
|
-
public with(models: Partial<ModelsContext>): void {
|
|
75
|
-
this.#models = createModelsContext(models);
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
public addNode(node: NodeModel): void {
|
|
79
|
-
this.#objects.nodes.set(node.id, node);
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
public addEdge(edge: EdgeModel): void {
|
|
83
|
-
this.#objects.edges.add(edge);
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
public addSubgraph(subgraph: SubgraphModel): void {
|
|
87
|
-
this.#objects.subgraphs.add(subgraph);
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
public existNode(nodeId: string): boolean {
|
|
91
|
-
return this.#objects.nodes.has(nodeId);
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
public existEdge(edge: EdgeModel): boolean {
|
|
95
|
-
return this.#objects.edges.has(edge);
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
public existSubgraph(subgraph: SubgraphModel): boolean {
|
|
99
|
-
return this.#objects.subgraphs.has(subgraph);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
public createSubgraph(
|
|
103
|
-
id?: string,
|
|
104
|
-
attributes?: SubgraphAttributesObject,
|
|
105
|
-
): SubgraphModel;
|
|
106
|
-
|
|
107
|
-
public createSubgraph(attributes?: SubgraphAttributesObject): SubgraphModel;
|
|
108
|
-
|
|
109
|
-
public createSubgraph(...args: unknown[]): SubgraphModel {
|
|
110
|
-
const subgraph = new this.#models.Subgraph(...args);
|
|
111
|
-
subgraph.with(this.#models);
|
|
112
|
-
this.addSubgraph(subgraph);
|
|
113
|
-
return subgraph;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
public removeNode(node: NodeModel | string): void {
|
|
117
|
-
this.#objects.nodes.delete(typeof node === 'string' ? node : node.id);
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
public removeEdge(edge: EdgeModel): void {
|
|
121
|
-
this.#objects.edges.delete(edge);
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
public removeSubgraph(subgraph: SubgraphModel): void {
|
|
125
|
-
this.#objects.subgraphs.delete(subgraph);
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
public createNode(id: string, attributes?: NodeAttributesObject): NodeModel {
|
|
129
|
-
const node = new this.#models.Node(id, attributes);
|
|
130
|
-
this.addNode(node);
|
|
131
|
-
return node;
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
public getSubgraph(id: string): SubgraphModel | undefined {
|
|
135
|
-
return Array.from(this.#objects.subgraphs.values()).find(
|
|
136
|
-
(subgraph) => subgraph.id === id,
|
|
137
|
-
);
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
public getNode(id: string): NodeModel | undefined {
|
|
141
|
-
return this.#objects.nodes.get(id);
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
public createEdge(
|
|
145
|
-
targets: EdgeTargetLikeTuple,
|
|
146
|
-
attributes?: EdgeAttributesObject,
|
|
147
|
-
): EdgeModel {
|
|
148
|
-
const ts = targets.map((t) =>
|
|
149
|
-
isNodeRefGroupLike(t) ? toNodeRefGroup(t) : toNodeRef(t),
|
|
150
|
-
) as EdgeTargetTuple;
|
|
151
|
-
const edge = new this.#models.Edge(ts, attributes);
|
|
152
|
-
this.addEdge(edge);
|
|
153
|
-
return edge;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
public subgraph(
|
|
157
|
-
id: string,
|
|
158
|
-
callback?: (subgraph: SubgraphModel) => void,
|
|
159
|
-
): SubgraphModel;
|
|
160
|
-
public subgraph(
|
|
161
|
-
id: string,
|
|
162
|
-
attributes: SubgraphAttributesObject,
|
|
163
|
-
callback?: (subgraph: SubgraphModel) => void,
|
|
164
|
-
): SubgraphModel;
|
|
165
|
-
public subgraph(
|
|
166
|
-
attributes: SubgraphAttributesObject,
|
|
167
|
-
callback?: (subgraph: SubgraphModel) => void,
|
|
168
|
-
): SubgraphModel;
|
|
169
|
-
public subgraph(callback?: (subgraph: SubgraphModel) => void): SubgraphModel;
|
|
170
|
-
public subgraph(...args: unknown[]): SubgraphModel {
|
|
171
|
-
const id = args.find(
|
|
172
|
-
(arg: unknown): arg is string => typeof arg === 'string',
|
|
173
|
-
);
|
|
174
|
-
const attributes = args.find(
|
|
175
|
-
(arg: unknown): arg is SubgraphAttributesObject =>
|
|
176
|
-
typeof arg === 'object' && arg !== null,
|
|
177
|
-
);
|
|
178
|
-
const callback = args.find(
|
|
179
|
-
(arg: unknown): arg is (subgraph: SubgraphModel) => void =>
|
|
180
|
-
typeof arg === 'function',
|
|
181
|
-
);
|
|
182
|
-
const subgraph: SubgraphModel = id
|
|
183
|
-
? this.getSubgraph(id) ?? this.createSubgraph(id)
|
|
184
|
-
: this.createSubgraph();
|
|
185
|
-
if (attributes !== undefined) {
|
|
186
|
-
subgraph.apply(attributes);
|
|
187
|
-
}
|
|
188
|
-
if (callback !== undefined) {
|
|
189
|
-
callback(subgraph);
|
|
190
|
-
}
|
|
191
|
-
return subgraph;
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
public node(id: string, callback?: (node: NodeModel) => void): NodeModel;
|
|
195
|
-
public node(
|
|
196
|
-
id: string,
|
|
197
|
-
attributes: NodeAttributesObject,
|
|
198
|
-
callback?: (node: NodeModel) => void,
|
|
199
|
-
): NodeModel;
|
|
200
|
-
public node(attributes: NodeAttributesObject): void;
|
|
201
|
-
public node(firstArg: unknown, ...args: unknown[]): NodeModel | undefined {
|
|
202
|
-
if (typeof firstArg === 'string') {
|
|
203
|
-
const id = firstArg;
|
|
204
|
-
const attributes = args.find(
|
|
205
|
-
(arg: unknown): arg is NodeAttributesObject =>
|
|
206
|
-
typeof arg === 'object' && arg !== null,
|
|
207
|
-
);
|
|
208
|
-
const callback = args.find(
|
|
209
|
-
(arg: unknown): arg is (node: NodeModel) => void =>
|
|
210
|
-
typeof arg === 'function',
|
|
211
|
-
);
|
|
212
|
-
const node = this.getNode(id) ?? this.createNode(id);
|
|
213
|
-
if (attributes !== undefined) {
|
|
214
|
-
node.attributes.apply(attributes);
|
|
215
|
-
}
|
|
216
|
-
if (callback !== undefined) {
|
|
217
|
-
callback(node);
|
|
218
|
-
}
|
|
219
|
-
return node;
|
|
220
|
-
}
|
|
221
|
-
if (typeof firstArg === 'object' && firstArg !== null) {
|
|
222
|
-
this.attributes.node.apply(firstArg);
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
public edge(
|
|
227
|
-
targets: EdgeTargetLikeTuple,
|
|
228
|
-
callback?: (edge: EdgeModel) => void,
|
|
229
|
-
): EdgeModel;
|
|
230
|
-
public edge(
|
|
231
|
-
targets: EdgeTargetLikeTuple,
|
|
232
|
-
attributes: EdgeAttributesObject,
|
|
233
|
-
callback?: (edge: EdgeModel) => void,
|
|
234
|
-
): EdgeModel;
|
|
235
|
-
public edge(attributes: EdgeAttributesObject): void;
|
|
236
|
-
public edge(
|
|
237
|
-
firstArg: EdgeTargetLikeTuple | EdgeAttributesObject,
|
|
238
|
-
...args: unknown[]
|
|
239
|
-
): EdgeModel | undefined {
|
|
240
|
-
if (Array.isArray(firstArg)) {
|
|
241
|
-
const targets = firstArg;
|
|
242
|
-
const attributes = args.find(
|
|
243
|
-
(arg: unknown): arg is EdgeAttributesObject => typeof arg === 'object',
|
|
244
|
-
);
|
|
245
|
-
const callback = args.find(
|
|
246
|
-
(arg: unknown): arg is (edge: EdgeModel) => void =>
|
|
247
|
-
typeof arg === 'function',
|
|
248
|
-
);
|
|
249
|
-
const edge = this.createEdge(targets, attributes);
|
|
250
|
-
if (callback !== undefined) {
|
|
251
|
-
callback(edge);
|
|
252
|
-
}
|
|
253
|
-
return edge;
|
|
254
|
-
}
|
|
255
|
-
if (typeof firstArg === 'object' && firstArg !== null) {
|
|
256
|
-
this.attributes.edge.apply(firstArg);
|
|
257
|
-
}
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
public graph(attributes: SubgraphAttributesObject): void {
|
|
261
|
-
this.attributes.graph.apply(attributes);
|
|
262
|
-
}
|
|
263
|
-
}
|
package/src/models/Node.spec.ts
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { beforeEach, describe, expect, it, test } from 'vitest';
|
|
2
|
-
import { attribute as _ } from '../attribute.js';
|
|
3
|
-
import { DotObject } from './DotObject.js';
|
|
4
|
-
import { Node } from './Node.js';
|
|
5
|
-
|
|
6
|
-
let node: Node;
|
|
7
|
-
beforeEach(() => {
|
|
8
|
-
node = new Node('test');
|
|
9
|
-
});
|
|
10
|
-
|
|
11
|
-
describe('Constructor', () => {
|
|
12
|
-
test('first argument is id, and second attributes object', () => {
|
|
13
|
-
node = new Node('test', {
|
|
14
|
-
[_.label]: 'Label',
|
|
15
|
-
});
|
|
16
|
-
expect(node.id).toBe('test');
|
|
17
|
-
expect(node.attributes.size).toBe(1);
|
|
18
|
-
expect(node.attributes.get(_.label)).toBe('Label');
|
|
19
|
-
});
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
it('should be instance of Node/DotObject', () => {
|
|
23
|
-
expect(node).toBeInstanceOf(Node);
|
|
24
|
-
expect(node).toBeInstanceOf(DotObject);
|
|
25
|
-
});
|
package/src/models/Node.ts
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
ForwardRefNode,
|
|
3
|
-
NodeAttributeKey,
|
|
4
|
-
NodeAttributesObject,
|
|
5
|
-
NodeModel,
|
|
6
|
-
Port,
|
|
7
|
-
} from '@ts-graphviz/common';
|
|
8
|
-
import { AttributesGroup } from './AttributesGroup.js';
|
|
9
|
-
import { DotObject } from './DotObject.js';
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* DOT object class representing a node.
|
|
13
|
-
* @group Models
|
|
14
|
-
*/
|
|
15
|
-
export class Node extends DotObject implements NodeModel {
|
|
16
|
-
public get $$type(): 'Node' {
|
|
17
|
-
return 'Node';
|
|
18
|
-
}
|
|
19
|
-
public comment?: string;
|
|
20
|
-
|
|
21
|
-
public readonly attributes: AttributesGroup<NodeAttributeKey>;
|
|
22
|
-
|
|
23
|
-
constructor(
|
|
24
|
-
public readonly id: string,
|
|
25
|
-
attributes?: NodeAttributesObject,
|
|
26
|
-
) {
|
|
27
|
-
super();
|
|
28
|
-
this.attributes = new AttributesGroup(attributes);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
public port(port: string | Partial<Port>): ForwardRefNode {
|
|
32
|
-
if (typeof port === 'string') {
|
|
33
|
-
return { id: this.id, port };
|
|
34
|
-
}
|
|
35
|
-
return { id: this.id, ...port };
|
|
36
|
-
}
|
|
37
|
-
}
|
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
import { beforeEach, describe, expect, it, test } from 'vitest';
|
|
2
|
-
import './registerModelContext.js';
|
|
3
|
-
|
|
4
|
-
import { attribute as _ } from '../attribute.js';
|
|
5
|
-
import { DotObject } from './DotObject.js';
|
|
6
|
-
import { GraphBase } from './GraphBase.js';
|
|
7
|
-
import { RootGraph } from './RootGraph.js';
|
|
8
|
-
|
|
9
|
-
class TestRootGraph extends RootGraph {
|
|
10
|
-
public directed = true;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
let g: RootGraph;
|
|
14
|
-
beforeEach(() => {
|
|
15
|
-
g = new TestRootGraph();
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
describe('Constructor', () => {
|
|
19
|
-
test('no args', () => {
|
|
20
|
-
g = new TestRootGraph();
|
|
21
|
-
expect(g.id).toBeUndefined();
|
|
22
|
-
expect(g.values).toStrictEqual([]);
|
|
23
|
-
expect(g.strict).toStrictEqual(false);
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
test('first argument is id', () => {
|
|
27
|
-
g = new TestRootGraph('foo');
|
|
28
|
-
expect(g.id).toStrictEqual('foo');
|
|
29
|
-
expect(g.values).toStrictEqual([]);
|
|
30
|
-
expect(g.strict).toStrictEqual(false);
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
test('first argument is id, seccond argument is strict, third argument is attribute object', () => {
|
|
34
|
-
g = new TestRootGraph('foo', true, { [_.label]: 'Test label' });
|
|
35
|
-
expect(g.id).toStrictEqual('foo');
|
|
36
|
-
expect(g.values).toStrictEqual([[_.label, 'Test label']]);
|
|
37
|
-
expect(g.strict).toStrictEqual(true);
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
test('first argument is attribute object', () => {
|
|
41
|
-
g = new TestRootGraph({ [_.label]: 'Test label' });
|
|
42
|
-
expect(g.id).toBeUndefined();
|
|
43
|
-
expect(g.values).toStrictEqual([[_.label, 'Test label']]);
|
|
44
|
-
expect(g.strict).toStrictEqual(false);
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
test('first argument is strict', () => {
|
|
48
|
-
g = new TestRootGraph(true);
|
|
49
|
-
expect(g.id).toBeUndefined();
|
|
50
|
-
expect(g.values).toStrictEqual([]);
|
|
51
|
-
expect(g.strict).toStrictEqual(true);
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
test('first argument is strict, seccond is attribute object', () => {
|
|
55
|
-
g = new TestRootGraph(true, { [_.label]: 'Test label' });
|
|
56
|
-
expect(g.id).toBeUndefined();
|
|
57
|
-
expect(g.values).toStrictEqual([[_.label, 'Test label']]);
|
|
58
|
-
expect(g.strict).toStrictEqual(true);
|
|
59
|
-
});
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
it('should be instance of GraphBase/DotObject', () => {
|
|
63
|
-
expect(g).toBeInstanceOf(GraphBase);
|
|
64
|
-
expect(g).toBeInstanceOf(DotObject);
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
test('$$type propaty should be "Graph"', () => {
|
|
68
|
-
expect(g.$$type).toStrictEqual('Graph');
|
|
69
|
-
});
|
package/src/models/RootGraph.ts
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
GraphAttributeKey,
|
|
3
|
-
GraphAttributesObject,
|
|
4
|
-
RootGraphModel,
|
|
5
|
-
} from '@ts-graphviz/common';
|
|
6
|
-
import { GraphBase } from './GraphBase.js';
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Base class representing a root graph(digraph, graph).
|
|
10
|
-
* @group Models
|
|
11
|
-
*/
|
|
12
|
-
export abstract class RootGraph
|
|
13
|
-
extends GraphBase<GraphAttributeKey>
|
|
14
|
-
implements RootGraphModel
|
|
15
|
-
{
|
|
16
|
-
public get $$type(): 'Graph' {
|
|
17
|
-
return 'Graph';
|
|
18
|
-
}
|
|
19
|
-
public readonly id?: string;
|
|
20
|
-
public abstract readonly directed: boolean;
|
|
21
|
-
public strict: boolean;
|
|
22
|
-
|
|
23
|
-
constructor(id?: string, attributes?: GraphAttributesObject);
|
|
24
|
-
|
|
25
|
-
constructor(
|
|
26
|
-
id?: string,
|
|
27
|
-
strict?: boolean,
|
|
28
|
-
attributes?: GraphAttributesObject,
|
|
29
|
-
);
|
|
30
|
-
|
|
31
|
-
constructor(strict?: boolean, attributes?: GraphAttributesObject);
|
|
32
|
-
|
|
33
|
-
constructor(attributes?: GraphAttributesObject);
|
|
34
|
-
|
|
35
|
-
constructor(...args: unknown[]) {
|
|
36
|
-
super();
|
|
37
|
-
this.id = args.find((arg): arg is string => typeof arg === 'string');
|
|
38
|
-
this.strict =
|
|
39
|
-
args.find((arg): arg is boolean => typeof arg === 'boolean') ?? false;
|
|
40
|
-
const attributes = args.find(
|
|
41
|
-
(arg): arg is GraphAttributesObject =>
|
|
42
|
-
typeof arg === 'object' && arg !== null,
|
|
43
|
-
);
|
|
44
|
-
if (attributes !== undefined) {
|
|
45
|
-
this.apply(attributes);
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
}
|
|
@@ -1,196 +0,0 @@
|
|
|
1
|
-
import { beforeEach, describe, expect, it, test } from 'vitest';
|
|
2
|
-
import './registerModelContext.js';
|
|
3
|
-
|
|
4
|
-
import { EdgeTargetTuple, SubgraphModel } from '@ts-graphviz/common';
|
|
5
|
-
import { attribute as _ } from '../attribute.js';
|
|
6
|
-
import { AttributesBase } from './AttributesBase.js';
|
|
7
|
-
import { DotObject } from './DotObject.js';
|
|
8
|
-
import { GraphBase } from './GraphBase.js';
|
|
9
|
-
import { Node } from './Node.js';
|
|
10
|
-
import { Subgraph } from './Subgraph.js';
|
|
11
|
-
|
|
12
|
-
describe('class Subgraph', () => {
|
|
13
|
-
let subgraph: SubgraphModel;
|
|
14
|
-
|
|
15
|
-
beforeEach(() => {
|
|
16
|
-
subgraph = new Subgraph();
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
it('should be instance of Subgraph/Cluster/AttributesBase/DotObject', () => {
|
|
20
|
-
expect(subgraph).toBeInstanceOf(Subgraph);
|
|
21
|
-
expect(subgraph).toBeInstanceOf(GraphBase);
|
|
22
|
-
expect(subgraph).toBeInstanceOf(AttributesBase);
|
|
23
|
-
expect(subgraph).toBeInstanceOf(DotObject);
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
describe('Constructor', () => {
|
|
27
|
-
test('first argument is id, and second attributes object', () => {
|
|
28
|
-
subgraph = new Subgraph('test', {
|
|
29
|
-
[_.K]: 1,
|
|
30
|
-
});
|
|
31
|
-
expect(subgraph.id).toBe('test');
|
|
32
|
-
expect(subgraph.get(_.K)).toBe(1);
|
|
33
|
-
});
|
|
34
|
-
test('first argument is attributes object', () => {
|
|
35
|
-
subgraph = new Subgraph({
|
|
36
|
-
[_.K]: 1,
|
|
37
|
-
});
|
|
38
|
-
expect(subgraph.get(_.K)).toBe(1);
|
|
39
|
-
});
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
describe('Declaratively set the attributes of the objects in the cluster', () => {
|
|
43
|
-
test('node', () => {
|
|
44
|
-
subgraph.node({
|
|
45
|
-
label: 'test label',
|
|
46
|
-
});
|
|
47
|
-
expect(subgraph.attributes.node.get(_.label)).toBe('test label');
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
test('edge', () => {
|
|
51
|
-
subgraph.edge({
|
|
52
|
-
label: 'test label',
|
|
53
|
-
});
|
|
54
|
-
expect(subgraph.attributes.edge.get(_.label)).toBe('test label');
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
test('graph', () => {
|
|
58
|
-
subgraph.graph({
|
|
59
|
-
label: 'test label',
|
|
60
|
-
});
|
|
61
|
-
expect(subgraph.attributes.graph.get(_.label)).toBe('test label');
|
|
62
|
-
});
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
test('set attributes', () => {
|
|
66
|
-
subgraph.set(_.rank, 'same');
|
|
67
|
-
expect(subgraph.get(_.rank)).toBe('same');
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
describe('set attributes by apply', () => {
|
|
71
|
-
test('with attributes object', () => {
|
|
72
|
-
subgraph.apply({
|
|
73
|
-
[_.rank]: 'same',
|
|
74
|
-
});
|
|
75
|
-
expect(subgraph.get(_.rank)).toBe('same');
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
test('with entities', () => {
|
|
79
|
-
subgraph.apply([[_.rank, 'same']]);
|
|
80
|
-
expect(subgraph.get(_.rank)).toBe('same');
|
|
81
|
-
});
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
it('should be subgraph, when subgraph id is "test"', () => {
|
|
85
|
-
expect(subgraph.isSubgraphCluster()).toBe(false);
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
test('create node with attributes', () => {
|
|
89
|
-
const node = subgraph.createNode('n', {
|
|
90
|
-
[_.label]: 'Label',
|
|
91
|
-
});
|
|
92
|
-
expect(node.id).toBe('n');
|
|
93
|
-
expect(node.attributes.size).toBe(1);
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
test('create edge with attributes', () => {
|
|
97
|
-
const nodes = [...Array(2)].map((_v, i) =>
|
|
98
|
-
subgraph.createNode(`node${i + 1}`),
|
|
99
|
-
) as EdgeTargetTuple;
|
|
100
|
-
const edge = subgraph.createEdge(nodes, {
|
|
101
|
-
[_.label]: 'Label',
|
|
102
|
-
});
|
|
103
|
-
expect(edge.attributes.size).toBe(1);
|
|
104
|
-
});
|
|
105
|
-
|
|
106
|
-
test('create edge by node group', () => {
|
|
107
|
-
const [node1, node2, node3, node4] = Array(4)
|
|
108
|
-
.fill(true)
|
|
109
|
-
.map((_v, i) => subgraph.createNode(`node${i + 1}`));
|
|
110
|
-
const edge = subgraph.createEdge([node1, [node2, node3], node4]);
|
|
111
|
-
expect(edge.targets.length).toBe(3);
|
|
112
|
-
});
|
|
113
|
-
|
|
114
|
-
test('create subgraph with attributes', () => {
|
|
115
|
-
subgraph = subgraph.createSubgraph('test', {
|
|
116
|
-
[_.label]: 'Label',
|
|
117
|
-
});
|
|
118
|
-
expect(subgraph.id).toBe('test');
|
|
119
|
-
expect(subgraph.size).toBe(1);
|
|
120
|
-
|
|
121
|
-
subgraph = subgraph.createSubgraph({
|
|
122
|
-
[_.label]: 'Label',
|
|
123
|
-
});
|
|
124
|
-
expect(subgraph.size).toBe(1);
|
|
125
|
-
});
|
|
126
|
-
|
|
127
|
-
test.each([
|
|
128
|
-
['cluster', true],
|
|
129
|
-
['cluster_hoge', true],
|
|
130
|
-
['hoge_cluster', false],
|
|
131
|
-
['example', false],
|
|
132
|
-
])(
|
|
133
|
-
'if cluster named "%s", isSubgraphCluster should be %p',
|
|
134
|
-
(id, expected) => {
|
|
135
|
-
subgraph = new Subgraph(id);
|
|
136
|
-
expect(subgraph.isSubgraphCluster()).toBe(expected);
|
|
137
|
-
},
|
|
138
|
-
);
|
|
139
|
-
|
|
140
|
-
describe('addXxx existXxx removeXxx APIs', () => {
|
|
141
|
-
it('Node operation methods works', () => {
|
|
142
|
-
const id = 'node';
|
|
143
|
-
expect(subgraph.existNode(id)).toBe(false);
|
|
144
|
-
const node = new Node(id);
|
|
145
|
-
subgraph.addNode(node);
|
|
146
|
-
expect(subgraph.existNode(id)).toBe(true);
|
|
147
|
-
subgraph.removeNode(node);
|
|
148
|
-
expect(subgraph.existNode(id)).toBe(false);
|
|
149
|
-
subgraph.addNode(node);
|
|
150
|
-
expect(subgraph.existNode(id)).toBe(true);
|
|
151
|
-
subgraph.removeNode(node.id);
|
|
152
|
-
expect(subgraph.existNode(id)).toBe(false);
|
|
153
|
-
});
|
|
154
|
-
|
|
155
|
-
it('Edge operation methods works', () => {
|
|
156
|
-
const nodes = [...Array(2)].map((_v, i) =>
|
|
157
|
-
subgraph.createNode(`node${i + 1}`),
|
|
158
|
-
) as EdgeTargetTuple;
|
|
159
|
-
const edge = subgraph.createEdge(nodes);
|
|
160
|
-
expect(subgraph.existEdge(edge)).toBe(true);
|
|
161
|
-
subgraph.removeEdge(edge);
|
|
162
|
-
expect(subgraph.existEdge(edge)).toBe(false);
|
|
163
|
-
});
|
|
164
|
-
|
|
165
|
-
it('Subgraph operation methods works', () => {
|
|
166
|
-
const sub = subgraph.createSubgraph('sub');
|
|
167
|
-
expect(subgraph.existSubgraph(sub)).toBe(true);
|
|
168
|
-
subgraph.removeSubgraph(sub);
|
|
169
|
-
expect(subgraph.existSubgraph(sub)).toBe(false);
|
|
170
|
-
subgraph.addSubgraph(sub);
|
|
171
|
-
expect(subgraph.existSubgraph(sub)).toBe(true);
|
|
172
|
-
subgraph.removeSubgraph(sub);
|
|
173
|
-
expect(subgraph.existSubgraph(sub)).toBe(false);
|
|
174
|
-
});
|
|
175
|
-
|
|
176
|
-
it('should be undefined, when id not set', () => {
|
|
177
|
-
const sub = subgraph.createSubgraph();
|
|
178
|
-
expect(sub.id).toBeUndefined();
|
|
179
|
-
expect(sub.isSubgraphCluster()).toBe(false);
|
|
180
|
-
});
|
|
181
|
-
|
|
182
|
-
it('throws an error when the EdgeTarget element is missing', () => {
|
|
183
|
-
const n = subgraph.node('n');
|
|
184
|
-
expect(() =>
|
|
185
|
-
subgraph.edge([] as unknown as EdgeTargetTuple),
|
|
186
|
-
).toThrowErrorMatchingInlineSnapshot(
|
|
187
|
-
`[Error: The element of Edge target is missing or not satisfied as Edge target.]`,
|
|
188
|
-
);
|
|
189
|
-
expect(() =>
|
|
190
|
-
subgraph.edge([n] as unknown as EdgeTargetTuple),
|
|
191
|
-
).toThrowErrorMatchingInlineSnapshot(
|
|
192
|
-
`[Error: The element of Edge target is missing or not satisfied as Edge target.]`,
|
|
193
|
-
);
|
|
194
|
-
});
|
|
195
|
-
});
|
|
196
|
-
});
|
package/src/models/Subgraph.ts
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
ClusterSubgraphAttributeKey,
|
|
3
|
-
SubgraphAttributeKey,
|
|
4
|
-
SubgraphAttributesObject,
|
|
5
|
-
SubgraphModel,
|
|
6
|
-
} from '@ts-graphviz/common';
|
|
7
|
-
import { GraphBase } from './GraphBase.js';
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* DOT object class representing a subgraph.
|
|
11
|
-
* @group Models
|
|
12
|
-
*/
|
|
13
|
-
export class Subgraph
|
|
14
|
-
extends GraphBase<SubgraphAttributeKey | ClusterSubgraphAttributeKey>
|
|
15
|
-
implements SubgraphModel
|
|
16
|
-
{
|
|
17
|
-
public get $$type(): 'Subgraph' {
|
|
18
|
-
return 'Subgraph';
|
|
19
|
-
}
|
|
20
|
-
public readonly id?: string;
|
|
21
|
-
|
|
22
|
-
constructor(id?: string, attributes?: SubgraphAttributesObject);
|
|
23
|
-
|
|
24
|
-
constructor(attributes?: SubgraphAttributesObject);
|
|
25
|
-
|
|
26
|
-
constructor(...args: unknown[]) {
|
|
27
|
-
super();
|
|
28
|
-
this.id = args.find((arg): arg is string => typeof arg === 'string');
|
|
29
|
-
const attributes = args.find(
|
|
30
|
-
(arg): arg is SubgraphAttributesObject =>
|
|
31
|
-
typeof arg === 'object' && arg !== null,
|
|
32
|
-
);
|
|
33
|
-
if (attributes !== undefined) {
|
|
34
|
-
this.apply(attributes);
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
public isSubgraphCluster(): boolean {
|
|
39
|
-
if (typeof this.id === 'string') {
|
|
40
|
-
return this.id.startsWith('cluster');
|
|
41
|
-
}
|
|
42
|
-
return false;
|
|
43
|
-
}
|
|
44
|
-
}
|
package/src/models/index.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import './registerModelContext.js';
|
|
2
|
-
|
|
3
|
-
export * from './DotObject.js';
|
|
4
|
-
export * from './AttributesBase.js';
|
|
5
|
-
export * from './AttributeList.js';
|
|
6
|
-
export * from './AttributesBase.js';
|
|
7
|
-
export * from './AttributesGroup.js';
|
|
8
|
-
export * from './Digraph.js';
|
|
9
|
-
export * from './DotObject.js';
|
|
10
|
-
export * from './Edge.js';
|
|
11
|
-
export * from './Graph.js';
|
|
12
|
-
export * from './GraphBase.js';
|
|
13
|
-
export * from './Node.js';
|
|
14
|
-
export * from './RootGraph.js';
|
|
15
|
-
export * from './Subgraph.js';
|