Skip to content

Commit

Permalink
Ensure only one vedge connects a combo to a node
Browse files Browse the repository at this point in the history
  • Loading branch information
Blakko committed Nov 7, 2024
1 parent 1baa161 commit 05520b7
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
6 changes: 6 additions & 0 deletions packages/core/src/graph/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2781,6 +2781,12 @@ export default abstract class AbstractGraph extends EventEmitter implements IAbs
if (addedVEdgeMap[key]) {
addedVEdgeMap[key].size += size;
return;
} else {
const inverseKey = `${vEdgeInfo.target}-${vEdgeInfo.source}`;
if (addedVEdgeMap[inverseKey]) {
addedVEdgeMap[inverseKey].size += size;
return;
}
}
addedVEdgeMap[key] = vEdgeInfo;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/util/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export const dataValidation = (data?: GraphData | TreeGraphData): boolean => {
// 3. 边的 source 和 target 必须存在于节点 或 Combo中
const ids = new Set<string>();
((nodes as NodeConfig[]) || []).forEach(node => ids.add(node.id));
((nodes as ComboConfig[]) || []).forEach(combo => ids.add(combo.id));
((combos as ComboConfig[]) || []).forEach(combo => ids.add(combo.id));
const nonEdges = ((edges as EdgeConfig[]) || []).find(function (edge) {
return !ids.has(edge.source) || !ids.has(edge.target);
});
Expand Down
27 changes: 25 additions & 2 deletions packages/core/tests/unit/graph/graph-combo-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,14 @@ const data = {
describe('graph with combo', () => {
let graph: Graph;

function makeGraph(config = {}) {
function makeGraph(config = {}, forceData = undefined) {
graph = new Graph({
container: div,
width: 500,
height: 600,
...config
});
graph.read(clone(data));
graph.read(clone(forceData || data));
}

it('createCombo', () => {
Expand Down Expand Up @@ -329,6 +329,29 @@ describe('graph with combo', () => {
graph.destroy();
});

it('collapse combo should create only one edge', () => {
// this data reproduces the issue where one combo was connected to a node with 2 vedges
const data = {
nodes: [
{ id: "node1", x: 250, y: 150, comboId: "combo" },
{ id: "node2", x: 350, y: 150, comboId: "combo" },
{ id: "node3", x: 250, y: 300 },
],
combos: [{ id: "combo", label: "Combo" }],
edges: [
{ id: "edge1", source: "node1", target: "node3" },
{ id: "edge2", target: "node2", source: "node3" },
],
};

makeGraph({}, data);

graph.collapseCombo('combo');

const edges = graph.get('vedges');
expect(edges).toHaveLength(1);
});

it('combo mapper', () => {
makeGraph({ groupByTypes: false });

Expand Down

0 comments on commit 05520b7

Please sign in to comment.