@packtrack/layout 1.0.2 → 1.0.4
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/.built/positioner/point.d.ts +2 -2
- package/.built/source/device/channel.d.ts +8 -0
- package/.built/source/device/channel.js +16 -0
- package/.built/source/device/device.d.ts +11 -0
- package/.built/source/device/device.js +24 -0
- package/.built/{district.d.ts → source/district.d.ts} +1 -1
- package/.built/{district.js → source/district.js} +2 -2
- package/.built/{index.d.ts → source/layout.d.ts} +1 -1
- package/.built/{index.js → source/layout.js} +2 -2
- package/.built/{track.d.ts → source/track.d.ts} +1 -1
- package/index.ts +9 -399
- package/package.json +1 -1
- package/positioner/point.ts +2 -2
- package/{district.ts → source/district.ts} +1 -1
- package/source/layout.ts +399 -0
- package/{track.ts → source/track.ts} +1 -1
- package/tsconfig.json +2 -1
- /package/.built/{position.d.ts → source/position.d.ts} +0 -0
- /package/.built/{position.js → source/position.js} +0 -0
- /package/.built/{power-district.d.ts → source/power-district.d.ts} +0 -0
- /package/.built/{power-district.js → source/power-district.js} +0 -0
- /package/.built/{route.d.ts → source/route.d.ts} +0 -0
- /package/.built/{route.js → source/route.js} +0 -0
- /package/.built/{router.d.ts → source/router.d.ts} +0 -0
- /package/.built/{router.js → source/router.js} +0 -0
- /package/.built/{section.d.ts → source/section.d.ts} +0 -0
- /package/.built/{section.js → source/section.js} +0 -0
- /package/.built/{tile.d.ts → source/tile.d.ts} +0 -0
- /package/.built/{tile.js → source/tile.js} +0 -0
- /package/.built/{track.js → source/track.js} +0 -0
- /package/{device → source/device}/channel.ts +0 -0
- /package/{device → source/device}/device.ts +0 -0
- /package/{position.ts → source/position.ts} +0 -0
- /package/{power-district.ts → source/power-district.ts} +0 -0
- /package/{route.ts → source/route.ts} +0 -0
- /package/{router.ts → source/router.ts} +0 -0
- /package/{section.ts → source/section.ts} +0 -0
- /package/{tile.ts → source/tile.ts} +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Positioner } from ".";
|
|
2
|
-
import { Channel } from "../device/channel";
|
|
3
|
-
import { Track } from "../track";
|
|
2
|
+
import { Channel } from "../source/device/channel";
|
|
3
|
+
import { Track } from "../source/track";
|
|
4
4
|
import { ResponderType } from "./responder-type";
|
|
5
5
|
export declare class PointPositioner extends Positioner {
|
|
6
6
|
track: Track;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Channel = void 0;
|
|
4
|
+
class Channel {
|
|
5
|
+
device;
|
|
6
|
+
name;
|
|
7
|
+
constructor(device, name) {
|
|
8
|
+
this.device = device;
|
|
9
|
+
this.name = name;
|
|
10
|
+
}
|
|
11
|
+
dump() {
|
|
12
|
+
console.log(`Channel '${this.name}' on ${this.device.identifier}`);
|
|
13
|
+
}
|
|
14
|
+
publish(data) { }
|
|
15
|
+
}
|
|
16
|
+
exports.Channel = Channel;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Device = void 0;
|
|
4
|
+
class Device {
|
|
5
|
+
identifier;
|
|
6
|
+
channels = [];
|
|
7
|
+
lastDiscovery;
|
|
8
|
+
constructor(identifier) {
|
|
9
|
+
this.identifier = identifier;
|
|
10
|
+
}
|
|
11
|
+
dump() {
|
|
12
|
+
console.group(`Device ${this.identifier}`);
|
|
13
|
+
if (this.lastDiscovery) {
|
|
14
|
+
console.log(`last discovery: ${this.lastDiscovery.date.toISOString()} ${this.lastDiscovery.address}`);
|
|
15
|
+
}
|
|
16
|
+
console.group('channels');
|
|
17
|
+
for (let channel of this.channels) {
|
|
18
|
+
channel.dump();
|
|
19
|
+
}
|
|
20
|
+
console.groupEnd();
|
|
21
|
+
console.groupEnd();
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
exports.Device = Device;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.District = void 0;
|
|
4
|
-
const
|
|
4
|
+
const layout_1 = require("./layout");
|
|
5
5
|
class District {
|
|
6
6
|
name;
|
|
7
7
|
parent;
|
|
@@ -14,7 +14,7 @@ class District {
|
|
|
14
14
|
this.parent = parent;
|
|
15
15
|
}
|
|
16
16
|
get domainName() {
|
|
17
|
-
if (this.parent instanceof
|
|
17
|
+
if (this.parent instanceof layout_1.Layout) {
|
|
18
18
|
return `${this.name}.${this.parent.name}`;
|
|
19
19
|
}
|
|
20
20
|
return `${this.name}.${this.parent.domainName}`;
|
|
@@ -3,7 +3,7 @@ import { PowerDistrict } from "./power-district";
|
|
|
3
3
|
import { Router } from "./router";
|
|
4
4
|
import { Section } from "./section";
|
|
5
5
|
import { Device } from "./device/device";
|
|
6
|
-
import { ResponderType } from "
|
|
6
|
+
import { ResponderType } from "../positioner/responder-type";
|
|
7
7
|
import { Channel } from "./device/channel";
|
|
8
8
|
export declare class Layout {
|
|
9
9
|
name: string;
|
|
@@ -8,9 +8,9 @@ const router_1 = require("./router");
|
|
|
8
8
|
const section_1 = require("./section");
|
|
9
9
|
const tile_1 = require("./tile");
|
|
10
10
|
const track_1 = require("./track");
|
|
11
|
-
const point_1 = require("
|
|
11
|
+
const point_1 = require("../positioner/point");
|
|
12
12
|
const device_1 = require("./device/device");
|
|
13
|
-
const responder_type_1 = require("
|
|
13
|
+
const responder_type_1 = require("../positioner/responder-type");
|
|
14
14
|
const channel_1 = require("./device/channel");
|
|
15
15
|
class Layout {
|
|
16
16
|
name;
|
package/index.ts
CHANGED
|
@@ -1,399 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
import { ResponderType } from "./positioner/responder-type";
|
|
11
|
-
import { Channel } from "./device/channel";
|
|
12
|
-
|
|
13
|
-
export class Layout {
|
|
14
|
-
name: string;
|
|
15
|
-
|
|
16
|
-
districts: District[] = [];
|
|
17
|
-
|
|
18
|
-
devices: Device[] = [];
|
|
19
|
-
responderType: ResponderType[] = [];
|
|
20
|
-
|
|
21
|
-
get allDistricts() {
|
|
22
|
-
const districts: District[] = [];
|
|
23
|
-
|
|
24
|
-
function walkDistrict(district: District) {
|
|
25
|
-
districts.push(district);
|
|
26
|
-
|
|
27
|
-
for (let child of district.children) {
|
|
28
|
-
walkDistrict(child);
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
for (let district of this.districts) {
|
|
33
|
-
walkDistrict(district);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
return districts;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
static from(document: any) {
|
|
40
|
-
const layout = new Layout();
|
|
41
|
-
|
|
42
|
-
const railway = document.firstChild!;
|
|
43
|
-
layout.name = railway.getAttribute('name');
|
|
44
|
-
|
|
45
|
-
const version = railway.getAttribute('version');
|
|
46
|
-
|
|
47
|
-
if (version == '1') {
|
|
48
|
-
let district = railway.firstChild;
|
|
49
|
-
|
|
50
|
-
while (district) {
|
|
51
|
-
if (district.tagName == 'district') {
|
|
52
|
-
layout.districts.push(layout.loadDistrict(district, layout));
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
district = district.nextSibling;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
district = railway.firstChild;
|
|
59
|
-
let index = 0;
|
|
60
|
-
|
|
61
|
-
while (district) {
|
|
62
|
-
if (district.tagName == 'district') {
|
|
63
|
-
layout.linkDistrict(district, layout.districts[index]);
|
|
64
|
-
|
|
65
|
-
index++;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
district = district.nextSibling;
|
|
69
|
-
}
|
|
70
|
-
} else {
|
|
71
|
-
throw new Error(`unsupported railway definition file version '${version}'`);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
return layout;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
loadDistrict(source, parent: District | Layout) {
|
|
78
|
-
const district = new District(source.getAttribute('name'), parent);
|
|
79
|
-
|
|
80
|
-
let child = source.firstChild;
|
|
81
|
-
|
|
82
|
-
while (child) {
|
|
83
|
-
if (child.tagName == 'power-districts') {
|
|
84
|
-
let powerDistrict = child.firstChild;
|
|
85
|
-
|
|
86
|
-
while (powerDistrict) {
|
|
87
|
-
if (powerDistrict.tagName == 'power-district') {
|
|
88
|
-
district.powerDistricts.push(this.loadPowerDistrict(powerDistrict, district));
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
powerDistrict = powerDistrict.nextSibling;
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
if (child.tagName == 'section') {
|
|
96
|
-
this.loadSection(child, district);
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
if (child.tagName == 'router') {
|
|
100
|
-
district.routers.push(this.loadRouter(child, district));
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
if (child.tagName == 'district') {
|
|
104
|
-
district.children.push(this.loadDistrict(child, district));
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
child = child.nextSibling;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
return district;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
linkDistrict(source, district: District) {
|
|
114
|
-
let child = source.firstChild;
|
|
115
|
-
|
|
116
|
-
let sectionIndex = 0;
|
|
117
|
-
let childIndex = 0;
|
|
118
|
-
|
|
119
|
-
while (child) {
|
|
120
|
-
if (child.tagName == 'section') {
|
|
121
|
-
this.linkSection(child, district.sections[sectionIndex]);
|
|
122
|
-
|
|
123
|
-
sectionIndex++;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
if (child.tagName == 'router') {
|
|
127
|
-
this.linkRouter(child, district.routers.find(router => router.name == child.getAttribute('name'))!);
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
if (child.tagName == 'district') {
|
|
131
|
-
this.linkDistrict(child, district.children[childIndex]);
|
|
132
|
-
|
|
133
|
-
childIndex++;
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
child = child.nextSibling;
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
loadSection(source, district: District) {
|
|
141
|
-
const section = new Section(source.getAttribute('name'), district);
|
|
142
|
-
district.sections.push(section);
|
|
143
|
-
|
|
144
|
-
let child = source.firstChild;
|
|
145
|
-
|
|
146
|
-
while (child) {
|
|
147
|
-
if (child.tagName == 'tracks') {
|
|
148
|
-
let trackNode = child.firstChild;
|
|
149
|
-
|
|
150
|
-
while (trackNode) {
|
|
151
|
-
if (trackNode.tagName == 'track') {
|
|
152
|
-
const track = new Track(
|
|
153
|
-
section,
|
|
154
|
-
+trackNode.getAttribute('length'),
|
|
155
|
-
trackNode.getAttribute('path')
|
|
156
|
-
);
|
|
157
|
-
|
|
158
|
-
section.tracks.push(track);
|
|
159
|
-
|
|
160
|
-
let trackChild = trackNode.firstChild;
|
|
161
|
-
|
|
162
|
-
while (trackChild) {
|
|
163
|
-
if (trackChild.tagName == 'positioners') {
|
|
164
|
-
let positioner = trackChild.firstChild;
|
|
165
|
-
|
|
166
|
-
while (positioner) {
|
|
167
|
-
if (positioner.tagName == 'point') {
|
|
168
|
-
const device = this.findDevice(positioner.getAttribute('device'));
|
|
169
|
-
const channel = this.findChannel(device, positioner.getAttribute('channel'));
|
|
170
|
-
const responderType = this.findResponderType(positioner.getAttribute('responder'));
|
|
171
|
-
|
|
172
|
-
track.positioners.push(new PointPositioner(
|
|
173
|
-
track,
|
|
174
|
-
+positioner.getAttribute('offset'),
|
|
175
|
-
channel,
|
|
176
|
-
responderType
|
|
177
|
-
));
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
positioner = positioner.nextSibling;
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
trackChild = trackChild.nextSibling;
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
trackNode = trackNode.nextSibling;
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
if (child.tagName == 'tile') {
|
|
193
|
-
const pattern = child.getAttribute('pattern');
|
|
194
|
-
|
|
195
|
-
if (!(pattern in TilePattern.patterns)) {
|
|
196
|
-
throw new Error(`Unknown tile pattern '${pattern}' in tile ${section.tiles.length + 1} in ${section.domainName}`);
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
section.tiles.push(new Tile(section, +child.getAttribute('x'), +child.getAttribute('y'), TilePattern.patterns[pattern]))
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
child = child.nextSibling;
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
findDevice(identifier: string) {
|
|
207
|
-
let device = this.devices.find(device => device.identifier == identifier);
|
|
208
|
-
|
|
209
|
-
if (device) {
|
|
210
|
-
return device;
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
device = new Device(identifier);
|
|
214
|
-
this.devices.push(device);
|
|
215
|
-
|
|
216
|
-
return device;
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
findChannel(device: Device, name: string) {
|
|
220
|
-
let channel = device.channels.find(channel => channel.name == name);
|
|
221
|
-
|
|
222
|
-
if (channel) {
|
|
223
|
-
return channel;
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
channel = new Channel(device, name);
|
|
227
|
-
device.channels.push(channel);
|
|
228
|
-
|
|
229
|
-
return channel;
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
findResponderType(name: string) {
|
|
233
|
-
let type = this.responderType.find(type => type.name == name);
|
|
234
|
-
|
|
235
|
-
if (type) {
|
|
236
|
-
return type;
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
type = new ResponderType(name);
|
|
240
|
-
this.responderType.push(type);
|
|
241
|
-
|
|
242
|
-
return type;
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
linkSection(source, section: Section) {
|
|
246
|
-
let child = source.firstChild;
|
|
247
|
-
|
|
248
|
-
while (child) {
|
|
249
|
-
if (child.tagName == 'out') {
|
|
250
|
-
const out = this.findSection(child.getAttribute('section'), section.district);
|
|
251
|
-
|
|
252
|
-
section.out = out;
|
|
253
|
-
out.in = section;
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
child = child.nextSibling;
|
|
257
|
-
}
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
findSection(path: string, base: District, source = base) {
|
|
261
|
-
const parts = path.split('.');
|
|
262
|
-
|
|
263
|
-
if (parts.length == 0) {
|
|
264
|
-
throw `section '${path}' not found from '${source.name}': invalid name`;
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
if (parts.length == 1) {
|
|
268
|
-
const localSection = base.sections.find(section => section.name == parts[0]);
|
|
269
|
-
|
|
270
|
-
if (!localSection) {
|
|
271
|
-
throw new Error(`Section '${path}' not found from '${source.name}': section does not exist in '${base.name}'`);
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
return localSection;
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
const sectionName = parts.pop()!;
|
|
278
|
-
|
|
279
|
-
let pool: District | Layout = base;
|
|
280
|
-
|
|
281
|
-
for (let index = 0; index < parts.length; index++) {
|
|
282
|
-
if (pool instanceof Layout || !pool.parent) {
|
|
283
|
-
throw new Error(`Section '${path}' could not be found from '${source.name}': district '${pool.name}' does not have a parent`);
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
pool = pool.parent!;
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
for (let part of parts) {
|
|
290
|
-
const child = (pool instanceof District ? pool.children : pool.districts).find(child => child.name == part);
|
|
291
|
-
|
|
292
|
-
if (!child) {
|
|
293
|
-
throw new Error(`Section '${path}' could not be found from '${source.name}': district '${pool.name}' does not have a child named '${part}'`);
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
pool = child;
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
if (pool instanceof Layout) {
|
|
300
|
-
throw new Error(`Section '${path}' could not be found from '${source.name}': a layout cannot directly include a section`);
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
return this.findSection(sectionName, pool, source);
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
loadRouter(source, district: District) {
|
|
307
|
-
const router = new Router(source.getAttribute('name'), district);
|
|
308
|
-
|
|
309
|
-
return router;
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
linkRouter(source, router: Router) {
|
|
313
|
-
let child = source.firstChild;
|
|
314
|
-
|
|
315
|
-
while (child) {
|
|
316
|
-
if (child.tagName == 'route') {
|
|
317
|
-
const route = new Route(child.getAttribute('name'), router);
|
|
318
|
-
|
|
319
|
-
route.in = this.findSection(child.getAttribute('in'), router.district);
|
|
320
|
-
route.in.out = router;
|
|
321
|
-
|
|
322
|
-
route.out = this.findSection(child.getAttribute('out'), router.district);
|
|
323
|
-
route.out.in = router;
|
|
324
|
-
|
|
325
|
-
router.routes.push(route);
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
child = child.nextSibling;
|
|
329
|
-
}
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
loadPowerDistrict(source, district: District) {
|
|
333
|
-
const powerDistrict = new PowerDistrict(source.getAttribute('name'), district);
|
|
334
|
-
|
|
335
|
-
return powerDistrict;
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
toDot() {
|
|
339
|
-
let dot = 'digraph G {';
|
|
340
|
-
|
|
341
|
-
for (let district of this.districts) {
|
|
342
|
-
dot += district.toDotDefinition();
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
for (let district of this.districts) {
|
|
346
|
-
dot += district.toDotConnection();
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
return `${dot}}`;
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
toSVG(inject = '') {
|
|
353
|
-
const positons = this.districts.map(district => district.findSVGPositions()).flat(Infinity);
|
|
354
|
-
|
|
355
|
-
const width = Math.max(...positons.map(position => position.x));
|
|
356
|
-
const height = Math.max(...positons.map(position => position.y));
|
|
357
|
-
|
|
358
|
-
let svg = `<svg width="100vw" height="100vh" viewBox="0 0 ${width + 1} ${height + 1}" xmlns="http://www.w3.org/2000/svg">
|
|
359
|
-
<style>
|
|
360
|
-
|
|
361
|
-
path {
|
|
362
|
-
fill: none;
|
|
363
|
-
stroke: #000;
|
|
364
|
-
stroke-width: 0.2;
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
</style>
|
|
368
|
-
`;
|
|
369
|
-
|
|
370
|
-
for (let district of this.districts) {
|
|
371
|
-
svg += district.toSVG();
|
|
372
|
-
}
|
|
373
|
-
|
|
374
|
-
return `${svg}${inject}</svg>`;
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
dump() {
|
|
378
|
-
console.group(`Layout ${this.name}`);
|
|
379
|
-
console.log('devices');
|
|
380
|
-
|
|
381
|
-
for (let device of this.devices) {
|
|
382
|
-
device.dump();
|
|
383
|
-
}
|
|
384
|
-
|
|
385
|
-
console.log('responder types');
|
|
386
|
-
|
|
387
|
-
for (let type of this.responderType) {
|
|
388
|
-
type.dump();
|
|
389
|
-
}
|
|
390
|
-
|
|
391
|
-
console.log('districts');
|
|
392
|
-
|
|
393
|
-
for (let district of this.districts) {
|
|
394
|
-
district.dump();
|
|
395
|
-
}
|
|
396
|
-
|
|
397
|
-
console.groupEnd();
|
|
398
|
-
}
|
|
399
|
-
}
|
|
1
|
+
export * from './source/district';
|
|
2
|
+
export * from './source/layout';
|
|
3
|
+
export * from './source/position';
|
|
4
|
+
export * from './source/power-district';
|
|
5
|
+
export * from './source/route';
|
|
6
|
+
export * from './source/router';
|
|
7
|
+
export * from './source/section';
|
|
8
|
+
export * from './source/tile';
|
|
9
|
+
export * from './source/track';
|
package/package.json
CHANGED
package/positioner/point.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Positioner } from ".";
|
|
2
|
-
import { Channel } from "../device/channel";
|
|
3
|
-
import { Track } from "../track";
|
|
2
|
+
import { Channel } from "../source/device/channel";
|
|
3
|
+
import { Track } from "../source/track";
|
|
4
4
|
import { ResponderType } from "./responder-type";
|
|
5
5
|
|
|
6
6
|
export class PointPositioner extends Positioner {
|
package/source/layout.ts
ADDED
|
@@ -0,0 +1,399 @@
|
|
|
1
|
+
import { District } from "./district";
|
|
2
|
+
import { PowerDistrict } from "./power-district";
|
|
3
|
+
import { Route } from "./route";
|
|
4
|
+
import { Router } from "./router";
|
|
5
|
+
import { Section } from "./section";
|
|
6
|
+
import { TilePattern, Tile } from "./tile";
|
|
7
|
+
import { Track } from "./track";
|
|
8
|
+
import { PointPositioner } from "../positioner/point";
|
|
9
|
+
import { Device } from "./device/device";
|
|
10
|
+
import { ResponderType } from "../positioner/responder-type";
|
|
11
|
+
import { Channel } from "./device/channel";
|
|
12
|
+
|
|
13
|
+
export class Layout {
|
|
14
|
+
name: string;
|
|
15
|
+
|
|
16
|
+
districts: District[] = [];
|
|
17
|
+
|
|
18
|
+
devices: Device[] = [];
|
|
19
|
+
responderType: ResponderType[] = [];
|
|
20
|
+
|
|
21
|
+
get allDistricts() {
|
|
22
|
+
const districts: District[] = [];
|
|
23
|
+
|
|
24
|
+
function walkDistrict(district: District) {
|
|
25
|
+
districts.push(district);
|
|
26
|
+
|
|
27
|
+
for (let child of district.children) {
|
|
28
|
+
walkDistrict(child);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
for (let district of this.districts) {
|
|
33
|
+
walkDistrict(district);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return districts;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
static from(document: any) {
|
|
40
|
+
const layout = new Layout();
|
|
41
|
+
|
|
42
|
+
const railway = document.firstChild!;
|
|
43
|
+
layout.name = railway.getAttribute('name');
|
|
44
|
+
|
|
45
|
+
const version = railway.getAttribute('version');
|
|
46
|
+
|
|
47
|
+
if (version == '1') {
|
|
48
|
+
let district = railway.firstChild;
|
|
49
|
+
|
|
50
|
+
while (district) {
|
|
51
|
+
if (district.tagName == 'district') {
|
|
52
|
+
layout.districts.push(layout.loadDistrict(district, layout));
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
district = district.nextSibling;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
district = railway.firstChild;
|
|
59
|
+
let index = 0;
|
|
60
|
+
|
|
61
|
+
while (district) {
|
|
62
|
+
if (district.tagName == 'district') {
|
|
63
|
+
layout.linkDistrict(district, layout.districts[index]);
|
|
64
|
+
|
|
65
|
+
index++;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
district = district.nextSibling;
|
|
69
|
+
}
|
|
70
|
+
} else {
|
|
71
|
+
throw new Error(`unsupported railway definition file version '${version}'`);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return layout;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
loadDistrict(source, parent: District | Layout) {
|
|
78
|
+
const district = new District(source.getAttribute('name'), parent);
|
|
79
|
+
|
|
80
|
+
let child = source.firstChild;
|
|
81
|
+
|
|
82
|
+
while (child) {
|
|
83
|
+
if (child.tagName == 'power-districts') {
|
|
84
|
+
let powerDistrict = child.firstChild;
|
|
85
|
+
|
|
86
|
+
while (powerDistrict) {
|
|
87
|
+
if (powerDistrict.tagName == 'power-district') {
|
|
88
|
+
district.powerDistricts.push(this.loadPowerDistrict(powerDistrict, district));
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
powerDistrict = powerDistrict.nextSibling;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (child.tagName == 'section') {
|
|
96
|
+
this.loadSection(child, district);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (child.tagName == 'router') {
|
|
100
|
+
district.routers.push(this.loadRouter(child, district));
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
if (child.tagName == 'district') {
|
|
104
|
+
district.children.push(this.loadDistrict(child, district));
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
child = child.nextSibling;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return district;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
linkDistrict(source, district: District) {
|
|
114
|
+
let child = source.firstChild;
|
|
115
|
+
|
|
116
|
+
let sectionIndex = 0;
|
|
117
|
+
let childIndex = 0;
|
|
118
|
+
|
|
119
|
+
while (child) {
|
|
120
|
+
if (child.tagName == 'section') {
|
|
121
|
+
this.linkSection(child, district.sections[sectionIndex]);
|
|
122
|
+
|
|
123
|
+
sectionIndex++;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if (child.tagName == 'router') {
|
|
127
|
+
this.linkRouter(child, district.routers.find(router => router.name == child.getAttribute('name'))!);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
if (child.tagName == 'district') {
|
|
131
|
+
this.linkDistrict(child, district.children[childIndex]);
|
|
132
|
+
|
|
133
|
+
childIndex++;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
child = child.nextSibling;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
loadSection(source, district: District) {
|
|
141
|
+
const section = new Section(source.getAttribute('name'), district);
|
|
142
|
+
district.sections.push(section);
|
|
143
|
+
|
|
144
|
+
let child = source.firstChild;
|
|
145
|
+
|
|
146
|
+
while (child) {
|
|
147
|
+
if (child.tagName == 'tracks') {
|
|
148
|
+
let trackNode = child.firstChild;
|
|
149
|
+
|
|
150
|
+
while (trackNode) {
|
|
151
|
+
if (trackNode.tagName == 'track') {
|
|
152
|
+
const track = new Track(
|
|
153
|
+
section,
|
|
154
|
+
+trackNode.getAttribute('length'),
|
|
155
|
+
trackNode.getAttribute('path')
|
|
156
|
+
);
|
|
157
|
+
|
|
158
|
+
section.tracks.push(track);
|
|
159
|
+
|
|
160
|
+
let trackChild = trackNode.firstChild;
|
|
161
|
+
|
|
162
|
+
while (trackChild) {
|
|
163
|
+
if (trackChild.tagName == 'positioners') {
|
|
164
|
+
let positioner = trackChild.firstChild;
|
|
165
|
+
|
|
166
|
+
while (positioner) {
|
|
167
|
+
if (positioner.tagName == 'point') {
|
|
168
|
+
const device = this.findDevice(positioner.getAttribute('device'));
|
|
169
|
+
const channel = this.findChannel(device, positioner.getAttribute('channel'));
|
|
170
|
+
const responderType = this.findResponderType(positioner.getAttribute('responder'));
|
|
171
|
+
|
|
172
|
+
track.positioners.push(new PointPositioner(
|
|
173
|
+
track,
|
|
174
|
+
+positioner.getAttribute('offset'),
|
|
175
|
+
channel,
|
|
176
|
+
responderType
|
|
177
|
+
));
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
positioner = positioner.nextSibling;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
trackChild = trackChild.nextSibling;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
trackNode = trackNode.nextSibling;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
if (child.tagName == 'tile') {
|
|
193
|
+
const pattern = child.getAttribute('pattern');
|
|
194
|
+
|
|
195
|
+
if (!(pattern in TilePattern.patterns)) {
|
|
196
|
+
throw new Error(`Unknown tile pattern '${pattern}' in tile ${section.tiles.length + 1} in ${section.domainName}`);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
section.tiles.push(new Tile(section, +child.getAttribute('x'), +child.getAttribute('y'), TilePattern.patterns[pattern]))
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
child = child.nextSibling;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
findDevice(identifier: string) {
|
|
207
|
+
let device = this.devices.find(device => device.identifier == identifier);
|
|
208
|
+
|
|
209
|
+
if (device) {
|
|
210
|
+
return device;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
device = new Device(identifier);
|
|
214
|
+
this.devices.push(device);
|
|
215
|
+
|
|
216
|
+
return device;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
findChannel(device: Device, name: string) {
|
|
220
|
+
let channel = device.channels.find(channel => channel.name == name);
|
|
221
|
+
|
|
222
|
+
if (channel) {
|
|
223
|
+
return channel;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
channel = new Channel(device, name);
|
|
227
|
+
device.channels.push(channel);
|
|
228
|
+
|
|
229
|
+
return channel;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
findResponderType(name: string) {
|
|
233
|
+
let type = this.responderType.find(type => type.name == name);
|
|
234
|
+
|
|
235
|
+
if (type) {
|
|
236
|
+
return type;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
type = new ResponderType(name);
|
|
240
|
+
this.responderType.push(type);
|
|
241
|
+
|
|
242
|
+
return type;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
linkSection(source, section: Section) {
|
|
246
|
+
let child = source.firstChild;
|
|
247
|
+
|
|
248
|
+
while (child) {
|
|
249
|
+
if (child.tagName == 'out') {
|
|
250
|
+
const out = this.findSection(child.getAttribute('section'), section.district);
|
|
251
|
+
|
|
252
|
+
section.out = out;
|
|
253
|
+
out.in = section;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
child = child.nextSibling;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
findSection(path: string, base: District, source = base) {
|
|
261
|
+
const parts = path.split('.');
|
|
262
|
+
|
|
263
|
+
if (parts.length == 0) {
|
|
264
|
+
throw `section '${path}' not found from '${source.name}': invalid name`;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
if (parts.length == 1) {
|
|
268
|
+
const localSection = base.sections.find(section => section.name == parts[0]);
|
|
269
|
+
|
|
270
|
+
if (!localSection) {
|
|
271
|
+
throw new Error(`Section '${path}' not found from '${source.name}': section does not exist in '${base.name}'`);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
return localSection;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
const sectionName = parts.pop()!;
|
|
278
|
+
|
|
279
|
+
let pool: District | Layout = base;
|
|
280
|
+
|
|
281
|
+
for (let index = 0; index < parts.length; index++) {
|
|
282
|
+
if (pool instanceof Layout || !pool.parent) {
|
|
283
|
+
throw new Error(`Section '${path}' could not be found from '${source.name}': district '${pool.name}' does not have a parent`);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
pool = pool.parent!;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
for (let part of parts) {
|
|
290
|
+
const child = (pool instanceof District ? pool.children : pool.districts).find(child => child.name == part);
|
|
291
|
+
|
|
292
|
+
if (!child) {
|
|
293
|
+
throw new Error(`Section '${path}' could not be found from '${source.name}': district '${pool.name}' does not have a child named '${part}'`);
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
pool = child;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
if (pool instanceof Layout) {
|
|
300
|
+
throw new Error(`Section '${path}' could not be found from '${source.name}': a layout cannot directly include a section`);
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
return this.findSection(sectionName, pool, source);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
loadRouter(source, district: District) {
|
|
307
|
+
const router = new Router(source.getAttribute('name'), district);
|
|
308
|
+
|
|
309
|
+
return router;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
linkRouter(source, router: Router) {
|
|
313
|
+
let child = source.firstChild;
|
|
314
|
+
|
|
315
|
+
while (child) {
|
|
316
|
+
if (child.tagName == 'route') {
|
|
317
|
+
const route = new Route(child.getAttribute('name'), router);
|
|
318
|
+
|
|
319
|
+
route.in = this.findSection(child.getAttribute('in'), router.district);
|
|
320
|
+
route.in.out = router;
|
|
321
|
+
|
|
322
|
+
route.out = this.findSection(child.getAttribute('out'), router.district);
|
|
323
|
+
route.out.in = router;
|
|
324
|
+
|
|
325
|
+
router.routes.push(route);
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
child = child.nextSibling;
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
loadPowerDistrict(source, district: District) {
|
|
333
|
+
const powerDistrict = new PowerDistrict(source.getAttribute('name'), district);
|
|
334
|
+
|
|
335
|
+
return powerDistrict;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
toDot() {
|
|
339
|
+
let dot = 'digraph G {';
|
|
340
|
+
|
|
341
|
+
for (let district of this.districts) {
|
|
342
|
+
dot += district.toDotDefinition();
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
for (let district of this.districts) {
|
|
346
|
+
dot += district.toDotConnection();
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
return `${dot}}`;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
toSVG(inject = '') {
|
|
353
|
+
const positons = this.districts.map(district => district.findSVGPositions()).flat(Infinity);
|
|
354
|
+
|
|
355
|
+
const width = Math.max(...positons.map(position => position.x));
|
|
356
|
+
const height = Math.max(...positons.map(position => position.y));
|
|
357
|
+
|
|
358
|
+
let svg = `<svg width="100vw" height="100vh" viewBox="0 0 ${width + 1} ${height + 1}" xmlns="http://www.w3.org/2000/svg">
|
|
359
|
+
<style>
|
|
360
|
+
|
|
361
|
+
path {
|
|
362
|
+
fill: none;
|
|
363
|
+
stroke: #000;
|
|
364
|
+
stroke-width: 0.2;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
</style>
|
|
368
|
+
`;
|
|
369
|
+
|
|
370
|
+
for (let district of this.districts) {
|
|
371
|
+
svg += district.toSVG();
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
return `${svg}${inject}</svg>`;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
dump() {
|
|
378
|
+
console.group(`Layout ${this.name}`);
|
|
379
|
+
console.log('devices');
|
|
380
|
+
|
|
381
|
+
for (let device of this.devices) {
|
|
382
|
+
device.dump();
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
console.log('responder types');
|
|
386
|
+
|
|
387
|
+
for (let type of this.responderType) {
|
|
388
|
+
type.dump();
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
console.log('districts');
|
|
392
|
+
|
|
393
|
+
for (let district of this.districts) {
|
|
394
|
+
district.dump();
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
console.groupEnd();
|
|
398
|
+
}
|
|
399
|
+
}
|
package/tsconfig.json
CHANGED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|