@graphty/graph-io 0.3.44 → 0.3.46
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/dist/chunks/{approximations-3Q4TsdTh.js → approximations-wWjCRzKj.js} +2 -2
- package/dist/chunks/{approximations-3Q4TsdTh.js.map → approximations-wWjCRzKj.js.map} +1 -1
- package/dist/chunks/{block-JQiwViEt.js → block-DiGq58c3.js} +50 -44
- package/dist/chunks/block-DiGq58c3.js.map +1 -0
- package/dist/chunks/{choose-BemygfH8.js → choose--jM0yYCo.js} +3 -3
- package/dist/chunks/{choose-BemygfH8.js.map → choose--jM0yYCo.js.map} +1 -1
- package/dist/chunks/{constants-BFUR_CtU.js → constants-BKwqelYD.js} +2 -2
- package/dist/chunks/{constants-BFUR_CtU.js.map → constants-BKwqelYD.js.map} +1 -1
- package/dist/chunks/{emit-DCwFLts5.js → emit-CwU8cUmG.js} +3 -3
- package/dist/chunks/{emit-DCwFLts5.js.map → emit-CwU8cUmG.js.map} +1 -1
- package/dist/chunks/{escape-B1rMftd9.js → escape-CgzxiYPg.js} +6 -6
- package/dist/chunks/{escape-B1rMftd9.js.map → escape-CgzxiYPg.js.map} +1 -1
- package/dist/chunks/{export-D7IGzTG4.js → export-BvqAXCKh.js} +4 -4
- package/dist/chunks/{export-D7IGzTG4.js.map → export-BvqAXCKh.js.map} +1 -1
- package/dist/chunks/{exporter-ChPyhBOv.js → exporter--Mk9gXCJ.js} +4 -4
- package/dist/chunks/{exporter-ChPyhBOv.js.map → exporter--Mk9gXCJ.js.map} +1 -1
- package/dist/chunks/{exporter-KEPxYUXD.js → exporter-CJgfhF-l.js} +7 -7
- package/dist/chunks/{exporter-KEPxYUXD.js.map → exporter-CJgfhF-l.js.map} +1 -1
- package/dist/chunks/{exporter-quPUCBiC.js → exporter-CrDWIyiq.js} +9 -9
- package/dist/chunks/{exporter-quPUCBiC.js.map → exporter-CrDWIyiq.js.map} +1 -1
- package/dist/chunks/{exporter-BtnqYuuj.js → exporter-DndFlGSa.js} +6 -6
- package/dist/chunks/{exporter-BtnqYuuj.js.map → exporter-DndFlGSa.js.map} +1 -1
- package/dist/chunks/{format-GDmO6m6Q.js → format-BpQBx8MZ.js} +35 -35
- package/dist/chunks/{format-GDmO6m6Q.js.map → format-BpQBx8MZ.js.map} +1 -1
- package/dist/chunks/{generic-BL06jrJf.js → generic-Dq1ntt9q.js} +4 -4
- package/dist/chunks/{generic-BL06jrJf.js.map → generic-Dq1ntt9q.js.map} +1 -1
- package/dist/chunks/{importer-dwoYpSqb.js → importer-BSvfrX_h.js} +7 -7
- package/dist/chunks/{importer-dwoYpSqb.js.map → importer-BSvfrX_h.js.map} +1 -1
- package/dist/chunks/{importer-CdzeN6X4.js → importer-CMmeMWlC.js} +6 -6
- package/dist/chunks/{importer-CdzeN6X4.js.map → importer-CMmeMWlC.js.map} +1 -1
- package/dist/chunks/{importer-C2nzGhdV.js → importer-COTK9Tsl.js} +17 -9
- package/dist/chunks/importer-COTK9Tsl.js.map +1 -0
- package/dist/chunks/{importer-ChKNC_GN.js → importer-DiZRmOQG.js} +8 -8
- package/dist/chunks/{importer-ChKNC_GN.js.map → importer-DiZRmOQG.js.map} +1 -1
- package/dist/chunks/{importer-CbX2_rNF.js → importer-e6LGpqHB.js} +10 -10
- package/dist/chunks/importer-e6LGpqHB.js.map +1 -0
- package/dist/chunks/{importer-B7r2owVf.js → importer-iFYPEKaJ.js} +9 -9
- package/dist/chunks/{importer-B7r2owVf.js.map → importer-iFYPEKaJ.js.map} +1 -1
- package/dist/chunks/{importer-BFmOP96W.js → importer-yh_aBCU6.js} +6 -6
- package/dist/chunks/{importer-BFmOP96W.js.map → importer-yh_aBCU6.js.map} +1 -1
- package/dist/chunks/{json-elements-C4FwgnH5.js → json-elements-DJZRZ6jd.js} +4 -4
- package/dist/chunks/{json-elements-C4FwgnH5.js.map → json-elements-DJZRZ6jd.js.map} +1 -1
- package/dist/chunks/{lexicon-B-knUi-l.js → lexicon-CT69Kd87.js} +5 -4
- package/dist/chunks/{lexicon-B-knUi-l.js.map → lexicon-CT69Kd87.js.map} +1 -1
- package/dist/chunks/{ontology-export-BSpbY5FL.js → ontology-export-G4MqRdux.js} +3 -3
- package/dist/chunks/{ontology-export-BSpbY5FL.js.map → ontology-export-G4MqRdux.js.map} +1 -1
- package/dist/chunks/{records-Nwz_rbNt.js → records-CiaWZrFH.js} +2 -2
- package/dist/chunks/{records-Nwz_rbNt.js.map → records-CiaWZrFH.js.map} +1 -1
- package/dist/chunks/{styles-_N-C8S-U.js → styles-BQnsga7p.js} +5 -5
- package/dist/chunks/{styles-_N-C8S-U.js.map → styles-BQnsga7p.js.map} +1 -1
- package/dist/chunks/{styles-Brt9JMfs.js → styles-D3fRphrO.js} +4 -4
- package/dist/chunks/{styles-Brt9JMfs.js.map → styles-D3fRphrO.js.map} +1 -1
- package/dist/chunks/{styles-yed-DAfcIkf7.js → styles-yed-BF5x3_Y5.js} +17 -17
- package/dist/chunks/{styles-yed-DAfcIkf7.js.map → styles-yed-BF5x3_Y5.js.map} +1 -1
- package/dist/chunks/{text-CgzMqHQb.js → text-CzJNFOS9.js} +2 -2
- package/dist/chunks/{text-CgzMqHQb.js.map → text-CzJNFOS9.js.map} +1 -1
- package/dist/chunks/writer-DMKvthQE.js +448 -0
- package/dist/chunks/writer-DMKvthQE.js.map +1 -0
- package/dist/csv.js +5 -5
- package/dist/cx.js +1 -1
- package/dist/cx2.js +2 -2
- package/dist/cys.js +1 -1
- package/dist/dot.js +2 -2
- package/dist/gexf.js +5 -5
- package/dist/gml.js +3884 -8
- package/dist/gml.js.map +1 -1
- package/dist/graph-io.js +90 -89
- package/dist/graph-io.js.map +1 -1
- package/dist/graphml.js +3 -3
- package/dist/json.js +1 -1
- package/dist/neo4j.js +4 -4
- package/dist/obo.js +1 -1
- package/dist/pajek.js +1 -1
- package/dist/src/formats/dot/styles.d.ts +1 -1
- package/dist/src/formats/dot/styles.d.ts.map +1 -1
- package/dist/src/formats/dot/styles.js +12 -0
- package/dist/src/formats/dot/styles.js.map +1 -1
- package/dist/src/formats/pajek/styles.d.ts +1 -1
- package/dist/src/formats/pajek/styles.d.ts.map +1 -1
- package/dist/src/formats/pajek/styles.js +4 -3
- package/dist/src/formats/pajek/styles.js.map +1 -1
- package/dist/src/styles/canon/color-tables.d.ts +2 -1
- package/dist/src/styles/canon/color-tables.d.ts.map +1 -1
- package/dist/src/styles/canon/color-tables.js +7 -2
- package/dist/src/styles/canon/color-tables.js.map +1 -1
- package/dist/src/styles/cyjs/emit.d.ts.map +1 -1
- package/dist/src/styles/cyjs/emit.js +2 -1
- package/dist/src/styles/cyjs/emit.js.map +1 -1
- package/dist/src/styles/cytoscape/lexicon.d.ts.map +1 -1
- package/dist/src/styles/cytoscape/lexicon.js +3 -1
- package/dist/src/styles/cytoscape/lexicon.js.map +1 -1
- package/dist/src/styles/defaults.d.ts.map +1 -1
- package/dist/src/styles/defaults.js +7 -0
- package/dist/src/styles/defaults.js.map +1 -1
- package/dist/src/styles/io/writer.d.ts +9 -0
- package/dist/src/styles/io/writer.d.ts.map +1 -1
- package/dist/src/styles/io/writer.js +31 -1
- package/dist/src/styles/io/writer.js.map +1 -1
- package/dist/styles.js +21 -21
- package/dist/styles.js.map +1 -1
- package/dist/xgmml.js +3 -3
- package/package.json +1 -1
- package/src/formats/dot/styles.ts +15 -1
- package/src/formats/pajek/styles.ts +4 -3
- package/src/styles/canon/color-tables.ts +7 -2
- package/src/styles/cyjs/emit.ts +2 -1
- package/src/styles/cytoscape/lexicon.ts +3 -1
- package/src/styles/defaults.ts +8 -0
- package/src/styles/io/writer.ts +36 -1
- package/dist/chunks/block-JQiwViEt.js.map +0 -1
- package/dist/chunks/importer-C2nzGhdV.js.map +0 -1
- package/dist/chunks/importer-CbX2_rNF.js.map +0 -1
- package/dist/chunks/index-B9fdNpTA.js +0 -4310
- package/dist/chunks/index-B9fdNpTA.js.map +0 -1
package/dist/gml.js
CHANGED
|
@@ -1,11 +1,3887 @@
|
|
|
1
|
-
import "./chunks/format-
|
|
2
|
-
import {
|
|
1
|
+
import { a8 as SYNTAX_CODE, p as plural, a as agree, ad as formatInteger, aP as formatGmlReal, R as ROLE_TAKEN_CODE$1, a7 as WIDENED_CODE$1, P as PRECISION_CODE$1, k as DUPLICATE_NODE_CODE$1, n as MISSING_ENDPOINT_CODE$1, o as MISSING_ID_CODE$1, i as MULTIPLE_GRAPHS_CODE, N as NO_GRAPH_CODE$1, Z as ImportReportBuilder, C as COLUMN_RENAMED_CODE, M as MIXED_DIRECTION_CODE, D as DIRECTION_FORCED_CODE, b as DIRECTION_REFUSED_CODE, S as SINK_OPTION_CODE, O as OPTION_IGNORED_CODE, I as ID_MERGED_CODE, a2 as AMBIGUOUS_GRAPH_NAME_CODE, a3 as GRAPH_NOT_FOUND_CODE, s as UNKNOWN_ENCODING_CODE, t as ENCODING_FALLBACK_CODE, u as INVALID_ENCODING_CODE, v as INVALID_UTF8_CODE, y as ELEMENT_ISSUE, z as INPUT_ISSUE } from "./chunks/format-BpQBx8MZ.js";
|
|
2
|
+
import { GraphFormatError, INVALID_INDEX } from "@graphty/graph-format";
|
|
3
|
+
import { c as capabilities, a as checkCapabilities, L as LOSS, p as pairFolding, G as sanitizeIds, ai as countMixedEdges, e as explicitWeights, r as resolveExportOptions, D as DictHeuristic, ag as DICT_SAMPLE_ROWS, g as resolveImportOptions, i as reportUnusedOptions, ab as readText, h as throwIfAborted, j as reportSinkOptions, k as graphChosen, I as IdCoercer, l as DirectionResolver, u as uniqueColumnName, w as weightFromValue, ak as reportWeightPrecision, s as parseWeightText, d as declareResolved } from "./chunks/export-BvqAXCKh.js";
|
|
4
|
+
import { n as decodeGmlString, o as quoteGmlString } from "./chunks/escape-CgzxiYPg.js";
|
|
5
|
+
import { j as joinText, e as encodeChunks } from "./chunks/writer-DxjgHwSe.js";
|
|
6
|
+
import { y as yedValue, a as yedColor, b as yedSpelled, c as bendsToPoints, Y as YED_SHAPES, d as YED_LINES, v as valueColumns, e as canonBends, f as flowchartShape, F as FONT_STYLES, g as YED_ARROWS } from "./chunks/styles-yed-BF5x3_Y5.js";
|
|
7
|
+
import { w as writeStyleBlock, c as styleBlockLosses, t as StyleNotes, b as styleTag, p as plainCell, u as parseStyleBlock, s as styleFingerprint, v as blockStyles, o as canonText, r as resolveStyles, h as canonNumber, l as canonEnum, n as noteAt, M as MAX_STYLE_VALUE_LENGTH, a as STYLE_ISSUE, g as canonColor, x as colorOpacity, k as scalarText, S as STYLE_BLOCK_NAME } from "./chunks/block-DiGq58c3.js";
|
|
8
|
+
import { C as CYTOSCAPE_LEXICON } from "./chunks/lexicon-CT69Kd87.js";
|
|
9
|
+
import { S as StyleApproximations } from "./chunks/approximations-wWjCRzKj.js";
|
|
10
|
+
import { d as dataRead } from "./chunks/writer-DMKvthQE.js";
|
|
11
|
+
import { r as gephiRules } from "./chunks/styles-D3fRphrO.js";
|
|
12
|
+
import { i as importChosenGraph } from "./chunks/choose--jM0yYCo.js";
|
|
13
|
+
import { t as toolOf } from "./chunks/units-XMZFIFe1.js";
|
|
14
|
+
import { m as median } from "./chunks/convert-BcQ8_jMo.js";
|
|
15
|
+
const TOKEN_WORD = 0;
|
|
16
|
+
const TOKEN_INT = 1;
|
|
17
|
+
const TOKEN_REAL = 2;
|
|
18
|
+
const TOKEN_STRING = 3;
|
|
19
|
+
const TOKEN_OPEN = 4;
|
|
20
|
+
const TOKEN_CLOSE = 5;
|
|
21
|
+
const LIST_START_MARKER = "_networkx_list_start";
|
|
22
|
+
const EMPTY_LIST_TEXT = "[]";
|
|
23
|
+
const EMPTY_TUPLE_TEXT = "()";
|
|
24
|
+
const ORIGINAL_ID_KEY = "graphty_originalId";
|
|
25
|
+
const SYNTAX_STRUCTURE_CODE = SYNTAX_CODE;
|
|
26
|
+
const SYNTAX_TOKEN_CODE = SYNTAX_CODE;
|
|
27
|
+
const SYNTAX_STRING_CODE = SYNTAX_CODE;
|
|
28
|
+
const SYNTAX_BRACKET_CODE = SYNTAX_CODE;
|
|
29
|
+
const KEY_TEXT = /^[A-Za-z_]\w*$/;
|
|
30
|
+
const STRICT_KEY_TEXT = /^[A-Za-z]\w*$/;
|
|
31
|
+
const INT_TEXT = /^[+-]?\d+$/;
|
|
32
|
+
const REAL_TEXT = /^[+-]?(?:(?:\d+\.\d*|\.\d+)(?:[eE][+-]?\d+)?|\d+[eE][+-]?\d+)$/;
|
|
33
|
+
const NON_FINITE_TEXT = /^[+-]?(?:inf|infinity|nan)$/i;
|
|
34
|
+
const NON_FINITE_WORD = /^(?:inf|infinity|nan)$/i;
|
|
35
|
+
class GmlSyntaxError extends Error {
|
|
36
|
+
/**
|
|
37
|
+
* Create a syntax error.
|
|
38
|
+
* @param code - the issue code
|
|
39
|
+
* @param message - a plain-ASCII message
|
|
40
|
+
* @param line - the 1-based line
|
|
41
|
+
*/
|
|
42
|
+
constructor(code, message, line) {
|
|
43
|
+
super(message);
|
|
44
|
+
this.name = "GmlSyntaxError";
|
|
45
|
+
this.code = code;
|
|
46
|
+
this.line = line;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
class GmlTokens {
|
|
50
|
+
/**
|
|
51
|
+
* Create an empty token list over a text.
|
|
52
|
+
* @param text - the source text
|
|
53
|
+
* @param capacity - the initial capacity
|
|
54
|
+
*/
|
|
55
|
+
constructor(text, capacity = 1024) {
|
|
56
|
+
this.count = 0;
|
|
57
|
+
this.backslashEscapes = false;
|
|
58
|
+
this.onUnknownEntity = null;
|
|
59
|
+
this.text = text;
|
|
60
|
+
this.kind = new Uint8Array(capacity);
|
|
61
|
+
this.start = new Uint32Array(capacity);
|
|
62
|
+
this.end = new Uint32Array(capacity);
|
|
63
|
+
this.line = new Uint32Array(capacity);
|
|
64
|
+
this.match = new Uint32Array(0);
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Append a token.
|
|
68
|
+
* @param kind - the kind
|
|
69
|
+
* @param start - the start offset
|
|
70
|
+
* @param end - the end offset
|
|
71
|
+
* @param line - the 1-based line
|
|
72
|
+
*/
|
|
73
|
+
push(kind, start, end, line) {
|
|
74
|
+
if (this.count === this.kind.length) {
|
|
75
|
+
this.grow();
|
|
76
|
+
}
|
|
77
|
+
const i = this.count++;
|
|
78
|
+
this.kind[i] = kind;
|
|
79
|
+
this.start[i] = start;
|
|
80
|
+
this.end[i] = end;
|
|
81
|
+
this.line[i] = line;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* The raw text of a token (a string's body without the quotes, entities undecoded).
|
|
85
|
+
* @param i - the token index
|
|
86
|
+
* @returns the text
|
|
87
|
+
*/
|
|
88
|
+
textOf(i) {
|
|
89
|
+
return this.text.slice(this.start[i], this.end[i]);
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* The decoded value of a string token.
|
|
93
|
+
* @param i - the token index
|
|
94
|
+
* @returns the body with character references decoded
|
|
95
|
+
*/
|
|
96
|
+
stringOf(i) {
|
|
97
|
+
let text = this.textOf(i);
|
|
98
|
+
if (text.includes("\r")) {
|
|
99
|
+
text = text.replaceAll(/\r\n?/g, "\n");
|
|
100
|
+
}
|
|
101
|
+
if (this.backslashEscapes && text.includes("\\")) {
|
|
102
|
+
text = text.replaceAll(/\\([\s\S])/g, "$1");
|
|
103
|
+
}
|
|
104
|
+
if (!text.includes("&")) {
|
|
105
|
+
return text;
|
|
106
|
+
}
|
|
107
|
+
const report = this.onUnknownEntity;
|
|
108
|
+
return decodeGmlString(
|
|
109
|
+
text,
|
|
110
|
+
report === null ? void 0 : (entity) => {
|
|
111
|
+
report(entity, this.line[i]);
|
|
112
|
+
}
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Whether a token is a `[` or `]`.
|
|
117
|
+
* @param i - the token index
|
|
118
|
+
* @returns true for a bracket
|
|
119
|
+
*/
|
|
120
|
+
isBracket(i) {
|
|
121
|
+
return this.kind[i] >= TOKEN_OPEN;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* The index of the pair following the pair whose key is at `key`.
|
|
125
|
+
* @param key - the index of a key token
|
|
126
|
+
* @returns the index after the pair's value (after the matching `]` for a record)
|
|
127
|
+
*/
|
|
128
|
+
nextPair(key) {
|
|
129
|
+
const value = key + 1;
|
|
130
|
+
return this.kind[value] === TOKEN_OPEN ? this.match[value] + 1 : value + 1;
|
|
131
|
+
}
|
|
132
|
+
/** Double every array. */
|
|
133
|
+
grow() {
|
|
134
|
+
const capacity = this.kind.length * 2;
|
|
135
|
+
const kind = new Uint8Array(capacity);
|
|
136
|
+
kind.set(this.kind);
|
|
137
|
+
this.kind = kind;
|
|
138
|
+
const start = new Uint32Array(capacity);
|
|
139
|
+
start.set(this.start);
|
|
140
|
+
this.start = start;
|
|
141
|
+
const end = new Uint32Array(capacity);
|
|
142
|
+
end.set(this.end);
|
|
143
|
+
this.end = end;
|
|
144
|
+
const line = new Uint32Array(capacity);
|
|
145
|
+
line.set(this.line);
|
|
146
|
+
this.line = line;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
function tokenizeGml(text) {
|
|
150
|
+
const tokens = new GmlTokens(text, Math.max(1024, text.length >>> 3));
|
|
151
|
+
tokens.backslashEscapes = writtenByOgdf(text);
|
|
152
|
+
const lex = { text, tokens, i: 0, line: 1, spanningLine: 0 };
|
|
153
|
+
while (lex.i < text.length) {
|
|
154
|
+
const c = text.codePointAt(lex.i) ?? 0;
|
|
155
|
+
if (c === 34) {
|
|
156
|
+
lexString(lex);
|
|
157
|
+
} else if (c === 91 || c === 93) {
|
|
158
|
+
tokens.push(c === 91 ? TOKEN_OPEN : TOKEN_CLOSE, lex.i, lex.i + 1, lex.line);
|
|
159
|
+
lex.i++;
|
|
160
|
+
} else if (!skipSpace(lex, c)) {
|
|
161
|
+
lexBare(lex);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
validateStructure(tokens);
|
|
165
|
+
return tokens;
|
|
166
|
+
}
|
|
167
|
+
function skipSpace(lex, c) {
|
|
168
|
+
const { text } = lex;
|
|
169
|
+
if (c === 10 || c === 13) {
|
|
170
|
+
lex.line++;
|
|
171
|
+
lex.i++;
|
|
172
|
+
if (c === 13 && lex.i < text.length && text.codePointAt(lex.i) === 10) {
|
|
173
|
+
lex.i++;
|
|
174
|
+
}
|
|
175
|
+
} else if (c === 32 || c === 9 || c === 11 || c === 12 || c === 65279) {
|
|
176
|
+
lex.i++;
|
|
177
|
+
} else if (c === 35) {
|
|
178
|
+
lex.i = endOfLine(text, lex.i);
|
|
179
|
+
} else {
|
|
180
|
+
return false;
|
|
181
|
+
}
|
|
182
|
+
return true;
|
|
183
|
+
}
|
|
184
|
+
function lexString(lex) {
|
|
185
|
+
const { text, tokens, i } = lex;
|
|
186
|
+
const close = tokens.backslashEscapes ? escapedClose(text, i + 1) : text.indexOf('"', i + 1);
|
|
187
|
+
if (close < 0) {
|
|
188
|
+
const hint = lex.spanningLine === 0 ? "" : `; the string opened at line ${lex.spanningLine} spans lines, so a quote may be missing there`;
|
|
189
|
+
throw new GmlSyntaxError(SYNTAX_STRING_CODE, `unclosed string opened at line ${lex.line}${hint}`, lex.line);
|
|
190
|
+
}
|
|
191
|
+
tokens.push(TOKEN_STRING, i + 1, close, lex.line);
|
|
192
|
+
const opened = lex.line;
|
|
193
|
+
for (let k = i + 1; k < close; k++) {
|
|
194
|
+
const d = text.codePointAt(k);
|
|
195
|
+
if (d === 10 || d === 13 && text.codePointAt(k + 1) !== 10) {
|
|
196
|
+
lex.line++;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
if (lex.line !== opened && lex.spanningLine === 0) {
|
|
200
|
+
lex.spanningLine = opened;
|
|
201
|
+
}
|
|
202
|
+
lex.i = close + 1;
|
|
203
|
+
}
|
|
204
|
+
function lexBare(lex) {
|
|
205
|
+
const { text, i } = lex;
|
|
206
|
+
let j = i + 1;
|
|
207
|
+
while (j < text.length && !isDelimiter(text.codePointAt(j) ?? 0)) {
|
|
208
|
+
j++;
|
|
209
|
+
}
|
|
210
|
+
lex.tokens.push(classifyBare(text, i, j, lex.line), i, j, lex.line);
|
|
211
|
+
lex.i = j;
|
|
212
|
+
}
|
|
213
|
+
function writtenByOgdf(text) {
|
|
214
|
+
const end = Math.min(text.length, 512);
|
|
215
|
+
let i = 0;
|
|
216
|
+
while (i < end) {
|
|
217
|
+
const c = text.codePointAt(i);
|
|
218
|
+
if (c === 35) {
|
|
219
|
+
while (i < end && text.codePointAt(i) !== 10 && text.codePointAt(i) !== 13) {
|
|
220
|
+
i++;
|
|
221
|
+
}
|
|
222
|
+
} else if (c === 32 || c === 9 || c === 10 || c === 13 || c === 12 || c === 11) {
|
|
223
|
+
i++;
|
|
224
|
+
} else {
|
|
225
|
+
break;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
return /^Creator\s+"ogdf::/.test(text.slice(i, i + 64));
|
|
229
|
+
}
|
|
230
|
+
function escapedClose(text, from) {
|
|
231
|
+
let k = from;
|
|
232
|
+
while (k < text.length) {
|
|
233
|
+
const c = text.codePointAt(k);
|
|
234
|
+
if (c === 34) {
|
|
235
|
+
return k;
|
|
236
|
+
}
|
|
237
|
+
k += c === 92 ? 2 : 1;
|
|
238
|
+
}
|
|
239
|
+
return -1;
|
|
240
|
+
}
|
|
241
|
+
function endOfLine(text, from) {
|
|
242
|
+
const n = text.length;
|
|
243
|
+
for (let i = from; i < n; i++) {
|
|
244
|
+
const c = text.charCodeAt(i);
|
|
245
|
+
if (c === 10 || c === 13) {
|
|
246
|
+
return i;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
return n;
|
|
250
|
+
}
|
|
251
|
+
function isDelimiter(c) {
|
|
252
|
+
return c === 32 || c === 9 || c === 10 || c === 13 || c === 11 || c === 12 || c === 91 || c === 93 || c === 34 || c === 35;
|
|
253
|
+
}
|
|
254
|
+
function classifyBare(text, start, end, line) {
|
|
255
|
+
const token = text.slice(start, end);
|
|
256
|
+
if (KEY_TEXT.test(token)) {
|
|
257
|
+
return TOKEN_WORD;
|
|
258
|
+
}
|
|
259
|
+
if (INT_TEXT.test(token)) {
|
|
260
|
+
return TOKEN_INT;
|
|
261
|
+
}
|
|
262
|
+
if (REAL_TEXT.test(token) || NON_FINITE_TEXT.test(token)) {
|
|
263
|
+
return TOKEN_REAL;
|
|
264
|
+
}
|
|
265
|
+
const hex = (c) => (c.codePointAt(0) ?? 0).toString(16).toUpperCase().padStart(4, "0");
|
|
266
|
+
const head = token.length > 20 ? `${token.slice(0, 20)}...` : token;
|
|
267
|
+
const shown = head.replaceAll(/[^\x20-\x7e]/gu, (c) => String.raw`\u` + hex(c));
|
|
268
|
+
const odd = /[^\x21-\x7e]/u.exec(token);
|
|
269
|
+
const note2 = odd === null ? "" : ` (it holds U+${hex(odd[0])}, which GML allows only inside a string)`;
|
|
270
|
+
throw new GmlSyntaxError(SYNTAX_TOKEN_CODE, `cannot tokenize "${shown}" at line ${line}${note2}`, line);
|
|
271
|
+
}
|
|
272
|
+
function validateStructure(tokens) {
|
|
273
|
+
const { count, kind } = tokens;
|
|
274
|
+
const match = new Uint32Array(count);
|
|
275
|
+
const stack = [];
|
|
276
|
+
let i = 0;
|
|
277
|
+
while (i < count) {
|
|
278
|
+
const k = kind[i];
|
|
279
|
+
if (k === TOKEN_CLOSE) {
|
|
280
|
+
const open = stack.pop();
|
|
281
|
+
if (open === void 0) {
|
|
282
|
+
throw new GmlSyntaxError(
|
|
283
|
+
SYNTAX_STRUCTURE_CODE,
|
|
284
|
+
`unexpected "]" at line ${tokens.line[i]}: no open "["`,
|
|
285
|
+
tokens.line[i]
|
|
286
|
+
);
|
|
287
|
+
}
|
|
288
|
+
match[open] = i;
|
|
289
|
+
i++;
|
|
290
|
+
continue;
|
|
291
|
+
}
|
|
292
|
+
if (k !== TOKEN_WORD) {
|
|
293
|
+
throw new GmlSyntaxError(
|
|
294
|
+
SYNTAX_STRUCTURE_CODE,
|
|
295
|
+
`expected a key at line ${tokens.line[i]}, found ${describeToken(tokens, i)}`,
|
|
296
|
+
tokens.line[i]
|
|
297
|
+
);
|
|
298
|
+
}
|
|
299
|
+
const value = i + 1;
|
|
300
|
+
if (value >= count) {
|
|
301
|
+
throw new GmlSyntaxError(
|
|
302
|
+
SYNTAX_STRUCTURE_CODE,
|
|
303
|
+
`key "${tokens.textOf(i)}" at line ${tokens.line[i]} has no value`,
|
|
304
|
+
tokens.line[i]
|
|
305
|
+
);
|
|
306
|
+
}
|
|
307
|
+
if (checkValue(tokens, i) === TOKEN_OPEN) {
|
|
308
|
+
stack.push(value);
|
|
309
|
+
}
|
|
310
|
+
i = value + 1;
|
|
311
|
+
}
|
|
312
|
+
if (stack.length > 0) {
|
|
313
|
+
const open = stack[stack.length - 1];
|
|
314
|
+
throw new GmlSyntaxError(
|
|
315
|
+
SYNTAX_BRACKET_CODE,
|
|
316
|
+
`unexpected end of input: ${stack.length} bracket${plural(stack.length)} still open, the last opened at line ${tokens.line[open]}`,
|
|
317
|
+
tokens.line[open]
|
|
318
|
+
);
|
|
319
|
+
}
|
|
320
|
+
tokens.match = match;
|
|
321
|
+
}
|
|
322
|
+
function checkValue(tokens, key) {
|
|
323
|
+
const { kind } = tokens;
|
|
324
|
+
const value = key + 1;
|
|
325
|
+
const word = kind[value] === TOKEN_WORD && !NON_FINITE_WORD.test(tokens.textOf(value));
|
|
326
|
+
if (word && tokens.textOf(key) === "Creator") {
|
|
327
|
+
kind[value] = TOKEN_STRING;
|
|
328
|
+
return TOKEN_STRING;
|
|
329
|
+
}
|
|
330
|
+
if (kind[value] === TOKEN_CLOSE || word) {
|
|
331
|
+
throw new GmlSyntaxError(
|
|
332
|
+
SYNTAX_STRUCTURE_CODE,
|
|
333
|
+
`expected a value for key "${tokens.textOf(key)}" at line ${tokens.line[key]}, found ${describeToken(tokens, value)}`,
|
|
334
|
+
tokens.line[value]
|
|
335
|
+
);
|
|
336
|
+
}
|
|
337
|
+
return kind[value];
|
|
338
|
+
}
|
|
339
|
+
function describeToken(tokens, i) {
|
|
340
|
+
switch (tokens.kind[i]) {
|
|
341
|
+
case TOKEN_OPEN:
|
|
342
|
+
return '"["';
|
|
343
|
+
case TOKEN_CLOSE:
|
|
344
|
+
return '"]"';
|
|
345
|
+
case TOKEN_STRING:
|
|
346
|
+
return `the string "${tokens.textOf(i)}"`;
|
|
347
|
+
default:
|
|
348
|
+
return `"${tokens.textOf(i)}"`;
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
function numberOfText(text) {
|
|
352
|
+
if (NON_FINITE_TEXT.test(text)) {
|
|
353
|
+
const lower = text.toLowerCase();
|
|
354
|
+
if (lower.endsWith("nan")) {
|
|
355
|
+
return Number.NaN;
|
|
356
|
+
}
|
|
357
|
+
return lower.startsWith("-") ? -Infinity : Infinity;
|
|
358
|
+
}
|
|
359
|
+
return Number(text);
|
|
360
|
+
}
|
|
361
|
+
function isGmlKey(text) {
|
|
362
|
+
return STRICT_KEY_TEXT.test(text);
|
|
363
|
+
}
|
|
364
|
+
const KEY_MANGLED_CODE = "W_GML_KEY_MANGLED";
|
|
365
|
+
function mangleGmlKey(text) {
|
|
366
|
+
const body = text.replaceAll(/\W/g, "_");
|
|
367
|
+
return /^[A-Za-z]/.test(body) ? body : `x${body}`;
|
|
368
|
+
}
|
|
369
|
+
function parseRecord(tokens, open) {
|
|
370
|
+
const root = /* @__PURE__ */ Object.create(null);
|
|
371
|
+
const stack = [
|
|
372
|
+
{ record: root, listKeys: null, close: tokens.match[open], next: open + 1, key: null }
|
|
373
|
+
];
|
|
374
|
+
while (stack.length > 0) {
|
|
375
|
+
const frame = stack[stack.length - 1];
|
|
376
|
+
if (frame.next >= frame.close) {
|
|
377
|
+
stack.pop();
|
|
378
|
+
if (frame.key !== null) {
|
|
379
|
+
storeValue(stack[stack.length - 1], frame.key, frame.record);
|
|
380
|
+
}
|
|
381
|
+
continue;
|
|
382
|
+
}
|
|
383
|
+
const p = frame.next;
|
|
384
|
+
frame.next = tokens.nextPair(p);
|
|
385
|
+
const key = tokens.textOf(p);
|
|
386
|
+
const v = p + 1;
|
|
387
|
+
if (tokens.kind[v] === TOKEN_STRING) {
|
|
388
|
+
storeString(frame, key, tokens.stringOf(v));
|
|
389
|
+
} else if (tokens.kind[v] === TOKEN_OPEN) {
|
|
390
|
+
stack.push({
|
|
391
|
+
record: /* @__PURE__ */ Object.create(null),
|
|
392
|
+
listKeys: null,
|
|
393
|
+
close: tokens.match[v],
|
|
394
|
+
next: v + 1,
|
|
395
|
+
key
|
|
396
|
+
});
|
|
397
|
+
} else {
|
|
398
|
+
storeValue(frame, key, numberOfText(tokens.textOf(v)));
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
return root;
|
|
402
|
+
}
|
|
403
|
+
function storeString(frame, key, text) {
|
|
404
|
+
if (text === LIST_START_MARKER) {
|
|
405
|
+
frame.record[key] = [];
|
|
406
|
+
frame.listKeys ??= /* @__PURE__ */ new Set();
|
|
407
|
+
frame.listKeys.add(key);
|
|
408
|
+
return;
|
|
409
|
+
}
|
|
410
|
+
storeValue(frame, key, text === EMPTY_LIST_TEXT || text === EMPTY_TUPLE_TEXT ? [] : text);
|
|
411
|
+
}
|
|
412
|
+
function storeValue(frame, key, value) {
|
|
413
|
+
const { record } = frame;
|
|
414
|
+
if (frame.listKeys?.has(key)) {
|
|
415
|
+
record[key].push(value);
|
|
416
|
+
} else if (key in record) {
|
|
417
|
+
record[key] = [record[key], value];
|
|
418
|
+
frame.listKeys ??= /* @__PURE__ */ new Set();
|
|
419
|
+
frame.listKeys.add(key);
|
|
420
|
+
} else {
|
|
421
|
+
record[key] = value;
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
function scalarOrRecord(tokens, v) {
|
|
425
|
+
return tokens.kind[v] === TOKEN_OPEN ? parseRecord(tokens, v) : numberOfText(tokens.textOf(v));
|
|
426
|
+
}
|
|
427
|
+
const WRITERS = [
|
|
428
|
+
[
|
|
429
|
+
/^\s*(yfiles|yed)\b/i,
|
|
430
|
+
{ dialect: "yed", tool: "yed", vocabulary: "yed", spelling: "yed", units: "px", labelKey: false }
|
|
431
|
+
],
|
|
432
|
+
[
|
|
433
|
+
/^\s*cytoscape\b/i,
|
|
434
|
+
{
|
|
435
|
+
dialect: "cytoscape",
|
|
436
|
+
tool: "cytoscape",
|
|
437
|
+
vocabulary: "cytoscape",
|
|
438
|
+
spelling: "cytoscape",
|
|
439
|
+
units: "px",
|
|
440
|
+
labelKey: true
|
|
441
|
+
}
|
|
442
|
+
],
|
|
443
|
+
[
|
|
444
|
+
/^\s*gephi\b/i,
|
|
445
|
+
{
|
|
446
|
+
dialect: "gephi",
|
|
447
|
+
tool: "gephi",
|
|
448
|
+
vocabulary: "graphlet",
|
|
449
|
+
spelling: "css",
|
|
450
|
+
units: "relative",
|
|
451
|
+
labelKey: false
|
|
452
|
+
}
|
|
453
|
+
],
|
|
454
|
+
[
|
|
455
|
+
/^\s*ogdf::/i,
|
|
456
|
+
{ dialect: "ogdf", tool: "ogdf", vocabulary: "ogdf", spelling: "css", units: "px", labelKey: true }
|
|
457
|
+
],
|
|
458
|
+
// igraph writes a Creator line, but no drawing defaults of igraph's are stated by a primary source
|
|
459
|
+
[
|
|
460
|
+
/^\s*igraph\b/i,
|
|
461
|
+
{ dialect: "igraph", tool: null, vocabulary: "graphlet", spelling: "css", units: "px", labelKey: true }
|
|
462
|
+
]
|
|
463
|
+
];
|
|
464
|
+
const UNKNOWN_WRITER = {
|
|
465
|
+
dialect: "gml",
|
|
466
|
+
tool: null,
|
|
467
|
+
vocabulary: "graphlet",
|
|
468
|
+
spelling: "css",
|
|
469
|
+
units: "px",
|
|
470
|
+
labelKey: true
|
|
471
|
+
};
|
|
472
|
+
const GRAPHTY_WRITER = { ...WRITERS[0][1], tool: null };
|
|
473
|
+
const GML_STYLE_BLOCK_KEY = mangleGmlKey(STYLE_BLOCK_NAME);
|
|
474
|
+
const TAG_PREFIX = mangleGmlKey("graphty.style.");
|
|
475
|
+
function tagKey(target, key) {
|
|
476
|
+
return mangleGmlKey(styleTag(target, key));
|
|
477
|
+
}
|
|
478
|
+
function graphText(snapshot, name) {
|
|
479
|
+
if (!snapshot.graph.has(name)) {
|
|
480
|
+
return null;
|
|
481
|
+
}
|
|
482
|
+
const v = plainCell(snapshot.graph.value(name, 0));
|
|
483
|
+
if (typeof v === "string") {
|
|
484
|
+
return v;
|
|
485
|
+
}
|
|
486
|
+
return typeof v === "number" ? String(v) : null;
|
|
487
|
+
}
|
|
488
|
+
function writerOf(snapshot) {
|
|
489
|
+
if (snapshot.graph.has(GML_STYLE_BLOCK_KEY)) {
|
|
490
|
+
return GRAPHTY_WRITER;
|
|
491
|
+
}
|
|
492
|
+
const creator = snapshot.meta.creator ?? graphText(snapshot, "Creator") ?? "";
|
|
493
|
+
for (const [pattern, writer] of WRITERS) {
|
|
494
|
+
if (pattern.test(creator)) {
|
|
495
|
+
return writer;
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
return UNKNOWN_WRITER;
|
|
499
|
+
}
|
|
500
|
+
const ARROW_ENDS = /* @__PURE__ */ new Map([
|
|
501
|
+
["none", ["none", "none"]],
|
|
502
|
+
["last", ["none", "triangle"]],
|
|
503
|
+
["first", ["triangle", "none"]],
|
|
504
|
+
["both", ["triangle", "triangle"]]
|
|
505
|
+
]);
|
|
506
|
+
const OGDF_STROKES = ["none", "solid", "dashed", "dotted", "dash-dot", "dash-dot"];
|
|
507
|
+
const OGDF_SHAPES = /* @__PURE__ */ new Map([
|
|
508
|
+
["rect", "rectangle"],
|
|
509
|
+
["rhomb", "diamond"],
|
|
510
|
+
["trapeze", "trapezoid"],
|
|
511
|
+
["invtrapeze", "trapezoid"],
|
|
512
|
+
["invtriangle", "triangle"],
|
|
513
|
+
["invparallelogram", "parallelogram"]
|
|
514
|
+
]);
|
|
515
|
+
const YED_LABEL_KEYS = [
|
|
516
|
+
"outline",
|
|
517
|
+
"autoSizePolicy",
|
|
518
|
+
"model",
|
|
519
|
+
"configuration",
|
|
520
|
+
"borderDistance",
|
|
521
|
+
"modelName",
|
|
522
|
+
"modelPosition"
|
|
523
|
+
];
|
|
524
|
+
const cytoscapeArrow = CYTOSCAPE_LEXICON.edge.get("targetArrow")?.[0]?.[1];
|
|
525
|
+
function sampleOf(raw) {
|
|
526
|
+
return typeof raw === "string" || typeof raw === "number" ? String(raw).slice(0, MAX_STYLE_VALUE_LENGTH) : "a record";
|
|
527
|
+
}
|
|
528
|
+
function isRec(v) {
|
|
529
|
+
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
530
|
+
}
|
|
531
|
+
function first(r, ...keys) {
|
|
532
|
+
for (const k of keys) {
|
|
533
|
+
if (Object.hasOwn(r, k) && r[k] !== void 0) {
|
|
534
|
+
return r[k];
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
return void 0;
|
|
538
|
+
}
|
|
539
|
+
function readColor(raw, property, opacity, writer, put, at) {
|
|
540
|
+
if (raw === void 0) {
|
|
541
|
+
return;
|
|
542
|
+
}
|
|
543
|
+
const c = canonColor(raw, writer.spelling, at(property));
|
|
544
|
+
if (c === null) {
|
|
545
|
+
return;
|
|
546
|
+
}
|
|
547
|
+
put(property, c.color);
|
|
548
|
+
const alpha = colorOpacity(c, null, at(property), opacity !== null);
|
|
549
|
+
if (opacity !== null) {
|
|
550
|
+
put(opacity, alpha);
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
function strokeStyle(raw, kind, writer, at) {
|
|
554
|
+
if (raw === void 0) {
|
|
555
|
+
return null;
|
|
556
|
+
}
|
|
557
|
+
if (writer.vocabulary === "ogdf") {
|
|
558
|
+
const stroke = OGDF_STROKES[Number(raw)];
|
|
559
|
+
if (stroke === void 0 || stroke === "none") {
|
|
560
|
+
noteAt(at, STYLE_ISSUE.UNKNOWN_VALUE, sampleOf(raw));
|
|
561
|
+
return null;
|
|
562
|
+
}
|
|
563
|
+
return stroke;
|
|
564
|
+
}
|
|
565
|
+
return canonEnum(kind, raw, writer.spelling, at);
|
|
566
|
+
}
|
|
567
|
+
function readNodeGraphics(g, writer, put, at) {
|
|
568
|
+
const v = writer.vocabulary;
|
|
569
|
+
readNodeFill(g, writer, put, at);
|
|
570
|
+
readNodeOutline(g, writer, put, at);
|
|
571
|
+
put(
|
|
572
|
+
"borderStyle",
|
|
573
|
+
strokeStyle(v === "ogdf" ? g.stipple : g.outlineStyle, "borderStyle", writer, at("borderStyle"))
|
|
574
|
+
);
|
|
575
|
+
put("width", g.w === void 0 ? null : canonNumber(g.w, at("width")));
|
|
576
|
+
put("height", g.h === void 0 ? null : canonNumber(g.h, at("height")));
|
|
577
|
+
if (g.type !== void 0) {
|
|
578
|
+
const raw = v === "ogdf" && typeof g.type === "string" ? OGDF_SHAPES.get(g.type.toLowerCase()) ?? g.type : g.type;
|
|
579
|
+
put("shape", canonEnum("shape", raw, writer.spelling, at("shape")));
|
|
580
|
+
} else if (v === "yed" && typeof g.customconfiguration === "string") {
|
|
581
|
+
put("shape", flowchartShape(g.customconfiguration));
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
function readNodeFill(g, writer, put, at) {
|
|
585
|
+
if (writer.vocabulary === "yed" && Number(g.hasFill) === 0) {
|
|
586
|
+
noteAt(at("fill"), STYLE_ISSUE.NO_FILL, "hasFill 0");
|
|
587
|
+
put("fillOpacity", 0);
|
|
588
|
+
return;
|
|
589
|
+
}
|
|
590
|
+
readColor(g.fill, "fill", "fillOpacity", writer, put, at);
|
|
591
|
+
if (g.fill2 !== void 0) {
|
|
592
|
+
noteAt(at("fill"), STYLE_ISSUE.GRADIENT, sampleOf(g.fill2));
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
function readNodeOutline(g, writer, put, at) {
|
|
596
|
+
const v = writer.vocabulary;
|
|
597
|
+
if (v === "yed" && Number(g.hasOutline) === 0) {
|
|
598
|
+
put("borderWidth", 0);
|
|
599
|
+
return;
|
|
600
|
+
}
|
|
601
|
+
readColor(v === "ogdf" ? g.line : g.outline, "borderColor", null, writer, put, at);
|
|
602
|
+
const width = outlineWidthOf(g, v);
|
|
603
|
+
put("borderWidth", width === void 0 ? null : canonNumber(width, at("borderWidth")));
|
|
604
|
+
}
|
|
605
|
+
function outlineWidthOf(g, v) {
|
|
606
|
+
switch (v) {
|
|
607
|
+
case "yed":
|
|
608
|
+
return g.outlineWidth;
|
|
609
|
+
case "cytoscape":
|
|
610
|
+
return first(g, "outline_width", "width");
|
|
611
|
+
case "ogdf":
|
|
612
|
+
return g.lineWidth;
|
|
613
|
+
default:
|
|
614
|
+
return first(g, "width", "outlineWidth");
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
function readEdgeGraphics(g, writer, put, at) {
|
|
618
|
+
const v = writer.vocabulary;
|
|
619
|
+
readColor(g.fill, "color", "opacity", writer, put, at);
|
|
620
|
+
const width = v === "ogdf" ? first(g, "lineWidth", "width") : g.width;
|
|
621
|
+
put("width", width === void 0 ? null : canonNumber(width, at("width")));
|
|
622
|
+
put("lineStyle", strokeStyle(v === "ogdf" ? g.stipple : g.style, "lineStyle", writer, at("lineStyle")));
|
|
623
|
+
if (typeof g.arrow === "string") {
|
|
624
|
+
const ends = ARROW_ENDS.get(g.arrow.trim().toLowerCase());
|
|
625
|
+
if (ends === void 0) {
|
|
626
|
+
noteAt(at("targetArrow"), STYLE_ISSUE.UNKNOWN_VALUE, g.arrow.slice(0, MAX_STYLE_VALUE_LENGTH));
|
|
627
|
+
} else {
|
|
628
|
+
put("sourceArrow", ends[0]);
|
|
629
|
+
put("targetArrow", ends[1]);
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
for (const [keys, property] of [
|
|
633
|
+
[["sourceArrow", "source_arrow"], "sourceArrow"],
|
|
634
|
+
[["targetArrow", "target_arrow"], "targetArrow"]
|
|
635
|
+
]) {
|
|
636
|
+
const raw = first(g, ...keys);
|
|
637
|
+
if (raw === void 0) {
|
|
638
|
+
continue;
|
|
639
|
+
}
|
|
640
|
+
put(
|
|
641
|
+
property,
|
|
642
|
+
v === "cytoscape" && cytoscapeArrow !== void 0 ? cytoscapeArrow(raw, at(property)) : canonEnum("arrow", raw, writer.spelling, at(property))
|
|
643
|
+
);
|
|
644
|
+
}
|
|
645
|
+
if (v === "yed") {
|
|
646
|
+
readYedCurve(g, put);
|
|
647
|
+
}
|
|
648
|
+
}
|
|
649
|
+
function readYedCurve(g, put) {
|
|
650
|
+
if (g.smoothBends !== void 0) {
|
|
651
|
+
const points = isRec(g.Line) ? g.Line.point : void 0;
|
|
652
|
+
put("curved", Number(g.smoothBends) === 1 && Array.isArray(points) && points.length > 2);
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
function readLabelGraphics(raw, writer, put, at) {
|
|
656
|
+
const visible = (Array.isArray(raw) ? raw : [raw]).filter((l) => isRec(l) && Number(l.visible) !== 0);
|
|
657
|
+
const lg = visible.at(0);
|
|
658
|
+
if (lg === void 0) {
|
|
659
|
+
return;
|
|
660
|
+
}
|
|
661
|
+
const extra = visible.slice(1).filter((l) => l.text !== void 0).length;
|
|
662
|
+
if (extra > 0) {
|
|
663
|
+
noteAt(at("label"), STYLE_ISSUE.EXTRA_LABELS, `${String(extra + 1)} labels`);
|
|
664
|
+
}
|
|
665
|
+
if (lg.text !== void 0) {
|
|
666
|
+
put("label", canonText(lg.text, at("label")));
|
|
667
|
+
}
|
|
668
|
+
const yedLabel = writer.vocabulary === "yed" || YED_LABEL_KEYS.some((k) => Object.hasOwn(lg, k));
|
|
669
|
+
if (lg.color !== void 0) {
|
|
670
|
+
readColor(lg.color, "labelColor", null, writer, put, at);
|
|
671
|
+
} else if (lg.fill !== void 0 && !yedLabel) {
|
|
672
|
+
readColor(lg.fill, "labelColor", null, writer, put, at);
|
|
673
|
+
}
|
|
674
|
+
if (lg.fill !== void 0 && yedLabel) {
|
|
675
|
+
noteAt(at("label"), STYLE_ISSUE.NOT_CARRIED, "label background");
|
|
676
|
+
}
|
|
677
|
+
readLabelFont(lg, put, at);
|
|
678
|
+
}
|
|
679
|
+
function readLabelFont(lg, put, at) {
|
|
680
|
+
put("labelSize", lg.fontSize === void 0 ? null : canonNumber(lg.fontSize, at("labelSize")));
|
|
681
|
+
put("labelFont", lg.fontName === void 0 ? null : canonText(lg.fontName, at("labelFont")));
|
|
682
|
+
const style = scalarText(lg.fontStyle, at("labelWeight"))?.trim().toLowerCase();
|
|
683
|
+
if (style !== void 0) {
|
|
684
|
+
const known = FONT_STYLES.get(style);
|
|
685
|
+
if (known === void 0) {
|
|
686
|
+
noteAt(at("labelWeight"), STYLE_ISSUE.UNKNOWN_VALUE, style.slice(0, MAX_STYLE_VALUE_LENGTH));
|
|
687
|
+
} else {
|
|
688
|
+
put("labelWeight", known[0]);
|
|
689
|
+
put("labelItalic", known[1]);
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
function readElement(target, g, lg, label, writer, put, at) {
|
|
694
|
+
let text = false;
|
|
695
|
+
const putLabel = (property, value) => {
|
|
696
|
+
text ||= property === "label" && value !== null;
|
|
697
|
+
put(property, value);
|
|
698
|
+
};
|
|
699
|
+
if (isRec(g)) {
|
|
700
|
+
(target === "node" ? readNodeGraphics : readEdgeGraphics)(g, writer, put, at);
|
|
701
|
+
}
|
|
702
|
+
if (lg !== null && lg !== void 0) {
|
|
703
|
+
readLabelGraphics(lg, writer, putLabel, at);
|
|
704
|
+
}
|
|
705
|
+
const drawn = writer.vocabulary === "cytoscape" ? target === "node" : writer.labelKey && (isRec(g) || lg !== void 0);
|
|
706
|
+
if (!text && drawn && label !== null && label !== void 0) {
|
|
707
|
+
put("label", canonText(label, at("label")));
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
function cellOf(table, name, i) {
|
|
711
|
+
if (!table.has(name)) {
|
|
712
|
+
return void 0;
|
|
713
|
+
}
|
|
714
|
+
const v = plainCell(table.value(name, i));
|
|
715
|
+
return v ?? void 0;
|
|
716
|
+
}
|
|
717
|
+
function centerOf(snapshot, i) {
|
|
718
|
+
const column = snapshot.nodes.byRole("position");
|
|
719
|
+
const v = column?.isSet(i) === true ? column.value(i) : null;
|
|
720
|
+
return v === null ? [Number.NaN, Number.NaN] : [v[0], v[1]];
|
|
721
|
+
}
|
|
722
|
+
function lineBends(snapshot, g, e) {
|
|
723
|
+
const raw = isRec(g) && isRec(g.Line) ? g.Line.point : void 0;
|
|
724
|
+
if (!Array.isArray(raw) || raw.length < 3) {
|
|
725
|
+
return null;
|
|
726
|
+
}
|
|
727
|
+
const points = raw.slice(1, -1).map((p) => isRec(p) ? [Number(p.x), Number(p.y)] : [Number.NaN, 0]);
|
|
728
|
+
return canonBends(
|
|
729
|
+
centerOf(snapshot, snapshot.edgeSource(e)),
|
|
730
|
+
centerOf(snapshot, snapshot.edgeTarget(e)),
|
|
731
|
+
points
|
|
732
|
+
);
|
|
733
|
+
}
|
|
734
|
+
function readValues(snapshot, writer, notes, context) {
|
|
735
|
+
let found = false;
|
|
736
|
+
const read = (target) => {
|
|
737
|
+
const table = target === "node" ? snapshot.nodes : snapshot.edges;
|
|
738
|
+
const n = target === "node" ? snapshot.nodeCount : snapshot.edgeCount;
|
|
739
|
+
const out = valueColumns(n);
|
|
740
|
+
if (!table.has("graphics") && !table.has("LabelGraphics") && writer.vocabulary !== "cytoscape") {
|
|
741
|
+
return out.values;
|
|
742
|
+
}
|
|
743
|
+
for (let i = 0; i < n; i++) {
|
|
744
|
+
context.budget.spend(target, 1);
|
|
745
|
+
const at = (property) => ({ notes, target, property, element: i, budget: context.budget });
|
|
746
|
+
readElement(
|
|
747
|
+
target,
|
|
748
|
+
cellOf(table, "graphics", i),
|
|
749
|
+
cellOf(table, "LabelGraphics", i),
|
|
750
|
+
cellOf(table, "label", i),
|
|
751
|
+
writer,
|
|
752
|
+
(property, value) => {
|
|
753
|
+
if (value !== null) {
|
|
754
|
+
found = true;
|
|
755
|
+
out.put(i, property, value);
|
|
756
|
+
}
|
|
757
|
+
},
|
|
758
|
+
at
|
|
759
|
+
);
|
|
760
|
+
if (target === "edge") {
|
|
761
|
+
const bends = lineBends(snapshot, cellOf(table, "graphics", i), i);
|
|
762
|
+
if (bends !== null) {
|
|
763
|
+
found = true;
|
|
764
|
+
out.put(i, "bends", bends);
|
|
765
|
+
}
|
|
766
|
+
}
|
|
767
|
+
}
|
|
768
|
+
return out.values;
|
|
769
|
+
};
|
|
770
|
+
const node = read("node");
|
|
771
|
+
const edge = read("edge");
|
|
772
|
+
return { node, edge, found };
|
|
773
|
+
}
|
|
774
|
+
function blockWithValues(snapshot, text) {
|
|
775
|
+
const parsed = parseStyleBlock(text, styleFingerprint(snapshot));
|
|
776
|
+
if (!parsed.ok || parsed.block.values !== void 0) {
|
|
777
|
+
return text;
|
|
778
|
+
}
|
|
779
|
+
const node = tagValues(snapshot, "node", parsed.block.columns.node);
|
|
780
|
+
const edge = tagValues(snapshot, "edge", parsed.block.columns.edge);
|
|
781
|
+
if (node === null || edge === null) {
|
|
782
|
+
return text;
|
|
783
|
+
}
|
|
784
|
+
const raw = JSON.parse(text);
|
|
785
|
+
return JSON.stringify({ ...raw, values: { node, edge } });
|
|
786
|
+
}
|
|
787
|
+
function tagValues(snapshot, target, keys) {
|
|
788
|
+
const table = target === "node" ? snapshot.nodes : snapshot.edges;
|
|
789
|
+
const n = target === "node" ? snapshot.nodeCount : snapshot.edgeCount;
|
|
790
|
+
const values = {};
|
|
791
|
+
for (const key of keys) {
|
|
792
|
+
const name = tagKey(target, key);
|
|
793
|
+
if (!table.has(name)) {
|
|
794
|
+
return null;
|
|
795
|
+
}
|
|
796
|
+
values[key] = Array.from({ length: n }, (_, i) => {
|
|
797
|
+
const v = plainCell(table.value(name, i));
|
|
798
|
+
return typeof v === "string" || typeof v === "number" || typeof v === "boolean" ? v : null;
|
|
799
|
+
});
|
|
800
|
+
}
|
|
801
|
+
return values;
|
|
802
|
+
}
|
|
803
|
+
function styleColumns(table) {
|
|
804
|
+
return [...table.names()].filter((c) => c === "graphics" || c === "LabelGraphics" || c.startsWith(TAG_PREFIX));
|
|
805
|
+
}
|
|
806
|
+
function gmlStyles(snapshot, context) {
|
|
807
|
+
const notes = new StyleNotes();
|
|
808
|
+
const writer = writerOf(snapshot);
|
|
809
|
+
const values = readValues(snapshot, writer, notes, context);
|
|
810
|
+
const blockText = graphText(snapshot, GML_STYLE_BLOCK_KEY);
|
|
811
|
+
const block = blockText === null ? null : blockWithValues(snapshot, blockText);
|
|
812
|
+
if (block !== null) {
|
|
813
|
+
exactFromBlock(snapshot, block, values);
|
|
814
|
+
}
|
|
815
|
+
if (writer.tool === null && !values.found && block === null) {
|
|
816
|
+
return null;
|
|
817
|
+
}
|
|
818
|
+
const gephi = writer.tool === "gephi" ? gephiRules(snapshot, values.node.fill, values.edge.color, context.budget, values.edge.width) : { rules: [], columns: /* @__PURE__ */ new Map() };
|
|
819
|
+
return {
|
|
820
|
+
canonical: true,
|
|
821
|
+
dialect: writer.dialect,
|
|
822
|
+
tool: writer.tool,
|
|
823
|
+
units: writer.units,
|
|
824
|
+
node: { values: values.node },
|
|
825
|
+
edge: { values: values.edge, rules: gephi.rules, columns: gephi.columns },
|
|
826
|
+
notes: notes.list(),
|
|
827
|
+
opacityInColor: true,
|
|
828
|
+
block,
|
|
829
|
+
// the graphics records and the per-element style values of a file graphty wrote hold styles, not data
|
|
830
|
+
readFrom: { node: styleColumns(snapshot.nodes), edge: styleColumns(snapshot.edges) }
|
|
831
|
+
};
|
|
832
|
+
}
|
|
833
|
+
const GML_STYLE_CAPABILITIES = Object.freeze({
|
|
834
|
+
node: Object.freeze([
|
|
835
|
+
"fill",
|
|
836
|
+
"fillOpacity",
|
|
837
|
+
"borderColor",
|
|
838
|
+
"borderWidth",
|
|
839
|
+
"borderStyle",
|
|
840
|
+
"shape",
|
|
841
|
+
"width",
|
|
842
|
+
"height",
|
|
843
|
+
"label",
|
|
844
|
+
"labelColor",
|
|
845
|
+
"labelSize",
|
|
846
|
+
"labelFont",
|
|
847
|
+
"labelWeight",
|
|
848
|
+
"labelItalic"
|
|
849
|
+
]),
|
|
850
|
+
edge: Object.freeze([
|
|
851
|
+
"color",
|
|
852
|
+
"opacity",
|
|
853
|
+
"width",
|
|
854
|
+
"lineStyle",
|
|
855
|
+
"sourceArrow",
|
|
856
|
+
"targetArrow",
|
|
857
|
+
"curved",
|
|
858
|
+
"bends",
|
|
859
|
+
"label",
|
|
860
|
+
"labelColor",
|
|
861
|
+
"labelSize",
|
|
862
|
+
"labelFont",
|
|
863
|
+
"labelWeight",
|
|
864
|
+
"labelItalic"
|
|
865
|
+
]),
|
|
866
|
+
block: "native"
|
|
867
|
+
});
|
|
868
|
+
const OWNED = {
|
|
869
|
+
node: /* @__PURE__ */ new Set([
|
|
870
|
+
"fill",
|
|
871
|
+
"fill2",
|
|
872
|
+
"hasFill",
|
|
873
|
+
"outline",
|
|
874
|
+
"hasOutline",
|
|
875
|
+
"outlineWidth",
|
|
876
|
+
"outline_width",
|
|
877
|
+
"outlineStyle",
|
|
878
|
+
"width",
|
|
879
|
+
"line",
|
|
880
|
+
"lineWidth",
|
|
881
|
+
"stipple",
|
|
882
|
+
"w",
|
|
883
|
+
"h",
|
|
884
|
+
"type"
|
|
885
|
+
]),
|
|
886
|
+
edge: /* @__PURE__ */ new Set([
|
|
887
|
+
"fill",
|
|
888
|
+
"width",
|
|
889
|
+
"lineWidth",
|
|
890
|
+
"style",
|
|
891
|
+
"stipple",
|
|
892
|
+
"arrow",
|
|
893
|
+
"sourceArrow",
|
|
894
|
+
"targetArrow",
|
|
895
|
+
"source_arrow",
|
|
896
|
+
"target_arrow",
|
|
897
|
+
"smoothBends"
|
|
898
|
+
]),
|
|
899
|
+
label: /* @__PURE__ */ new Set(["text", "color", "fill", "fontSize", "fontName", "fontStyle", "visible"])
|
|
900
|
+
};
|
|
901
|
+
function nodeGraphics(get, approximate) {
|
|
902
|
+
const g = {};
|
|
903
|
+
const width = get("width");
|
|
904
|
+
const height = get("height");
|
|
905
|
+
if (typeof width === "number") {
|
|
906
|
+
g.w = width;
|
|
907
|
+
}
|
|
908
|
+
if (typeof height === "number") {
|
|
909
|
+
g.h = height;
|
|
910
|
+
}
|
|
911
|
+
const shape = get("shape");
|
|
912
|
+
if (typeof shape === "string") {
|
|
913
|
+
const w = yedValue(YED_SHAPES, shape, shape === "vee" ? "triangle" : "rectangle");
|
|
914
|
+
if (!w.exact) {
|
|
915
|
+
approximate("shape", shape);
|
|
916
|
+
}
|
|
917
|
+
g.type = w.value;
|
|
918
|
+
}
|
|
919
|
+
const fill = get("fill");
|
|
920
|
+
const fillOpacity = get("fillOpacity");
|
|
921
|
+
if (typeof fill === "string") {
|
|
922
|
+
g.fill = yedColor(fill, fillOpacity);
|
|
923
|
+
} else if (fillOpacity === 0) {
|
|
924
|
+
g.hasFill = 0;
|
|
925
|
+
}
|
|
926
|
+
const borderColor = get("borderColor");
|
|
927
|
+
const borderWidth = get("borderWidth");
|
|
928
|
+
const borderStyle = get("borderStyle");
|
|
929
|
+
if (typeof borderColor === "string") {
|
|
930
|
+
g.outline = borderColor.toUpperCase();
|
|
931
|
+
}
|
|
932
|
+
if (typeof borderStyle === "string") {
|
|
933
|
+
const w = yedValue(YED_LINES, borderStyle, "line");
|
|
934
|
+
if (!w.exact) {
|
|
935
|
+
approximate("borderStyle", borderStyle);
|
|
936
|
+
}
|
|
937
|
+
g.outlineStyle = w.value;
|
|
938
|
+
}
|
|
939
|
+
if (borderWidth === 0) {
|
|
940
|
+
g.hasOutline = 0;
|
|
941
|
+
} else if (typeof borderWidth === "number") {
|
|
942
|
+
g.outlineWidth = borderWidth;
|
|
943
|
+
}
|
|
944
|
+
return g;
|
|
945
|
+
}
|
|
946
|
+
function edgeGraphics(get, approximate) {
|
|
947
|
+
const g = {};
|
|
948
|
+
const color = get("color");
|
|
949
|
+
if (typeof color === "string") {
|
|
950
|
+
g.fill = yedColor(color, get("opacity"));
|
|
951
|
+
}
|
|
952
|
+
const width = get("width");
|
|
953
|
+
if (typeof width === "number") {
|
|
954
|
+
g.width = width;
|
|
955
|
+
}
|
|
956
|
+
const lineStyle = get("lineStyle");
|
|
957
|
+
if (typeof lineStyle === "string") {
|
|
958
|
+
g.style = yedSpelled(
|
|
959
|
+
YED_LINES,
|
|
960
|
+
lineStyle,
|
|
961
|
+
lineStyle === "long-dash" ? "dashed" : "line",
|
|
962
|
+
"lineStyle",
|
|
963
|
+
approximate
|
|
964
|
+
);
|
|
965
|
+
}
|
|
966
|
+
for (const property of ["sourceArrow", "targetArrow"]) {
|
|
967
|
+
const arrow = get(property);
|
|
968
|
+
if (typeof arrow === "string") {
|
|
969
|
+
g[property] = yedSpelled(YED_ARROWS, arrow, "standard", property, approximate);
|
|
970
|
+
}
|
|
971
|
+
}
|
|
972
|
+
const curved = get("curved");
|
|
973
|
+
if (typeof curved === "boolean") {
|
|
974
|
+
g.smoothBends = curved ? 1 : 0;
|
|
975
|
+
}
|
|
976
|
+
return g;
|
|
977
|
+
}
|
|
978
|
+
function labelGraphics(get) {
|
|
979
|
+
const text = get("label");
|
|
980
|
+
if (typeof text !== "string" || text.trim().length === 0) {
|
|
981
|
+
return null;
|
|
982
|
+
}
|
|
983
|
+
const lg = { text };
|
|
984
|
+
const color = get("labelColor");
|
|
985
|
+
if (typeof color === "string") {
|
|
986
|
+
lg.color = color.toUpperCase();
|
|
987
|
+
}
|
|
988
|
+
const size = get("labelSize");
|
|
989
|
+
if (typeof size === "number") {
|
|
990
|
+
lg.fontSize = size;
|
|
991
|
+
}
|
|
992
|
+
const font = get("labelFont");
|
|
993
|
+
if (typeof font === "string") {
|
|
994
|
+
lg.fontName = font;
|
|
995
|
+
}
|
|
996
|
+
const weight = get("labelWeight");
|
|
997
|
+
const italic = get("labelItalic");
|
|
998
|
+
if (weight !== null || italic !== null) {
|
|
999
|
+
const style = (weight === "bold" ? "bold" : "") + (italic === true ? "italic" : "");
|
|
1000
|
+
lg.fontStyle = style.length === 0 ? "plain" : style;
|
|
1001
|
+
}
|
|
1002
|
+
return lg;
|
|
1003
|
+
}
|
|
1004
|
+
function merged(old, owned, written) {
|
|
1005
|
+
const out = {};
|
|
1006
|
+
if (isRec(old)) {
|
|
1007
|
+
for (const [k, v] of Object.entries(old)) {
|
|
1008
|
+
if (!owned.has(k)) {
|
|
1009
|
+
out[k] = v;
|
|
1010
|
+
}
|
|
1011
|
+
}
|
|
1012
|
+
}
|
|
1013
|
+
Object.assign(out, written);
|
|
1014
|
+
return Object.keys(out).length === 0 ? void 0 : out;
|
|
1015
|
+
}
|
|
1016
|
+
function mergedLabels(old, written) {
|
|
1017
|
+
const labels = Array.isArray(old) ? old : [old];
|
|
1018
|
+
const head = merged(labels[0], OWNED.label, written ?? {});
|
|
1019
|
+
const rest = labels.slice(1).filter((l) => l !== null && l !== void 0);
|
|
1020
|
+
if (rest.length === 0) {
|
|
1021
|
+
return head;
|
|
1022
|
+
}
|
|
1023
|
+
return head === void 0 ? rest : [head, ...rest];
|
|
1024
|
+
}
|
|
1025
|
+
function getter(picture, target) {
|
|
1026
|
+
const columns = /* @__PURE__ */ new Map();
|
|
1027
|
+
for (const p of GML_STYLE_CAPABILITIES[target]) {
|
|
1028
|
+
columns.set(p, picture(target, p));
|
|
1029
|
+
}
|
|
1030
|
+
return (i) => (property) => columns.get(property)?.[i] ?? null;
|
|
1031
|
+
}
|
|
1032
|
+
function exactFromBlock(snapshot, text, read) {
|
|
1033
|
+
const parsed = parseStyleBlock(text, styleFingerprint(snapshot));
|
|
1034
|
+
const ex = parsed.ok ? blockStyles(parsed.block, snapshot, "gml") : null;
|
|
1035
|
+
if (ex === null) {
|
|
1036
|
+
return;
|
|
1037
|
+
}
|
|
1038
|
+
const notes = new StyleNotes();
|
|
1039
|
+
for (const target of ["node", "edge"]) {
|
|
1040
|
+
const n = target === "node" ? snapshot.nodeCount : snapshot.edgeCount;
|
|
1041
|
+
const get = getter((t, p) => resolveStyles(ex, t, p, snapshot), target);
|
|
1042
|
+
const values = read[target];
|
|
1043
|
+
for (let i = 0; i < n; i++) {
|
|
1044
|
+
const want = get(i);
|
|
1045
|
+
const back = writtenAndReadBack(target, want, notes);
|
|
1046
|
+
for (const property of GML_STYLE_CAPABILITIES[target]) {
|
|
1047
|
+
const native = values[property]?.[i] ?? null;
|
|
1048
|
+
const w = want(property);
|
|
1049
|
+
if (native !== w && native === (back.get(property) ?? null)) {
|
|
1050
|
+
values[property] ??= new Array(n).fill(null);
|
|
1051
|
+
values[property][i] = w;
|
|
1052
|
+
}
|
|
1053
|
+
}
|
|
1054
|
+
}
|
|
1055
|
+
}
|
|
1056
|
+
}
|
|
1057
|
+
function writtenAndReadBack(target, want, notes) {
|
|
1058
|
+
const ignore = () => void 0;
|
|
1059
|
+
const g = target === "node" ? nodeGraphics(want, ignore) : edgeGraphics(want, ignore);
|
|
1060
|
+
const back = /* @__PURE__ */ new Map();
|
|
1061
|
+
readElement(
|
|
1062
|
+
target,
|
|
1063
|
+
g,
|
|
1064
|
+
labelGraphics(want) ?? void 0,
|
|
1065
|
+
void 0,
|
|
1066
|
+
GRAPHTY_WRITER,
|
|
1067
|
+
(property, value) => {
|
|
1068
|
+
if (value !== null) {
|
|
1069
|
+
back.set(property, value);
|
|
1070
|
+
}
|
|
1071
|
+
},
|
|
1072
|
+
(property) => ({ notes, target, property })
|
|
1073
|
+
);
|
|
1074
|
+
return back;
|
|
1075
|
+
}
|
|
1076
|
+
function gmlStyledExport(snapshot, input) {
|
|
1077
|
+
if (input === void 0) {
|
|
1078
|
+
return null;
|
|
1079
|
+
}
|
|
1080
|
+
const approximated = new StyleApproximations();
|
|
1081
|
+
const out = snapshot.withColumns();
|
|
1082
|
+
const renames = [];
|
|
1083
|
+
for (const target of ["node", "edge"]) {
|
|
1084
|
+
const table = target === "node" ? out.nodes : out.edges;
|
|
1085
|
+
writeGraphicsColumns(snapshot, input, target, table, approximated);
|
|
1086
|
+
const read = dataRead(input.rules.styles.rules, target);
|
|
1087
|
+
for (const name of table.names().filter((c) => mangleGmlKey(c).startsWith(TAG_PREFIX))) {
|
|
1088
|
+
const col = table.get(name);
|
|
1089
|
+
table.remove(name);
|
|
1090
|
+
if (col !== null && !(name.startsWith(TAG_PREFIX) && !read.has(name))) {
|
|
1091
|
+
renames.push(renameDataColumn(table, target, name, col));
|
|
1092
|
+
}
|
|
1093
|
+
}
|
|
1094
|
+
}
|
|
1095
|
+
let { block } = input;
|
|
1096
|
+
const keys = [...input.rules.columns.node.keys()].map((k) => tagKey("node", k));
|
|
1097
|
+
const edgeKeys = [...input.rules.columns.edge.keys()].map((k) => tagKey("edge", k));
|
|
1098
|
+
if (new Set(keys).size !== keys.length || new Set(edgeKeys).size !== edgeKeys.length) {
|
|
1099
|
+
block = writeStyleBlock(input.rules, { inlineColumns: true, snapshot });
|
|
1100
|
+
} else {
|
|
1101
|
+
setTagColumns(out, input);
|
|
1102
|
+
}
|
|
1103
|
+
out.graph.set(GML_STYLE_BLOCK_KEY, [block], { dtype: "string" });
|
|
1104
|
+
const notes = [
|
|
1105
|
+
...renames,
|
|
1106
|
+
...block === input.block ? [] : styleBlockLosses(block, input.rules.styles.rules.length, "size"),
|
|
1107
|
+
...approximated.notes("yEd's GML")
|
|
1108
|
+
];
|
|
1109
|
+
return { snapshot: out, notes };
|
|
1110
|
+
}
|
|
1111
|
+
function writeGraphicsColumns(snapshot, input, target, table, approximated) {
|
|
1112
|
+
const n = target === "node" ? snapshot.nodeCount : snapshot.edgeCount;
|
|
1113
|
+
const approximate = (property, value) => {
|
|
1114
|
+
approximated.add(target, property, value);
|
|
1115
|
+
};
|
|
1116
|
+
const get = getter((t, p) => input.picture(t, p), target);
|
|
1117
|
+
const graphics = [];
|
|
1118
|
+
const labels = [];
|
|
1119
|
+
for (let i = 0; i < n; i++) {
|
|
1120
|
+
const g = target === "node" ? nodeGraphics(get(i), approximate) : edgeGraphics(get(i), approximate);
|
|
1121
|
+
if (target === "edge") {
|
|
1122
|
+
const line = bendsLine(snapshot, get(i)("bends"), i);
|
|
1123
|
+
if (line !== null) {
|
|
1124
|
+
g.Line = line;
|
|
1125
|
+
}
|
|
1126
|
+
}
|
|
1127
|
+
graphics.push(merged(cellOf(table, "graphics", i), OWNED[target], g));
|
|
1128
|
+
labels.push(mergedLabels(cellOf(table, "LabelGraphics", i), labelGraphics(get(i))));
|
|
1129
|
+
}
|
|
1130
|
+
for (const [name, cells] of [
|
|
1131
|
+
["graphics", graphics],
|
|
1132
|
+
["LabelGraphics", labels]
|
|
1133
|
+
]) {
|
|
1134
|
+
if (cells.some((c) => c !== void 0)) {
|
|
1135
|
+
table.set(name, cells, { dtype: "json", nullable: true });
|
|
1136
|
+
} else if (table.has(name)) {
|
|
1137
|
+
table.remove(name);
|
|
1138
|
+
}
|
|
1139
|
+
}
|
|
1140
|
+
}
|
|
1141
|
+
function bendsLine(snapshot, bends, e) {
|
|
1142
|
+
const source = centerOf(snapshot, snapshot.edgeSource(e));
|
|
1143
|
+
const target = centerOf(snapshot, snapshot.edgeTarget(e));
|
|
1144
|
+
const points = typeof bends === "string" ? bendsToPoints(source, target, bends) : null;
|
|
1145
|
+
if (points === null || ![...source, ...target].every(Number.isFinite)) {
|
|
1146
|
+
return null;
|
|
1147
|
+
}
|
|
1148
|
+
return { point: [source, ...points, target].map(([x, y]) => ({ x, y })) };
|
|
1149
|
+
}
|
|
1150
|
+
function renameDataColumn(table, target, name, col) {
|
|
1151
|
+
let renamed = `data_${name}`;
|
|
1152
|
+
let k = 2;
|
|
1153
|
+
while (table.has(renamed)) {
|
|
1154
|
+
renamed = `data_${name}_${String(k)}`;
|
|
1155
|
+
k++;
|
|
1156
|
+
}
|
|
1157
|
+
table.set(renamed, col);
|
|
1158
|
+
return Object.freeze({
|
|
1159
|
+
code: KEY_MANGLED_CODE,
|
|
1160
|
+
message: `${target} column "${name}" is written as "${renamed}": keys starting with ${TAG_PREFIX} hold graphty's style values`,
|
|
1161
|
+
column: name,
|
|
1162
|
+
count: null
|
|
1163
|
+
});
|
|
1164
|
+
}
|
|
1165
|
+
function setTagColumns(out, input) {
|
|
1166
|
+
for (const target of ["node", "edge"]) {
|
|
1167
|
+
const table = target === "node" ? out.nodes : out.edges;
|
|
1168
|
+
for (const [key, col] of input.rules.columns[target]) {
|
|
1169
|
+
const kinds = new Set(col.filter((v) => v !== null).map((v) => typeof v));
|
|
1170
|
+
const text = kinds.size > 1 || kinds.has("string");
|
|
1171
|
+
let dtype = kinds.has("boolean") ? "bool" : "f64";
|
|
1172
|
+
if (text) {
|
|
1173
|
+
dtype = "string";
|
|
1174
|
+
}
|
|
1175
|
+
table.set(tagKey(target, key), text ? col.map((v) => v === null ? null : String(v)) : col, { dtype });
|
|
1176
|
+
}
|
|
1177
|
+
}
|
|
1178
|
+
}
|
|
1179
|
+
const RECORD_NUMBER_TYPE_CODE = "W_GML_RECORD_NUMBER_TYPE";
|
|
1180
|
+
const RECORD_BOOLEAN_CODE = "W_GML_RECORD_BOOLEAN";
|
|
1181
|
+
const RECORD_NULL_CODE = "W_GML_RECORD_NULL";
|
|
1182
|
+
const NESTED_ARRAY_CODE = "E_GML_NESTED_ARRAY";
|
|
1183
|
+
const JSON_ARRAY_CODE = "W_GML_JSON_ARRAY";
|
|
1184
|
+
const INVALID_KEY_CODE = "E_GML_INVALID_KEY";
|
|
1185
|
+
const RESERVED_KEY_CODE = "E_GML_RESERVED_KEY";
|
|
1186
|
+
const POSITION_COMPONENTS_CODE = "W_GML_POSITION_COMPONENTS";
|
|
1187
|
+
const GRAPHICS_OVERRIDDEN_CODE = "W_GML_GRAPHICS_OVERRIDDEN";
|
|
1188
|
+
const GRAPHICS_CONFLICT_CODE = "E_GML_GRAPHICS_CONFLICT";
|
|
1189
|
+
const DEFAULT_WEIGHT_KEY = "value";
|
|
1190
|
+
const KEPT_ROLES = /* @__PURE__ */ new Set(["label", "id", "key"]);
|
|
1191
|
+
const ROLE_NAMES = Object.freeze({
|
|
1192
|
+
label: "label",
|
|
1193
|
+
id: "id",
|
|
1194
|
+
key: "key"
|
|
1195
|
+
});
|
|
1196
|
+
const GML_CAPABILITIES = capabilities({
|
|
1197
|
+
mixedDirection: false,
|
|
1198
|
+
multiEdges: true,
|
|
1199
|
+
selfLoops: true,
|
|
1200
|
+
edgeIds: "optional",
|
|
1201
|
+
idCharset: "integer",
|
|
1202
|
+
dtypes: ["i32", "f64", "string", "dict", "json"],
|
|
1203
|
+
components: false,
|
|
1204
|
+
lists: true,
|
|
1205
|
+
json: true,
|
|
1206
|
+
defaults: false,
|
|
1207
|
+
options: false,
|
|
1208
|
+
hierarchy: false,
|
|
1209
|
+
temporal: "none",
|
|
1210
|
+
graphAttributes: true,
|
|
1211
|
+
positions: true,
|
|
1212
|
+
viz: false
|
|
1213
|
+
});
|
|
1214
|
+
const SKIPPED_ROLES = /* @__PURE__ */ new Set([
|
|
1215
|
+
"directed",
|
|
1216
|
+
"pair",
|
|
1217
|
+
"mutual",
|
|
1218
|
+
"weight",
|
|
1219
|
+
"timeText",
|
|
1220
|
+
"originalId",
|
|
1221
|
+
"position",
|
|
1222
|
+
"color",
|
|
1223
|
+
"size",
|
|
1224
|
+
"shape",
|
|
1225
|
+
"thickness",
|
|
1226
|
+
"parent",
|
|
1227
|
+
"parents",
|
|
1228
|
+
"start",
|
|
1229
|
+
"end",
|
|
1230
|
+
"timestamp",
|
|
1231
|
+
"timestamps",
|
|
1232
|
+
"spells",
|
|
1233
|
+
"open",
|
|
1234
|
+
"spellsOpen"
|
|
1235
|
+
]);
|
|
1236
|
+
const NODE_RESERVED = /* @__PURE__ */ new Set(["id"]);
|
|
1237
|
+
const EDGE_RESERVED = /* @__PURE__ */ new Set(["source", "target", "directed"]);
|
|
1238
|
+
const GRAPH_RESERVED = /* @__PURE__ */ new Set(["node", "edge", "directed", "multigraph"]);
|
|
1239
|
+
const TOP_RESERVED = /* @__PURE__ */ new Set(["graph"]);
|
|
1240
|
+
const NUMBER_TEXT = /^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?$/;
|
|
1241
|
+
function planOf(snapshot, options) {
|
|
1242
|
+
const common = resolveExportOptions(options);
|
|
1243
|
+
const mode = options?.sanitizeKeys ?? "error";
|
|
1244
|
+
if (mode !== "error" && mode !== "mangle") {
|
|
1245
|
+
throw new GraphFormatError(
|
|
1246
|
+
"E_UNSUPPORTED",
|
|
1247
|
+
`option sanitizeKeys: ${JSON.stringify(mode)} is not "error" or "mangle"`,
|
|
1248
|
+
{
|
|
1249
|
+
option: "sanitizeKeys",
|
|
1250
|
+
found: mode
|
|
1251
|
+
}
|
|
1252
|
+
);
|
|
1253
|
+
}
|
|
1254
|
+
let weightKey = options?.weightKey;
|
|
1255
|
+
if (weightKey === void 0) {
|
|
1256
|
+
const origin = snapshot.meta.weightOrigin;
|
|
1257
|
+
weightKey = origin !== null && origin.format === "gml" && origin.id !== null ? origin.id : DEFAULT_WEIGHT_KEY;
|
|
1258
|
+
} else if (typeof weightKey !== "string" || !isGmlKey(weightKey)) {
|
|
1259
|
+
throw new GraphFormatError("E_UNSUPPORTED", `option weightKey: ${JSON.stringify(weightKey)} is not a GML key`, {
|
|
1260
|
+
option: "weightKey",
|
|
1261
|
+
found: weightKey
|
|
1262
|
+
});
|
|
1263
|
+
}
|
|
1264
|
+
return { common, weightKey, mangle: mode === "mangle" };
|
|
1265
|
+
}
|
|
1266
|
+
function isWritten(column) {
|
|
1267
|
+
const { role } = column.meta;
|
|
1268
|
+
return role === null || !SKIPPED_ROLES.has(role);
|
|
1269
|
+
}
|
|
1270
|
+
function preferredKey(meta) {
|
|
1271
|
+
const { origin, name } = meta;
|
|
1272
|
+
if (origin?.format !== "gml" || origin.id === null) {
|
|
1273
|
+
return name;
|
|
1274
|
+
}
|
|
1275
|
+
return name === origin.id || name.startsWith(`${origin.id}#`) ? origin.id : name;
|
|
1276
|
+
}
|
|
1277
|
+
function hasWeights(snapshot) {
|
|
1278
|
+
return snapshot.flags.weighted;
|
|
1279
|
+
}
|
|
1280
|
+
function isTopLevel(column) {
|
|
1281
|
+
return column.meta.extra.gmlTopLevel === true;
|
|
1282
|
+
}
|
|
1283
|
+
function versionText(text) {
|
|
1284
|
+
return NUMBER_TEXT.test(text) ? text : quoteGmlString(text);
|
|
1285
|
+
}
|
|
1286
|
+
function labelRoleColumn(columns, label, used) {
|
|
1287
|
+
if (label !== "node" && label !== "edge") {
|
|
1288
|
+
return void 0;
|
|
1289
|
+
}
|
|
1290
|
+
const column = columns.find((c) => c.meta.role === "label");
|
|
1291
|
+
if (column !== void 0) {
|
|
1292
|
+
used.add("label");
|
|
1293
|
+
}
|
|
1294
|
+
return column;
|
|
1295
|
+
}
|
|
1296
|
+
function selectColumns(table, reserved, label, plan, notes, filter = () => true) {
|
|
1297
|
+
const used = new Set(reserved);
|
|
1298
|
+
const out = [];
|
|
1299
|
+
const columns = [...table].filter((c) => isWritten(c) && filter(c));
|
|
1300
|
+
const labelRole = labelRoleColumn(columns, label, used);
|
|
1301
|
+
for (const column of columns) {
|
|
1302
|
+
const preferred = column === labelRole ? "label" : preferredKey(column.meta);
|
|
1303
|
+
let key = preferred;
|
|
1304
|
+
const valid = isGmlKey(preferred);
|
|
1305
|
+
const taken = column !== labelRole && used.has(preferred);
|
|
1306
|
+
if (!valid || taken) {
|
|
1307
|
+
const code = valid ? RESERVED_KEY_CODE : INVALID_KEY_CODE;
|
|
1308
|
+
let why = "is not a GML key";
|
|
1309
|
+
if (valid) {
|
|
1310
|
+
why = reserved.has(preferred) ? "collides with a structural GML key" : "repeats another column's key";
|
|
1311
|
+
}
|
|
1312
|
+
if (!plan.mangle) {
|
|
1313
|
+
if (notes === null) {
|
|
1314
|
+
throw new GraphFormatError(
|
|
1315
|
+
"E_UNSUPPORTED",
|
|
1316
|
+
`${label} column "${column.meta.name}" ${why}; pass sanitizeKeys: "mangle" to rewrite it`,
|
|
1317
|
+
{
|
|
1318
|
+
reason: "gml key",
|
|
1319
|
+
column: column.meta.name,
|
|
1320
|
+
key: preferred
|
|
1321
|
+
}
|
|
1322
|
+
);
|
|
1323
|
+
}
|
|
1324
|
+
notes.push(
|
|
1325
|
+
note(
|
|
1326
|
+
code,
|
|
1327
|
+
`${label} column "${column.meta.name}" ${why}; the save fails unless sanitizeKeys is "mangle"`,
|
|
1328
|
+
column.meta.name
|
|
1329
|
+
)
|
|
1330
|
+
);
|
|
1331
|
+
continue;
|
|
1332
|
+
}
|
|
1333
|
+
key = uniqueKey(valid ? preferred : mangleGmlKey(preferred), used);
|
|
1334
|
+
notes?.push(
|
|
1335
|
+
note(
|
|
1336
|
+
KEY_MANGLED_CODE,
|
|
1337
|
+
`${label} column "${column.meta.name}" is written as "${key}"`,
|
|
1338
|
+
column.meta.name
|
|
1339
|
+
)
|
|
1340
|
+
);
|
|
1341
|
+
}
|
|
1342
|
+
used.add(key);
|
|
1343
|
+
out.push({ column, key });
|
|
1344
|
+
}
|
|
1345
|
+
return out;
|
|
1346
|
+
}
|
|
1347
|
+
function uniqueKey(base, used) {
|
|
1348
|
+
let candidate = base;
|
|
1349
|
+
for (let k = 2; used.has(candidate); k++) {
|
|
1350
|
+
candidate = `${base}_${k}`;
|
|
1351
|
+
}
|
|
1352
|
+
return candidate;
|
|
1353
|
+
}
|
|
1354
|
+
function note(code, message, column = null, count = null) {
|
|
1355
|
+
return Object.freeze({ code, message, column, count });
|
|
1356
|
+
}
|
|
1357
|
+
function countOnce(seen, flag, stats, counter) {
|
|
1358
|
+
if (!seen[flag]) {
|
|
1359
|
+
seen[flag] = true;
|
|
1360
|
+
stats[counter]++;
|
|
1361
|
+
}
|
|
1362
|
+
}
|
|
1363
|
+
function inspectJson(value, stats, inArray, seen) {
|
|
1364
|
+
if (value === null) {
|
|
1365
|
+
countOnce(seen, "nulls", stats, "nulls");
|
|
1366
|
+
return;
|
|
1367
|
+
}
|
|
1368
|
+
switch (typeof value) {
|
|
1369
|
+
case "number":
|
|
1370
|
+
countOnce(seen, "numbers", stats, "numbers");
|
|
1371
|
+
return;
|
|
1372
|
+
case "boolean":
|
|
1373
|
+
countOnce(seen, "booleans", stats, "booleans");
|
|
1374
|
+
return;
|
|
1375
|
+
case "string":
|
|
1376
|
+
return;
|
|
1377
|
+
}
|
|
1378
|
+
if (Array.isArray(value)) {
|
|
1379
|
+
if (inArray) {
|
|
1380
|
+
countOnce(seen, "nested", stats, "nestedArrays");
|
|
1381
|
+
}
|
|
1382
|
+
for (const item of value) {
|
|
1383
|
+
inspectJson(item, stats, true, seen);
|
|
1384
|
+
}
|
|
1385
|
+
return;
|
|
1386
|
+
}
|
|
1387
|
+
const record = value;
|
|
1388
|
+
for (const key of Object.keys(record)) {
|
|
1389
|
+
if (!isGmlKey(key)) {
|
|
1390
|
+
stats.invalidKeys++;
|
|
1391
|
+
}
|
|
1392
|
+
inspectJson(record[key], stats, false, seen);
|
|
1393
|
+
}
|
|
1394
|
+
}
|
|
1395
|
+
function rowCount(count) {
|
|
1396
|
+
return `${count} row${plural(count)}`;
|
|
1397
|
+
}
|
|
1398
|
+
function pushCounted(notes, count, code, text, column) {
|
|
1399
|
+
if (count > 0) {
|
|
1400
|
+
notes.push(note(code, text, column, count));
|
|
1401
|
+
}
|
|
1402
|
+
}
|
|
1403
|
+
function jsonNotes(columns, label, plan, notes) {
|
|
1404
|
+
for (const { column } of columns) {
|
|
1405
|
+
const stats = jsonStatsOf(column);
|
|
1406
|
+
if (stats === null) {
|
|
1407
|
+
continue;
|
|
1408
|
+
}
|
|
1409
|
+
const { name } = column.meta;
|
|
1410
|
+
const where = `${label} column "${name}"`;
|
|
1411
|
+
const counted = (count, code, text) => {
|
|
1412
|
+
pushCounted(notes, count, code, text, name);
|
|
1413
|
+
};
|
|
1414
|
+
counted(
|
|
1415
|
+
stats.numbers,
|
|
1416
|
+
RECORD_NUMBER_TYPE_CODE,
|
|
1417
|
+
`${where} holds numbers in ${rowCount(stats.numbers)}; GML records cannot keep the int / real distinction`
|
|
1418
|
+
);
|
|
1419
|
+
counted(
|
|
1420
|
+
stats.booleans,
|
|
1421
|
+
RECORD_BOOLEAN_CODE,
|
|
1422
|
+
`${where} holds booleans in ${rowCount(stats.booleans)}; written as 1 / 0`
|
|
1423
|
+
);
|
|
1424
|
+
counted(
|
|
1425
|
+
stats.nulls,
|
|
1426
|
+
RECORD_NULL_CODE,
|
|
1427
|
+
`${where} holds nulls in ${rowCount(stats.nulls)}; GML has no null, the key is omitted`
|
|
1428
|
+
);
|
|
1429
|
+
counted(
|
|
1430
|
+
stats.arrays,
|
|
1431
|
+
JSON_ARRAY_CODE,
|
|
1432
|
+
`${where} holds arrays as values in ${rowCount(stats.arrays)}; written as repeated keys, they re-import as a list`
|
|
1433
|
+
);
|
|
1434
|
+
counted(
|
|
1435
|
+
stats.nestedArrays,
|
|
1436
|
+
NESTED_ARRAY_CODE,
|
|
1437
|
+
`${where} holds arrays nested in arrays in ${rowCount(stats.nestedArrays)}; GML cannot write them and the save fails`
|
|
1438
|
+
);
|
|
1439
|
+
const keys = `${stats.invalidKeys} record key${plural(stats.invalidKeys)} ${agree(stats.invalidKeys, "is", "are")} not GML keys`;
|
|
1440
|
+
counted(
|
|
1441
|
+
stats.invalidKeys,
|
|
1442
|
+
plan.mangle ? KEY_MANGLED_CODE : INVALID_KEY_CODE,
|
|
1443
|
+
plan.mangle ? `${where}: ${keys} and are rewritten` : `${where}: ${keys}; the save fails unless sanitizeKeys is "mangle"`
|
|
1444
|
+
);
|
|
1445
|
+
}
|
|
1446
|
+
}
|
|
1447
|
+
function jsonStatsOf(column) {
|
|
1448
|
+
const stats = { numbers: 0, booleans: 0, nulls: 0, nestedArrays: 0, arrays: 0, invalidKeys: 0 };
|
|
1449
|
+
if (column.dtype === "json") {
|
|
1450
|
+
for (let r = 0; r < column.length; r++) {
|
|
1451
|
+
if (!column.isSet(r)) {
|
|
1452
|
+
continue;
|
|
1453
|
+
}
|
|
1454
|
+
const value = column.values[r];
|
|
1455
|
+
const seen = { numbers: false, booleans: false, nulls: false, nested: false };
|
|
1456
|
+
if (Array.isArray(value)) {
|
|
1457
|
+
stats.arrays++;
|
|
1458
|
+
for (const item of value) {
|
|
1459
|
+
inspectJson(item, stats, true, seen);
|
|
1460
|
+
}
|
|
1461
|
+
} else {
|
|
1462
|
+
inspectJson(value, stats, false, seen);
|
|
1463
|
+
}
|
|
1464
|
+
}
|
|
1465
|
+
return stats;
|
|
1466
|
+
}
|
|
1467
|
+
if (column.dtype === "list" && column.meta.itemDtype === "json") {
|
|
1468
|
+
for (let r = 0; r < column.length; r++) {
|
|
1469
|
+
if (!column.isSet(r)) {
|
|
1470
|
+
continue;
|
|
1471
|
+
}
|
|
1472
|
+
const seen = { numbers: false, booleans: false, nulls: false, nested: false };
|
|
1473
|
+
for (const item of column.sliceOf(r)) {
|
|
1474
|
+
inspectJson(item, stats, true, seen);
|
|
1475
|
+
}
|
|
1476
|
+
}
|
|
1477
|
+
return stats;
|
|
1478
|
+
}
|
|
1479
|
+
return null;
|
|
1480
|
+
}
|
|
1481
|
+
function positionColumn(snapshot) {
|
|
1482
|
+
const column = snapshot.nodes.byRole("position");
|
|
1483
|
+
if (column === null) {
|
|
1484
|
+
return null;
|
|
1485
|
+
}
|
|
1486
|
+
switch (column.dtype) {
|
|
1487
|
+
case "f32":
|
|
1488
|
+
case "f64":
|
|
1489
|
+
case "i32":
|
|
1490
|
+
case "u32":
|
|
1491
|
+
case "u8":
|
|
1492
|
+
return column;
|
|
1493
|
+
default:
|
|
1494
|
+
return null;
|
|
1495
|
+
}
|
|
1496
|
+
}
|
|
1497
|
+
function graphicsColumn(snapshot) {
|
|
1498
|
+
return snapshot.nodes.typed("graphics", "json");
|
|
1499
|
+
}
|
|
1500
|
+
function graphicsNotes(snapshot, notes) {
|
|
1501
|
+
const position = positionColumn(snapshot);
|
|
1502
|
+
if (position === null) {
|
|
1503
|
+
return;
|
|
1504
|
+
}
|
|
1505
|
+
if (position.meta.components > 3) {
|
|
1506
|
+
notes.push(
|
|
1507
|
+
note(
|
|
1508
|
+
POSITION_COMPONENTS_CODE,
|
|
1509
|
+
`node column "${position.meta.name}" has ${position.meta.components} components; only x, y and z are written`,
|
|
1510
|
+
position.meta.name,
|
|
1511
|
+
position.length - position.nullCount
|
|
1512
|
+
)
|
|
1513
|
+
);
|
|
1514
|
+
}
|
|
1515
|
+
const graphics = graphicsColumn(snapshot);
|
|
1516
|
+
if (graphics === null) {
|
|
1517
|
+
return;
|
|
1518
|
+
}
|
|
1519
|
+
let overridden = 0;
|
|
1520
|
+
let conflicts = 0;
|
|
1521
|
+
for (let r = 0; r < snapshot.nodeCount; r++) {
|
|
1522
|
+
if (!position.isSet(r) || !graphics.isSet(r)) {
|
|
1523
|
+
continue;
|
|
1524
|
+
}
|
|
1525
|
+
const value = graphics.values[r];
|
|
1526
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
1527
|
+
conflicts++;
|
|
1528
|
+
} else if ("x" in value || "y" in value || "z" in value) {
|
|
1529
|
+
overridden++;
|
|
1530
|
+
}
|
|
1531
|
+
}
|
|
1532
|
+
if (overridden > 0) {
|
|
1533
|
+
notes.push(
|
|
1534
|
+
note(
|
|
1535
|
+
GRAPHICS_OVERRIDDEN_CODE,
|
|
1536
|
+
`${overridden} node graphics record${plural(overridden)} ${agree(overridden, "has", "have")} x / y / z keys the position column replaces`,
|
|
1537
|
+
"graphics",
|
|
1538
|
+
overridden
|
|
1539
|
+
)
|
|
1540
|
+
);
|
|
1541
|
+
}
|
|
1542
|
+
if (conflicts > 0) {
|
|
1543
|
+
notes.push(
|
|
1544
|
+
note(
|
|
1545
|
+
GRAPHICS_CONFLICT_CODE,
|
|
1546
|
+
`${conflicts} node graphics value${plural(conflicts)} ${agree(conflicts, "is", "are")} not records and cannot hold the position; the save fails`,
|
|
1547
|
+
"graphics",
|
|
1548
|
+
conflicts
|
|
1549
|
+
)
|
|
1550
|
+
);
|
|
1551
|
+
}
|
|
1552
|
+
}
|
|
1553
|
+
function reimportNotes(columns, domain, notes) {
|
|
1554
|
+
for (const { column, key } of columns) {
|
|
1555
|
+
const { meta } = column;
|
|
1556
|
+
const set = column.length - column.nullCount;
|
|
1557
|
+
if (set === 0) {
|
|
1558
|
+
notes.push(
|
|
1559
|
+
note(
|
|
1560
|
+
LOSS.EMPTY_COLUMN,
|
|
1561
|
+
`${domain} column "${meta.name}" has no set cell and is not written: GML writes cells, never declarations`,
|
|
1562
|
+
meta.name,
|
|
1563
|
+
0
|
|
1564
|
+
)
|
|
1565
|
+
);
|
|
1566
|
+
continue;
|
|
1567
|
+
}
|
|
1568
|
+
if (meta.role === null && (key === "label" || domain === "edge" && (key === "id" || key === "key"))) {
|
|
1569
|
+
notes.push(
|
|
1570
|
+
note(
|
|
1571
|
+
LOSS.ROLE_ASSUMED,
|
|
1572
|
+
`${domain} column "${meta.name}" is written under "${key}" and reads back with the ${key === "label" ? "label" : key} role`,
|
|
1573
|
+
meta.name,
|
|
1574
|
+
set
|
|
1575
|
+
)
|
|
1576
|
+
);
|
|
1577
|
+
continue;
|
|
1578
|
+
}
|
|
1579
|
+
if (column.dtype === "list" || !(column.dtype === "string" || column.dtype === "dict")) {
|
|
1580
|
+
continue;
|
|
1581
|
+
}
|
|
1582
|
+
const heuristic = new DictHeuristic(DICT_SAMPLE_ROWS);
|
|
1583
|
+
for (let r = 0; r < column.length && !heuristic.decided; r++) {
|
|
1584
|
+
if (column.isSet(r)) {
|
|
1585
|
+
heuristic.observe(column.value(r));
|
|
1586
|
+
}
|
|
1587
|
+
}
|
|
1588
|
+
const readsAsDict = heuristic.decide() === "dict";
|
|
1589
|
+
if (column.dtype === "string" && readsAsDict) {
|
|
1590
|
+
notes.push(
|
|
1591
|
+
note(
|
|
1592
|
+
LOSS.STORAGE_CLASS,
|
|
1593
|
+
`${domain} column "${meta.name}" reads back as dict (low cardinality)`,
|
|
1594
|
+
meta.name,
|
|
1595
|
+
null
|
|
1596
|
+
)
|
|
1597
|
+
);
|
|
1598
|
+
} else if (column.dtype === "dict" && !readsAsDict) {
|
|
1599
|
+
notes.push(
|
|
1600
|
+
note(
|
|
1601
|
+
LOSS.STORAGE_CLASS,
|
|
1602
|
+
`${domain} column "${meta.name}" reads back as string (cardinality too high for a dict)`,
|
|
1603
|
+
meta.name,
|
|
1604
|
+
null
|
|
1605
|
+
)
|
|
1606
|
+
);
|
|
1607
|
+
}
|
|
1608
|
+
}
|
|
1609
|
+
}
|
|
1610
|
+
function positionNameNote(snapshot, nodeColumns, notes) {
|
|
1611
|
+
const position = positionColumn(snapshot);
|
|
1612
|
+
if (position === null) {
|
|
1613
|
+
return;
|
|
1614
|
+
}
|
|
1615
|
+
const { name } = position.meta;
|
|
1616
|
+
const plainPosition = nodeColumns.some((c) => c.key === "position");
|
|
1617
|
+
const reimportName = plainPosition ? "position#graphics" : "position";
|
|
1618
|
+
if (name !== reimportName) {
|
|
1619
|
+
notes.push(
|
|
1620
|
+
note(
|
|
1621
|
+
LOSS.COLUMN_NAME_CHANGED,
|
|
1622
|
+
`node column "${name}" (position) is written as the graphics x / y / z keys and reads back as "${reimportName}"`,
|
|
1623
|
+
name,
|
|
1624
|
+
position.length - position.nullCount
|
|
1625
|
+
)
|
|
1626
|
+
);
|
|
1627
|
+
}
|
|
1628
|
+
}
|
|
1629
|
+
function check(snapshot, options) {
|
|
1630
|
+
const plan = planOf(snapshot, options);
|
|
1631
|
+
const notes = checkCapabilities(snapshot, GML_CAPABILITIES, plan.common, {
|
|
1632
|
+
positionDtype: "f64",
|
|
1633
|
+
roles: KEPT_ROLES,
|
|
1634
|
+
roleNames: ROLE_NAMES
|
|
1635
|
+
});
|
|
1636
|
+
const nodeReserved = new Set(NODE_RESERVED);
|
|
1637
|
+
if (plan.common.sanitizeIds === "mangle" && countMangled(snapshot) > 0 || snapshot.nodes.byRole("originalId") !== null) {
|
|
1638
|
+
nodeReserved.add(ORIGINAL_ID_KEY);
|
|
1639
|
+
}
|
|
1640
|
+
const edgeIds = snapshot.edges.byRole("id");
|
|
1641
|
+
const edgeReserved = edgeReservedKeys(snapshot, plan, edgeIds);
|
|
1642
|
+
const topReserved = topReservedKeys(snapshot);
|
|
1643
|
+
const nodeColumns = selectColumns(snapshot.nodes, nodeReserved, "node", plan, notes);
|
|
1644
|
+
const edgeColumns = selectColumns(snapshot.edges, edgeReserved, "edge", plan, notes, (c) => c !== edgeIds);
|
|
1645
|
+
const graphColumns = selectColumns(snapshot.graph, GRAPH_RESERVED, "graph", plan, notes, (c) => !isTopLevel(c));
|
|
1646
|
+
const topColumns = selectColumns(snapshot.graph, topReserved, "top-level", plan, notes, isTopLevel);
|
|
1647
|
+
jsonNotes(nodeColumns, "node", plan, notes);
|
|
1648
|
+
jsonNotes(edgeColumns, "edge", plan, notes);
|
|
1649
|
+
jsonNotes(graphColumns, "graph", plan, notes);
|
|
1650
|
+
jsonNotes(topColumns, "top-level", plan, notes);
|
|
1651
|
+
graphicsNotes(snapshot, notes);
|
|
1652
|
+
positionNameNote(snapshot, nodeColumns, notes);
|
|
1653
|
+
reimportNotes(nodeColumns, "node", notes);
|
|
1654
|
+
reimportNotes(edgeColumns, "edge", notes);
|
|
1655
|
+
if (!hasWeights(snapshot)) {
|
|
1656
|
+
const clash = edgeColumns.find((c) => c.key === plan.weightKey);
|
|
1657
|
+
if (clash !== void 0) {
|
|
1658
|
+
notes.push(
|
|
1659
|
+
note(
|
|
1660
|
+
LOSS.WEIGHT_KEY_CLASH,
|
|
1661
|
+
`edge column "${clash.column.meta.name}" is written under "${plan.weightKey}", the key the importer reads edge weights from; it reads back as the weight, not as a column`,
|
|
1662
|
+
clash.column.meta.name,
|
|
1663
|
+
clash.column.length - clash.column.nullCount
|
|
1664
|
+
)
|
|
1665
|
+
);
|
|
1666
|
+
}
|
|
1667
|
+
}
|
|
1668
|
+
const folding = pairFolding(snapshot);
|
|
1669
|
+
if (folding.mutualCount > 0) {
|
|
1670
|
+
notes.push(
|
|
1671
|
+
note(
|
|
1672
|
+
LOSS.MUTUAL_EXPANDED,
|
|
1673
|
+
`${folding.mutualCount} mutual pair${plural(folding.mutualCount)} ${agree(folding.mutualCount, "is", "are")} written as two directed edges; the mutual mark is lost`,
|
|
1674
|
+
null,
|
|
1675
|
+
folding.mutualCount
|
|
1676
|
+
)
|
|
1677
|
+
);
|
|
1678
|
+
}
|
|
1679
|
+
return Object.freeze(notes);
|
|
1680
|
+
}
|
|
1681
|
+
function edgeReservedKeys(snapshot, plan, edgeIds) {
|
|
1682
|
+
const reserved = new Set(EDGE_RESERVED);
|
|
1683
|
+
if (hasWeights(snapshot)) {
|
|
1684
|
+
reserved.add(plan.weightKey);
|
|
1685
|
+
}
|
|
1686
|
+
if (edgeIds !== null) {
|
|
1687
|
+
reserved.add("id");
|
|
1688
|
+
}
|
|
1689
|
+
return reserved;
|
|
1690
|
+
}
|
|
1691
|
+
function topReservedKeys(snapshot) {
|
|
1692
|
+
const reserved = new Set(TOP_RESERVED);
|
|
1693
|
+
if (snapshot.meta.creator !== null) {
|
|
1694
|
+
reserved.add("Creator");
|
|
1695
|
+
}
|
|
1696
|
+
if (snapshot.meta.sourceFormat === "gml" && snapshot.meta.sourceVersion !== null) {
|
|
1697
|
+
reserved.add("Version");
|
|
1698
|
+
}
|
|
1699
|
+
return reserved;
|
|
1700
|
+
}
|
|
1701
|
+
function countMangled(snapshot) {
|
|
1702
|
+
let count = 0;
|
|
1703
|
+
for (let i = 0; i < snapshot.nodeCount; i++) {
|
|
1704
|
+
const id = snapshot.ids.idOf(i);
|
|
1705
|
+
if (typeof id !== "number" || !Number.isSafeInteger(id)) {
|
|
1706
|
+
count++;
|
|
1707
|
+
}
|
|
1708
|
+
}
|
|
1709
|
+
return count;
|
|
1710
|
+
}
|
|
1711
|
+
function numberText(value, meta) {
|
|
1712
|
+
switch (meta.dtype) {
|
|
1713
|
+
case "i32":
|
|
1714
|
+
case "u32":
|
|
1715
|
+
case "u8":
|
|
1716
|
+
return formatInteger(value);
|
|
1717
|
+
case "f32":
|
|
1718
|
+
return formatGmlReal(value, "f32");
|
|
1719
|
+
default:
|
|
1720
|
+
if (meta.origin?.type === "int" && Number.isInteger(value)) {
|
|
1721
|
+
return formatInteger(value);
|
|
1722
|
+
}
|
|
1723
|
+
return formatGmlReal(value, "f64");
|
|
1724
|
+
}
|
|
1725
|
+
}
|
|
1726
|
+
function jsonScalarText(value) {
|
|
1727
|
+
switch (typeof value) {
|
|
1728
|
+
case "number":
|
|
1729
|
+
return Number.isInteger(value) ? formatInteger(value) : formatGmlReal(value, "f64");
|
|
1730
|
+
case "boolean":
|
|
1731
|
+
return value ? "1" : "0";
|
|
1732
|
+
default:
|
|
1733
|
+
return quoteGmlString(value);
|
|
1734
|
+
}
|
|
1735
|
+
}
|
|
1736
|
+
function itemText(item, itemDtype, meta) {
|
|
1737
|
+
switch (typeof item) {
|
|
1738
|
+
case "number":
|
|
1739
|
+
switch (itemDtype) {
|
|
1740
|
+
case "i32":
|
|
1741
|
+
case "u32":
|
|
1742
|
+
case "u8":
|
|
1743
|
+
return formatInteger(item);
|
|
1744
|
+
case "f32":
|
|
1745
|
+
return formatGmlReal(item, "f32");
|
|
1746
|
+
case "json":
|
|
1747
|
+
return jsonScalarText(item);
|
|
1748
|
+
default:
|
|
1749
|
+
return meta.origin?.type === "int" && Number.isInteger(item) ? formatInteger(item) : formatGmlReal(item, "f64");
|
|
1750
|
+
}
|
|
1751
|
+
case "boolean":
|
|
1752
|
+
return item ? "1" : "0";
|
|
1753
|
+
case "string":
|
|
1754
|
+
return quoteGmlString(item);
|
|
1755
|
+
default:
|
|
1756
|
+
throw new GraphFormatError(
|
|
1757
|
+
"E_COLUMN_TYPE",
|
|
1758
|
+
`column "${meta.name}": a ${typeof item} list item cannot be written as GML`,
|
|
1759
|
+
{
|
|
1760
|
+
column: meta.name,
|
|
1761
|
+
found: typeof item
|
|
1762
|
+
}
|
|
1763
|
+
);
|
|
1764
|
+
}
|
|
1765
|
+
}
|
|
1766
|
+
class GmlWriter {
|
|
1767
|
+
/**
|
|
1768
|
+
* Create a writer.
|
|
1769
|
+
* @param mangle - whether record keys are rewritten rather than refused
|
|
1770
|
+
*/
|
|
1771
|
+
constructor(mangle) {
|
|
1772
|
+
this.lines = [];
|
|
1773
|
+
this.mangle = mangle;
|
|
1774
|
+
}
|
|
1775
|
+
/**
|
|
1776
|
+
* Take the lines written so far as one string.
|
|
1777
|
+
* @returns the text; the writer is empty afterwards
|
|
1778
|
+
*/
|
|
1779
|
+
take() {
|
|
1780
|
+
const text = this.lines.join("");
|
|
1781
|
+
this.lines.length = 0;
|
|
1782
|
+
return text;
|
|
1783
|
+
}
|
|
1784
|
+
/**
|
|
1785
|
+
* Write one `key value` line.
|
|
1786
|
+
* @param indent - the indentation
|
|
1787
|
+
* @param key - the key
|
|
1788
|
+
* @param value - the value text
|
|
1789
|
+
*/
|
|
1790
|
+
line(indent, key, value) {
|
|
1791
|
+
this.lines.push(`${indent}${key} ${value}
|
|
1792
|
+
`);
|
|
1793
|
+
}
|
|
1794
|
+
/**
|
|
1795
|
+
* Write a raw line.
|
|
1796
|
+
* @param text - the line without its terminator
|
|
1797
|
+
*/
|
|
1798
|
+
raw(text) {
|
|
1799
|
+
this.lines.push(`${text}
|
|
1800
|
+
`);
|
|
1801
|
+
}
|
|
1802
|
+
/**
|
|
1803
|
+
* Write one column cell of a row.
|
|
1804
|
+
* @param indent - the indentation
|
|
1805
|
+
* @param key - the key
|
|
1806
|
+
* @param column - the column
|
|
1807
|
+
* @param row - the row
|
|
1808
|
+
*/
|
|
1809
|
+
cell(indent, key, column, row) {
|
|
1810
|
+
const { meta, dtype } = column;
|
|
1811
|
+
switch (dtype) {
|
|
1812
|
+
case "f32":
|
|
1813
|
+
case "f64":
|
|
1814
|
+
case "i32":
|
|
1815
|
+
case "u32":
|
|
1816
|
+
case "u8": {
|
|
1817
|
+
const { components } = meta;
|
|
1818
|
+
if (components === 1) {
|
|
1819
|
+
this.line(indent, key, numberText(column.data[row], meta));
|
|
1820
|
+
} else {
|
|
1821
|
+
for (let k = 0; k < components; k++) {
|
|
1822
|
+
this.line(indent, key, numberText(column.data[row * components + k], meta));
|
|
1823
|
+
}
|
|
1824
|
+
}
|
|
1825
|
+
return;
|
|
1826
|
+
}
|
|
1827
|
+
case "bool":
|
|
1828
|
+
this.line(indent, key, column.value(row) === true ? "1" : "0");
|
|
1829
|
+
return;
|
|
1830
|
+
case "dict":
|
|
1831
|
+
case "string":
|
|
1832
|
+
this.line(indent, key, quoteGmlString(column.value(row) ?? ""));
|
|
1833
|
+
return;
|
|
1834
|
+
case "list":
|
|
1835
|
+
this.list(indent, key, column.sliceOf(row), meta.itemDtype ?? "string", meta);
|
|
1836
|
+
return;
|
|
1837
|
+
case "json":
|
|
1838
|
+
this.json(indent, key, column.values[row], meta.name);
|
|
1839
|
+
return;
|
|
1840
|
+
default: {
|
|
1841
|
+
const name = dtype;
|
|
1842
|
+
throw new GraphFormatError("E_COLUMN_TYPE", `unknown dtype ${name}`, { dtype: name });
|
|
1843
|
+
}
|
|
1844
|
+
}
|
|
1845
|
+
}
|
|
1846
|
+
/**
|
|
1847
|
+
* Write a list as repeated keys with the NetworkX conventions.
|
|
1848
|
+
* @param indent - the indentation
|
|
1849
|
+
* @param key - the key
|
|
1850
|
+
* @param items - the items
|
|
1851
|
+
* @param itemDtype - the item dtype
|
|
1852
|
+
* @param meta - the column metadata
|
|
1853
|
+
*/
|
|
1854
|
+
list(indent, key, items, itemDtype, meta) {
|
|
1855
|
+
if (items.length === 0) {
|
|
1856
|
+
this.line(indent, key, quoteGmlString(EMPTY_LIST_TEXT));
|
|
1857
|
+
return;
|
|
1858
|
+
}
|
|
1859
|
+
if (items.length === 1) {
|
|
1860
|
+
this.line(indent, key, quoteGmlString(LIST_START_MARKER));
|
|
1861
|
+
}
|
|
1862
|
+
for (const item of items) {
|
|
1863
|
+
if (itemDtype === "json" && typeof item === "object") {
|
|
1864
|
+
this.json(indent, key, item, meta.name, true);
|
|
1865
|
+
} else {
|
|
1866
|
+
this.line(indent, key, itemText(item, itemDtype, meta));
|
|
1867
|
+
}
|
|
1868
|
+
}
|
|
1869
|
+
}
|
|
1870
|
+
/**
|
|
1871
|
+
* Write a JSON value: a record for an object, repeated keys for an array, a scalar otherwise;
|
|
1872
|
+
* null writes nothing.
|
|
1873
|
+
* @param indent - the indentation
|
|
1874
|
+
* @param key - the key
|
|
1875
|
+
* @param value - the value
|
|
1876
|
+
* @param column - the column name, for errors
|
|
1877
|
+
* @param inArray - whether the value is an array item (an array here has no GML spelling)
|
|
1878
|
+
*/
|
|
1879
|
+
json(indent, key, value, column, inArray = false) {
|
|
1880
|
+
if (value === null || value === void 0) {
|
|
1881
|
+
return;
|
|
1882
|
+
}
|
|
1883
|
+
if (Array.isArray(value)) {
|
|
1884
|
+
if (inArray) {
|
|
1885
|
+
throw new GraphFormatError(
|
|
1886
|
+
"E_UNSUPPORTED",
|
|
1887
|
+
`column "${column}": an array nested in an array cannot be written as GML`,
|
|
1888
|
+
{
|
|
1889
|
+
reason: "nested array",
|
|
1890
|
+
column
|
|
1891
|
+
}
|
|
1892
|
+
);
|
|
1893
|
+
}
|
|
1894
|
+
if (value.length === 0) {
|
|
1895
|
+
this.line(indent, key, quoteGmlString(EMPTY_LIST_TEXT));
|
|
1896
|
+
return;
|
|
1897
|
+
}
|
|
1898
|
+
if (value.length === 1) {
|
|
1899
|
+
this.line(indent, key, quoteGmlString(LIST_START_MARKER));
|
|
1900
|
+
}
|
|
1901
|
+
for (const item of value) {
|
|
1902
|
+
this.json(indent, key, item, column, true);
|
|
1903
|
+
}
|
|
1904
|
+
return;
|
|
1905
|
+
}
|
|
1906
|
+
if (typeof value === "object") {
|
|
1907
|
+
this.record(indent, key, value, column);
|
|
1908
|
+
return;
|
|
1909
|
+
}
|
|
1910
|
+
if (typeof value === "number" || typeof value === "string" || typeof value === "boolean") {
|
|
1911
|
+
this.line(indent, key, jsonScalarText(value));
|
|
1912
|
+
return;
|
|
1913
|
+
}
|
|
1914
|
+
throw new GraphFormatError("E_COLUMN_TYPE", `column "${column}": a ${typeof value} cannot be written as GML`, {
|
|
1915
|
+
column,
|
|
1916
|
+
found: typeof value
|
|
1917
|
+
});
|
|
1918
|
+
}
|
|
1919
|
+
/**
|
|
1920
|
+
* Write a record `key [ ... ]`.
|
|
1921
|
+
* @param indent - the indentation
|
|
1922
|
+
* @param key - the key
|
|
1923
|
+
* @param record - the object
|
|
1924
|
+
* @param column - the column name, for errors
|
|
1925
|
+
* @param extra - lines to write first (the position of a graphics record), or null
|
|
1926
|
+
*/
|
|
1927
|
+
record(indent, key, record, column, extra = null) {
|
|
1928
|
+
this.raw(`${indent}${key} [`);
|
|
1929
|
+
const inner = `${indent} `;
|
|
1930
|
+
const used = /* @__PURE__ */ new Set();
|
|
1931
|
+
if (extra !== null) {
|
|
1932
|
+
for (const [k, v] of extra) {
|
|
1933
|
+
this.line(inner, k, v);
|
|
1934
|
+
used.add(k);
|
|
1935
|
+
}
|
|
1936
|
+
}
|
|
1937
|
+
for (const name of Object.keys(record)) {
|
|
1938
|
+
if (used.has(name) && extra !== null && (name === "x" || name === "y" || name === "z")) {
|
|
1939
|
+
continue;
|
|
1940
|
+
}
|
|
1941
|
+
const written = this.recordKey(name, used, column);
|
|
1942
|
+
used.add(written);
|
|
1943
|
+
this.json(inner, written, record[name], column);
|
|
1944
|
+
}
|
|
1945
|
+
this.raw(`${indent}]`);
|
|
1946
|
+
}
|
|
1947
|
+
/**
|
|
1948
|
+
* The key a record field is written under.
|
|
1949
|
+
* @param name - the field name
|
|
1950
|
+
* @param used - the keys already written in the record
|
|
1951
|
+
* @param column - the column name, for errors
|
|
1952
|
+
* @returns a valid, unused key; E_UNSUPPORTED for an invalid key unless mangling
|
|
1953
|
+
*/
|
|
1954
|
+
recordKey(name, used, column) {
|
|
1955
|
+
if (isGmlKey(name) && !used.has(name)) {
|
|
1956
|
+
return name;
|
|
1957
|
+
}
|
|
1958
|
+
if (!this.mangle) {
|
|
1959
|
+
throw new GraphFormatError(
|
|
1960
|
+
"E_UNSUPPORTED",
|
|
1961
|
+
`column "${column}": record key "${name}" ${isGmlKey(name) ? "repeats a key" : "is not a GML key"}; pass sanitizeKeys: "mangle" to rewrite it`,
|
|
1962
|
+
{
|
|
1963
|
+
reason: "gml key",
|
|
1964
|
+
column,
|
|
1965
|
+
key: name
|
|
1966
|
+
}
|
|
1967
|
+
);
|
|
1968
|
+
}
|
|
1969
|
+
return uniqueKey(isGmlKey(name) ? name : mangleGmlKey(name), used);
|
|
1970
|
+
}
|
|
1971
|
+
}
|
|
1972
|
+
function contextOf(snapshot, options) {
|
|
1973
|
+
const plan = planOf(snapshot, options);
|
|
1974
|
+
const ids = sanitizeIds(snapshot, "integer", plan.common.sanitizeIds);
|
|
1975
|
+
const mixed = countMixedEdges(snapshot);
|
|
1976
|
+
if (mixed > 0 && plan.common.onMixedDirection === "error") {
|
|
1977
|
+
throw new GraphFormatError(
|
|
1978
|
+
"E_DIRECTED",
|
|
1979
|
+
`${mixed} undirected edge${plural(mixed)} in a directed graph, and GML holds one direction per file, so the save fails unless onMixedDirection is "directed" or "undirected"`,
|
|
1980
|
+
{
|
|
1981
|
+
reason: "mixed direction",
|
|
1982
|
+
count: mixed
|
|
1983
|
+
}
|
|
1984
|
+
);
|
|
1985
|
+
}
|
|
1986
|
+
const foldPairs = mixed > 0;
|
|
1987
|
+
const writeDirected = snapshot.directed && plan.common.onMixedDirection !== "undirected";
|
|
1988
|
+
const originalIds = snapshot.nodes.byRole("originalId");
|
|
1989
|
+
const nodeReserved = new Set(NODE_RESERVED);
|
|
1990
|
+
if (ids.changed > 0 || originalIds !== null) {
|
|
1991
|
+
nodeReserved.add(ORIGINAL_ID_KEY);
|
|
1992
|
+
}
|
|
1993
|
+
const edgeIds = snapshot.edges.byRole("id");
|
|
1994
|
+
const edgeReserved = edgeReservedKeys(snapshot, plan, edgeIds);
|
|
1995
|
+
const topReserved = topReservedKeys(snapshot);
|
|
1996
|
+
return {
|
|
1997
|
+
snapshot,
|
|
1998
|
+
plan,
|
|
1999
|
+
ids,
|
|
2000
|
+
writeDirected,
|
|
2001
|
+
foldPairs,
|
|
2002
|
+
nodeColumns: selectColumns(
|
|
2003
|
+
snapshot.nodes,
|
|
2004
|
+
nodeReserved,
|
|
2005
|
+
"node",
|
|
2006
|
+
plan,
|
|
2007
|
+
null,
|
|
2008
|
+
(c) => !(c.meta.name === "graphics" && c.dtype === "json")
|
|
2009
|
+
),
|
|
2010
|
+
edgeColumns: selectColumns(snapshot.edges, edgeReserved, "edge", plan, null, (c) => c !== edgeIds),
|
|
2011
|
+
graphColumns: selectColumns(snapshot.graph, GRAPH_RESERVED, "graph", plan, null, (c) => !isTopLevel(c)),
|
|
2012
|
+
topColumns: selectColumns(snapshot.graph, topReserved, "top-level", plan, null, isTopLevel),
|
|
2013
|
+
position: positionColumn(snapshot),
|
|
2014
|
+
graphics: graphicsColumn(snapshot),
|
|
2015
|
+
originalIds,
|
|
2016
|
+
edgeIds,
|
|
2017
|
+
weights: explicitWeights(snapshot),
|
|
2018
|
+
folding: pairFolding(snapshot)
|
|
2019
|
+
};
|
|
2020
|
+
}
|
|
2021
|
+
function originalIdText(id) {
|
|
2022
|
+
if (typeof id === "string") {
|
|
2023
|
+
return quoteGmlString(id);
|
|
2024
|
+
}
|
|
2025
|
+
return Number.isSafeInteger(id) ? formatInteger(id) : formatGmlReal(id, "f64");
|
|
2026
|
+
}
|
|
2027
|
+
function* gmlParts(context) {
|
|
2028
|
+
const { snapshot, plan, ids } = context;
|
|
2029
|
+
const w = new GmlWriter(plan.mangle);
|
|
2030
|
+
const { meta } = snapshot;
|
|
2031
|
+
if (meta.creator !== null) {
|
|
2032
|
+
w.line("", "Creator", quoteGmlString(meta.creator));
|
|
2033
|
+
}
|
|
2034
|
+
if (meta.sourceFormat === "gml" && meta.sourceVersion !== null) {
|
|
2035
|
+
w.line("", "Version", versionText(meta.sourceVersion));
|
|
2036
|
+
}
|
|
2037
|
+
for (const { column, key } of context.topColumns) {
|
|
2038
|
+
if (column.isSet(0)) {
|
|
2039
|
+
w.cell("", key, column, 0);
|
|
2040
|
+
}
|
|
2041
|
+
}
|
|
2042
|
+
w.raw("graph [");
|
|
2043
|
+
w.line(" ", "directed", context.writeDirected ? "1" : "0");
|
|
2044
|
+
const multigraph = meta.declaredMultigraph ?? (snapshot.flags.multigraph ? true : null);
|
|
2045
|
+
if (multigraph !== null) {
|
|
2046
|
+
w.line(" ", "multigraph", multigraph ? "1" : "0");
|
|
2047
|
+
}
|
|
2048
|
+
for (const { column, key } of context.graphColumns) {
|
|
2049
|
+
if (column.isSet(0)) {
|
|
2050
|
+
w.cell(" ", key, column, 0);
|
|
2051
|
+
}
|
|
2052
|
+
}
|
|
2053
|
+
yield w.take();
|
|
2054
|
+
for (let i = 0; i < snapshot.nodeCount; i++) {
|
|
2055
|
+
w.raw(" node [");
|
|
2056
|
+
w.line(" ", "id", formatInteger(ids.idAt(i)));
|
|
2057
|
+
if (ids.isChanged(i)) {
|
|
2058
|
+
w.line(" ", ORIGINAL_ID_KEY, originalIdText(ids.originalAt(i)));
|
|
2059
|
+
} else if (context.originalIds?.isSet(i)) {
|
|
2060
|
+
const original = context.originalIds.value(i);
|
|
2061
|
+
if (typeof original === "string" || typeof original === "number") {
|
|
2062
|
+
w.line(" ", ORIGINAL_ID_KEY, originalIdText(original));
|
|
2063
|
+
}
|
|
2064
|
+
}
|
|
2065
|
+
for (const { column, key } of context.nodeColumns) {
|
|
2066
|
+
if (column.isSet(i)) {
|
|
2067
|
+
w.cell(" ", key, column, i);
|
|
2068
|
+
}
|
|
2069
|
+
}
|
|
2070
|
+
writeGraphics(w, context, i);
|
|
2071
|
+
w.raw(" ]");
|
|
2072
|
+
yield w.take();
|
|
2073
|
+
}
|
|
2074
|
+
const el = snapshot.edgeList();
|
|
2075
|
+
const origin = meta.weightOrigin;
|
|
2076
|
+
const integerWeights = origin !== null && origin.type === "int";
|
|
2077
|
+
for (let e = 0; e < snapshot.edgeCount; e++) {
|
|
2078
|
+
if (context.foldPairs && context.folding.folded(e)) {
|
|
2079
|
+
continue;
|
|
2080
|
+
}
|
|
2081
|
+
w.raw(" edge [");
|
|
2082
|
+
w.line(" ", "source", formatInteger(ids.idAt(el.src[e])));
|
|
2083
|
+
w.line(" ", "target", formatInteger(ids.idAt(el.dst[e])));
|
|
2084
|
+
if (context.edgeIds?.isSet(e)) {
|
|
2085
|
+
w.cell(" ", "id", context.edgeIds, e);
|
|
2086
|
+
}
|
|
2087
|
+
if (context.weights.isExplicit(e)) {
|
|
2088
|
+
const weight = context.weights.value(e);
|
|
2089
|
+
const text = integerWeights && Number.isInteger(weight) ? formatInteger(weight) : formatGmlReal(weight, context.weights.dtype);
|
|
2090
|
+
w.line(" ", plan.weightKey, text);
|
|
2091
|
+
}
|
|
2092
|
+
for (const { column, key } of context.edgeColumns) {
|
|
2093
|
+
if (column.isSet(e)) {
|
|
2094
|
+
w.cell(" ", key, column, e);
|
|
2095
|
+
}
|
|
2096
|
+
}
|
|
2097
|
+
w.raw(" ]");
|
|
2098
|
+
yield w.take();
|
|
2099
|
+
}
|
|
2100
|
+
w.raw("]");
|
|
2101
|
+
yield w.take();
|
|
2102
|
+
}
|
|
2103
|
+
function writeGraphics(w, context, i) {
|
|
2104
|
+
const { position, graphics } = context;
|
|
2105
|
+
const hasPosition = position?.isSet(i) === true;
|
|
2106
|
+
const graphicsValue = graphics?.isSet(i) ? graphics.values[i] : void 0;
|
|
2107
|
+
if (!hasPosition) {
|
|
2108
|
+
if (graphics !== null && graphicsValue !== void 0) {
|
|
2109
|
+
w.cell(" ", "graphics", graphics, i);
|
|
2110
|
+
}
|
|
2111
|
+
return;
|
|
2112
|
+
}
|
|
2113
|
+
const numeric = position;
|
|
2114
|
+
const { components } = numeric.meta;
|
|
2115
|
+
const lanes = Math.min(components, 3);
|
|
2116
|
+
const extra = [];
|
|
2117
|
+
const names = ["x", "y", "z"];
|
|
2118
|
+
for (let k = 0; k < lanes; k++) {
|
|
2119
|
+
extra.push([names[k], numberText(numeric.data[i * components + k], numeric.meta)]);
|
|
2120
|
+
}
|
|
2121
|
+
let record = {};
|
|
2122
|
+
if (graphicsValue !== void 0) {
|
|
2123
|
+
if (typeof graphicsValue !== "object" || graphicsValue === null || Array.isArray(graphicsValue)) {
|
|
2124
|
+
throw new GraphFormatError(
|
|
2125
|
+
"E_UNSUPPORTED",
|
|
2126
|
+
`node ${i}: the graphics value is not a record and cannot hold the position`,
|
|
2127
|
+
{
|
|
2128
|
+
reason: "graphics conflict",
|
|
2129
|
+
node: i
|
|
2130
|
+
}
|
|
2131
|
+
);
|
|
2132
|
+
}
|
|
2133
|
+
record = graphicsValue;
|
|
2134
|
+
}
|
|
2135
|
+
w.record(" ", "graphics", record, "graphics", extra);
|
|
2136
|
+
}
|
|
2137
|
+
const gmlExporter = Object.freeze({
|
|
2138
|
+
format: "gml",
|
|
2139
|
+
options: Object.freeze(["sanitizeKeys", "weightKey"]),
|
|
2140
|
+
capabilities: GML_CAPABILITIES,
|
|
2141
|
+
styles: Object.freeze({ capabilities: GML_STYLE_CAPABILITIES }),
|
|
2142
|
+
/**
|
|
2143
|
+
* Pre-flight: the common capability notes, the GML-specific ones and, with styles, the values written as the
|
|
2144
|
+
* nearest ones yEd's GML has.
|
|
2145
|
+
* @param snapshot - the snapshot
|
|
2146
|
+
* @param options - the options
|
|
2147
|
+
* @returns the notes, empty when the export is exact
|
|
2148
|
+
*/
|
|
2149
|
+
check(snapshot, options) {
|
|
2150
|
+
const styled = gmlStyledExport(snapshot, options?.styles);
|
|
2151
|
+
if (styled === null) {
|
|
2152
|
+
return check(snapshot, options);
|
|
2153
|
+
}
|
|
2154
|
+
return Object.freeze([...check(styled.snapshot, options), ...styled.notes]);
|
|
2155
|
+
},
|
|
2156
|
+
/**
|
|
2157
|
+
* Write the snapshot as UTF-8 chunks; the context is resolved on the first pull, so the
|
|
2158
|
+
* errors check() announced surface from the iteration.
|
|
2159
|
+
* @param snapshot - the snapshot
|
|
2160
|
+
* @param options - the options
|
|
2161
|
+
* @returns the chunks
|
|
2162
|
+
*/
|
|
2163
|
+
export(snapshot, options) {
|
|
2164
|
+
return encodeChunks(lazyParts(snapshot, options));
|
|
2165
|
+
},
|
|
2166
|
+
/**
|
|
2167
|
+
* Write the snapshot as one string.
|
|
2168
|
+
* @param snapshot - the snapshot
|
|
2169
|
+
* @param options - the options
|
|
2170
|
+
* @returns the document
|
|
2171
|
+
*/
|
|
2172
|
+
exportToString(snapshot, options) {
|
|
2173
|
+
return joinText(lazyParts(snapshot, options));
|
|
2174
|
+
}
|
|
2175
|
+
});
|
|
2176
|
+
function* lazyParts(snapshot, options) {
|
|
2177
|
+
yield* gmlParts(contextOf(gmlStyledExport(snapshot, options?.styles)?.snapshot ?? snapshot, options));
|
|
2178
|
+
}
|
|
2179
|
+
const GRAPHICS_COLUMN = "graphics";
|
|
2180
|
+
function gmlPositions(snapshot) {
|
|
2181
|
+
if (snapshot.nodes.byRole("position") === null) {
|
|
2182
|
+
return null;
|
|
2183
|
+
}
|
|
2184
|
+
const tool = toolOf(snapshot.meta.creator);
|
|
2185
|
+
return {
|
|
2186
|
+
units: "px",
|
|
2187
|
+
// a z that is the same on every node collapses to 2D
|
|
2188
|
+
sourceDims: 3,
|
|
2189
|
+
yAxis: tool === "gephi" || tool === "tulip" ? "up" : "down",
|
|
2190
|
+
tool,
|
|
2191
|
+
medianNodeSize: graphicsMedian(snapshot)
|
|
2192
|
+
};
|
|
2193
|
+
}
|
|
2194
|
+
function graphicsMedian(snapshot) {
|
|
2195
|
+
const graphics = snapshot.nodes.get(GRAPHICS_COLUMN);
|
|
2196
|
+
const sizes = [];
|
|
2197
|
+
for (let row = 0; graphics !== null && row < graphics.length; row++) {
|
|
2198
|
+
const g = graphics.isSet(row) ? graphics.value(row) : void 0;
|
|
2199
|
+
const { w, h } = typeof g === "object" && g !== null ? g : {};
|
|
2200
|
+
if (typeof w === "number" && typeof h === "number") {
|
|
2201
|
+
sizes.push((w + h) / 2);
|
|
2202
|
+
}
|
|
2203
|
+
}
|
|
2204
|
+
return median(sizes);
|
|
2205
|
+
}
|
|
2206
|
+
const NO_GRAPH_CODE = NO_GRAPH_CODE$1;
|
|
2207
|
+
const SECOND_GRAPH_CODE = MULTIPLE_GRAPHS_CODE;
|
|
2208
|
+
const MISSING_ID_CODE = MISSING_ID_CODE$1;
|
|
2209
|
+
const MISSING_LABEL_CODE = "E_GML_MISSING_LABEL";
|
|
2210
|
+
const MISSING_ENDPOINT_CODE = MISSING_ENDPOINT_CODE$1;
|
|
2211
|
+
const ID_TYPE_CODE = "E_GML_ID_TYPE";
|
|
2212
|
+
const STRING_ID_CODE = "W_GML_STRING_ID";
|
|
2213
|
+
const DUPLICATE_NODE_CODE = DUPLICATE_NODE_CODE$1;
|
|
2214
|
+
const REPEATED_KEY_CODE = "E_GML_REPEATED_KEY";
|
|
2215
|
+
const ELEMENT_TYPE_CODE = "E_GML_ELEMENT_TYPE";
|
|
2216
|
+
const FLAG_TYPE_CODE = "E_GML_FLAG_TYPE";
|
|
2217
|
+
const FLAG_VALUE_CODE = "W_GML_FLAG_VALUE";
|
|
2218
|
+
const QUOTED_INT = /^\s*[+-]?\d+\s*$/;
|
|
2219
|
+
const UNKNOWN_ENTITY_CODE = "W_GML_UNKNOWN_ENTITY";
|
|
2220
|
+
const GRAPHICS_CODE = "W_GML_GRAPHICS";
|
|
2221
|
+
const NESTED_ELEMENT_CODE = "W_GML_NESTED_ELEMENT";
|
|
2222
|
+
const GROUPS_CODE = "W_GML_GROUPS";
|
|
2223
|
+
const WIDENED_CODE = WIDENED_CODE$1;
|
|
2224
|
+
const PRECISION_CODE = PRECISION_CODE$1;
|
|
2225
|
+
const COLUMN_RENAMED_CODE_GML = COLUMN_RENAMED_CODE;
|
|
2226
|
+
const ROLE_TAKEN_CODE = ROLE_TAKEN_CODE$1;
|
|
2227
|
+
const ID_DROPPED_CODE = "W_GML_ID_DROPPED";
|
|
2228
|
+
const FORMAT_DEFAULTS = { ids: "canonical", defaultDirected: false, weightFrom: "value" };
|
|
2229
|
+
const KIND_INT = 1;
|
|
2230
|
+
const KIND_REAL = 2;
|
|
2231
|
+
const KIND_STRING = 4;
|
|
2232
|
+
const KIND_RECORD = 8;
|
|
2233
|
+
const I32_MIN = -2147483648;
|
|
2234
|
+
const I32_MAX = 2147483647;
|
|
2235
|
+
const ABORT_CHECK_INTERVAL = 64;
|
|
2236
|
+
const USED_OPTIONS = /* @__PURE__ */ new Set([
|
|
2237
|
+
"ids",
|
|
2238
|
+
"nodeIdFrom",
|
|
2239
|
+
"addMissingNodes",
|
|
2240
|
+
"duplicateEdges",
|
|
2241
|
+
"selfLoops",
|
|
2242
|
+
"onMixedDirection",
|
|
2243
|
+
"defaultDirected",
|
|
2244
|
+
"weightFrom",
|
|
2245
|
+
"weightDtype",
|
|
2246
|
+
"restoreMangledIds",
|
|
2247
|
+
"long",
|
|
2248
|
+
"errorLimit",
|
|
2249
|
+
"signal",
|
|
2250
|
+
"onProgress"
|
|
2251
|
+
]);
|
|
2252
|
+
const GRAPH_SEQ = -1;
|
|
2253
|
+
const TOP_SEQ = -2;
|
|
2254
|
+
function isLabel(domain, key) {
|
|
2255
|
+
return roleOf(domain, key) === "label";
|
|
2256
|
+
}
|
|
2257
|
+
function isRecord(value) {
|
|
2258
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
2259
|
+
}
|
|
2260
|
+
function roleOf(domain, key) {
|
|
2261
|
+
if (key === "label" && domain !== "graph") {
|
|
2262
|
+
return "label";
|
|
2263
|
+
}
|
|
2264
|
+
if (domain === "edge") {
|
|
2265
|
+
if (key === "id") {
|
|
2266
|
+
return "id";
|
|
2267
|
+
}
|
|
2268
|
+
if (key === "key") {
|
|
2269
|
+
return "key";
|
|
2270
|
+
}
|
|
2271
|
+
}
|
|
2272
|
+
return null;
|
|
2273
|
+
}
|
|
2274
|
+
class GmlImport {
|
|
2275
|
+
/**
|
|
2276
|
+
* Create one import.
|
|
2277
|
+
* @param sink - the sink
|
|
2278
|
+
* @param report - the report
|
|
2279
|
+
* @param options - the resolved common options
|
|
2280
|
+
* @param format - the format-specific options
|
|
2281
|
+
* @param which - which `graph [ ... ]` block to read (0: the first)
|
|
2282
|
+
*/
|
|
2283
|
+
constructor(sink, report, options, format, which = 0) {
|
|
2284
|
+
this.which = which;
|
|
2285
|
+
this.tokens = null;
|
|
2286
|
+
this.graphCount = 0;
|
|
2287
|
+
this.resolver = null;
|
|
2288
|
+
this.nodeSchema = /* @__PURE__ */ new Map();
|
|
2289
|
+
this.edgeSchema = /* @__PURE__ */ new Map();
|
|
2290
|
+
this.graphSchema = /* @__PURE__ */ new Map();
|
|
2291
|
+
this.topSchema = /* @__PURE__ */ new Map();
|
|
2292
|
+
this.nodePlans = /* @__PURE__ */ new Map();
|
|
2293
|
+
this.edgePlans = /* @__PURE__ */ new Map();
|
|
2294
|
+
this.graphPlans = /* @__PURE__ */ new Map();
|
|
2295
|
+
this.topPlans = /* @__PURE__ */ new Map();
|
|
2296
|
+
this.graphNames = /* @__PURE__ */ new Set();
|
|
2297
|
+
this.graphOpen = -1;
|
|
2298
|
+
this.directedToken = -1;
|
|
2299
|
+
this.multigraphToken = -1;
|
|
2300
|
+
this.creatorToken = -1;
|
|
2301
|
+
this.versionToken = -1;
|
|
2302
|
+
this.nodeCount = 0;
|
|
2303
|
+
this.edgeCount = 0;
|
|
2304
|
+
this.weightKinds = 0;
|
|
2305
|
+
this.hasOriginalId = false;
|
|
2306
|
+
this.headerDirected = false;
|
|
2307
|
+
this.seq = 0;
|
|
2308
|
+
this.nodeOrdinal = 0;
|
|
2309
|
+
this.elementsSinceCheck = 0;
|
|
2310
|
+
this.idMap = null;
|
|
2311
|
+
this.skippedIds = /* @__PURE__ */ new Set();
|
|
2312
|
+
this.declared = new Uint32Array(64);
|
|
2313
|
+
this.touched = [];
|
|
2314
|
+
this.graphTouched = [];
|
|
2315
|
+
this.seqNow = 0;
|
|
2316
|
+
this.sink = sink;
|
|
2317
|
+
this.report = report;
|
|
2318
|
+
this.options = options;
|
|
2319
|
+
this.positions = format?.positions ?? true;
|
|
2320
|
+
this.keepPositionSource = format?.keepPositionSource ?? false;
|
|
2321
|
+
this.dictionaries = format?.dictionaries ?? true;
|
|
2322
|
+
this.coercer = new IdCoercer(options.ids);
|
|
2323
|
+
}
|
|
2324
|
+
/**
|
|
2325
|
+
* Run the import over the tokens of a text.
|
|
2326
|
+
* @param tokens - the whole text, tokenized (tokenize())
|
|
2327
|
+
*/
|
|
2328
|
+
run(tokens) {
|
|
2329
|
+
this.tokens = tokens;
|
|
2330
|
+
tokens.onUnknownEntity = (entity, line) => {
|
|
2331
|
+
this.report.warnOnce(
|
|
2332
|
+
"parse-error",
|
|
2333
|
+
UNKNOWN_ENTITY_CODE,
|
|
2334
|
+
`character entity ${entity} cannot be decoded; it is kept as written`,
|
|
2335
|
+
{ line, element: entity },
|
|
2336
|
+
`${UNKNOWN_ENTITY_CODE}:${entity}`
|
|
2337
|
+
);
|
|
2338
|
+
};
|
|
2339
|
+
this.scan();
|
|
2340
|
+
this.push();
|
|
2341
|
+
}
|
|
2342
|
+
// ============================================================ pass 1: the schema
|
|
2343
|
+
/**
|
|
2344
|
+
* Walk the top level: find the graph block, the metadata keys and the top-level attributes,
|
|
2345
|
+
* and derive every table's schema.
|
|
2346
|
+
*/
|
|
2347
|
+
scan() {
|
|
2348
|
+
const t = this.requireTokens();
|
|
2349
|
+
let scalarGraph = -1;
|
|
2350
|
+
for (let p = 0; p < t.count; p = t.nextPair(p)) {
|
|
2351
|
+
const key = t.textOf(p);
|
|
2352
|
+
const v = p + 1;
|
|
2353
|
+
const record = t.kind[v] === TOKEN_OPEN;
|
|
2354
|
+
if (key === "graph" && !record && scalarGraph < 0) {
|
|
2355
|
+
scalarGraph = v;
|
|
2356
|
+
}
|
|
2357
|
+
this.scanTopPair(key, record, v);
|
|
2358
|
+
}
|
|
2359
|
+
if (this.graphOpen < 0) {
|
|
2360
|
+
this.failNoGraph(t, scalarGraph);
|
|
2361
|
+
}
|
|
2362
|
+
}
|
|
2363
|
+
/**
|
|
2364
|
+
* Handle one top-level pair other than a scalar `graph`.
|
|
2365
|
+
* @param key - the key
|
|
2366
|
+
* @param record - whether the value is a `[ ... ]` block
|
|
2367
|
+
* @param v - the value token
|
|
2368
|
+
*/
|
|
2369
|
+
scanTopPair(key, record, v) {
|
|
2370
|
+
if (key === "graph" && record) {
|
|
2371
|
+
if (this.graphCount++ === this.which) {
|
|
2372
|
+
this.graphOpen = v;
|
|
2373
|
+
this.scanGraph(v);
|
|
2374
|
+
}
|
|
2375
|
+
} else if (key === "Creator" && !record && this.creatorToken < 0) {
|
|
2376
|
+
this.creatorToken = v;
|
|
2377
|
+
} else if (key === "Version" && !record && this.versionToken < 0) {
|
|
2378
|
+
this.versionToken = v;
|
|
2379
|
+
} else {
|
|
2380
|
+
this.observe(this.topSchema, "graph", key, v, TOP_SEQ);
|
|
2381
|
+
}
|
|
2382
|
+
}
|
|
2383
|
+
/**
|
|
2384
|
+
* Fail because the input holds no graph block.
|
|
2385
|
+
* @param t - the tokens
|
|
2386
|
+
* @param scalarGraph - the token of a scalar `graph` key, or -1
|
|
2387
|
+
*/
|
|
2388
|
+
failNoGraph(t, scalarGraph) {
|
|
2389
|
+
if (scalarGraph >= 0) {
|
|
2390
|
+
this.report.fail(
|
|
2391
|
+
NO_GRAPH_CODE,
|
|
2392
|
+
`the graph key at line ${t.line[scalarGraph]} holds ${describeValue(t, scalarGraph)}, not a [ ... ] block; the input contains no graph block`,
|
|
2393
|
+
{ line: t.line[scalarGraph] }
|
|
2394
|
+
);
|
|
2395
|
+
}
|
|
2396
|
+
this.report.fail(NO_GRAPH_CODE, "the input contains no graph [ ... ] block", { line: null });
|
|
2397
|
+
}
|
|
2398
|
+
/**
|
|
2399
|
+
* Walk the graph block for the schema.
|
|
2400
|
+
* @param open - the index of the block's `[`
|
|
2401
|
+
*/
|
|
2402
|
+
scanGraph(open) {
|
|
2403
|
+
const t = this.requireTokens();
|
|
2404
|
+
const close = t.match[open];
|
|
2405
|
+
for (let p = open + 1; p < close; p = t.nextPair(p)) {
|
|
2406
|
+
const key = t.textOf(p);
|
|
2407
|
+
const v = p + 1;
|
|
2408
|
+
switch (key) {
|
|
2409
|
+
case "node":
|
|
2410
|
+
if (t.kind[v] === TOKEN_OPEN) {
|
|
2411
|
+
this.nodeCount++;
|
|
2412
|
+
this.scanElement("node", v);
|
|
2413
|
+
}
|
|
2414
|
+
break;
|
|
2415
|
+
case "edge":
|
|
2416
|
+
if (t.kind[v] === TOKEN_OPEN) {
|
|
2417
|
+
this.edgeCount++;
|
|
2418
|
+
this.scanElement("edge", v);
|
|
2419
|
+
}
|
|
2420
|
+
break;
|
|
2421
|
+
case "directed":
|
|
2422
|
+
if (this.directedToken < 0) {
|
|
2423
|
+
this.directedToken = v;
|
|
2424
|
+
}
|
|
2425
|
+
break;
|
|
2426
|
+
case "multigraph":
|
|
2427
|
+
if (this.multigraphToken < 0) {
|
|
2428
|
+
this.multigraphToken = v;
|
|
2429
|
+
}
|
|
2430
|
+
break;
|
|
2431
|
+
default:
|
|
2432
|
+
this.observe(this.graphSchema, "graph", key, v, GRAPH_SEQ);
|
|
2433
|
+
}
|
|
2434
|
+
}
|
|
2435
|
+
}
|
|
2436
|
+
/**
|
|
2437
|
+
* Walk one node or edge block for the schema.
|
|
2438
|
+
* @param domain - node or edge
|
|
2439
|
+
* @param open - the block's `[`
|
|
2440
|
+
*/
|
|
2441
|
+
scanElement(domain, open) {
|
|
2442
|
+
const t = this.requireTokens();
|
|
2443
|
+
const close = t.match[open];
|
|
2444
|
+
const seq = this.seq++;
|
|
2445
|
+
const schema = domain === "node" ? this.nodeSchema : this.edgeSchema;
|
|
2446
|
+
for (let p = open + 1; p < close; p = t.nextPair(p)) {
|
|
2447
|
+
const key = t.textOf(p);
|
|
2448
|
+
const v = p + 1;
|
|
2449
|
+
if (this.skipsKey(domain, key, v)) {
|
|
2450
|
+
continue;
|
|
2451
|
+
}
|
|
2452
|
+
this.observe(schema, domain, key, v, seq);
|
|
2453
|
+
}
|
|
2454
|
+
}
|
|
2455
|
+
/**
|
|
2456
|
+
* Whether a key of a node or edge block is structural (id, endpoints, weight, original id), not
|
|
2457
|
+
* a column; notes the original id and the weight's kinds on the way.
|
|
2458
|
+
* @param domain - node or edge
|
|
2459
|
+
* @param key - the key
|
|
2460
|
+
* @param v - the value token
|
|
2461
|
+
* @returns true when the key is not a column
|
|
2462
|
+
*/
|
|
2463
|
+
skipsKey(domain, key, v) {
|
|
2464
|
+
const { weightFrom, restoreMangledIds } = this.options;
|
|
2465
|
+
if (domain === "node") {
|
|
2466
|
+
if (key === ORIGINAL_ID_KEY && restoreMangledIds) {
|
|
2467
|
+
this.hasOriginalId = true;
|
|
2468
|
+
return true;
|
|
2469
|
+
}
|
|
2470
|
+
return key === "id";
|
|
2471
|
+
}
|
|
2472
|
+
if (key === "source" || key === "target" || key === "directed") {
|
|
2473
|
+
return true;
|
|
2474
|
+
}
|
|
2475
|
+
if (key === weightFrom) {
|
|
2476
|
+
this.weightKinds |= kindBit(this.requireTokens(), v);
|
|
2477
|
+
return true;
|
|
2478
|
+
}
|
|
2479
|
+
return false;
|
|
2480
|
+
}
|
|
2481
|
+
/**
|
|
2482
|
+
* Record one value of one key in a table's schema.
|
|
2483
|
+
* @param schema - the table's schema
|
|
2484
|
+
* @param domain - the table
|
|
2485
|
+
* @param key - the key
|
|
2486
|
+
* @param v - the value token
|
|
2487
|
+
* @param seq - the element sequence number
|
|
2488
|
+
*/
|
|
2489
|
+
observe(schema, domain, key, v, seq) {
|
|
2490
|
+
const t = this.requireTokens();
|
|
2491
|
+
this.noteStructure(domain, key, v);
|
|
2492
|
+
let entry = schema.get(key);
|
|
2493
|
+
const graphics = domain === "node" && key === "graphics" && this.positions;
|
|
2494
|
+
if (entry === void 0) {
|
|
2495
|
+
entry = {
|
|
2496
|
+
key,
|
|
2497
|
+
kinds: 0,
|
|
2498
|
+
list: false,
|
|
2499
|
+
lastSeq: seq,
|
|
2500
|
+
wide: false,
|
|
2501
|
+
unsafe: false,
|
|
2502
|
+
dict: this.dictionaries && roleOf(domain, key) === null ? new DictHeuristic() : null,
|
|
2503
|
+
position: false,
|
|
2504
|
+
rest: false
|
|
2505
|
+
};
|
|
2506
|
+
schema.set(key, entry);
|
|
2507
|
+
} else if (entry.lastSeq === seq) {
|
|
2508
|
+
entry.list = true;
|
|
2509
|
+
entry.rest ||= graphics;
|
|
2510
|
+
} else {
|
|
2511
|
+
entry.lastSeq = seq;
|
|
2512
|
+
}
|
|
2513
|
+
if (graphics && t.kind[v] !== TOKEN_OPEN) {
|
|
2514
|
+
entry.rest = true;
|
|
2515
|
+
}
|
|
2516
|
+
switch (t.kind[v]) {
|
|
2517
|
+
case TOKEN_STRING: {
|
|
2518
|
+
const raw = t.textOf(v);
|
|
2519
|
+
if (raw === LIST_START_MARKER || !isLabel(domain, key) && (raw === EMPTY_LIST_TEXT || raw === EMPTY_TUPLE_TEXT)) {
|
|
2520
|
+
entry.list = true;
|
|
2521
|
+
return;
|
|
2522
|
+
}
|
|
2523
|
+
entry.kinds |= KIND_STRING;
|
|
2524
|
+
if (entry.dict !== null && !entry.dict.decided) {
|
|
2525
|
+
entry.dict.observe(raw);
|
|
2526
|
+
}
|
|
2527
|
+
return;
|
|
2528
|
+
}
|
|
2529
|
+
case TOKEN_INT: {
|
|
2530
|
+
entry.kinds |= KIND_INT;
|
|
2531
|
+
const n = Number(t.textOf(v));
|
|
2532
|
+
if (n < I32_MIN || n > I32_MAX) {
|
|
2533
|
+
entry.wide = true;
|
|
2534
|
+
if (!Number.isSafeInteger(n)) {
|
|
2535
|
+
entry.unsafe = true;
|
|
2536
|
+
}
|
|
2537
|
+
}
|
|
2538
|
+
return;
|
|
2539
|
+
}
|
|
2540
|
+
case TOKEN_OPEN:
|
|
2541
|
+
entry.kinds |= KIND_RECORD;
|
|
2542
|
+
if (domain === "node" && key === "graphics" && this.positions) {
|
|
2543
|
+
this.inspectGraphics(entry, v);
|
|
2544
|
+
}
|
|
2545
|
+
return;
|
|
2546
|
+
default: {
|
|
2547
|
+
entry.kinds |= KIND_REAL;
|
|
2548
|
+
const text = t.textOf(v);
|
|
2549
|
+
if (/\d/.test(text) && !Number.isFinite(Number(text))) {
|
|
2550
|
+
this.report.warnOnce(
|
|
2551
|
+
"precision",
|
|
2552
|
+
PRECISION_CODE,
|
|
2553
|
+
`${domain} key "${key}" holds the real ${text}, beyond the f64 range; it is stored as ${numberOfText(text) > 0 ? "" : "-"}Infinity`,
|
|
2554
|
+
{ line: t.line[v], element: key },
|
|
2555
|
+
`${PRECISION_CODE}:overflow:${domain}:${key}`
|
|
2556
|
+
);
|
|
2557
|
+
}
|
|
2558
|
+
}
|
|
2559
|
+
}
|
|
2560
|
+
}
|
|
2561
|
+
/**
|
|
2562
|
+
* Warn about keys of a node or edge whose meaning GML readers give but this import does not:
|
|
2563
|
+
* a nested graph / node / edge record, and yEd's group hierarchy.
|
|
2564
|
+
* @param domain - the table
|
|
2565
|
+
* @param key - the key
|
|
2566
|
+
* @param v - the value token
|
|
2567
|
+
*/
|
|
2568
|
+
noteStructure(domain, key, v) {
|
|
2569
|
+
const t = this.requireTokens();
|
|
2570
|
+
if (domain === "graph") {
|
|
2571
|
+
return;
|
|
2572
|
+
}
|
|
2573
|
+
if ((key === "graph" || key === "node" || key === "edge") && t.kind[v] === TOKEN_OPEN) {
|
|
2574
|
+
this.report.warnOnce(
|
|
2575
|
+
"unsupported",
|
|
2576
|
+
NESTED_ELEMENT_CODE,
|
|
2577
|
+
`a nested ${key} record inside a ${domain} is kept as the json column "${key}"; it is not read as structure`,
|
|
2578
|
+
{ line: t.line[v], element: key },
|
|
2579
|
+
`${NESTED_ELEMENT_CODE}:${domain}:${key}`
|
|
2580
|
+
);
|
|
2581
|
+
} else if (domain === "node" && (key === "isGroup" || key === "gid")) {
|
|
2582
|
+
this.report.warnOnce(
|
|
2583
|
+
"unsupported",
|
|
2584
|
+
GROUPS_CODE,
|
|
2585
|
+
"yEd group keys (isGroup, gid) are kept as plain node columns; the group hierarchy is not read as containment",
|
|
2586
|
+
{ line: t.line[v], element: key }
|
|
2587
|
+
);
|
|
2588
|
+
}
|
|
2589
|
+
}
|
|
2590
|
+
/**
|
|
2591
|
+
* Note whether a node's graphics record carries a position (numeric x and y) and whether
|
|
2592
|
+
* anything besides the position remains.
|
|
2593
|
+
* @param entry - the graphics schema entry
|
|
2594
|
+
* @param open - the record's `[`
|
|
2595
|
+
*/
|
|
2596
|
+
inspectGraphics(entry, open) {
|
|
2597
|
+
const t = this.requireTokens();
|
|
2598
|
+
const close = t.match[open];
|
|
2599
|
+
let x = false;
|
|
2600
|
+
let y = false;
|
|
2601
|
+
let other = false;
|
|
2602
|
+
for (let p = open + 1; p < close; p = t.nextPair(p)) {
|
|
2603
|
+
const key = t.textOf(p);
|
|
2604
|
+
const numeric = t.kind[p + 1] === TOKEN_INT || t.kind[p + 1] === TOKEN_REAL;
|
|
2605
|
+
if (key === "x" && numeric && !x) {
|
|
2606
|
+
x = true;
|
|
2607
|
+
} else if (key === "y" && numeric && !y) {
|
|
2608
|
+
y = true;
|
|
2609
|
+
} else if (!(key === "z" && numeric)) {
|
|
2610
|
+
other = true;
|
|
2611
|
+
}
|
|
2612
|
+
}
|
|
2613
|
+
const positioned = x && y;
|
|
2614
|
+
entry.position ||= positioned;
|
|
2615
|
+
entry.rest ||= positioned ? other || this.keepPositionSource : true;
|
|
2616
|
+
}
|
|
2617
|
+
// ============================================================ pass 2: the push
|
|
2618
|
+
/** Declare what the schema needs, set the header and push every element. */
|
|
2619
|
+
push() {
|
|
2620
|
+
const { sink, report, options } = this;
|
|
2621
|
+
const t = this.requireTokens();
|
|
2622
|
+
sink.reserve(this.nodeCount, this.edgeCount);
|
|
2623
|
+
this.buildPlans();
|
|
2624
|
+
this.setMetadata();
|
|
2625
|
+
this.headerDirected = this.readFlag(this.directedToken, "directed") ?? options.defaultDirected;
|
|
2626
|
+
const resolver = new DirectionResolver(sink, report, options.onMixedDirection);
|
|
2627
|
+
resolver.setHeader(this.headerDirected, {
|
|
2628
|
+
line: this.directedToken >= 0 ? t.line[this.directedToken] : t.line[this.graphOpen]
|
|
2629
|
+
});
|
|
2630
|
+
this.resolver = resolver;
|
|
2631
|
+
if (options.nodeIdFrom !== "id" || this.hasOriginalId) {
|
|
2632
|
+
this.idMap = /* @__PURE__ */ new Map();
|
|
2633
|
+
}
|
|
2634
|
+
if (options.nodeIdFrom !== "id") {
|
|
2635
|
+
report.loss(
|
|
2636
|
+
ID_DROPPED_CODE,
|
|
2637
|
+
`node ids taken from the ${options.nodeIdFrom}; the integer id keys are not kept as a column`,
|
|
2638
|
+
null,
|
|
2639
|
+
this.nodeCount
|
|
2640
|
+
);
|
|
2641
|
+
}
|
|
2642
|
+
this.pushTopLevel();
|
|
2643
|
+
this.pushNodes();
|
|
2644
|
+
this.pushEdges();
|
|
2645
|
+
}
|
|
2646
|
+
/** Turn every schema entry into a column plan. */
|
|
2647
|
+
buildPlans() {
|
|
2648
|
+
for (const [key, entry] of this.nodeSchema) {
|
|
2649
|
+
this.nodePlans.set(key, this.planOf("node", entry));
|
|
2650
|
+
}
|
|
2651
|
+
for (const [key, entry] of this.edgeSchema) {
|
|
2652
|
+
this.edgePlans.set(key, this.planOf("edge", entry));
|
|
2653
|
+
}
|
|
2654
|
+
for (const [key, entry] of this.graphSchema) {
|
|
2655
|
+
this.graphPlans.set(key, this.planOf("graph", entry));
|
|
2656
|
+
}
|
|
2657
|
+
for (const [key, entry] of this.topSchema) {
|
|
2658
|
+
this.topPlans.set(key, this.planOf("graph", entry, true));
|
|
2659
|
+
}
|
|
2660
|
+
}
|
|
2661
|
+
/**
|
|
2662
|
+
* Warn about a key that mixes numbers and strings, or holds integers beyond 2^53.
|
|
2663
|
+
* @param domain - the table
|
|
2664
|
+
* @param entry - the schema entry
|
|
2665
|
+
* @param asText - whether the integers are kept as text
|
|
2666
|
+
*/
|
|
2667
|
+
warnKinds(domain, entry, asText) {
|
|
2668
|
+
if ((entry.kinds & KIND_STRING) !== 0 && (entry.kinds & (KIND_INT | KIND_REAL)) !== 0 && (entry.kinds & KIND_RECORD) === 0) {
|
|
2669
|
+
this.report.warnOnce(
|
|
2670
|
+
"coercion",
|
|
2671
|
+
WIDENED_CODE,
|
|
2672
|
+
`${domain} key "${entry.key}" mixes numbers and strings; the column is string and the numbers are kept as written`,
|
|
2673
|
+
{ element: entry.key },
|
|
2674
|
+
`${WIDENED_CODE}:${domain}:${entry.key}`
|
|
2675
|
+
);
|
|
2676
|
+
}
|
|
2677
|
+
if (entry.unsafe && !asText) {
|
|
2678
|
+
this.report.warnOnce(
|
|
2679
|
+
"precision",
|
|
2680
|
+
PRECISION_CODE,
|
|
2681
|
+
`${domain} key "${entry.key}" holds integers beyond 2^53; they are stored as the nearest f64 (pass long: "string" to keep every digit as text)`,
|
|
2682
|
+
{ element: entry.key },
|
|
2683
|
+
`${PRECISION_CODE}:${domain}:${entry.key}`
|
|
2684
|
+
);
|
|
2685
|
+
}
|
|
2686
|
+
}
|
|
2687
|
+
/**
|
|
2688
|
+
* The plan of a node `graphics` record: the plan plus its position and rest columns.
|
|
2689
|
+
* @param plan - the record's plan
|
|
2690
|
+
* @param entry - the schema entry
|
|
2691
|
+
* @returns the plan with its position (and rest) plans
|
|
2692
|
+
*/
|
|
2693
|
+
graphicsPlan(plan, entry) {
|
|
2694
|
+
const taken = (candidate) => this.nodeSchema.has(candidate) || this.sink.nodeColumn(candidate) !== INVALID_INDEX;
|
|
2695
|
+
const position = {
|
|
2696
|
+
...plan,
|
|
2697
|
+
name: uniqueColumnName("position", "graphics", taken),
|
|
2698
|
+
dtype: "f64",
|
|
2699
|
+
itemDtype: null,
|
|
2700
|
+
kind: "real",
|
|
2701
|
+
list: false,
|
|
2702
|
+
role: "position"
|
|
2703
|
+
};
|
|
2704
|
+
const rest = entry.rest ? { ...plan, dtype: "json", itemDtype: null, kind: "record", list: false } : null;
|
|
2705
|
+
return { ...plan, position, rest };
|
|
2706
|
+
}
|
|
2707
|
+
/**
|
|
2708
|
+
* The plan of one schema entry.
|
|
2709
|
+
* @param domain - the table
|
|
2710
|
+
* @param entry - the schema entry
|
|
2711
|
+
* @param top - whether the key is a top-level one (graph table)
|
|
2712
|
+
* @returns the plan
|
|
2713
|
+
*/
|
|
2714
|
+
planOf(domain, entry, top = false) {
|
|
2715
|
+
const asText = entry.unsafe && this.options.long === "string" && kindOf(entry) === "int";
|
|
2716
|
+
const kind = asText ? "string" : kindOf(entry);
|
|
2717
|
+
let scalar = dtypeOfKind(kind, entry);
|
|
2718
|
+
if (scalar === "string" && !entry.list && entry.dict !== null && entry.kinds === KIND_STRING && entry.dict.decide() === "dict") {
|
|
2719
|
+
scalar = "dict";
|
|
2720
|
+
}
|
|
2721
|
+
this.warnKinds(domain, entry, asText);
|
|
2722
|
+
const name = domain === "graph" ? this.uniqueGraphName(entry.key) : entry.key;
|
|
2723
|
+
let itemDtype = null;
|
|
2724
|
+
if (entry.list) {
|
|
2725
|
+
itemDtype = scalar === "dict" ? "string" : scalar;
|
|
2726
|
+
}
|
|
2727
|
+
const plan = {
|
|
2728
|
+
key: entry.key,
|
|
2729
|
+
name,
|
|
2730
|
+
dtype: entry.list ? "list" : scalar,
|
|
2731
|
+
itemDtype,
|
|
2732
|
+
kind,
|
|
2733
|
+
list: entry.list,
|
|
2734
|
+
role: roleOf(domain, entry.key),
|
|
2735
|
+
top,
|
|
2736
|
+
position: null,
|
|
2737
|
+
rest: null,
|
|
2738
|
+
handle: INVALID_INDEX,
|
|
2739
|
+
declared: false,
|
|
2740
|
+
items: null,
|
|
2741
|
+
seq: Number.NEGATIVE_INFINITY
|
|
2742
|
+
};
|
|
2743
|
+
if (domain === "node" && entry.key === "graphics" && this.positions && (entry.kinds & KIND_RECORD) !== 0) {
|
|
2744
|
+
return this.graphicsPlan(plan, entry);
|
|
2745
|
+
}
|
|
2746
|
+
return plan;
|
|
2747
|
+
}
|
|
2748
|
+
/**
|
|
2749
|
+
* A graph column name not yet used by this import (top-level and in-graph keys share the table).
|
|
2750
|
+
* @param key - the key
|
|
2751
|
+
* @returns the name
|
|
2752
|
+
*/
|
|
2753
|
+
uniqueGraphName(key) {
|
|
2754
|
+
const name = uniqueColumnName(key, key, (candidate) => this.graphNames.has(candidate));
|
|
2755
|
+
this.graphNames.add(name);
|
|
2756
|
+
if (name !== key) {
|
|
2757
|
+
this.report.warning(
|
|
2758
|
+
"coercion",
|
|
2759
|
+
COLUMN_RENAMED_CODE_GML,
|
|
2760
|
+
`the top-level key "${key}" and a key of the graph block share the graph column name; the top-level value is stored as "${name}"`,
|
|
2761
|
+
{ element: key }
|
|
2762
|
+
);
|
|
2763
|
+
}
|
|
2764
|
+
return name;
|
|
2765
|
+
}
|
|
2766
|
+
/** Set the graph metadata: source format, creator, version, multigraph flag, weight origin. */
|
|
2767
|
+
setMetadata() {
|
|
2768
|
+
const t = this.requireTokens();
|
|
2769
|
+
const { sink, options } = this;
|
|
2770
|
+
const textOf = (v) => t.kind[v] === TOKEN_STRING ? t.stringOf(v) : t.textOf(v);
|
|
2771
|
+
const multigraph = this.readFlag(this.multigraphToken, "multigraph");
|
|
2772
|
+
sink.setMeta({
|
|
2773
|
+
sourceFormat: "gml",
|
|
2774
|
+
creator: this.creatorToken >= 0 ? textOf(this.creatorToken) : void 0,
|
|
2775
|
+
sourceVersion: this.versionToken >= 0 ? textOf(this.versionToken) : void 0,
|
|
2776
|
+
declaredMultigraph: multigraph ?? void 0,
|
|
2777
|
+
weightOrigin: this.weightKinds !== 0 && options.weightFrom !== null ? {
|
|
2778
|
+
format: "gml",
|
|
2779
|
+
id: options.weightFrom,
|
|
2780
|
+
title: null,
|
|
2781
|
+
type: this.weightKinds === KIND_INT ? "int" : "real",
|
|
2782
|
+
namespace: null
|
|
2783
|
+
} : void 0,
|
|
2784
|
+
// The header as written, so a reader can say which words set the direction and tell
|
|
2785
|
+
// `directed 0` from a file that relies on the specification's default.
|
|
2786
|
+
extra: this.directedToken >= 0 ? { gml: { directed: textOf(this.directedToken) } } : void 0
|
|
2787
|
+
});
|
|
2788
|
+
}
|
|
2789
|
+
/**
|
|
2790
|
+
* Read a `directed` / `multigraph` flag token.
|
|
2791
|
+
* @param v - the value token, or -1 when absent
|
|
2792
|
+
* @param name - the flag name, for issues
|
|
2793
|
+
* @returns the flag, or null when absent or not an integer
|
|
2794
|
+
*/
|
|
2795
|
+
readFlag(v, name) {
|
|
2796
|
+
if (v < 0) {
|
|
2797
|
+
return null;
|
|
2798
|
+
}
|
|
2799
|
+
const t = this.requireTokens();
|
|
2800
|
+
const quoted = t.kind[v] === TOKEN_STRING && QUOTED_INT.test(t.stringOf(v));
|
|
2801
|
+
if (quoted) {
|
|
2802
|
+
this.report.warning(
|
|
2803
|
+
"validation-error",
|
|
2804
|
+
FLAG_VALUE_CODE,
|
|
2805
|
+
`flag "${name}" is written as the string ${describeValue(t, v)}; read as an integer`,
|
|
2806
|
+
{ line: t.line[v], element: name }
|
|
2807
|
+
);
|
|
2808
|
+
} else if (t.kind[v] !== TOKEN_INT) {
|
|
2809
|
+
this.report.error(
|
|
2810
|
+
"validation-error",
|
|
2811
|
+
FLAG_TYPE_CODE,
|
|
2812
|
+
`flag "${name}" must be the integer 0 or 1, found ${describeValue(t, v)}`,
|
|
2813
|
+
{ line: t.line[v], element: name }
|
|
2814
|
+
);
|
|
2815
|
+
return null;
|
|
2816
|
+
}
|
|
2817
|
+
const n = Number(quoted ? t.stringOf(v) : t.textOf(v));
|
|
2818
|
+
if (n !== 0 && n !== 1) {
|
|
2819
|
+
this.report.warning(
|
|
2820
|
+
"validation-error",
|
|
2821
|
+
FLAG_VALUE_CODE,
|
|
2822
|
+
`flag "${name}" is ${n}; read as ${n !== 0 ? "1" : "0"}`,
|
|
2823
|
+
{ line: t.line[v], element: name }
|
|
2824
|
+
);
|
|
2825
|
+
}
|
|
2826
|
+
return n !== 0;
|
|
2827
|
+
}
|
|
2828
|
+
/** Push the top-level attributes (every top-level key but graph, Creator and Version). */
|
|
2829
|
+
pushTopLevel() {
|
|
2830
|
+
const t = this.requireTokens();
|
|
2831
|
+
for (let p = 0; p < t.count; p = t.nextPair(p)) {
|
|
2832
|
+
const v = p + 1;
|
|
2833
|
+
if (v === this.graphOpen || v === this.creatorToken || v === this.versionToken) {
|
|
2834
|
+
continue;
|
|
2835
|
+
}
|
|
2836
|
+
const plan = this.topPlans.get(t.textOf(p));
|
|
2837
|
+
if (plan !== void 0) {
|
|
2838
|
+
this.writeGraph(plan, v, TOP_SEQ);
|
|
2839
|
+
}
|
|
2840
|
+
}
|
|
2841
|
+
this.flushGraphLists();
|
|
2842
|
+
}
|
|
2843
|
+
/** Push every node block and the graph block's own attributes, in file order. */
|
|
2844
|
+
pushNodes() {
|
|
2845
|
+
const t = this.requireTokens();
|
|
2846
|
+
const close = t.match[this.graphOpen];
|
|
2847
|
+
for (let p = this.graphOpen + 1; p < close; p = t.nextPair(p)) {
|
|
2848
|
+
const key = t.textOf(p);
|
|
2849
|
+
const v = p + 1;
|
|
2850
|
+
switch (key) {
|
|
2851
|
+
case "node":
|
|
2852
|
+
if (t.kind[v] === TOKEN_OPEN) {
|
|
2853
|
+
this.importNode(v);
|
|
2854
|
+
} else {
|
|
2855
|
+
this.badElement("node", p);
|
|
2856
|
+
this.report.counts.skippedNodes++;
|
|
2857
|
+
}
|
|
2858
|
+
break;
|
|
2859
|
+
case "edge":
|
|
2860
|
+
break;
|
|
2861
|
+
case "directed":
|
|
2862
|
+
case "multigraph":
|
|
2863
|
+
if (v !== this.directedToken && v !== this.multigraphToken) {
|
|
2864
|
+
this.report.warning(
|
|
2865
|
+
"validation-error",
|
|
2866
|
+
FLAG_VALUE_CODE,
|
|
2867
|
+
`graph flag "${key}" repeated; the first value stands`,
|
|
2868
|
+
{ line: t.line[p], element: key }
|
|
2869
|
+
);
|
|
2870
|
+
}
|
|
2871
|
+
break;
|
|
2872
|
+
default: {
|
|
2873
|
+
const plan = this.graphPlans.get(key);
|
|
2874
|
+
if (plan !== void 0) {
|
|
2875
|
+
this.writeGraph(plan, v, GRAPH_SEQ);
|
|
2876
|
+
}
|
|
2877
|
+
}
|
|
2878
|
+
}
|
|
2879
|
+
}
|
|
2880
|
+
this.flushGraphLists();
|
|
2881
|
+
}
|
|
2882
|
+
/** Push every edge block, in file order. */
|
|
2883
|
+
pushEdges() {
|
|
2884
|
+
const t = this.requireTokens();
|
|
2885
|
+
const close = t.match[this.graphOpen];
|
|
2886
|
+
let ordinal = 0;
|
|
2887
|
+
for (let p = this.graphOpen + 1; p < close; p = t.nextPair(p)) {
|
|
2888
|
+
if (t.textOf(p) !== "edge") {
|
|
2889
|
+
continue;
|
|
2890
|
+
}
|
|
2891
|
+
const v = p + 1;
|
|
2892
|
+
if (t.kind[v] === TOKEN_OPEN) {
|
|
2893
|
+
this.importEdge(v, ordinal);
|
|
2894
|
+
} else {
|
|
2895
|
+
this.badElement("edge", p);
|
|
2896
|
+
this.report.counts.skippedEdges++;
|
|
2897
|
+
}
|
|
2898
|
+
ordinal++;
|
|
2899
|
+
}
|
|
2900
|
+
}
|
|
2901
|
+
/**
|
|
2902
|
+
* Record a `node` / `edge` key whose value is not a block.
|
|
2903
|
+
* @param what - node or edge
|
|
2904
|
+
* @param p - the key token
|
|
2905
|
+
*/
|
|
2906
|
+
badElement(what, p) {
|
|
2907
|
+
const t = this.requireTokens();
|
|
2908
|
+
this.report.error(
|
|
2909
|
+
"validation-error",
|
|
2910
|
+
ELEMENT_TYPE_CODE,
|
|
2911
|
+
`${what} must be a [ ... ] block, found ${describeValue(t, p + 1)}`,
|
|
2912
|
+
{ line: t.line[p], element: what }
|
|
2913
|
+
);
|
|
2914
|
+
}
|
|
2915
|
+
/**
|
|
2916
|
+
* Push one node block.
|
|
2917
|
+
* @param open - the block's `[`
|
|
2918
|
+
*/
|
|
2919
|
+
importNode(open) {
|
|
2920
|
+
const t = this.requireTokens();
|
|
2921
|
+
const { sink, report, options } = this;
|
|
2922
|
+
const close = t.match[open];
|
|
2923
|
+
const line = t.line[open];
|
|
2924
|
+
const ordinal = this.nodeOrdinal++;
|
|
2925
|
+
this.checkAbort();
|
|
2926
|
+
let index;
|
|
2927
|
+
let sinkId;
|
|
2928
|
+
let fileId;
|
|
2929
|
+
try {
|
|
2930
|
+
let idTok = -1;
|
|
2931
|
+
let labelTok = -1;
|
|
2932
|
+
let originalTok = -1;
|
|
2933
|
+
for (let p = open + 1; p < close; p = t.nextPair(p)) {
|
|
2934
|
+
const key = t.textOf(p);
|
|
2935
|
+
if (key === "id") {
|
|
2936
|
+
idTok = this.structuralToken(idTok, p, "id");
|
|
2937
|
+
} else if (key === "label") {
|
|
2938
|
+
labelTok = labelTok < 0 ? p + 1 : labelTok;
|
|
2939
|
+
} else if (key === ORIGINAL_ID_KEY && options.restoreMangledIds) {
|
|
2940
|
+
originalTok = originalTok < 0 ? p + 1 : originalTok;
|
|
2941
|
+
}
|
|
2942
|
+
}
|
|
2943
|
+
if (idTok < 0) {
|
|
2944
|
+
throw new GraphFormatError("E_INVALID_ID", "node has no id", { reason: "missing id" });
|
|
2945
|
+
}
|
|
2946
|
+
fileId = this.coerceFileId(idTok, this.fileIdOf(idTok, "id"));
|
|
2947
|
+
if (originalTok >= 0) {
|
|
2948
|
+
sinkId = this.restoredId(originalTok);
|
|
2949
|
+
} else {
|
|
2950
|
+
switch (options.nodeIdFrom) {
|
|
2951
|
+
case "label":
|
|
2952
|
+
if (labelTok < 0) {
|
|
2953
|
+
throw new GraphFormatError("E_INVALID_ID", "node has no label", {
|
|
2954
|
+
reason: "missing label"
|
|
2955
|
+
});
|
|
2956
|
+
}
|
|
2957
|
+
sinkId = this.coerceValueToken(labelTok);
|
|
2958
|
+
break;
|
|
2959
|
+
case "index":
|
|
2960
|
+
sinkId = ordinal;
|
|
2961
|
+
break;
|
|
2962
|
+
default:
|
|
2963
|
+
sinkId = fileId;
|
|
2964
|
+
}
|
|
2965
|
+
}
|
|
2966
|
+
index = sink.addNode(sinkId);
|
|
2967
|
+
if (this.idMap !== null) {
|
|
2968
|
+
const before = this.idMap.get(fileId);
|
|
2969
|
+
if (before !== void 0 && before !== sinkId) {
|
|
2970
|
+
report.warning(
|
|
2971
|
+
"merged",
|
|
2972
|
+
DUPLICATE_NODE_CODE,
|
|
2973
|
+
`file id ${JSON.stringify(fileId)} is declared twice (nodes ${JSON.stringify(before)} and ${JSON.stringify(sinkId)}); edges to it resolve to the later`,
|
|
2974
|
+
{ line, element: String(fileId) }
|
|
2975
|
+
);
|
|
2976
|
+
}
|
|
2977
|
+
this.idMap.set(fileId, sinkId);
|
|
2978
|
+
}
|
|
2979
|
+
} catch (err) {
|
|
2980
|
+
this.recordNodeError(err, line);
|
|
2981
|
+
report.counts.skippedNodes++;
|
|
2982
|
+
if (fileId !== void 0) {
|
|
2983
|
+
this.skippedIds.add(fileId);
|
|
2984
|
+
}
|
|
2985
|
+
return;
|
|
2986
|
+
}
|
|
2987
|
+
if (this.isDeclared(index)) {
|
|
2988
|
+
report.warning(
|
|
2989
|
+
"merged",
|
|
2990
|
+
DUPLICATE_NODE_CODE,
|
|
2991
|
+
`node ${JSON.stringify(sinkId)} is declared twice; later keys overwrite`,
|
|
2992
|
+
{ line, element: String(sinkId) }
|
|
2993
|
+
);
|
|
2994
|
+
} else {
|
|
2995
|
+
this.markDeclared(index);
|
|
2996
|
+
report.counts.nodes++;
|
|
2997
|
+
}
|
|
2998
|
+
this.beginElement(this.seq++);
|
|
2999
|
+
try {
|
|
3000
|
+
const graphics = [];
|
|
3001
|
+
let graphicsPlan = null;
|
|
3002
|
+
for (let p = open + 1; p < close; p = t.nextPair(p)) {
|
|
3003
|
+
const key = t.textOf(p);
|
|
3004
|
+
if (key === "id" || key === ORIGINAL_ID_KEY && options.restoreMangledIds) {
|
|
3005
|
+
continue;
|
|
3006
|
+
}
|
|
3007
|
+
const plan = this.nodePlans.get(key);
|
|
3008
|
+
if (plan !== void 0 && plan.position !== null) {
|
|
3009
|
+
graphicsPlan = plan;
|
|
3010
|
+
graphics.push(p + 1);
|
|
3011
|
+
} else if (plan !== void 0) {
|
|
3012
|
+
this.writeNode(plan, index, p + 1);
|
|
3013
|
+
}
|
|
3014
|
+
}
|
|
3015
|
+
if (graphicsPlan !== null) {
|
|
3016
|
+
this.writeGraphics(graphicsPlan, index, graphics, {
|
|
3017
|
+
line: t.line[graphics[0]],
|
|
3018
|
+
element: String(sinkId)
|
|
3019
|
+
});
|
|
3020
|
+
}
|
|
3021
|
+
this.flushLists("node", index);
|
|
3022
|
+
} catch (err) {
|
|
3023
|
+
this.touched.length = 0;
|
|
3024
|
+
report.recordError(err, { line, element: String(sinkId) });
|
|
3025
|
+
}
|
|
3026
|
+
}
|
|
3027
|
+
/**
|
|
3028
|
+
* Push one edge block.
|
|
3029
|
+
* @param open - the block's `[`
|
|
3030
|
+
* @param ordinal - the edge's ordinal among edge blocks, for messages
|
|
3031
|
+
*/
|
|
3032
|
+
importEdge(open, ordinal) {
|
|
3033
|
+
const t = this.requireTokens();
|
|
3034
|
+
const { sink, report, options } = this;
|
|
3035
|
+
const close = t.match[open];
|
|
3036
|
+
const line = t.line[open];
|
|
3037
|
+
const element = `edge #${ordinal}`;
|
|
3038
|
+
this.checkAbort();
|
|
3039
|
+
let edge;
|
|
3040
|
+
try {
|
|
3041
|
+
let sourceTok = -1;
|
|
3042
|
+
let targetTok = -1;
|
|
3043
|
+
let weightTok = -1;
|
|
3044
|
+
let directedTok = -1;
|
|
3045
|
+
for (let p = open + 1; p < close; p = t.nextPair(p)) {
|
|
3046
|
+
const key = t.textOf(p);
|
|
3047
|
+
if (key === "source") {
|
|
3048
|
+
sourceTok = this.structuralToken(sourceTok, p, "source");
|
|
3049
|
+
} else if (key === "target") {
|
|
3050
|
+
targetTok = this.structuralToken(targetTok, p, "target");
|
|
3051
|
+
} else if (key === "directed") {
|
|
3052
|
+
directedTok = this.structuralToken(directedTok, p, "directed");
|
|
3053
|
+
} else if (key === options.weightFrom) {
|
|
3054
|
+
weightTok = p + 1;
|
|
3055
|
+
}
|
|
3056
|
+
}
|
|
3057
|
+
if (sourceTok < 0 || targetTok < 0) {
|
|
3058
|
+
throw new GraphFormatError("E_UNKNOWN_NODE", `edge has no ${sourceTok < 0 ? "source" : "target"}`, {
|
|
3059
|
+
reason: "missing endpoint"
|
|
3060
|
+
});
|
|
3061
|
+
}
|
|
3062
|
+
const source = this.endpoint(sourceTok, "source");
|
|
3063
|
+
const target = this.endpoint(targetTok, "target");
|
|
3064
|
+
const weight = weightTok >= 0 ? this.weightOf(weightTok) : void 0;
|
|
3065
|
+
const before = sink.edgeCount;
|
|
3066
|
+
const sourceNew = sink.indexOf(source) === INVALID_INDEX;
|
|
3067
|
+
const targetNew = source !== target && sink.indexOf(target) === INVALID_INDEX;
|
|
3068
|
+
const directed = this.readFlag(directedTok, "directed") ?? this.headerDirected;
|
|
3069
|
+
edge = this.requireResolver().addEdge(source, target, directed ? "directed" : "undirected", weight, {
|
|
3070
|
+
line,
|
|
3071
|
+
element
|
|
3072
|
+
});
|
|
3073
|
+
report.counts.edges += sink.edgeCount - before;
|
|
3074
|
+
report.counts.nodes += (sourceNew ? 1 : 0) + (targetNew ? 1 : 0);
|
|
3075
|
+
} catch (err) {
|
|
3076
|
+
this.recordEdgeError(err, line, element);
|
|
3077
|
+
report.counts.skippedEdges++;
|
|
3078
|
+
return;
|
|
3079
|
+
}
|
|
3080
|
+
this.beginElement(this.seq++);
|
|
3081
|
+
try {
|
|
3082
|
+
for (let p = open + 1; p < close; p = t.nextPair(p)) {
|
|
3083
|
+
const key = t.textOf(p);
|
|
3084
|
+
if (key === "source" || key === "target" || key === "directed" || key === options.weightFrom) {
|
|
3085
|
+
continue;
|
|
3086
|
+
}
|
|
3087
|
+
const plan = this.edgePlans.get(key);
|
|
3088
|
+
if (plan !== void 0) {
|
|
3089
|
+
this.writeEdge(plan, edge, p + 1);
|
|
3090
|
+
}
|
|
3091
|
+
}
|
|
3092
|
+
this.flushLists("edge", edge);
|
|
3093
|
+
} catch (err) {
|
|
3094
|
+
this.touched.length = 0;
|
|
3095
|
+
report.recordError(err, { line, element });
|
|
3096
|
+
}
|
|
3097
|
+
}
|
|
3098
|
+
// ============================================================ ids and weights
|
|
3099
|
+
/**
|
|
3100
|
+
* Note a structural key token, refusing a repeat.
|
|
3101
|
+
* @param previous - the token seen before, or -1
|
|
3102
|
+
* @param p - the key token
|
|
3103
|
+
* @param key - the key name
|
|
3104
|
+
* @returns the value token
|
|
3105
|
+
*/
|
|
3106
|
+
structuralToken(previous, p, key) {
|
|
3107
|
+
if (previous >= 0) {
|
|
3108
|
+
throw new GraphFormatError("E_INVALID_ID", `key "${key}" appears twice in one block`, {
|
|
3109
|
+
reason: "duplicate key",
|
|
3110
|
+
key
|
|
3111
|
+
});
|
|
3112
|
+
}
|
|
3113
|
+
return p + 1;
|
|
3114
|
+
}
|
|
3115
|
+
/**
|
|
3116
|
+
* The value of an id / source / target token: an integer (the GML spec's type) as a number, a
|
|
3117
|
+
* string (NetworkX and Gephi write them) as its text with one warning per file.
|
|
3118
|
+
* @param v - the value token
|
|
3119
|
+
* @param key - the key name, for the error
|
|
3120
|
+
* @returns the number or the string
|
|
3121
|
+
*/
|
|
3122
|
+
fileIdOf(v, key) {
|
|
3123
|
+
const t = this.requireTokens();
|
|
3124
|
+
if (t.kind[v] === TOKEN_STRING) {
|
|
3125
|
+
this.report.warnOnce(
|
|
3126
|
+
"validation-error",
|
|
3127
|
+
STRING_ID_CODE,
|
|
3128
|
+
`${key} is the string "${t.textOf(v)}"; GML ids are integers, string ids are kept under the ids rule`,
|
|
3129
|
+
{ line: t.line[v], element: t.stringOf(v) }
|
|
3130
|
+
);
|
|
3131
|
+
return t.stringOf(v);
|
|
3132
|
+
}
|
|
3133
|
+
if (t.kind[v] !== TOKEN_INT) {
|
|
3134
|
+
throw new GraphFormatError(
|
|
3135
|
+
"E_INVALID_ID",
|
|
3136
|
+
`${key} must be an integer or a string, found ${describeValue(t, v)}`,
|
|
3137
|
+
{
|
|
3138
|
+
reason: "not an integer",
|
|
3139
|
+
key,
|
|
3140
|
+
value: t.kind[v] === TOKEN_OPEN ? "[...]" : t.textOf(v)
|
|
3141
|
+
}
|
|
3142
|
+
);
|
|
3143
|
+
}
|
|
3144
|
+
return Number(t.textOf(v));
|
|
3145
|
+
}
|
|
3146
|
+
/**
|
|
3147
|
+
* Coerce an id / source / target value under the `ids` rule: a string by its text, an integer
|
|
3148
|
+
* through coerceInteger().
|
|
3149
|
+
* @param v - the value token
|
|
3150
|
+
* @param id - its value (fileIdOf())
|
|
3151
|
+
* @returns the id
|
|
3152
|
+
*/
|
|
3153
|
+
coerceFileId(v, id) {
|
|
3154
|
+
return typeof id === "string" ? this.coercer.text(id) : this.coerceInteger(v, id);
|
|
3155
|
+
}
|
|
3156
|
+
/**
|
|
3157
|
+
* Coerce an integer id token under the `ids` rule: a safe integer passes as a typed number
|
|
3158
|
+
* (so `01` is 1 and `-0` is 0), a larger one goes through the text rule (a string under
|
|
3159
|
+
* "canonical"), and "string" keeps the text as written.
|
|
3160
|
+
* @param v - the value token
|
|
3161
|
+
* @param n - its numeric value
|
|
3162
|
+
* @returns the id
|
|
3163
|
+
*/
|
|
3164
|
+
coerceInteger(v, n) {
|
|
3165
|
+
if (Number.isSafeInteger(n) && this.coercer.mode !== "string") {
|
|
3166
|
+
return this.coercer.value(n);
|
|
3167
|
+
}
|
|
3168
|
+
return this.coercer.text(this.requireTokens().textOf(v));
|
|
3169
|
+
}
|
|
3170
|
+
/**
|
|
3171
|
+
* Coerce a label token under the `ids` rule: a string by its text, a number as a typed value.
|
|
3172
|
+
* @param v - the value token
|
|
3173
|
+
* @returns the id
|
|
3174
|
+
*/
|
|
3175
|
+
coerceValueToken(v) {
|
|
3176
|
+
const t = this.requireTokens();
|
|
3177
|
+
switch (t.kind[v]) {
|
|
3178
|
+
case TOKEN_STRING:
|
|
3179
|
+
return this.coercer.text(t.stringOf(v));
|
|
3180
|
+
case TOKEN_INT:
|
|
3181
|
+
return this.coerceInteger(v, Number(t.textOf(v)));
|
|
3182
|
+
case TOKEN_OPEN:
|
|
3183
|
+
throw new GraphFormatError("E_INVALID_ID", "label must be a string or a number, found a record", {
|
|
3184
|
+
reason: "record label"
|
|
3185
|
+
});
|
|
3186
|
+
default:
|
|
3187
|
+
return this.coercer.value(numberOfText(t.textOf(v)));
|
|
3188
|
+
}
|
|
3189
|
+
}
|
|
3190
|
+
/**
|
|
3191
|
+
* The id a `graphty_originalId` key restores: typed, never coerced (the exporter wrote it exactly).
|
|
3192
|
+
* @param v - the value token
|
|
3193
|
+
* @returns the original id
|
|
3194
|
+
*/
|
|
3195
|
+
restoredId(v) {
|
|
3196
|
+
const t = this.requireTokens();
|
|
3197
|
+
switch (t.kind[v]) {
|
|
3198
|
+
case TOKEN_STRING:
|
|
3199
|
+
return t.stringOf(v);
|
|
3200
|
+
case TOKEN_OPEN:
|
|
3201
|
+
throw new GraphFormatError("E_INVALID_ID", `${ORIGINAL_ID_KEY} must be a string or a number`, {
|
|
3202
|
+
reason: "record original id"
|
|
3203
|
+
});
|
|
3204
|
+
default:
|
|
3205
|
+
return numberOfText(t.textOf(v));
|
|
3206
|
+
}
|
|
3207
|
+
}
|
|
3208
|
+
/**
|
|
3209
|
+
* Resolve a source / target token to a sink id.
|
|
3210
|
+
* @param v - the value token
|
|
3211
|
+
* @param key - source or target
|
|
3212
|
+
* @returns the id
|
|
3213
|
+
*/
|
|
3214
|
+
endpoint(v, key) {
|
|
3215
|
+
const id = this.coerceFileId(v, this.fileIdOf(v, key));
|
|
3216
|
+
const mapped = this.idMap?.get(id);
|
|
3217
|
+
if (mapped !== void 0) {
|
|
3218
|
+
return mapped;
|
|
3219
|
+
}
|
|
3220
|
+
if (this.skippedIds.has(id)) {
|
|
3221
|
+
throw new GraphFormatError(
|
|
3222
|
+
"E_UNKNOWN_NODE",
|
|
3223
|
+
`${key} ${JSON.stringify(id)} names a node block that was skipped after an error`,
|
|
3224
|
+
{ reason: "skipped node", id }
|
|
3225
|
+
);
|
|
3226
|
+
}
|
|
3227
|
+
return id;
|
|
3228
|
+
}
|
|
3229
|
+
/**
|
|
3230
|
+
* The weight of an edge from its weight token.
|
|
3231
|
+
* @param v - the value token
|
|
3232
|
+
* @returns the weight; E_INVALID_WEIGHT for a record or non-numeric text
|
|
3233
|
+
*/
|
|
3234
|
+
weightOf(v) {
|
|
3235
|
+
const t = this.requireTokens();
|
|
3236
|
+
switch (t.kind[v]) {
|
|
3237
|
+
case TOKEN_STRING:
|
|
3238
|
+
return parseWeightText(t.stringOf(v), this.report);
|
|
3239
|
+
case TOKEN_OPEN:
|
|
3240
|
+
throw new GraphFormatError("E_INVALID_WEIGHT", "edge weight is a record", { value: "[...]" });
|
|
3241
|
+
default: {
|
|
3242
|
+
const text = t.textOf(v);
|
|
3243
|
+
const weight = weightFromValue(numberOfText(text));
|
|
3244
|
+
if (weight !== void 0) {
|
|
3245
|
+
reportWeightPrecision(text, weight, this.report);
|
|
3246
|
+
}
|
|
3247
|
+
return weight;
|
|
3248
|
+
}
|
|
3249
|
+
}
|
|
3250
|
+
}
|
|
3251
|
+
/**
|
|
3252
|
+
* Record a node-level error under the GML code it implies.
|
|
3253
|
+
* @param err - the thrown value
|
|
3254
|
+
* @param line - the block's line
|
|
3255
|
+
*/
|
|
3256
|
+
recordNodeError(err, line) {
|
|
3257
|
+
const { report } = this;
|
|
3258
|
+
if (err instanceof GraphFormatError && err.code === "E_INVALID_ID") {
|
|
3259
|
+
const { reason } = err.details;
|
|
3260
|
+
switch (reason) {
|
|
3261
|
+
case "missing id":
|
|
3262
|
+
report.error("missing-value", MISSING_ID_CODE, err.message, { line, element: "node" });
|
|
3263
|
+
return;
|
|
3264
|
+
case "missing label":
|
|
3265
|
+
report.error("missing-value", MISSING_LABEL_CODE, err.message, { line, element: "node" });
|
|
3266
|
+
return;
|
|
3267
|
+
case "duplicate key":
|
|
3268
|
+
report.error("validation-error", REPEATED_KEY_CODE, err.message, { line, element: "node" });
|
|
3269
|
+
return;
|
|
3270
|
+
case "not an integer":
|
|
3271
|
+
report.error("validation-error", ID_TYPE_CODE, err.message, { line, element: "node" });
|
|
3272
|
+
return;
|
|
3273
|
+
}
|
|
3274
|
+
}
|
|
3275
|
+
report.recordError(err, { line, element: "node" });
|
|
3276
|
+
}
|
|
3277
|
+
/**
|
|
3278
|
+
* Record an edge-level error under the GML code it implies.
|
|
3279
|
+
* @param err - the thrown value
|
|
3280
|
+
* @param line - the block's line
|
|
3281
|
+
* @param element - the edge description
|
|
3282
|
+
*/
|
|
3283
|
+
recordEdgeError(err, line, element) {
|
|
3284
|
+
const { report } = this;
|
|
3285
|
+
if (err instanceof GraphFormatError) {
|
|
3286
|
+
const { reason } = err.details;
|
|
3287
|
+
if (err.code === "E_UNKNOWN_NODE" && reason === "missing endpoint") {
|
|
3288
|
+
report.error("missing-value", MISSING_ENDPOINT_CODE, err.message, { line, element });
|
|
3289
|
+
return;
|
|
3290
|
+
}
|
|
3291
|
+
if (err.code === "E_INVALID_ID" && reason === "duplicate key") {
|
|
3292
|
+
report.error("validation-error", REPEATED_KEY_CODE, err.message, { line, element });
|
|
3293
|
+
return;
|
|
3294
|
+
}
|
|
3295
|
+
if (err.code === "E_INVALID_ID" && reason === "not an integer") {
|
|
3296
|
+
report.error("validation-error", ID_TYPE_CODE, err.message, { line, element });
|
|
3297
|
+
return;
|
|
3298
|
+
}
|
|
3299
|
+
}
|
|
3300
|
+
report.recordError(err, { line, element });
|
|
3301
|
+
}
|
|
3302
|
+
// ============================================================ values
|
|
3303
|
+
/**
|
|
3304
|
+
* Write one node value.
|
|
3305
|
+
* @param plan - the key's plan
|
|
3306
|
+
* @param index - the node index
|
|
3307
|
+
* @param v - the value token
|
|
3308
|
+
*/
|
|
3309
|
+
writeNode(plan, index, v) {
|
|
3310
|
+
if (plan.list) {
|
|
3311
|
+
this.collectItem(plan, v, this.seqNow, this.touched);
|
|
3312
|
+
return;
|
|
3313
|
+
}
|
|
3314
|
+
this.sink.setNodeValue(this.handleOf(plan, "node"), index, this.scalarOf(plan, v));
|
|
3315
|
+
}
|
|
3316
|
+
/**
|
|
3317
|
+
* Write one edge value.
|
|
3318
|
+
* @param plan - the key's plan
|
|
3319
|
+
* @param edge - the edge index
|
|
3320
|
+
* @param v - the value token
|
|
3321
|
+
*/
|
|
3322
|
+
writeEdge(plan, edge, v) {
|
|
3323
|
+
if (plan.list) {
|
|
3324
|
+
this.collectItem(plan, v, this.seqNow, this.touched);
|
|
3325
|
+
return;
|
|
3326
|
+
}
|
|
3327
|
+
this.sink.setEdgeValue(this.handleOf(plan, "edge"), edge, this.scalarOf(plan, v));
|
|
3328
|
+
}
|
|
3329
|
+
/**
|
|
3330
|
+
* Write one graph value.
|
|
3331
|
+
* @param plan - the key's plan
|
|
3332
|
+
* @param v - the value token
|
|
3333
|
+
* @param seq - TOP_SEQ or GRAPH_SEQ
|
|
3334
|
+
*/
|
|
3335
|
+
writeGraph(plan, v, seq) {
|
|
3336
|
+
if (plan.list) {
|
|
3337
|
+
this.collectItem(plan, v, seq, this.graphTouched);
|
|
3338
|
+
return;
|
|
3339
|
+
}
|
|
3340
|
+
const value = this.scalarOf(plan, v);
|
|
3341
|
+
this.sink.setGraphValue(plan.name, value, graphDecl(plan));
|
|
3342
|
+
if (seq === GRAPH_SEQ && plan.key === "name" && typeof value === "string") {
|
|
3343
|
+
this.sink.setMeta({ name: value });
|
|
3344
|
+
}
|
|
3345
|
+
}
|
|
3346
|
+
/**
|
|
3347
|
+
* Write a node's graphics values: the position column from the x / y / z of the first record,
|
|
3348
|
+
* everything else (the remaining keys, a value that is not a record, a repeated record) to the
|
|
3349
|
+
* json column, with a warning when the value cannot give a position as written.
|
|
3350
|
+
* @param plan - the graphics plan
|
|
3351
|
+
* @param index - the node index
|
|
3352
|
+
* @param vs - the value tokens of the node's graphics keys, in order
|
|
3353
|
+
* @param where - the line and the node, for issues
|
|
3354
|
+
*/
|
|
3355
|
+
writeGraphics(plan, index, vs, where) {
|
|
3356
|
+
const t = this.requireTokens();
|
|
3357
|
+
const { sink } = this;
|
|
3358
|
+
const values = vs.map((v) => t.kind[v] === TOKEN_OPEN ? parseRecord(t, v) : this.scalarOf(plan, v));
|
|
3359
|
+
const first2 = values[0];
|
|
3360
|
+
const positionPlan = plan.position;
|
|
3361
|
+
let rest = first2;
|
|
3362
|
+
if (isRecord(first2) && positionPlan !== null) {
|
|
3363
|
+
const left = this.writePosition(positionPlan, index, this.keepPositionSource ? { ...first2 } : first2, where);
|
|
3364
|
+
rest = this.keepPositionSource ? first2 : left;
|
|
3365
|
+
} else if (!isRecord(first2)) {
|
|
3366
|
+
this.graphicsWarning("scalar", `node ${where.element}'s graphics is not a record`, where);
|
|
3367
|
+
}
|
|
3368
|
+
if (values.length > 1) {
|
|
3369
|
+
this.graphicsWarning(
|
|
3370
|
+
"repeated",
|
|
3371
|
+
`node ${where.element} has ${values.length} graphics records; the position comes from the first`,
|
|
3372
|
+
where
|
|
3373
|
+
);
|
|
3374
|
+
rest = values;
|
|
3375
|
+
}
|
|
3376
|
+
if (rest !== void 0 && plan.rest !== null) {
|
|
3377
|
+
sink.setNodeValue(this.handleOf(plan.rest, "node"), index, rest);
|
|
3378
|
+
}
|
|
3379
|
+
}
|
|
3380
|
+
/**
|
|
3381
|
+
* Write a node's position from the x / y / z of its graphics record, taking them out of it.
|
|
3382
|
+
* @param positionPlan - the position column's plan
|
|
3383
|
+
* @param index - the node index
|
|
3384
|
+
* @param record - the graphics record
|
|
3385
|
+
* @param where - the line and the node, for issues
|
|
3386
|
+
* @returns what is left of the record for the json column, or undefined when nothing is
|
|
3387
|
+
*/
|
|
3388
|
+
writePosition(positionPlan, index, record, where) {
|
|
3389
|
+
const { x, y, z } = record;
|
|
3390
|
+
let rest = record;
|
|
3391
|
+
if (typeof x === "number" && typeof y === "number") {
|
|
3392
|
+
const depth = typeof z === "number" ? z : 0;
|
|
3393
|
+
this.sink.setNodeValue(this.handleOf(positionPlan, "node"), index, [x, y, depth]);
|
|
3394
|
+
delete record.x;
|
|
3395
|
+
delete record.y;
|
|
3396
|
+
if (typeof z === "number") {
|
|
3397
|
+
delete record.z;
|
|
3398
|
+
}
|
|
3399
|
+
rest = Object.keys(record).length > 0 ? record : void 0;
|
|
3400
|
+
}
|
|
3401
|
+
if ("x" in record || "y" in record || "z" in record) {
|
|
3402
|
+
this.graphicsWarning(
|
|
3403
|
+
"coordinate",
|
|
3404
|
+
`node ${where.element}'s graphics x / y / z is not one number each`,
|
|
3405
|
+
where
|
|
3406
|
+
);
|
|
3407
|
+
}
|
|
3408
|
+
return rest;
|
|
3409
|
+
}
|
|
3410
|
+
/**
|
|
3411
|
+
* Warn once per kind of graphics problem.
|
|
3412
|
+
* @param kind - the problem
|
|
3413
|
+
* @param what - the message start, naming the node
|
|
3414
|
+
* @param where - the line and the node
|
|
3415
|
+
*/
|
|
3416
|
+
graphicsWarning(kind, what, where) {
|
|
3417
|
+
this.report.warnOnce(
|
|
3418
|
+
"validation-error",
|
|
3419
|
+
GRAPHICS_CODE,
|
|
3420
|
+
`${what}; the value is kept in the graphics json column (warned once for every such node)`,
|
|
3421
|
+
where,
|
|
3422
|
+
`${GRAPHICS_CODE}:${kind}`
|
|
3423
|
+
);
|
|
3424
|
+
}
|
|
3425
|
+
/**
|
|
3426
|
+
* Collect one list item.
|
|
3427
|
+
* @param plan - the list plan
|
|
3428
|
+
* @param v - the value token
|
|
3429
|
+
* @param seq - the sequence number of the element the item belongs to
|
|
3430
|
+
* @param touched - the plans touched by that element
|
|
3431
|
+
*/
|
|
3432
|
+
collectItem(plan, v, seq, touched) {
|
|
3433
|
+
const t = this.requireTokens();
|
|
3434
|
+
if (plan.seq !== seq) {
|
|
3435
|
+
plan.seq = seq;
|
|
3436
|
+
plan.items = [];
|
|
3437
|
+
touched.push(plan);
|
|
3438
|
+
}
|
|
3439
|
+
if (t.kind[v] === TOKEN_STRING) {
|
|
3440
|
+
const raw = t.textOf(v);
|
|
3441
|
+
if (raw === LIST_START_MARKER || raw === EMPTY_LIST_TEXT || raw === EMPTY_TUPLE_TEXT) {
|
|
3442
|
+
return;
|
|
3443
|
+
}
|
|
3444
|
+
}
|
|
3445
|
+
plan.items.push(this.scalarOf(plan, v));
|
|
3446
|
+
}
|
|
3447
|
+
/**
|
|
3448
|
+
* Start a node or edge: later list items belong to it.
|
|
3449
|
+
* @param seq - the element's sequence number
|
|
3450
|
+
*/
|
|
3451
|
+
beginElement(seq) {
|
|
3452
|
+
this.seqNow = seq;
|
|
3453
|
+
this.touched.length = 0;
|
|
3454
|
+
}
|
|
3455
|
+
/**
|
|
3456
|
+
* Write the lists collected for the current node or edge.
|
|
3457
|
+
* @param domain - node or edge
|
|
3458
|
+
* @param row - the row
|
|
3459
|
+
*/
|
|
3460
|
+
flushLists(domain, row) {
|
|
3461
|
+
const { sink, touched } = this;
|
|
3462
|
+
for (const plan of touched) {
|
|
3463
|
+
const handle = this.handleOf(plan, domain);
|
|
3464
|
+
if (domain === "node") {
|
|
3465
|
+
sink.setNodeValue(handle, row, plan.items);
|
|
3466
|
+
} else {
|
|
3467
|
+
sink.setEdgeValue(handle, row, plan.items);
|
|
3468
|
+
}
|
|
3469
|
+
plan.items = null;
|
|
3470
|
+
}
|
|
3471
|
+
touched.length = 0;
|
|
3472
|
+
}
|
|
3473
|
+
/** Write the lists collected for the graph table. */
|
|
3474
|
+
flushGraphLists() {
|
|
3475
|
+
const { sink, graphTouched } = this;
|
|
3476
|
+
for (const plan of graphTouched) {
|
|
3477
|
+
sink.setGraphValue(plan.name, plan.items, graphDecl(plan));
|
|
3478
|
+
plan.items = null;
|
|
3479
|
+
}
|
|
3480
|
+
graphTouched.length = 0;
|
|
3481
|
+
}
|
|
3482
|
+
/**
|
|
3483
|
+
* The JS value of a scalar value token (or list item) under a plan's storage class.
|
|
3484
|
+
* @param plan - the plan
|
|
3485
|
+
* @param v - the value token
|
|
3486
|
+
* @returns the value to push
|
|
3487
|
+
*/
|
|
3488
|
+
scalarOf(plan, v) {
|
|
3489
|
+
const t = this.requireTokens();
|
|
3490
|
+
const kind = t.kind[v];
|
|
3491
|
+
switch (plan.kind) {
|
|
3492
|
+
case "int":
|
|
3493
|
+
return Number(t.textOf(v));
|
|
3494
|
+
case "real":
|
|
3495
|
+
return numberOfText(t.textOf(v));
|
|
3496
|
+
case "string":
|
|
3497
|
+
return kind === TOKEN_STRING ? t.stringOf(v) : t.textOf(v);
|
|
3498
|
+
default:
|
|
3499
|
+
return kind === TOKEN_STRING ? t.stringOf(v) : scalarOrRecord(t, v);
|
|
3500
|
+
}
|
|
3501
|
+
}
|
|
3502
|
+
/**
|
|
3503
|
+
* The handle of a plan's column, declaring it on first use. A declaration the sink refuses
|
|
3504
|
+
* because a column of the name exists with another declaration falls back to that column
|
|
3505
|
+
* (reported once); a taken role is dropped (reported once).
|
|
3506
|
+
* @param plan - the plan
|
|
3507
|
+
* @param domain - node or edge
|
|
3508
|
+
* @returns the handle
|
|
3509
|
+
*/
|
|
3510
|
+
handleOf(plan, domain) {
|
|
3511
|
+
if (plan.declared) {
|
|
3512
|
+
return plan.handle;
|
|
3513
|
+
}
|
|
3514
|
+
plan.declared = true;
|
|
3515
|
+
const { sink, report } = this;
|
|
3516
|
+
const decl = {
|
|
3517
|
+
name: plan.name,
|
|
3518
|
+
dtype: plan.dtype,
|
|
3519
|
+
nullable: true,
|
|
3520
|
+
origin: { format: "gml", id: plan.key, title: null, type: originTypeOf(plan), namespace: null }
|
|
3521
|
+
};
|
|
3522
|
+
if (plan.list) {
|
|
3523
|
+
decl.itemDtype = plan.itemDtype ?? "string";
|
|
3524
|
+
}
|
|
3525
|
+
if (plan.role === "position") {
|
|
3526
|
+
decl.components = 3;
|
|
3527
|
+
}
|
|
3528
|
+
if (plan.role !== null) {
|
|
3529
|
+
decl.role = plan.role;
|
|
3530
|
+
}
|
|
3531
|
+
const resolved = declareResolved(sink, domain, decl, report, { element: plan.name });
|
|
3532
|
+
plan.handle = resolved.handle;
|
|
3533
|
+
if (resolved.renamed) {
|
|
3534
|
+
plan.name = resolved.decl.name;
|
|
3535
|
+
}
|
|
3536
|
+
return plan.handle;
|
|
3537
|
+
}
|
|
3538
|
+
// ============================================================ bookkeeping
|
|
3539
|
+
/**
|
|
3540
|
+
* Whether a node index was declared by a node block of this import.
|
|
3541
|
+
* @param index - the node index
|
|
3542
|
+
* @returns true when declared
|
|
3543
|
+
*/
|
|
3544
|
+
isDeclared(index) {
|
|
3545
|
+
const word = index >>> 5;
|
|
3546
|
+
return word < this.declared.length && (this.declared[word] >>> (index & 31) & 1) === 1;
|
|
3547
|
+
}
|
|
3548
|
+
/**
|
|
3549
|
+
* Mark a node index as declared.
|
|
3550
|
+
* @param index - the node index
|
|
3551
|
+
*/
|
|
3552
|
+
markDeclared(index) {
|
|
3553
|
+
const word = index >>> 5;
|
|
3554
|
+
if (word >= this.declared.length) {
|
|
3555
|
+
const grown = new Uint32Array(Math.max(word + 1, this.declared.length * 2));
|
|
3556
|
+
grown.set(this.declared);
|
|
3557
|
+
this.declared = grown;
|
|
3558
|
+
}
|
|
3559
|
+
this.declared[word] |= 1 << (index & 31);
|
|
3560
|
+
}
|
|
3561
|
+
/** Check the abort signal every ABORT_CHECK_INTERVAL elements. */
|
|
3562
|
+
checkAbort() {
|
|
3563
|
+
if (++this.elementsSinceCheck >= ABORT_CHECK_INTERVAL) {
|
|
3564
|
+
this.elementsSinceCheck = 0;
|
|
3565
|
+
throwIfAborted(this.options.signal);
|
|
3566
|
+
}
|
|
3567
|
+
}
|
|
3568
|
+
/**
|
|
3569
|
+
* The tokens, which exist after run() lexed the text.
|
|
3570
|
+
* @returns the tokens
|
|
3571
|
+
*/
|
|
3572
|
+
requireTokens() {
|
|
3573
|
+
if (this.tokens === null) {
|
|
3574
|
+
throw new GraphFormatError("E_UNSUPPORTED", "the GML text was not lexed", { reason: "no tokens" });
|
|
3575
|
+
}
|
|
3576
|
+
return this.tokens;
|
|
3577
|
+
}
|
|
3578
|
+
/**
|
|
3579
|
+
* The direction resolver, which exists once the header was set.
|
|
3580
|
+
* @returns the resolver
|
|
3581
|
+
*/
|
|
3582
|
+
requireResolver() {
|
|
3583
|
+
if (this.resolver === null) {
|
|
3584
|
+
throw new GraphFormatError("E_DIRECTED", "the header was not set", { reason: "no resolver" });
|
|
3585
|
+
}
|
|
3586
|
+
return this.resolver;
|
|
3587
|
+
}
|
|
3588
|
+
}
|
|
3589
|
+
function kindBit(t, v) {
|
|
3590
|
+
switch (t.kind[v]) {
|
|
3591
|
+
case TOKEN_INT:
|
|
3592
|
+
return KIND_INT;
|
|
3593
|
+
case TOKEN_STRING:
|
|
3594
|
+
return KIND_STRING;
|
|
3595
|
+
case TOKEN_OPEN:
|
|
3596
|
+
return KIND_RECORD;
|
|
3597
|
+
default:
|
|
3598
|
+
return KIND_REAL;
|
|
3599
|
+
}
|
|
3600
|
+
}
|
|
3601
|
+
function kindOf(entry) {
|
|
3602
|
+
if ((entry.kinds & KIND_RECORD) !== 0) {
|
|
3603
|
+
return "record";
|
|
3604
|
+
}
|
|
3605
|
+
if ((entry.kinds & KIND_STRING) !== 0) {
|
|
3606
|
+
return "string";
|
|
3607
|
+
}
|
|
3608
|
+
if ((entry.kinds & KIND_REAL) !== 0) {
|
|
3609
|
+
return "real";
|
|
3610
|
+
}
|
|
3611
|
+
return "int";
|
|
3612
|
+
}
|
|
3613
|
+
function dtypeOfKind(kind, entry) {
|
|
3614
|
+
switch (kind) {
|
|
3615
|
+
case "int":
|
|
3616
|
+
return entry.wide ? "f64" : "i32";
|
|
3617
|
+
case "real":
|
|
3618
|
+
return "f64";
|
|
3619
|
+
case "string":
|
|
3620
|
+
return "string";
|
|
3621
|
+
default:
|
|
3622
|
+
return "json";
|
|
3623
|
+
}
|
|
3624
|
+
}
|
|
3625
|
+
function originTypeOf(plan) {
|
|
3626
|
+
return plan.kind;
|
|
3627
|
+
}
|
|
3628
|
+
function graphDecl(plan) {
|
|
3629
|
+
const decl = {
|
|
3630
|
+
name: plan.name,
|
|
3631
|
+
dtype: plan.dtype,
|
|
3632
|
+
nullable: true,
|
|
3633
|
+
origin: { format: "gml", id: plan.key, title: null, type: originTypeOf(plan), namespace: null }
|
|
3634
|
+
};
|
|
3635
|
+
if (plan.list) {
|
|
3636
|
+
decl.itemDtype = plan.itemDtype ?? "string";
|
|
3637
|
+
}
|
|
3638
|
+
if (plan.top) {
|
|
3639
|
+
decl.extra = { gmlTopLevel: true };
|
|
3640
|
+
}
|
|
3641
|
+
return decl;
|
|
3642
|
+
}
|
|
3643
|
+
function describeValue(t, v) {
|
|
3644
|
+
switch (t.kind[v]) {
|
|
3645
|
+
case TOKEN_OPEN:
|
|
3646
|
+
return "a record";
|
|
3647
|
+
case TOKEN_STRING:
|
|
3648
|
+
return `the string "${t.textOf(v)}"`;
|
|
3649
|
+
case TOKEN_REAL:
|
|
3650
|
+
return `the real ${t.textOf(v)}`;
|
|
3651
|
+
case TOKEN_WORD:
|
|
3652
|
+
return `the word ${t.textOf(v)}`;
|
|
3653
|
+
default:
|
|
3654
|
+
return t.textOf(v);
|
|
3655
|
+
}
|
|
3656
|
+
}
|
|
3657
|
+
const SNIFF_GRAPH_START = /^(?:\s|#[^\n]*\n)*graph\s*\[/;
|
|
3658
|
+
const SNIFF_GRAPH_ANYWHERE = /(?:^|\s)graph\s*\[/;
|
|
3659
|
+
const SNIFF_KEY_START = /^(?:\s|#[^\n]*\n)*[A-Za-z_]\w*\s+(?:"|[+-]?[0-9.]|\[)/;
|
|
3660
|
+
function sniffGml(head) {
|
|
3661
|
+
let text = new TextDecoder("utf-8", { fatal: false }).decode(head);
|
|
3662
|
+
if (text.charCodeAt(0) === 65279) {
|
|
3663
|
+
text = text.slice(1);
|
|
3664
|
+
}
|
|
3665
|
+
if (SNIFF_GRAPH_START.test(text)) {
|
|
3666
|
+
return 0.95;
|
|
3667
|
+
}
|
|
3668
|
+
if (SNIFF_KEY_START.test(text)) {
|
|
3669
|
+
return SNIFF_GRAPH_ANYWHERE.test(text) ? 0.85 : 0.4;
|
|
3670
|
+
}
|
|
3671
|
+
return 0;
|
|
3672
|
+
}
|
|
3673
|
+
const gmlImporter = Object.freeze({
|
|
3674
|
+
format: "gml",
|
|
3675
|
+
options: Object.freeze(["dictionaries", "keepPositionSource", "positions"]),
|
|
3676
|
+
extensions: Object.freeze([".gml"]),
|
|
3677
|
+
mimeTypes: Object.freeze(["text/x-gml", "text/plain"]),
|
|
3678
|
+
sniff: sniffGml,
|
|
3679
|
+
styles: gmlStyles,
|
|
3680
|
+
positions: gmlPositions,
|
|
3681
|
+
/**
|
|
3682
|
+
* Read a GML text into a sink.
|
|
3683
|
+
* @param input - the text, bytes or stream
|
|
3684
|
+
* @param sink - the sink
|
|
3685
|
+
* @param options - format-specific and common options
|
|
3686
|
+
* @returns the report
|
|
3687
|
+
*/
|
|
3688
|
+
async import(input, sink, options) {
|
|
3689
|
+
if (graphChosen(options)) {
|
|
3690
|
+
return importChosenGraph(gmlImporter, input, sink, options);
|
|
3691
|
+
}
|
|
3692
|
+
const resolved = resolveImportOptions(options, FORMAT_DEFAULTS);
|
|
3693
|
+
const report = new ImportReportBuilder("gml", resolved.errorLimit);
|
|
3694
|
+
reportSinkOptions(sink, options, report);
|
|
3695
|
+
reportUnusedOptions(options, report, USED_OPTIONS);
|
|
3696
|
+
const tokens = tokenize(await readText(input, report, resolved), report);
|
|
3697
|
+
throwIfAborted(resolved.signal);
|
|
3698
|
+
const gml = new GmlImport(sink, report, resolved, options);
|
|
3699
|
+
gml.run(tokens);
|
|
3700
|
+
if (gml.graphCount > 1) {
|
|
3701
|
+
report.warning(
|
|
3702
|
+
"unsupported",
|
|
3703
|
+
SECOND_GRAPH_CODE,
|
|
3704
|
+
`the input holds ${gml.graphCount - 1} more graph block${plural(gml.graphCount - 1)} after the first; the first is read (graphIndex or graphName chooses another; importAllGraphs() reads every one)`
|
|
3705
|
+
);
|
|
3706
|
+
}
|
|
3707
|
+
throwIfAborted(resolved.signal);
|
|
3708
|
+
return report.finish();
|
|
3709
|
+
},
|
|
3710
|
+
/**
|
|
3711
|
+
* Read every `graph [ ... ]` block of a GML text, each into its own sink; the top-level keys
|
|
3712
|
+
* (Creator, Version, ...) apply to each.
|
|
3713
|
+
* @param input - the text, bytes or stream
|
|
3714
|
+
* @param sinkFor - the sink of the graph with this index, called before its first push
|
|
3715
|
+
* @param options - format-specific and common options
|
|
3716
|
+
* @returns one report per graph block
|
|
3717
|
+
*/
|
|
3718
|
+
async importAll(input, sinkFor, options) {
|
|
3719
|
+
const resolved = resolveImportOptions(options, FORMAT_DEFAULTS);
|
|
3720
|
+
const first2 = new ImportReportBuilder("gml", resolved.errorLimit);
|
|
3721
|
+
reportUnusedOptions(options, first2, USED_OPTIONS);
|
|
3722
|
+
const tokens = tokenize(await readText(input, first2, resolved), first2);
|
|
3723
|
+
const reports = [];
|
|
3724
|
+
let count = 1;
|
|
3725
|
+
for (let i = 0; i < count; i++) {
|
|
3726
|
+
throwIfAborted(resolved.signal);
|
|
3727
|
+
const report = i === 0 ? first2 : new ImportReportBuilder("gml", resolved.errorLimit);
|
|
3728
|
+
const sink = sinkFor(i);
|
|
3729
|
+
reportSinkOptions(sink, options, report);
|
|
3730
|
+
const gml = new GmlImport(sink, report, resolved, options, i);
|
|
3731
|
+
gml.run(tokens);
|
|
3732
|
+
count = gml.graphCount;
|
|
3733
|
+
reports.push(report.finish());
|
|
3734
|
+
}
|
|
3735
|
+
throwIfAborted(resolved.signal);
|
|
3736
|
+
return reports;
|
|
3737
|
+
}
|
|
3738
|
+
});
|
|
3739
|
+
function tokenize(text, report) {
|
|
3740
|
+
try {
|
|
3741
|
+
return tokenizeGml(text);
|
|
3742
|
+
} catch (err) {
|
|
3743
|
+
if (err instanceof GmlSyntaxError) {
|
|
3744
|
+
report.fail(err.code, err.message, { line: err.line });
|
|
3745
|
+
}
|
|
3746
|
+
throw err;
|
|
3747
|
+
}
|
|
3748
|
+
}
|
|
3749
|
+
const GML_ISSUE = Object.freeze({
|
|
3750
|
+
...INPUT_ISSUE,
|
|
3751
|
+
...ELEMENT_ISSUE,
|
|
3752
|
+
/**
|
|
3753
|
+
* The text breaks GML's syntax: a word that is not a key or a value, an unclosed string or `[`, a stray `]`, or a
|
|
3754
|
+
* key without a value. The import stops.
|
|
3755
|
+
*/
|
|
3756
|
+
SYNTAX: SYNTAX_CODE,
|
|
3757
|
+
/** The input is not valid UTF-8. The import stops. */
|
|
3758
|
+
INVALID_UTF8: INVALID_UTF8_CODE,
|
|
3759
|
+
/**
|
|
3760
|
+
* Some bytes are not valid in the encoding that was chosen (by a byte order mark, the file's declaration or the
|
|
3761
|
+
* `encoding` option). The import stops.
|
|
3762
|
+
*/
|
|
3763
|
+
INVALID_ENCODING: INVALID_ENCODING_CODE,
|
|
3764
|
+
/** Bytes that are not UTF-8 and declare no encoding were read as windows-1252. */
|
|
3765
|
+
ENCODING_FALLBACK: ENCODING_FALLBACK_CODE,
|
|
3766
|
+
/** A declared encoding the platform cannot decode was ignored. */
|
|
3767
|
+
UNKNOWN_ENCODING: UNKNOWN_ENCODING_CODE,
|
|
3768
|
+
/** There is no `graph [` block. The import stops. */
|
|
3769
|
+
NO_GRAPH: NO_GRAPH_CODE,
|
|
3770
|
+
/** The file holds more than one `graph` block and only the first was read. It is not added when `graphIndex` or `graphName` chose the graph. */
|
|
3771
|
+
MULTIPLE_GRAPHS: SECOND_GRAPH_CODE,
|
|
3772
|
+
/** `graphIndex` or `graphName` names no graph of the file; the message lists the graphs it holds. The import stops. */
|
|
3773
|
+
GRAPH_NOT_FOUND: GRAPH_NOT_FOUND_CODE,
|
|
3774
|
+
/** `graphName` matches more than one graph; pass `graphIndex`. The import stops. */
|
|
3775
|
+
AMBIGUOUS_GRAPH_NAME: AMBIGUOUS_GRAPH_NAME_CODE,
|
|
3776
|
+
/** A node without an `id`. */
|
|
3777
|
+
MISSING_ID: MISSING_ID_CODE,
|
|
3778
|
+
/** A node without a `label` under nodeIdFrom "label". */
|
|
3779
|
+
MISSING_LABEL: MISSING_LABEL_CODE,
|
|
3780
|
+
/** An edge without `source` or `target`. */
|
|
3781
|
+
MISSING_ENDPOINT: MISSING_ENDPOINT_CODE,
|
|
3782
|
+
/** A node id, source or target that is neither an integer nor a string. */
|
|
3783
|
+
ID_TYPE: ID_TYPE_CODE,
|
|
3784
|
+
/**
|
|
3785
|
+
* Node ids, sources or targets are text, where GML expects integers. They are read under the `ids` option.
|
|
3786
|
+
* Reported once per file.
|
|
3787
|
+
*/
|
|
3788
|
+
STRING_ID: STRING_ID_CODE,
|
|
3789
|
+
/** A node id declared twice (later keys overwrite). */
|
|
3790
|
+
DUPLICATE_NODE: DUPLICATE_NODE_CODE,
|
|
3791
|
+
/** A structural key repeated in one element. */
|
|
3792
|
+
REPEATED_KEY: REPEATED_KEY_CODE,
|
|
3793
|
+
/** A `node` / `edge` key whose value is not a record. */
|
|
3794
|
+
ELEMENT_TYPE: ELEMENT_TYPE_CODE,
|
|
3795
|
+
/** A `directed` / `multigraph` flag that is not an integer. */
|
|
3796
|
+
FLAG_TYPE: FLAG_TYPE_CODE,
|
|
3797
|
+
/** A `directed` / `multigraph` flag outside 0 / 1, or repeated. */
|
|
3798
|
+
FLAG_VALUE: FLAG_VALUE_CODE,
|
|
3799
|
+
/** A named entity no table decodes, or a numeric reference beyond U+10FFFF; kept as written. */
|
|
3800
|
+
UNKNOWN_ENTITY: UNKNOWN_ENTITY_CODE,
|
|
3801
|
+
/** An integer beyond 2^53 was stored as the nearest 64-bit float; pass `long: "string"` to keep every digit. */
|
|
3802
|
+
PRECISION: PRECISION_CODE,
|
|
3803
|
+
/** A node's graphics value that cannot give a position as written; kept in the graphics json column. */
|
|
3804
|
+
GRAPHICS: GRAPHICS_CODE,
|
|
3805
|
+
/** A graph, node or edge record nested in a node or edge; kept as json, not read as structure. */
|
|
3806
|
+
NESTED_ELEMENT: NESTED_ELEMENT_CODE,
|
|
3807
|
+
/** YEd's isGroup / gid keys, kept as plain columns; the hierarchy is not read as containment. */
|
|
3808
|
+
GROUPS: GROUPS_CODE,
|
|
3809
|
+
/**
|
|
3810
|
+
* An attribute's type was widened because a later value did not fit: an integer above 2^31 in an integer column,
|
|
3811
|
+
* or two declared types for one attribute.
|
|
3812
|
+
*/
|
|
3813
|
+
WIDENED: WIDENED_CODE,
|
|
3814
|
+
/**
|
|
3815
|
+
* An attribute was renamed `<name>#<suffix>` because another attribute already has its name, for example
|
|
3816
|
+
* two attributes declared with the same name.
|
|
3817
|
+
*/
|
|
3818
|
+
COLUMN_RENAMED: COLUMN_RENAMED_CODE,
|
|
3819
|
+
/**
|
|
3820
|
+
* You read into a graph builder that already has an id, label or position attribute, so this file's one is kept as
|
|
3821
|
+
* a plain attribute.
|
|
3822
|
+
*/
|
|
3823
|
+
ROLE_TAKEN: ROLE_TAKEN_CODE,
|
|
3824
|
+
/** Two different id texts became the same number because `ids` is "number", so their nodes were merged. */
|
|
3825
|
+
ID_MERGED: ID_MERGED_CODE,
|
|
3826
|
+
/** You set an option this format does not use; it had no effect. The message names the option. */
|
|
3827
|
+
OPTION_IGNORED: OPTION_IGNORED_CODE,
|
|
3828
|
+
/**
|
|
3829
|
+
* You read into your own graph builder, which was created with a different `addMissingNodes`, `duplicateEdges`,
|
|
3830
|
+
* `selfLoops` or `weightDtype` than the option you passed; the builder's setting applies.
|
|
3831
|
+
*/
|
|
3832
|
+
SINK_OPTION: SINK_OPTION_CODE,
|
|
3833
|
+
/**
|
|
3834
|
+
* You read into a graph builder whose direction is already set, or which already holds edges, so the file is read
|
|
3835
|
+
* with the builder's direction instead of its own.
|
|
3836
|
+
*/
|
|
3837
|
+
DIRECTION_REFUSED: DIRECTION_REFUSED_CODE,
|
|
3838
|
+
/** Edges of the other direction were read with the direction `onMixedDirection` chose. */
|
|
3839
|
+
DIRECTION_FORCED: DIRECTION_FORCED_CODE,
|
|
3840
|
+
/**
|
|
3841
|
+
* The graph has both directed and undirected edges and `onMixedDirection` is "error". An import stops; a save to a
|
|
3842
|
+
* format that holds one direction per file fails with E_DIRECTED. Pass "directed" or "undirected" to read or write
|
|
3843
|
+
* it anyway.
|
|
3844
|
+
*/
|
|
3845
|
+
MIXED_DIRECTION: MIXED_DIRECTION_CODE,
|
|
3846
|
+
/**
|
|
3847
|
+
* Under `nodeIdFrom: "label"` or `"index"` the file's integer ids are not kept. It is listed in `report.lossy`, not
|
|
3848
|
+
* in `report.issues`.
|
|
3849
|
+
*/
|
|
3850
|
+
ID_DROPPED: ID_DROPPED_CODE
|
|
3851
|
+
});
|
|
3852
|
+
const GML_LOSS = Object.freeze({
|
|
3853
|
+
/** A json column holds numbers; GML records cannot keep int versus real. */
|
|
3854
|
+
RECORD_NUMBER_TYPE: RECORD_NUMBER_TYPE_CODE,
|
|
3855
|
+
/** A json column holds booleans, written 1 / 0. */
|
|
3856
|
+
RECORD_BOOLEAN: RECORD_BOOLEAN_CODE,
|
|
3857
|
+
/** A json column holds nulls, omitted. */
|
|
3858
|
+
RECORD_NULL: RECORD_NULL_CODE,
|
|
3859
|
+
/** An attribute holds an array inside an array, which GML cannot write; the save fails. */
|
|
3860
|
+
NESTED_ARRAY: NESTED_ARRAY_CODE,
|
|
3861
|
+
/** A json row that is an array, written as repeated keys. */
|
|
3862
|
+
JSON_ARRAY: JSON_ARRAY_CODE,
|
|
3863
|
+
/**
|
|
3864
|
+
* An attribute name GML cannot use as a key (keys are letters and digits, starting with a letter). The save fails
|
|
3865
|
+
* unless `sanitizeKeys` is "mangle".
|
|
3866
|
+
*/
|
|
3867
|
+
INVALID_KEY: INVALID_KEY_CODE,
|
|
3868
|
+
/** A column named like a structural key. */
|
|
3869
|
+
RESERVED_KEY: RESERVED_KEY_CODE,
|
|
3870
|
+
/** A key rewritten under sanitizeKeys "mangle", or a column renamed because its key would read back as a style value. */
|
|
3871
|
+
KEY_MANGLED: KEY_MANGLED_CODE,
|
|
3872
|
+
/** A position column with more than three components. */
|
|
3873
|
+
POSITION_COMPONENTS: POSITION_COMPONENTS_CODE,
|
|
3874
|
+
/** A graphics record whose x / y / z the position column overrides. */
|
|
3875
|
+
GRAPHICS_OVERRIDDEN: GRAPHICS_OVERRIDDEN_CODE,
|
|
3876
|
+
/** A graphics record that cannot hold the position. */
|
|
3877
|
+
GRAPHICS_CONFLICT: GRAPHICS_CONFLICT_CODE
|
|
3878
|
+
});
|
|
3
3879
|
export {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
3880
|
+
GML_ISSUE,
|
|
3881
|
+
GML_LOSS,
|
|
3882
|
+
GML_STYLE_BLOCK_KEY,
|
|
3883
|
+
GML_STYLE_CAPABILITIES,
|
|
3884
|
+
gmlExporter,
|
|
3885
|
+
gmlImporter
|
|
10
3886
|
};
|
|
11
3887
|
//# sourceMappingURL=gml.js.map
|