@packtrack/layout 1.0.10 → 1.0.11

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.
Files changed (40) hide show
  1. package/.built/device/channel.js +5 -1
  2. package/.built/device/device.js +5 -1
  3. package/.built/district.js +7 -3
  4. package/.built/layout.js +29 -25
  5. package/.built/position.js +5 -1
  6. package/.built/positioner/index.js +5 -1
  7. package/.built/positioner/point.js +6 -2
  8. package/.built/positioner/responder-type.js +5 -1
  9. package/.built/power-district.js +5 -1
  10. package/.built/route.js +5 -1
  11. package/.built/router.js +5 -1
  12. package/.built/section.js +12 -8
  13. package/.built/source/device/channel.d.ts +8 -0
  14. package/.built/source/device/channel.js +16 -0
  15. package/.built/source/device/device.d.ts +11 -0
  16. package/.built/source/device/device.js +24 -0
  17. package/.built/source/district.d.ts +20 -0
  18. package/.built/source/district.js +86 -0
  19. package/.built/source/layout.d.ts +29 -0
  20. package/.built/source/layout.js +286 -0
  21. package/.built/source/position.d.ts +10 -0
  22. package/.built/source/position.js +33 -0
  23. package/.built/source/power-district.d.ts +8 -0
  24. package/.built/source/power-district.js +18 -0
  25. package/.built/source/route.d.ts +10 -0
  26. package/.built/source/route.js +17 -0
  27. package/.built/source/router.d.ts +14 -0
  28. package/.built/source/router.js +40 -0
  29. package/.built/source/section.d.ts +40 -0
  30. package/.built/source/section.js +217 -0
  31. package/.built/source/tile.d.ts +57 -0
  32. package/.built/source/tile.js +78 -0
  33. package/.built/source/track.d.ts +12 -0
  34. package/.built/source/track.js +29 -0
  35. package/.built/tile.js +7 -2
  36. package/.built/track.js +7 -3
  37. package/package.json +1 -1
  38. package/source/index.ts +3 -0
  39. package/.built/index.d.ts +0 -12
  40. package/.built/index.js +0 -12
