Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 | 5x 90x 5x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 428x 428x 73x 355x 33x 14x 14x 19x 1x 18x 33x 18x 18x 18x 18x 18x 33x 17x 87x 33x 31x 31x 106x 106x 12x 106x 26x 26x 26x 26x 80x 80x 80x 145x 80x 80x 80x 80x 31x 81x 77x 4x 15x 15x 1x 33x 14x 14x 19x 4x 4x 2x 2x 2x 2x 2x 1x 1x 1x 1x 2x 2x 2x 2x 2x 3x 3x 3x 1x 1x 4x 14x 50x 118x 115x 115x 4x 4x 6x 90x 90x 2x 88x 40x 40x 20x 5x 15x 13x 2x 20x 20x 6x 24x 24x 24x 8x 8x 8x 6x 6x 6x 97x 94x 94x 16x 16x 12x 4x 8x 2x 4x 2x 2x 2x 4x 4x 4x 2x 18x 13x 5x 12x 3x 9x 2x 2x 2x 2x | import { Graph } from './graph';
import {
Edge,
GraphChangedEvent,
GraphViewOptions,
ID,
Node,
PlainObject,
} from './types';
import { doBFS, doDFS } from './utils/traverse';
const defaultFilter = () => true;
export class GraphView<N extends PlainObject, E extends PlainObject> {
private graph: Graph<N, E>;
private nodeFilter: (node: Node<N>) => boolean;
private edgeFilter: (edge: Edge<E>) => boolean;
// caches
public cacheEnabled: boolean;
private inEdgesMap: Map<ID, Array<Edge<E>>> = new Map();
private outEdgesMap: Map<ID, Array<Edge<E>>> = new Map();
private bothEdgesMap: Map<ID, Array<Edge<E>>> = new Map();
private allNodesMap: Map<ID, Node<N>> = new Map();
private allEdgesMap: Map<ID, Edge<E>> = new Map();
constructor(options: GraphViewOptions<N, E>) {
this.graph = options.graph;
const nodeFilter = options.nodeFilter || defaultFilter;
const edgeFilter = options.edgeFilter || defaultFilter;
this.nodeFilter = nodeFilter;
this.edgeFilter = (edge) => {
const { source, target } = this.graph.getEdgeDetail(edge.id);
if (!nodeFilter(source) || !nodeFilter(target)) {
return false;
}
return edgeFilter(edge, source, target);
};
if (options.cache === 'auto') {
this.cacheEnabled = true;
this.startAutoCache();
} else if (options.cache === 'manual') {
this.cacheEnabled = true;
} else {
this.cacheEnabled = false;
}
}
/**
* Clear all cache data. Therefore `getAllNodes()` will return `[]`.
* If you want to disable caching, use `graphView.cacheEnabled = false` instead.
*/
public clearCache = (): void => {
this.inEdgesMap.clear();
this.outEdgesMap.clear();
this.bothEdgesMap.clear();
this.allNodesMap.clear();
this.allEdgesMap.clear();
};
/**
* Fully refresh all cache data to the current graph state.
*/
public refreshCache = (): void => {
this.clearCache();
this.updateCache(this.graph.getAllNodes().map((node) => node.id));
};
/**
* Instead of a fully refreshment, this method partially update the cache data by specifying
* involved(added, removed, updated) nodes. It's more efficient when handling small changes
* on a large graph.
*/
public updateCache = (involvedNodeIds: Set<ID> | Array<ID>): void => {
const involvedEdgeIds = new Set<ID>();
involvedNodeIds.forEach((id) => {
// Collect all involved old edges.
const oldEdgesSet = this.bothEdgesMap.get(id);
if (oldEdgesSet) {
oldEdgesSet.forEach((edge) => involvedEdgeIds.add(edge.id));
}
if (!this.hasNode(id)) {
// When an involved node becomes unvisitable:
// 1. Delete its related edges cache.
this.inEdgesMap.delete(id);
this.outEdgesMap.delete(id);
this.bothEdgesMap.delete(id);
// 2. Delete it from the allNodesMap.
this.allNodesMap.delete(id);
} else {
// When an involved node becomes or stays visitable:
// 1. Collect its new edges.
const inEdges = this.graph
.getRelatedEdges(id, 'in')
.filter(this.edgeFilter);
const outEdges = this.graph
.getRelatedEdges(id, 'out')
.filter(this.edgeFilter);
const bothEdges = Array.from(new Set([...inEdges, ...outEdges]));
bothEdges.forEach((edge) => involvedEdgeIds.add(edge.id));
// 2. Update its related edges cache.
this.inEdgesMap.set(id, inEdges);
this.outEdgesMap.set(id, outEdges);
this.bothEdgesMap.set(id, bothEdges);
// 3. Add to allNodesMap.
this.allNodesMap.set(id, this.graph.getNode(id));
}
});
// Update allEdgesMap.
involvedEdgeIds.forEach((id) => {
if (this.hasEdge(id)) {
this.allEdgesMap.set(id, this.graph.getEdge(id));
} else {
this.allEdgesMap.delete(id);
}
});
};
public startAutoCache() {
this.refreshCache();
this.graph.on('changed', this.handleGraphChanged);
}
public stopAutoCache() {
this.graph.off('changed', this.handleGraphChanged);
}
private handleGraphChanged = (event: GraphChangedEvent<N, E>) => {
// Collect all involved nodes.
const involvedNodeIds: Set<ID> = new Set();
event.changes.forEach((change) => {
switch (change.type) {
case 'NodeAdded':
involvedNodeIds.add(change.value.id);
break;
case 'NodeDataUpdated':
involvedNodeIds.add(change.id);
break;
case 'EdgeAdded':
involvedNodeIds.add(change.value.source);
involvedNodeIds.add(change.value.target);
break;
case 'EdgeUpdated':
if (
change.propertyName === 'source' ||
change.propertyName === 'target'
) {
involvedNodeIds.add(change.oldValue);
involvedNodeIds.add(change.newValue);
}
break;
case 'EdgeDataUpdated':
if (event.graph.hasEdge(change.id)) {
const edge = event.graph.getEdge(change.id);
involvedNodeIds.add(edge.source);
involvedNodeIds.add(edge.target);
}
break;
case 'EdgeRemoved':
involvedNodeIds.add(change.value.source);
involvedNodeIds.add(change.value.target);
break;
case 'NodeRemoved':
involvedNodeIds.add(change.value.id);
break;
default:
break;
}
});
// Update their caches.
this.updateCache(involvedNodeIds);
};
// ================= Node =================
private checkNodeExistence(id: ID): void {
this.getNode(id);
}
hasNode(id: ID): boolean {
if (!this.graph.hasNode(id)) return false;
const node = this.graph.getNode(id);
return this.nodeFilter(node);
}
public areNeighbors(firstNodeId: ID, secondNodeId: ID): boolean {
this.checkNodeExistence(firstNodeId);
return this.getNeighbors(secondNodeId).some(
(neighbor) => neighbor.id === firstNodeId,
);
}
public getNode(id: ID): Node<N> {
const node = this.graph.getNode(id);
if (!this.nodeFilter(node)) {
throw new Error('Node not found for id: ' + id);
}
return node;
}
public getRelatedEdges(id: ID, direction?: 'in' | 'out' | 'both'): Edge<E>[] {
this.checkNodeExistence(id);
if (this.cacheEnabled) {
if (direction === 'in') {
return this.inEdgesMap.get(id)!;
} else if (direction === 'out') {
return this.outEdgesMap.get(id)!;
} else {
return this.bothEdgesMap.get(id)!;
}
}
const edges = this.graph.getRelatedEdges(id, direction);
return edges.filter(this.edgeFilter);
}
public getDegree(id: ID, direction?: 'in' | 'out' | 'both'): number {
return this.getRelatedEdges(id, direction).length;
}
public getSuccessors(id: ID): Node<N>[] {
const outEdges = this.getRelatedEdges(id, 'out');
const targets = outEdges.map((edge) => this.getNode(edge.target));
return Array.from(new Set(targets));
}
public getPredecessors(id: ID): Node<N>[] {
const inEdges = this.getRelatedEdges(id, 'in');
const sources = inEdges.map((edge) => this.getNode(edge.source));
return Array.from(new Set(sources));
}
public getNeighbors(id: ID): Node<N>[] {
const predecessors = this.getPredecessors(id);
const successors = this.getSuccessors(id);
return Array.from(new Set([...predecessors, ...successors]));
}
// ================= Edge =================
public hasEdge(id: ID): boolean {
if (!this.graph.hasEdge(id)) return false;
const edge = this.graph.getEdge(id);
return this.edgeFilter(edge);
}
public getEdge(id: ID): Edge<E> {
const edge = this.graph.getEdge(id);
if (!this.edgeFilter(edge)) {
throw new Error('Edge not found for id: ' + id);
}
return edge;
}
public getEdgeDetail(id: ID): {
edge: Edge<E>;
source: Node<N>;
target: Node<N>;
} {
const edge = this.getEdge(id);
return {
edge,
source: this.getNode(edge.source),
target: this.getNode(edge.target),
};
}
// ================= Tree =================
public hasTreeStructure(treeKey: string | undefined): boolean {
return this.graph.hasTreeStructure(treeKey);
}
public getRoots(treeKey?: string): Node<N>[] {
return this.graph.getRoots(treeKey).filter(this.nodeFilter);
}
public getChildren(id: ID, treeKey?: string): Node<N>[] {
this.checkNodeExistence(id);
return this.graph.getChildren(id, treeKey).filter(this.nodeFilter);
}
public getParent(id: ID, treeKey?: string): Node<N> | null {
this.checkNodeExistence(id);
const parent = this.graph.getParent(id, treeKey);
if (!parent || !this.nodeFilter(parent)) return null;
return parent;
}
// ================= Graph =================
public getAllNodes(): Node<N>[] {
if (this.cacheEnabled) {
return Array.from(this.allNodesMap.values());
}
return this.graph.getAllNodes().filter(this.nodeFilter);
}
public getAllEdges(): Edge<E>[] {
if (this.cacheEnabled) {
return Array.from(this.allEdgesMap.values());
}
return this.graph.getAllEdges().filter(this.edgeFilter);
}
public bfs(
id: ID,
fn: (node: Node<N>) => void,
direction: 'in' | 'out' | 'both' = 'out',
): void {
const navigator = {
in: this.getPredecessors.bind(this),
out: this.getSuccessors.bind(this),
both: this.getNeighbors.bind(this),
}[direction];
doBFS([this.getNode(id)], new Set(), fn, navigator);
}
public dfs(
id: ID,
fn: (node: Node<N>) => void,
direction: 'in' | 'out' | 'both' = 'out',
): void {
const navigator = {
in: this.getPredecessors.bind(this),
out: this.getSuccessors.bind(this),
both: this.getNeighbors.bind(this),
}[direction];
doDFS(this.getNode(id), new Set(), fn, navigator);
}
}
|