Skip to content

Commit

Permalink
CELE-32 style: Lint graphRendering code
Browse files Browse the repository at this point in the history
  • Loading branch information
afonsobspinto committed Jul 19, 2024
1 parent 8369397 commit ba77ed4
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 66 deletions.
98 changes: 50 additions & 48 deletions applications/visualizer/frontend/src/helpers/twoD/graphRendering.ts
Original file line number Diff line number Diff line change
@@ -1,58 +1,60 @@
// src/helpers/twoD/graphDiffUtils.ts
import type { Core } from "cytoscape";
import { createEdge, createNode } from "./twoDHelpers";

export const computeGraphDifferences = (cy: Core, connections: any[], workspace: any) => {
const currentNodes = new Set(cy.nodes().map((node) => node.id()));
const currentEdges = new Set(cy.edges().map((edge) => edge.id()));

const newNodes = new Set<string>();
const newEdges = new Set<string>();

const nodesToAdd: any[] = [];
const nodesToRemove: any[] = [];
const edgesToAdd: any[] = [];
const edgesToRemove: any[] = [];

// Compute new nodes and edges based on the current connections and workspace state
const filteredActiveNeurons = Array.from(workspace.activeNeurons).filter((neuronId: string) => {
const neuron = workspace.availableNeurons[neuronId];
if (!neuron) {
return false;
import type { Core, ElementDefinition, CollectionReturnValue } from 'cytoscape';
import { createEdge, createNode } from './twoDHelpers';
import {Connection} from "../../rest";
import {Workspace} from "../../models";

export const computeGraphDifferences = (cy: Core, connections: Connection[], workspace: Workspace) => {
const currentNodes = new Set(cy.nodes().map(node => node.id()));
const currentEdges = new Set(cy.edges().map(edge => edge.id()));

const newNodes = new Set<string>();
const newEdges = new Set<string>();

const nodesToAdd: ElementDefinition[] = [];
const nodesToRemove: CollectionReturnValue = cy.collection();
const edgesToAdd: ElementDefinition[] = [];
const edgesToRemove: CollectionReturnValue = cy.collection();

// Compute new nodes and edges based on the current connections and workspace state
const filteredActiveNeurons = Array.from(workspace.activeNeurons).filter((neuronId: string) => {
const neuron = workspace.availableNeurons[neuronId];
if (!neuron) {
return false;
}
const nclass = neuron.nclass;
if (neuronId === nclass) {
return true;
}
return !(workspace.activeNeurons.has(neuronId) && workspace.activeNeurons.has(nclass));
});

for (const nodeId of filteredActiveNeurons) {
newNodes.add(nodeId);
if (!currentNodes.has(nodeId)) {
nodesToAdd.push(createNode(nodeId, workspace.selectedNeurons.has(nodeId)));
}
}
const nclass = neuron.nclass;
if (neuronId === nclass) {
return true;
}
return !(workspace.activeNeurons.has(neuronId) && workspace.activeNeurons.has(nclass));
});

filteredActiveNeurons.forEach((nodeId: string) => {
newNodes.add(nodeId);
if (!currentNodes.has(nodeId)) {
nodesToAdd.push(createNode(nodeId, workspace.selectedNeurons.has(nodeId)));
}
});

currentNodes.forEach((nodeId) => {
if (!newNodes.has(nodeId)) {
nodesToRemove.push(cy.getElementById(nodeId));
for (const nodeId of currentNodes) {
if (!newNodes.has(nodeId)) {
nodesToRemove.merge(cy.getElementById(nodeId));
}
}
});

connections.forEach((conn) => {
const edgeId = `${conn.pre}-${conn.post}`;
newEdges.add(edgeId);
if (!currentEdges.has(edgeId)) {
edgesToAdd.push(createEdge(conn));
for (const conn of connections) {
const edgeId = `${conn.pre}-${conn.post}`;
newEdges.add(edgeId);
if (!currentEdges.has(edgeId)) {
edgesToAdd.push(createEdge(conn));
}
}
});

currentEdges.forEach((edgeId) => {
if (!newEdges.has(edgeId)) {
edgesToRemove.push(cy.getElementById(edgeId));
for (const edgeId of currentEdges) {
if (!newEdges.has(edgeId)) {
edgesToRemove.merge(cy.getElementById(edgeId));
}
}
});

return { nodesToAdd, nodesToRemove: cy.collection(nodesToRemove), edgesToAdd, edgesToRemove: cy.collection(edgesToRemove) };
return { nodesToAdd, nodesToRemove, edgesToAdd, edgesToRemove };
};
17 changes: 9 additions & 8 deletions applications/visualizer/frontend/src/helpers/twoD/twoDHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { Core } from "cytoscape";
import type {Core, ElementDefinition} from "cytoscape";
import type { Connection } from "../../rest";
import type { Workspace } from "../../models/workspace.ts";

export const createEdge = (conn: Connection) => {
export const createEdge = (conn: Connection): ElementDefinition => {
return {
group: "edges",
data: {
Expand All @@ -15,14 +15,15 @@ export const createEdge = (conn: Connection) => {
};
};

export const createNode = (nodeId: string, selected: boolean) => {
return {
group: "nodes",
data: { id: nodeId, label: nodeId },
classes: selected ? "selected" : "",
};
export const createNode = (nodeId: string, selected: boolean): ElementDefinition => {
return {
group: 'nodes',
data: { id: nodeId, label: nodeId },
classes: selected ? 'selected' : ''
};
};


export function applyLayout(cyRef: React.MutableRefObject<Core | null>, layout: string) {
if (cyRef.current) {
cyRef.current
Expand Down
16 changes: 6 additions & 10 deletions applications/visualizer/frontend/src/models/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,21 +161,17 @@ export class Workspace {
const uniqueNeurons = new Set<Neuron>();

// Flatten and deduplicate neurons
neuronArrays.flat().forEach((neuron) => {
uniqueNeurons.add(neuron);
// Add class neuron as well
const classNeuron = { ...neuron, name: neuron.nclass };
for (const neuronArray of neuronArrays.flat()) {
uniqueNeurons.add(neuronArray);
const classNeuron = { ...neuronArray, name: neuronArray.nclass };
uniqueNeurons.add(classNeuron);
});
}

return produce(updatedWorkspace, (draft: Workspace) => {
// Reset the availableNeurons map
draft.availableNeurons = {};

// Populate availableNeurons with unique neurons
uniqueNeurons.forEach((neuron) => {
for (const neuron of uniqueNeurons) {
draft.availableNeurons[neuron.name] = neuron;
});
}
});
} catch (error) {
console.error("Failed to fetch neurons:", error);
Expand Down

0 comments on commit ba77ed4

Please sign in to comment.