@@ -0,0 +1,286 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Layout = void 0;
4
+ const district_1 = require("./district");
5
+ const power_district_1 = require("./power-district");
6
+ const route_1 = require("./route");
7
+ const router_1 = require("./router");
8
+ const section_1 = require("./section");
9
+ const tile_1 = require("./tile");
10
+ const track_1 = require("./track");
11
+ const point_1 = require("../positioner/point");
12
+ const device_1 = require("./device/device");
13
+ const responder_type_1 = require("../positioner/responder-type");
14
+ const channel_1 = require("./device/channel");
15
+ class Layout {
16
+ name;
17
+ districts = [];
18
+ devices = [];
19
+ responderType = [];
20
+ get allDistricts() {
21
+ const districts = [];
22
+ function walkDistrict(district) {
23
+ districts.push(district);
24
+ for (let child of district.children) {
25
+ walkDistrict(child);
26
+ }
27
+ }
28
+ for (let district of this.districts) {
29
+ walkDistrict(district);
30
+ }
31
+ return districts;
32
+ }
33
+ static from(document) {
34
+ const layout = new Layout();
35
+ const railway = document.firstChild;
36
+ layout.name = railway.getAttribute('name');
37
+ const version = railway.getAttribute('version');
38
+ if (version == '1') {
39
+ let district = railway.firstChild;
40
+ while (district) {
41
+ if (district.tagName == 'district') {
42
+ layout.districts.push(layout.loadDistrict(district, layout));
43
+ }
44
+ district = district.nextSibling;
45
+ }
46
+ district = railway.firstChild;
47
+ let index = 0;
48
+ while (district) {
49
+ if (district.tagName == 'district') {
50
+ layout.linkDistrict(district, layout.districts[index]);
51
+ index++;
52
+ }
53
+ district = district.nextSibling;
54
+ }
55
+ }
56
+ else {
57
+ throw new Error(`unsupported railway definition file version '${version}'`);
58
+ }
59
+ return layout;
60
+ }
61
+ loadDistrict(source, parent) {
62
+ const district = new district_1.District(source.getAttribute('name'), parent);
63
+ let child = source.firstChild;
64
+ while (child) {
65
+ if (child.tagName == 'power-districts') {
66
+ let powerDistrict = child.firstChild;
67
+ while (powerDistrict) {
68
+ if (powerDistrict.tagName == 'power-district') {
69
+ district.powerDistricts.push(this.loadPowerDistrict(powerDistrict, district));
70
+ }
71
+ powerDistrict = powerDistrict.nextSibling;
72
+ }
73
+ }
74
+ if (child.tagName == 'section') {
75
+ this.loadSection(child, district);
76
+ }
77
+ if (child.tagName == 'router') {
78
+ district.routers.push(this.loadRouter(child, district));
79
+ }
80
+ if (child.tagName == 'district') {
81
+ district.children.push(this.loadDistrict(child, district));
82
+ }
83
+ child = child.nextSibling;
84
+ }
85
+ return district;
86
+ }
87
+ linkDistrict(source, district) {
88
+ let child = source.firstChild;
89
+ let sectionIndex = 0;
90
+ let childIndex = 0;
91
+ while (child) {
92
+ if (child.tagName == 'section') {
93
+ this.linkSection(child, district.sections[sectionIndex]);
94
+ sectionIndex++;
95
+ }
96
+ if (child.tagName == 'router') {
97
+ this.linkRouter(child, district.routers.find(router => router.name == child.getAttribute('name')));
98
+ }
99
+ if (child.tagName == 'district') {
100
+ this.linkDistrict(child, district.children[childIndex]);
101
+ childIndex++;
102
+ }
103
+ child = child.nextSibling;
104
+ }
105
+ }
106
+ loadSection(source, district) {
107
+ const section = new section_1.Section(source.getAttribute('name'), district);
108
+ district.sections.push(section);
109
+ let child = source.firstChild;
110
+ while (child) {
111
+ if (child.tagName == 'tracks') {
112
+ let trackNode = child.firstChild;
113
+ while (trackNode) {
114
+ if (trackNode.tagName == 'track') {
115
+ const track = new track_1.Track(section, +trackNode.getAttribute('length'), trackNode.getAttribute('path'));
116
+ section.tracks.push(track);
117
+ let trackChild = trackNode.firstChild;
118
+ while (trackChild) {
119
+ if (trackChild.tagName == 'positioners') {
120
+ let positioner = trackChild.firstChild;
121
+ while (positioner) {
122
+ if (positioner.tagName == 'point') {
123
+ const device = this.findDevice(positioner.getAttribute('device'));
124
+ const channel = this.findChannel(device, positioner.getAttribute('channel'));
125
+ const responderType = this.findResponderType(positioner.getAttribute('responder'));
126
+ track.positioners.push(new point_1.PointPositioner(track, +positioner.getAttribute('offset'), channel, responderType));
127
+ }
128
+ positioner = positioner.nextSibling;
129
+ }
130
+ }
131
+ trackChild = trackChild.nextSibling;
132
+ }
133
+ }
134
+ trackNode = trackNode.nextSibling;
135
+ }
136
+ }
137
+ if (child.tagName == 'tile') {
138
+ const pattern = child.getAttribute('pattern');
139
+ if (!(pattern in tile_1.TilePattern.patterns)) {
140
+ throw new Error(`Unknown tile pattern '${pattern}' in tile ${section.tiles.length + 1} in ${section.domainName}`);
141
+ }
142
+ section.tiles.push(new tile_1.Tile(section, +child.getAttribute('x'), +child.getAttribute('y'), tile_1.TilePattern.patterns[pattern]));
143
+ }
144
+ child = child.nextSibling;
145
+ }
146
+ }
147
+ findDevice(identifier) {
148
+ let device = this.devices.find(device => device.identifier == identifier);
149
+ if (device) {
150
+ return device;
151
+ }
152
+ device = new device_1.Device(identifier);
153
+ this.devices.push(device);
154
+ return device;
155
+ }
156
+ findChannel(device, name) {
157
+ let channel = device.channels.find(channel => channel.name == name);
158
+ if (channel) {
159
+ return channel;
160
+ }
161
+ channel = new channel_1.Channel(device, name);
162
+ device.channels.push(channel);
163
+ return channel;
164
+ }
165
+ findResponderType(name) {
166
+ let type = this.responderType.find(type => type.name == name);
167
+ if (type) {
168
+ return type;
169
+ }
170
+ type = new responder_type_1.ResponderType(name);
171
+ this.responderType.push(type);
172
+ return type;
173
+ }
174
+ linkSection(source, section) {
175
+ let child = source.firstChild;
176
+ while (child) {
177
+ if (child.tagName == 'out') {
178
+ const out = this.findSection(child.getAttribute('section'), section.district);
179
+ section.out = out;
180
+ out.in = section;
181
+ }
182
+ child = child.nextSibling;
183
+ }
184
+ }
185
+ findSection(path, base, source = base) {
186
+ const parts = path.split('.');
187
+ if (parts.length == 0) {
188
+ throw `section '${path}' not found from '${source.name}': invalid name`;
189
+ }
190
+ if (parts.length == 1) {
191
+ const localSection = base.sections.find(section => section.name == parts[0]);
192
+ if (!localSection) {
193
+ throw new Error(`Section '${path}' not found from '${source.name}': section does not exist in '${base.name}'`);
194
+ }
195
+ return localSection;
196
+ }
197
+ const sectionName = parts.pop();
198
+ let pool = base;
199
+ for (let index = 0; index < parts.length; index++) {
200
+ if (pool instanceof Layout || !pool.parent) {
201
+ throw new Error(`Section '${path}' could not be found from '${source.name}': district '${pool.name}' does not have a parent`);
202
+ }
203
+ pool = pool.parent;
204
+ }
205
+ for (let part of parts) {
206
+ const child = (pool instanceof district_1.District ? pool.children : pool.districts).find(child => child.name == part);
207
+ if (!child) {
208
+ throw new Error(`Section '${path}' could not be found from '${source.name}': district '${pool.name}' does not have a child named '${part}'`);
209
+ }
210
+ pool = child;
211
+ }
212
+ if (pool instanceof Layout) {
213
+ throw new Error(`Section '${path}' could not be found from '${source.name}': a layout cannot directly include a section`);
214
+ }
215
+ return this.findSection(sectionName, pool, source);
216
+ }
217
+ loadRouter(source, district) {
218
+ const router = new router_1.Router(source.getAttribute('name'), district);
219
+ return router;
220
+ }
221
+ linkRouter(source, router) {
222
+ let child = source.firstChild;
223
+ while (child) {
224
+ if (child.tagName == 'route') {
225
+ const route = new route_1.Route(child.getAttribute('name'), router);
226
+ route.in = this.findSection(child.getAttribute('in'), router.district);
227
+ route.in.out = router;
228
+ route.out = this.findSection(child.getAttribute('out'), router.district);
229
+ route.out.in = router;
230
+ router.routes.push(route);
231
+ }
232
+ child = child.nextSibling;
233
+ }
234
+ }
235
+ loadPowerDistrict(source, district) {
236
+ const powerDistrict = new power_district_1.PowerDistrict(source.getAttribute('name'), district);
237
+ return powerDistrict;
238
+ }
239
+ toDot() {
240
+ let dot = 'digraph G {';
241
+ for (let district of this.districts) {
242
+ dot += district.toDotDefinition();
243
+ }
244
+ for (let district of this.districts) {
245
+ dot += district.toDotConnection();
246
+ }
247
+ return `${dot}}`;
248
+ }
249
+ toSVG(inject = '') {
250
+ const positons = this.districts.map(district => district.findSVGPositions()).flat(Infinity);
251
+ const width = Math.max(...positons.map(position => position.x));
252
+ const height = Math.max(...positons.map(position => position.y));
253
+ let svg = `<svg width="100vw" height="100vh" viewBox="0 0 ${width + 1} ${height + 1}" xmlns="http://www.w3.org/2000/svg">
254
+ <style>
255
+
256
+ path {
257
+ fill: none;
258
+ stroke: #000;
259
+ stroke-width: 0.2;
260
+ }
261
+
262
+ </style>
263
+ `;
264
+ for (let district of this.districts) {
265
+ svg += district.toSVG();
266
+ }
267
+ return `${svg}${inject}</svg>`;
268
+ }
269
+ dump() {
270
+ console.group(`Layout ${this.name}`);
271
+ console.log('devices');
272
+ for (let device of this.devices) {
273
+ device.dump();
274
+ }
275
+ console.log('responder types');
276
+ for (let type of this.responderType) {
277
+ type.dump();
278
+ }
279
+ console.log('districts');
280
+ for (let district of this.districts) {
281
+ district.dump();
282
+ }
283
+ console.groupEnd();
284
+ }
285
+ }
286
+ exports.Layout = Layout;
@@ -0,0 +1,10 @@
1
+ import { Section } from './section.js';
2
+ export declare class SectionPosition {
3
+ section: Section;
4
+ offset: number;
5
+ reversed: boolean;
6
+ constructor(section: Section, offset: number, reversed: boolean);
7
+ get absolutePosition(): number;
8
+ advance(distance: number): any;
9
+ toString(): string;
10
+ }
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SectionPosition = void 0;
4
+ class SectionPosition {
5
+ section;
6
+ offset;
7
+ reversed;
8
+ constructor(section, offset, reversed) {
9
+ this.section = section;
10
+ this.offset = offset;
11
+ this.reversed = reversed;
12
+ }
13
+ get absolutePosition() {
14
+ if (this.reversed) {
15
+ return this.section.length - this.offset;
16
+ }
17
+ return this.offset;
18
+ }
19
+ advance(distance) {
20
+ if (this.offset + distance > this.section.length) {
21
+ const next = this.section.next(this.reversed);
22
+ if (!next) {
23
+ throw new Error(`Illegal advancement ${this} + ${distance}`);
24
+ }
25
+ return new SectionPosition(next, 0, this.reversed).advance(this.offset + distance - this.section.length);
26
+ }
27
+ return new SectionPosition(this.section, this.offset + distance, this.reversed);
28
+ }
29
+ toString() {
30
+ return `${this.section.name} @ ${this.offset.toFixed(1)} ${this.reversed ? 'backward' : 'forward'}`;
31
+ }
32
+ }
33
+ exports.SectionPosition = SectionPosition;
@@ -0,0 +1,8 @@
1
+ import { District } from "./district";
2
+ export declare class PowerDistrict {
3
+ name: string;
4
+ district: District;
5
+ constructor(name: string, district: District);
6
+ get domainName(): string;
7
+ dump(): void;
8
+ }
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PowerDistrict = void 0;
4
+ class PowerDistrict {
5
+ name;
6
+ district;
7
+ constructor(name, district) {
8
+ this.name = name;
9
+ this.district = district;
10
+ }
11
+ get domainName() {
12
+ return `${this.name}.${this.district.domainName}`;
13
+ }
14
+ dump() {
15
+ console.log(this.name);
16
+ }
17
+ }
18
+ exports.PowerDistrict = PowerDistrict;
@@ -0,0 +1,10 @@
1
+ import { Router } from "./router";
2
+ import { Section } from "./section";
3
+ export declare class Route {
4
+ name: string;
5
+ router: Router;
6
+ in: Section;
7
+ out: Section;
8
+ constructor(name: string, router: Router);
9
+ dump(): void;
10
+ }
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Route = void 0;
4
+ class Route {
5
+ name;
6
+ router;
7
+ in;
8
+ out;
9
+ constructor(name, router) {
10
+ this.name = name;
11
+ this.router = router;
12
+ }
13
+ dump() {
14
+ console.log(`Route ${this.name}: ${this.in.domainName} → ${this.out.domainName}`);
15
+ }
16
+ }
17
+ exports.Route = Route;
@@ -0,0 +1,14 @@
1
+ import { District } from "./district";
2
+ import { Route } from "./route";
3
+ export declare class Router {
4
+ name: string;
5
+ district: District;
6
+ activeRoute?: Route;
7
+ routes: Route[];
8
+ constructor(name: string, district: District);
9
+ get domainName(): string;
10
+ dump(): void;
11
+ toDotReference(): string;
12
+ toDotDefinition(): string;
13
+ toDotConnection(): string;
14
+ }
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Router = void 0;
4
+ class Router {
5
+ name;
6
+ district;
7
+ activeRoute;
8
+ routes = [];
9
+ constructor(name, district) {
10
+ this.name = name;
11
+ this.district = district;
12
+ }
13
+ get domainName() {
14
+ return `${this.name}.${this.district.domainName}`;
15
+ }
16
+ dump() {
17
+ console.group(`Router ${this.domainName}`);
18
+ for (let route of this.routes) {
19
+ route.dump();
20
+ }
21
+ console.groupEnd();
22
+ }
23
+ toDotReference() {
24
+ return `router_${this.name.replace(/-/g, '_')}_${this.district.toDotReference()}`;
25
+ }
26
+ toDotDefinition() {
27
+ return `
28
+ ${this.toDotReference()} [ label = ${JSON.stringify(this.name)}, shape = diamond ]
29
+ `;
30
+ }
31
+ toDotConnection() {
32
+ return `
33
+ ${this.routes.map(route => `
34
+ ${route.in.toDotReference()} -> ${this.toDotReference()} [ headlabel = ${JSON.stringify(route.name)} ]
35
+ ${this.toDotReference()} -> ${route.out.toDotReference()} [ taillabel = ${JSON.stringify(route.name)} ]
36
+ `).join('')}
37
+ `;
38
+ }
39
+ }
40
+ exports.Router = Router;
@@ -0,0 +1,40 @@
1
+ import { District } from "./district";
2
+ import { SectionPosition } from "./position";
3
+ import { PowerDistrict } from "./power-district";
4
+ import { Router } from "./router";
5
+ import { Tile } from "./tile";
6
+ import { Track } from "./track";
7
+ export declare class Section {
8
+ name: string;
9
+ district: District;
10
+ powerDistrict: PowerDistrict;
11
+ tracks: Track[];
12
+ tiles: Tile[];
13
+ in?: Router | Section;
14
+ out?: Router | Section;
15
+ constructor(name: string, district: District);
16
+ get domainName(): string;
17
+ next(reverse: boolean): Section;
18
+ getTilesInRange(startPosition: SectionPosition, endPosition: SectionPosition): {
19
+ offset: {
20
+ start: number;
21
+ end: number;
22
+ };
23
+ tiles: Tile[];
24
+ };
25
+ dump(): void;
26
+ get length(): number;
27
+ get tileLength(): number;
28
+ trail(offset: number, reversed: boolean, length: number): {
29
+ sections: Section[];
30
+ tip: SectionPosition;
31
+ };
32
+ toDotReference(): string;
33
+ toDotDefinition(): string;
34
+ toDotConnection(): string;
35
+ toSVG(): string;
36
+ findSVGPositions(): {
37
+ x: number;
38
+ y: number;
39
+ }[];
40
+ }