forked from UniAgent-Platform/bispace-viewer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBigraphGridXMLParser.js
More file actions
276 lines (242 loc) · 11.5 KB
/
Copy pathBigraphGridXMLParser.js
File metadata and controls
276 lines (242 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/**
* *********************************************************************************************************************
* Bigraph XMI Parser Functions
* *********************************************************************************************************************
*/
/**
* Parse a bigraph XMI file (i.e., a bigrid) containing potentially multiple roots.
*
* This function handles multi-root XMI models, where each root may
* represent a distinct product of BiGridFactory elements. The XML is
* parsed and normalized into a common data structure used by the
* rendering pipeline.
*
* The argument `coordinatesAsLinks` indicates whether to use the outer names or CO-typed nodes to extract the
* coordinates encoded in a Locale.
*
* @param {string} xmlString XMI source as a string.
* @param {boolean} [coordinatesAsLinks=true] - Interpret coordinates as link edges.
* @returns {Object} Parsed bigraph model (cells, links).
*/
export function parseBigraphXML(xmlString, coordinatesAsLinks = true) {
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(xmlString, "application/xml");
const nsResolver = (prefix) => {
const ns = {
bigraphBaseModel: "http://org.bigraphs.model",
xmi: "http://www.omg.org/XMI",
xsi: "http://www.w3.org/2001/XMLSchema-instance"
};
return ns[prefix] || null;
};
// XPath to select all <bRoots> elements (note: they are unprefixed)
const xpathExpr = "//*[local-name()='bRoots']";
const bRootsResult = xmlDoc.evaluate(xpathExpr, xmlDoc, nsResolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
console.log("bRootsResult", bRootsResult)
const outerNamesResult = xmlDoc.evaluate("//*[local-name()='bOuterNames']", xmlDoc, nsResolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
console.log("outerNamesResult", outerNamesResult);
const outerNames = [];
for (let i = 0; i < outerNamesResult.snapshotLength; i++) {
const outerNameElement = outerNamesResult.snapshotItem(i);
const name = outerNameElement.getAttribute("name");
outerNames.push(name);
}
console.log("outerNames", outerNames);
const cells = [];
let localeRootIndex = 0;
// Now extract each bChild of type "bigraphBaseModel:Locale" and its corresponding bPorts
for (let i = 0; i < bRootsResult.snapshotLength; i++) {
const bRoot = bRootsResult.snapshotItem(i);
// Find all bChild elements inside the current bRoot
const bChildren = bRoot.getElementsByTagNameNS("", "bChild");
const bChild = bChildren[0];
// Check if this bChild is a Locale type (xsi:type="bigraphBaseModel:Locale")
const xsiType = bChild.getAttribute("xsi:type");
if (xsiType && xsiType.includes("bigraphBaseModel:Locale")) {
const locName = bChild.getAttribute("name");
// const res = extractLocalePortPoint(bChild, outerNames, /* preferredIndex: */ 4, parseParamControl);
// if (res) {
// // console.log(
// // `Locale: ${locName}, OuterName[${res.index}]="${res.outerName}", portIndex=${res.portIndex}, bLink="${res.bLink}", X,Y=[${res.point.x} | ${res.point.y}]`
// // );
// cells.push({index: res.index, locale: locName, point: res.point});
// }
// use outernames to retrieve coordinates
if (coordinatesAsLinks === true) {
// Now find all bPorts for this Locale
const res = extractLocalePortPoint(bChild, outerNames, /* preferredIndex: */ 4, parseParamControl);
if (res) {
// console.log(
// `Locale: ${locName}, OuterName[${res.index}]="${res.outerName}", portIndex=${res.portIndex}, bLink="${res.bLink}", X,Y=[${res.point.x} | ${res.point.y}]`
// );
cells.push({index: res.index, locale: locName, point: res.point});
}
}
const bCoordChild = bChild.getElementsByTagNameNS("", "bChild");
for (let i = 0; i < bCoordChild.length; i++) {
const bChildCoord = bCoordChild[i];
const xsiTypePorts = bChildCoord.getAttribute("xsi:type");
if (coordinatesAsLinks === false) {
if (xsiTypePorts && xsiTypePorts.includes("bigraphBaseModel:CO")) {
const coordNode = bChildCoord.getElementsByTagNameNS("", "bChild")[0];
const result = coordNode.getAttribute("xsi:type").split(":")[1];
const point = parseParamControl(result);
cells.push({
index: localeRootIndex++,
locale: locName,
point: point
});
}
}
}
}
}
const linkMap = new Map();
//TODO
return {cells, linkMap};
}
/**
*This parser handles a bigrid XMI encoding that has only a single root.
*
* It extracts cells and link information required by the rendering pipeline.
*
* The argument `coordinatesAsLinks` indicates whether to use the outer names or CO-typed nodes to extract the
* coordinates encoded in a Locale.
*
* @param {string} xmlString - The XML source to parse.
* @param {boolean} [coordinatesAsLinks=false] - Interpret coordinates as link edges.
* @returns {{ cells: Array, linkMap: Object }} Parsed cell data and link structure
*/
export function parseBigraphXML_singleBRoot(xmlString, coordinatesAsLinks = false) {
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(xmlString, "application/xml");
const nsResolver = (prefix) => {
const ns = {
bigraphBaseModel: "http://org.bigraphs.model",
xmi: "http://www.omg.org/XMI",
xsi: "http://www.w3.org/2001/XMLSchema-instance"
};
return ns[prefix] || null;
};
// XPath to select all <bRoots> elements (note: they are unprefixed)
const xpathExpr = "//*[local-name()='bRoots']";
const bRootsResult = xmlDoc.evaluate(xpathExpr, xmlDoc, nsResolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
console.log("bRootsResult=", bRootsResult)
const outerNamesResult = xmlDoc.evaluate("//*[local-name()='bOuterNames']", xmlDoc, nsResolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
console.log("outerNamesResult:", outerNamesResult);
const outerNames = [];
for (let i = 0; i < outerNamesResult.snapshotLength; i++) {
const outerNameElement = outerNamesResult.snapshotItem(i);
const name = outerNameElement.getAttribute("name");
outerNames.push(name); // Store the name
}
console.log("outerNames:", outerNames);
const cells = [];
const bRoot = bRootsResult.snapshotItem(0);
console.log("Single bRoot", bRoot);
// Find all bChild elements inside the current bRoot
const bChildren = bRoot.getElementsByTagNameNS("", "bChild");
let localRootIndex = 0;
for (let ix = 0; ix < bChildren.length; ix++) {
const bChild = bChildren[ix];
// Check if this bChild is a Locale type (xsi:type="bigraphBaseModel:Locale")
const xsiType = bChild.getAttribute("xsi:type");
if (xsiType && xsiType.includes("bigraphBaseModel:Locale")) {
const locName = bChild.getAttribute("name");
const bCoordChild = bChild.getElementsByTagNameNS("", "bChild");
// use outer names to retrieve coordinates
if (coordinatesAsLinks === true) {
// Now find all bPorts for this Locale
const res = extractLocalePortPoint(bChild, outerNames, /* preferredIndex: */ 4, parseParamControl);
if (res) {
// console.log(
// `Locale: ${locName}, OuterName[${res.index}]="${res.outerName}", portIndex=${res.portIndex}, bLink="${res.bLink}", X,Y=[${res.point.x} | ${res.point.y}]`
// );
cells.push({index: res.index, locale: locName, point: res.point});
}
}
for (let i = 0; i < bCoordChild.length; i++) {
const bChildCoord = bCoordChild[i];
const xsiTypePorts = bChildCoord.getAttribute("xsi:type");
if (coordinatesAsLinks === false) {
if (xsiTypePorts && xsiTypePorts.includes("bigraphBaseModel:CO")) {
const coordNode = bChildCoord.getElementsByTagNameNS("", "bChild")[0];
const result = coordNode.getAttribute("xsi:type").split(":")[1];
const point = parseParamControl(result);
cells.push({
index: localRootIndex++,
locale: locName,
point: point
});
}
}
}
}
}
const linkMap = new Map();
//TODO
return {cells, linkMap};
}
/**
* *********************************************************************************************************************
* Helper Functions
* *********************************************************************************************************************
*/
/**
* Extract outer-name index and point from a Locale's <bPorts>.
* Falls back gracefully if the preferred index is missing.
*
* @param {Element} bChild - the <bChild xsi:type="bigraphBaseModel:Locale"> element
* @param {string[]} outerNames - array of outer names (by index)
* @param {number|null} preferredIndex - optional preferred port index (e.g. 4)
* @param {(name:string)=>{x:number,y:number}} parseParamControl - your existing parser
* @returns {{index:number, point:{x:number,y:number}, outerName:string, bLink:string, portIndex:number} | null}
*/
function extractLocalePortPoint(bChild, outerNames, preferredIndex = null, parseParamControl) {
const bPorts = bChild.getElementsByTagNameNS("", "bPorts");
if (!bPorts || bPorts.length === 0) return null;
// try preferred index if given, else first with a valid bLink
let chosen = null;
if (preferredIndex != null && bPorts[preferredIndex]) {
chosen = bPorts[preferredIndex];
} else {
chosen = Array.from(bPorts).find(p => p.hasAttribute("bLink"));
}
if (!chosen) return null;
const bLink = chosen.getAttribute("bLink") || "";
const match = /bOuterNames\.(\d+)/.exec(bLink);
if (!match) return null;
const idx = Number(match[1]);
const outerName = outerNames[idx];
if (outerName == null) return null;
const point = parseParamControl(outerName);
return {
index: idx,
point,
outerName,
bLink,
portIndex: Array.prototype.indexOf.call(bPorts, chosen)
};
}
function parseParamControl(formattedString) {
if (
typeof formattedString !== "string" ||
!formattedString.startsWith("C_") ||
!formattedString.includes("__")
) {
throw new Error("Invalid format");
}
try {
const parts = formattedString.substring(2).split("__");
const xString = parts[0].replace(/N/g, "-").replace(/_/g, ".");
const yString = parts[1].replace(/N/g, "-").replace(/_/g, ".");
const x = parseFloat(xString);
const y = parseFloat(yString);
if (isNaN(x) || isNaN(y)) {
throw new Error("Invalid number format");
}
return {x, y}; // similar to Point2D.Float in structure
} catch (e) {
throw new Error("Invalid format: " + e.message);
}
}