@ts-graphviz/core 0.0.0-pr956-20240225073457 → 0.0.0-pr956-20240225160253
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 +292 -4
- package/lib/AttributeList.cjs +14 -0
- package/lib/AttributeList.d.ts +43 -0
- package/lib/AttributeList.js +14 -0
- package/lib/AttributesBase.cjs +40 -0
- package/lib/AttributesBase.d.ts +30 -0
- package/lib/AttributesBase.js +40 -0
- package/lib/AttributesGroup.cjs +7 -0
- package/lib/AttributesGroup.d.ts +39 -0
- package/lib/AttributesGroup.js +7 -0
- package/lib/Digraph.cjs +9 -0
- package/lib/Digraph.d.ts +107 -0
- package/lib/Digraph.js +9 -0
- package/lib/DotObject.cjs +5 -0
- package/lib/DotObject.d.ts +8 -0
- package/lib/DotObject.js +5 -0
- package/lib/Edge.cjs +23 -0
- package/lib/Edge.d.ts +26 -0
- package/lib/Edge.js +23 -0
- package/lib/Graph.cjs +9 -0
- package/lib/Graph.d.ts +107 -0
- package/lib/Graph.js +9 -0
- package/lib/GraphBase.cjs +152 -0
- package/lib/GraphBase.d.ts +81 -0
- package/lib/GraphBase.js +152 -0
- package/lib/Node.cjs +23 -0
- package/lib/Node.d.ts +64 -0
- package/lib/Node.js +23 -0
- package/lib/RootGraph.cjs +22 -0
- package/lib/RootGraph.d.ts +99 -0
- package/lib/RootGraph.js +22 -0
- package/lib/Subgraph.cjs +26 -0
- package/lib/Subgraph.d.ts +95 -0
- package/lib/Subgraph.js +26 -0
- package/lib/core.cjs +22 -395
- package/lib/core.d.ts +442 -177
- package/lib/core.js +12 -385
- package/lib/register-default.cjs +14 -0
- package/lib/register-default.d.ts +1 -0
- package/lib/register-default.js +13 -0
- package/package.json +59 -17
- 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/Graph.spec.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { expect, it, test } from 'vitest';
|
|
2
|
-
import { DotObject } from './DotObject.js';
|
|
3
|
-
import { Graph } from './Graph.js';
|
|
4
|
-
import { GraphBase } from './GraphBase.js';
|
|
5
|
-
|
|
6
|
-
import './registerModelContext.js';
|
|
7
|
-
|
|
8
|
-
const g = new Graph();
|
|
9
|
-
|
|
10
|
-
test('directed propaty should be false', () => {
|
|
11
|
-
expect(g.directed).toStrictEqual(false);
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
it('should be instance of Graph/GraphBase/DotObject', () => {
|
|
15
|
-
expect(g).toBeInstanceOf(Graph);
|
|
16
|
-
expect(g).toBeInstanceOf(GraphBase);
|
|
17
|
-
expect(g).toBeInstanceOf(DotObject);
|
|
18
|
-
});
|
package/src/models/Graph.ts
DELETED
|
@@ -1,364 +0,0 @@
|
|
|
1
|
-
import { beforeEach, describe, expect, it, test, vi } from 'vitest';
|
|
2
|
-
import './registerModelContext.js';
|
|
3
|
-
|
|
4
|
-
import { EdgeTargetTuple, NodeModel } 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 { Edge } from './Edge.js';
|
|
9
|
-
import { GraphBase } from './GraphBase.js';
|
|
10
|
-
import { Node } from './Node.js';
|
|
11
|
-
import { Subgraph } from './Subgraph.js';
|
|
12
|
-
|
|
13
|
-
class TestGraph extends GraphBase<any> {
|
|
14
|
-
public readonly directed = false;
|
|
15
|
-
public strict = true;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
let g: TestGraph;
|
|
19
|
-
beforeEach(() => {
|
|
20
|
-
g = new TestGraph();
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
it('should be instance of GraphBase/AttributesBase/DotObject', () => {
|
|
24
|
-
expect(g).toBeInstanceOf(GraphBase);
|
|
25
|
-
expect(g).toBeInstanceOf(AttributesBase);
|
|
26
|
-
expect(g).toBeInstanceOf(DotObject);
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
describe('Constructor', () => {
|
|
30
|
-
test('first argument is attributes object', () => {
|
|
31
|
-
const root = new TestGraph({
|
|
32
|
-
[_.label]: 'Label',
|
|
33
|
-
});
|
|
34
|
-
expect(root.size).toBe(1);
|
|
35
|
-
expect(root.get(_.label)).toBe('Label');
|
|
36
|
-
});
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
describe('Imperative API(addXxx existXxx removeXxx methods)', () => {
|
|
40
|
-
test('Node operation methods works', () => {
|
|
41
|
-
expect(g.existNode('foo')).toBe(false);
|
|
42
|
-
const node = new Node('foo');
|
|
43
|
-
g.addNode(node);
|
|
44
|
-
expect(g.existNode('foo')).toBe(true);
|
|
45
|
-
g.removeNode(node);
|
|
46
|
-
expect(g.existNode('foo')).toBe(false);
|
|
47
|
-
g.addNode(node);
|
|
48
|
-
expect(g.existNode('foo')).toBe(true);
|
|
49
|
-
g.removeNode('foo');
|
|
50
|
-
expect(g.existNode('foo')).toBe(false);
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
test('Edge operation methods works', () => {
|
|
54
|
-
const nodes = ['foo', 'bar'].map((id) =>
|
|
55
|
-
g.createNode(id),
|
|
56
|
-
) as EdgeTargetTuple;
|
|
57
|
-
const edge = new Edge(nodes);
|
|
58
|
-
expect(g.existEdge(edge)).toBe(false);
|
|
59
|
-
g.addEdge(edge);
|
|
60
|
-
expect(g.existEdge(edge)).toBe(true);
|
|
61
|
-
g.removeEdge(edge);
|
|
62
|
-
expect(g.existEdge(edge)).toBe(false);
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
test('Subgraph operation methods works', () => {
|
|
66
|
-
const sub = g.createSubgraph('sub');
|
|
67
|
-
expect(g.existSubgraph(sub)).toBe(true);
|
|
68
|
-
g.removeSubgraph(sub);
|
|
69
|
-
expect(g.existSubgraph(sub)).toBe(false);
|
|
70
|
-
g.addSubgraph(sub);
|
|
71
|
-
expect(g.existSubgraph(sub)).toBe(true);
|
|
72
|
-
g.removeSubgraph(sub);
|
|
73
|
-
expect(g.existSubgraph(sub)).toBe(false);
|
|
74
|
-
});
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
describe('Declarative API', () => {
|
|
78
|
-
describe('node method', () => {
|
|
79
|
-
describe('create node if not exists', () => {
|
|
80
|
-
test('by id', () => {
|
|
81
|
-
const createNodeSpy = vi.spyOn(g, 'createNode');
|
|
82
|
-
|
|
83
|
-
g.node('foo');
|
|
84
|
-
expect(g.existNode('foo')).toBe(true);
|
|
85
|
-
expect(createNodeSpy).toHaveBeenCalledWith('foo');
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
test('with attributes', () => {
|
|
89
|
-
expect(g.existNode('foo')).toBe(false);
|
|
90
|
-
const createNodeSpy = vi.spyOn(g, 'createNode');
|
|
91
|
-
|
|
92
|
-
const node = g.node('foo', {
|
|
93
|
-
[_.label]: 'Test label',
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
expect(g.existNode('foo')).toBe(true);
|
|
97
|
-
expect(createNodeSpy).toHaveBeenCalledWith('foo');
|
|
98
|
-
expect(node.attributes.get(_.label)).toStrictEqual('Test label');
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
describe('callback function is given, the callback function is executed with the created node as the argument', () => {
|
|
102
|
-
test('first argument is id, seccond argument is callback', () => {
|
|
103
|
-
const callback = vi.fn();
|
|
104
|
-
const node = g.node('foo', callback);
|
|
105
|
-
expect(callback).toHaveBeenCalledWith(node);
|
|
106
|
-
});
|
|
107
|
-
|
|
108
|
-
test('first argument is id, seccond argument is attribute object, third argument is callback', () => {
|
|
109
|
-
const callback = vi.fn();
|
|
110
|
-
const node = g.node('foo', { [_.label]: 'Test label' }, callback);
|
|
111
|
-
expect(callback).toHaveBeenCalledWith(node);
|
|
112
|
-
expect(node.attributes.get(_.label)).toStrictEqual('Test label');
|
|
113
|
-
});
|
|
114
|
-
});
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
describe('get node if exists', () => {
|
|
118
|
-
test('by id', () => {
|
|
119
|
-
const createdNode = g.createNode('foo');
|
|
120
|
-
const createNodeSpy = vi.spyOn(g, 'createNode');
|
|
121
|
-
|
|
122
|
-
const returnedNode = g.node('foo');
|
|
123
|
-
|
|
124
|
-
expect(createNodeSpy).not.toHaveBeenCalled();
|
|
125
|
-
expect(returnedNode).toBe(createdNode);
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
test('with attributes', () => {
|
|
129
|
-
const createdNode = g.createNode('foo');
|
|
130
|
-
const createNodeSpy = vi.spyOn(g, 'createNode');
|
|
131
|
-
|
|
132
|
-
const returnedNode = g.node('foo', { [_.label]: 'Test label' });
|
|
133
|
-
|
|
134
|
-
expect(createNodeSpy).not.toHaveBeenCalled();
|
|
135
|
-
expect(returnedNode).toBe(createdNode);
|
|
136
|
-
|
|
137
|
-
expect(returnedNode.attributes.get(_.label)).toStrictEqual(
|
|
138
|
-
'Test label',
|
|
139
|
-
);
|
|
140
|
-
});
|
|
141
|
-
|
|
142
|
-
describe('callback function is given, the callback function is executed with the created node as the argument', () => {
|
|
143
|
-
let createdNode: NodeModel;
|
|
144
|
-
beforeEach(() => {
|
|
145
|
-
createdNode = g.createNode('foo');
|
|
146
|
-
});
|
|
147
|
-
|
|
148
|
-
test('first argument is id, seccond argument is callback', () => {
|
|
149
|
-
const callback = vi.fn();
|
|
150
|
-
const node = g.node('foo', callback);
|
|
151
|
-
expect(callback).toHaveBeenCalledWith(node);
|
|
152
|
-
expect(node).toBe(createdNode);
|
|
153
|
-
});
|
|
154
|
-
|
|
155
|
-
test('first argument is id, seccond argument is attribute object, third argument is callback', () => {
|
|
156
|
-
const callback = vi.fn();
|
|
157
|
-
const node = g.node('foo', { [_.label]: 'Test label' }, callback);
|
|
158
|
-
expect(callback).toHaveBeenCalledWith(node);
|
|
159
|
-
expect(node.attributes.get(_.label)).toStrictEqual('Test label');
|
|
160
|
-
expect(node).toBe(createdNode);
|
|
161
|
-
});
|
|
162
|
-
});
|
|
163
|
-
});
|
|
164
|
-
|
|
165
|
-
test('apply atttibutes to nodes in graph', () => {
|
|
166
|
-
g.node({ [_.label]: 'Test label' });
|
|
167
|
-
expect(g.attributes.node.get(_.label)).toStrictEqual('Test label');
|
|
168
|
-
});
|
|
169
|
-
});
|
|
170
|
-
|
|
171
|
-
describe('edge method', () => {
|
|
172
|
-
describe('create edge', () => {
|
|
173
|
-
let nodes: EdgeTargetTuple;
|
|
174
|
-
beforeEach(() => {
|
|
175
|
-
nodes = ['foo', 'bar'].map((id) => g.createNode(id)) as EdgeTargetTuple;
|
|
176
|
-
});
|
|
177
|
-
|
|
178
|
-
test('create edge with target nodes', () => {
|
|
179
|
-
const createEdgeSpy = vi.spyOn(g, 'createEdge');
|
|
180
|
-
g.edge(nodes);
|
|
181
|
-
expect(createEdgeSpy).toHaveBeenCalledWith(nodes, undefined);
|
|
182
|
-
});
|
|
183
|
-
|
|
184
|
-
test('create edge and apply attribute', () => {
|
|
185
|
-
const createEdgeSpy = vi.spyOn(g, 'createEdge');
|
|
186
|
-
g.edge(nodes, { [_.label]: 'Test label' });
|
|
187
|
-
expect(createEdgeSpy).toHaveBeenCalledWith(nodes, {
|
|
188
|
-
[_.label]: 'Test label',
|
|
189
|
-
});
|
|
190
|
-
});
|
|
191
|
-
|
|
192
|
-
test('apply atttibutes to edges in graph', () => {
|
|
193
|
-
g.edge({ [_.label]: 'Test label' });
|
|
194
|
-
expect(g.attributes.edge.get(_.label)).toStrictEqual('Test label');
|
|
195
|
-
});
|
|
196
|
-
|
|
197
|
-
describe('callback function is given, the callback function is executed with the created edge as the argument', () => {
|
|
198
|
-
test('first argument is id, seccond argument is callback', () => {
|
|
199
|
-
const callback = vi.fn();
|
|
200
|
-
const edge = g.edge(nodes, callback);
|
|
201
|
-
expect(callback).toHaveBeenCalledWith(edge);
|
|
202
|
-
});
|
|
203
|
-
|
|
204
|
-
test('first argument is id, seccond argument is attribute object, third argument is callback', () => {
|
|
205
|
-
const callback = vi.fn();
|
|
206
|
-
const edge = g.edge(nodes, { [_.label]: 'Test label' }, callback);
|
|
207
|
-
expect(callback).toHaveBeenCalledWith(edge);
|
|
208
|
-
expect(edge.attributes.get(_.label)).toStrictEqual('Test label');
|
|
209
|
-
});
|
|
210
|
-
});
|
|
211
|
-
});
|
|
212
|
-
|
|
213
|
-
test('apply atttibutes to edges in graph', () => {
|
|
214
|
-
g.edge({ [_.label]: 'Test label' });
|
|
215
|
-
expect(g.attributes.edge.get(_.label)).toStrictEqual('Test label');
|
|
216
|
-
});
|
|
217
|
-
});
|
|
218
|
-
|
|
219
|
-
describe('subgraph method', () => {
|
|
220
|
-
describe('create subgraph if not exists', () => {
|
|
221
|
-
test('no id', () => {
|
|
222
|
-
const createSubgraphSpy = vi.spyOn(g, 'createSubgraph');
|
|
223
|
-
const subgraph = g.subgraph();
|
|
224
|
-
expect(subgraph).toBeInstanceOf(Subgraph);
|
|
225
|
-
expect(createSubgraphSpy).toHaveBeenCalled();
|
|
226
|
-
});
|
|
227
|
-
|
|
228
|
-
test('only attributes', () => {
|
|
229
|
-
const createSubgraphSpy = vi.spyOn(g, 'createSubgraph');
|
|
230
|
-
const subgraph = g.subgraph({ [_.label]: 'Test label' });
|
|
231
|
-
expect(createSubgraphSpy).toHaveBeenCalled();
|
|
232
|
-
expect(subgraph.get(_.label)).toStrictEqual('Test label');
|
|
233
|
-
});
|
|
234
|
-
|
|
235
|
-
test('by id', () => {
|
|
236
|
-
const createSubgraphSpy = vi.spyOn(g, 'createSubgraph');
|
|
237
|
-
g.subgraph('foo');
|
|
238
|
-
expect(createSubgraphSpy).toHaveBeenCalledWith('foo');
|
|
239
|
-
});
|
|
240
|
-
|
|
241
|
-
test('with attributes', () => {
|
|
242
|
-
const createSubgraphSpy = vi.spyOn(g, 'createSubgraph');
|
|
243
|
-
const subgraph = g.subgraph('foo', { [_.label]: 'Test label' });
|
|
244
|
-
expect(createSubgraphSpy).toHaveBeenCalledWith('foo');
|
|
245
|
-
expect(subgraph.get(_.label)).toStrictEqual('Test label');
|
|
246
|
-
});
|
|
247
|
-
|
|
248
|
-
describe('callback function is given, the callback function is executed with the created subgraph as the argument', () => {
|
|
249
|
-
test('first argument is callback', () => {
|
|
250
|
-
const callback = vi.fn();
|
|
251
|
-
const subgraph = g.subgraph(callback);
|
|
252
|
-
expect(callback).toHaveBeenCalledWith(subgraph);
|
|
253
|
-
});
|
|
254
|
-
|
|
255
|
-
test('first argument is attribute, seccond argument is callback', () => {
|
|
256
|
-
const callback = vi.fn();
|
|
257
|
-
const subgraph = g.subgraph({ [_.label]: 'Test label' }, callback);
|
|
258
|
-
expect(callback).toHaveBeenCalledWith(subgraph);
|
|
259
|
-
expect(subgraph.get(_.label)).toStrictEqual('Test label');
|
|
260
|
-
});
|
|
261
|
-
|
|
262
|
-
test('first argument is id, seccond argument is callback', () => {
|
|
263
|
-
const callback = vi.fn();
|
|
264
|
-
const subgraph = g.subgraph('foo', callback);
|
|
265
|
-
expect(callback).toHaveBeenCalledWith(subgraph);
|
|
266
|
-
});
|
|
267
|
-
|
|
268
|
-
test('first argument is id, seccond argument is attribute object, third argument is callback', () => {
|
|
269
|
-
const callback = vi.fn();
|
|
270
|
-
const subgraph = g.subgraph(
|
|
271
|
-
'foo',
|
|
272
|
-
{ [_.label]: 'Test label' },
|
|
273
|
-
callback,
|
|
274
|
-
);
|
|
275
|
-
expect(callback).toHaveBeenCalledWith(subgraph);
|
|
276
|
-
expect(subgraph.get(_.label)).toStrictEqual('Test label');
|
|
277
|
-
});
|
|
278
|
-
});
|
|
279
|
-
});
|
|
280
|
-
|
|
281
|
-
describe('get subgraph if exists', () => {
|
|
282
|
-
test('by id', () => {
|
|
283
|
-
const createdSubgraph = g.createSubgraph('foo');
|
|
284
|
-
const createSubgraphSpy = vi.spyOn(g, 'createSubgraph');
|
|
285
|
-
|
|
286
|
-
const returnedSubgraph = g.subgraph('foo');
|
|
287
|
-
|
|
288
|
-
expect(createSubgraphSpy).not.toHaveBeenCalled();
|
|
289
|
-
expect(returnedSubgraph).toBe(createdSubgraph);
|
|
290
|
-
});
|
|
291
|
-
|
|
292
|
-
test('with attributes', () => {
|
|
293
|
-
const createdSubgraph = g.createSubgraph('foo');
|
|
294
|
-
const createSubgraphSpy = vi.spyOn(g, 'createSubgraph');
|
|
295
|
-
|
|
296
|
-
const returnedSubgraph = g.subgraph('foo', { [_.label]: 'Test label' });
|
|
297
|
-
|
|
298
|
-
expect(createSubgraphSpy).not.toHaveBeenCalled();
|
|
299
|
-
expect(returnedSubgraph).toBe(createdSubgraph);
|
|
300
|
-
|
|
301
|
-
expect(returnedSubgraph.get(_.label)).toStrictEqual('Test label');
|
|
302
|
-
});
|
|
303
|
-
});
|
|
304
|
-
});
|
|
305
|
-
|
|
306
|
-
describe('graph method', () => {
|
|
307
|
-
test('apply atttibutes to graphs in graph', () => {
|
|
308
|
-
g.graph({ [_.label]: 'Test label' });
|
|
309
|
-
expect(g.attributes.graph.get(_.label)).toStrictEqual('Test label');
|
|
310
|
-
});
|
|
311
|
-
});
|
|
312
|
-
});
|
|
313
|
-
|
|
314
|
-
describe('Models Context API', () => {
|
|
315
|
-
class TestNode extends Node {}
|
|
316
|
-
class TestEdge extends Edge {}
|
|
317
|
-
class TestSubgraphA extends Subgraph {}
|
|
318
|
-
class TestSubgraphB extends Subgraph {}
|
|
319
|
-
class TestSubgraphC extends Subgraph {}
|
|
320
|
-
|
|
321
|
-
describe('By providing a context in the with method, the object created by createXxx is created with the model given by the context.', () => {
|
|
322
|
-
test('with TestNode', () => {
|
|
323
|
-
g.with({ Node: TestNode });
|
|
324
|
-
|
|
325
|
-
const node = g.createNode('hoge');
|
|
326
|
-
expect(node).toBeInstanceOf(TestNode);
|
|
327
|
-
});
|
|
328
|
-
|
|
329
|
-
test('with TestEdge', () => {
|
|
330
|
-
g.with({ Edge: TestEdge });
|
|
331
|
-
|
|
332
|
-
const edge = g.createEdge(['foo', 'bar']);
|
|
333
|
-
expect(edge).toBeInstanceOf(TestEdge);
|
|
334
|
-
});
|
|
335
|
-
|
|
336
|
-
test('with TestSubgraph', () => {
|
|
337
|
-
g.with({ Subgraph: TestSubgraphA });
|
|
338
|
-
|
|
339
|
-
const subgraph = g.createSubgraph();
|
|
340
|
-
expect(subgraph).toBeInstanceOf(TestSubgraphA);
|
|
341
|
-
});
|
|
342
|
-
|
|
343
|
-
test('In nested contexts, the settings are inherited. It also does not affect the parent context.', () => {
|
|
344
|
-
const sub = g.createSubgraph();
|
|
345
|
-
expect(sub).toBeInstanceOf(Subgraph);
|
|
346
|
-
|
|
347
|
-
sub.with({ Subgraph: TestSubgraphA });
|
|
348
|
-
const A = sub.createSubgraph();
|
|
349
|
-
expect(A).toBeInstanceOf(TestSubgraphA);
|
|
350
|
-
|
|
351
|
-
A.with({ Subgraph: TestSubgraphB });
|
|
352
|
-
const B = A.createSubgraph();
|
|
353
|
-
expect(B).toBeInstanceOf(TestSubgraphB);
|
|
354
|
-
|
|
355
|
-
B.with({ Subgraph: TestSubgraphC });
|
|
356
|
-
const C = B.createSubgraph();
|
|
357
|
-
expect(C).toBeInstanceOf(TestSubgraphC);
|
|
358
|
-
|
|
359
|
-
expect(sub.createSubgraph()).toBeInstanceOf(TestSubgraphA);
|
|
360
|
-
expect(A.createSubgraph()).toBeInstanceOf(TestSubgraphB);
|
|
361
|
-
expect(B.createSubgraph()).toBeInstanceOf(TestSubgraphC);
|
|
362
|
-
});
|
|
363
|
-
});
|
|
364
|
-
});
|
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
|
-
});
|