Skip to content

Commit

Permalink
Merge branch 'new/simulator-fixes' into new/add-setters-and-getters
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexIchenskiy authored Mar 20, 2024
2 parents d802e50 + 3b74263 commit 428f4d2
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 11 deletions.
54 changes: 45 additions & 9 deletions src/models/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@ import { getEdgeOffsets } from './topology';
import { IEntityState, EntityState } from '../utils/entity.utils';
import { IObserver, IObserverDataPayload, ISubject, Subject } from '../utils/observer.utils';
import { patchProperties } from '../utils/object.utils';
import { dedupArrays } from '../utils/array.utils';

export interface IGraphData<N extends INodeBase, E extends IEdgeBase> {
nodes: N[];
edges: E[];
}

export interface IGraphObjectsIds {
nodeIds: any[];
edgeIds: any[];
}

export type IEdgeFilter<N extends INodeBase, E extends IEdgeBase> = (edge: IEdge<N, E>) => boolean;

export type INodeFilter<N extends INodeBase, E extends IEdgeBase> = (node: INode<N, E>) => boolean;
Expand All @@ -35,7 +41,10 @@ export interface IGraph<N extends INodeBase, E extends IEdgeBase> extends ISubje
setup(data: Partial<IGraphData<N, E>>): void;
clearPositions(): void;
merge(data: Partial<IGraphData<N, E>>): void;
remove(data: Partial<{ nodeIds: number[]; edgeIds: number[] }>): void;
remove(data: Partial<IGraphObjectsIds>): void;
removeAll(): void;
removeAllNodes(): void;
removeAllEdges(): void;
isEqual<T extends INodeBase, K extends IEdgeBase>(graph: Graph<T, K>): boolean;
getBoundingBox(): IRectangle;
getNearestNode(point: IPosition): INode<N, E> | undefined;
Expand All @@ -49,7 +58,7 @@ export interface IGraphSettings<N extends INodeBase, E extends IEdgeBase> {
onLoadedImages?: () => void;
onSetupData?: (data: Partial<IGraphData<N, E>>) => void;
onMergeData?: (data: Partial<IGraphData<N, E>>) => void;
onRemoveData?: (data: Partial<{ nodeIds: number[]; edgeIds: number[] }>) => void;
onRemoveData?: (data: Partial<IGraphObjectsIds>) => void;
listeners?: IObserver[];
}

Expand Down Expand Up @@ -269,18 +278,41 @@ export class Graph<N extends INodeBase, E extends IEdgeBase> extends Subject imp
this._settings?.onMergeData?.(data);
}

// TODO(dlozic): Add delete all mechanic.
remove(data: Partial<{ nodeIds: number[]; edgeIds: number[] }>) {
remove(data: Partial<IGraphObjectsIds>) {
const nodeIds = data.nodeIds ?? [];
const edgeIds = data.edgeIds ?? [];

this._removeNodes(nodeIds);
this._removeEdges(edgeIds);
const removedNodesData = this._removeNodes(nodeIds);
const removedEdgesData = this._removeEdges(edgeIds);

this._applyEdgeOffsets();
this._applyStyle();

this._settings?.onRemoveData?.(data);
if (this._settings && this._settings.onRemoveData) {
const removedData: IGraphObjectsIds = {
nodeIds: dedupArrays(removedNodesData.nodeIds, removedEdgesData.nodeIds),
edgeIds: dedupArrays(removedNodesData.edgeIds, removedEdgesData.edgeIds),
};

this._settings.onRemoveData(removedData);
}
}

removeAll() {
const nodeIds = this._nodes.getAll().map((node) => node.id);
const edgeIds = this._edges.getAll().map((edge) => edge.id);

this.remove({ nodeIds, edgeIds });
}

removeAllEdges() {
const edgeIds = this._edges.getAll().map((edge) => edge.id);

this.remove({ edgeIds });
}

removeAllNodes() {
this.removeAll();
}

isEqual<T extends INodeBase, K extends IEdgeBase>(graph: Graph<T, K>): boolean {
Expand Down Expand Up @@ -527,7 +559,7 @@ export class Graph<N extends INodeBase, E extends IEdgeBase> extends Subject imp
this._edges.removeMany(removedEdgeIds);
}

private _removeNodes(nodeIds: any[]) {
private _removeNodes(nodeIds: any[]): IGraphObjectsIds {
const removedNodeIds: any[] = [];
const removedEdgeIds: any[] = [];

Expand All @@ -549,9 +581,11 @@ export class Graph<N extends INodeBase, E extends IEdgeBase> extends Subject imp
}
this._nodes.removeMany(removedNodeIds);
this._edges.removeMany(removedEdgeIds);

return { nodeIds: removedNodeIds, edgeIds: removedEdgeIds };
}

private _removeEdges(edgeIds: any[]) {
private _removeEdges(edgeIds: any[]): IGraphObjectsIds {
const removedEdgeIds: any[] = [];

for (let i = 0; i < edgeIds.length; i++) {
Expand All @@ -565,6 +599,8 @@ export class Graph<N extends INodeBase, E extends IEdgeBase> extends Subject imp
removedEdgeIds.push(edge.getId());
}
this._edges.removeMany(removedEdgeIds);

return { nodeIds: [], edgeIds: removedEdgeIds };
}

private _applyEdgeOffsets() {
Expand Down
4 changes: 2 additions & 2 deletions src/simulator/engine/d3-simulator-engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { Emitter } from '../../utils/emitter.utils';
import { isObjectEqual, copyObject } from '../../utils/object.utils';

const MANY_BODY_MAX_DISTANCE_TO_LINK_DISTANCE_RATIO = 100;
const DEFAULT_LINK_DISTANCE = 30;
const DEFAULT_LINK_DISTANCE = 50;

export enum D3SimulatorEngineEventType {
SIMULATION_START = 'simulation-start',
Expand Down Expand Up @@ -115,7 +115,7 @@ export const DEFAULT_SETTINGS: ID3SimulatorEngineSettings = {
},
links: {
distance: DEFAULT_LINK_DISTANCE,
strength: undefined,
strength: 1,
iterations: 1,
},
manyBody: {
Expand Down
5 changes: 5 additions & 0 deletions src/utils/array.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,8 @@ export const copyArray = <T>(array: Array<T>): Array<T> => {
}
return newArray;
};

export const dedupArrays = <T>(...arrays: T[][]): T[] => {
const combinedArray = arrays.reduce((acc, curr) => acc.concat(curr), []);
return Array.from(new Set(combinedArray));
};

0 comments on commit 428f4d2

Please sign in to comment.