Skip to content

Commit

Permalink
Merge pull request #33 from kieler/32-duplicate-id-error-in-control-s…
Browse files Browse the repository at this point in the history
…tructure

sorting ports created duplicate ports if multiple edges are defined
  • Loading branch information
Drakae authored May 30, 2024
2 parents 3f46862 + 9c7cb88 commit f1cec56
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions extension/src-language-server/stpa/diagram/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ export function getAncestors(node: Node): (Node | Graph)[] {
export function sortPorts(nodes: CSNode[]): void {
for (const node of nodes) {
// sort the ports of the children
const children = node.children?.filter(child => child.type.startsWith("node")) as CSNode[] ?? [];
const children = (node.children?.filter(child => child.type.startsWith("node")) as CSNode[]) ?? [];
sortPorts(children);

// separate the ports from the other children
Expand All @@ -421,17 +421,23 @@ export function sortPorts(nodes: CSNode[]): void {

// sort the ports based on their associated edges
const newPorts: PastaPort[] = [];
for (const port of ports) {
for (let i = 0; i < ports.length; i++) {
const port = ports[i];
newPorts.push(port);
if (port.associatedEdge) {
for (const otherPort of ports) {
for (let j = i + 1; j < ports.length; j++) {
const otherPort = ports[j];
if (
port.associatedEdge.node1 === otherPort.associatedEdge?.node2 &&
port.associatedEdge.node2 === otherPort.associatedEdge.node1
(port.associatedEdge.node1 === otherPort.associatedEdge?.node2 &&
port.associatedEdge.node2 === otherPort.associatedEdge.node1) ||
// multiple edges between the same nodes may be defined
(port.associatedEdge.node1 === otherPort.associatedEdge?.node1 &&
port.associatedEdge.node2 === otherPort.associatedEdge.node2)
) {
// associated edges connect the same nodes but in the opposite direction -> add the other port to the list to group them together
newPorts.push(otherPort);
ports.splice(ports.indexOf(otherPort), 1);
ports.splice(j, 1);
j--;
}
}
}
Expand Down

0 comments on commit f1cec56

Please sign in to comment.