blob: b511886a0474f73a57cb7a673cd215faa6e874b7 [file] [log] [blame]
Sean Condon0c577f62018-11-18 22:40:05 +00001/*
2 * Copyright 2018-present Open Networking Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the 'License');
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an 'AS IS' BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16import { EventEmitter } from '@angular/core';
17import { Link } from './link';
18import { Node } from './node';
Sean Condon50855cf2018-12-23 15:37:42 +000019import * as d3 from 'd3-force';
20import {LogService} from 'gui2-fw-lib';
Sean Condon0c577f62018-11-18 22:40:05 +000021
22const FORCES = {
23 LINKS: 1 / 50,
24 COLLISION: 1,
Sean Condon50855cf2018-12-23 15:37:42 +000025 GRAVITY: 0.4,
26 FRICTION: 0.7
27};
28
29const CHARGES = {
30 device: -80,
31 host: -200,
32 region: -80,
33 _def_: -120
34};
35
36const LINK_DISTANCE = {
37 // note: key is link.type
38 direct: 100,
39 optical: 120,
40 UiEdgeLink: 100,
41 _def_: 50,
42};
43
44const LINK_STRENGTH = {
45 // note: key is link.type
46 // range: {0.0 ... 1.0}
47 _def_: 0.1
Sean Condon0c577f62018-11-18 22:40:05 +000048};
49
50export interface Options {
51 width: number;
52 height: number;
53}
54
55export class ForceDirectedGraph {
56 public ticker: EventEmitter<d3.Simulation<Node, Link>> = new EventEmitter();
57 public simulation: d3.Simulation<any, any>;
58
59 public nodes: Node[] = [];
60 public links: Link[] = [];
61
Sean Condon50855cf2018-12-23 15:37:42 +000062 constructor(options: Options, public log: LogService) {
Sean Condon0c577f62018-11-18 22:40:05 +000063 this.initSimulation(options);
64 }
65
66 initNodes() {
67 if (!this.simulation) {
68 throw new Error('simulation was not initialized yet');
69 }
70
71 this.simulation.nodes(this.nodes);
72 }
73
74 initLinks() {
75 if (!this.simulation) {
76 throw new Error('simulation was not initialized yet');
77 }
78
79 // Initializing the links force simulation
80 this.simulation.force('links',
81 d3.forceLink(this.links)
Sean Condon50855cf2018-12-23 15:37:42 +000082 .strength(this.strength.bind(this))
83 .distance(this.distance.bind(this))
Sean Condon0c577f62018-11-18 22:40:05 +000084 );
85 }
86
Sean Condon50855cf2018-12-23 15:37:42 +000087 charges(node) {
88 const nodeType = node.nodeType;
89 return CHARGES[nodeType] || CHARGES._def_;
90 }
91
92 distance(node) {
93 const nodeType = node.nodeType;
94 return LINK_DISTANCE[nodeType] || LINK_DISTANCE._def_;
95 }
96
97 strength(node) {
98 const nodeType = node.nodeType;
99 return LINK_STRENGTH[nodeType] || LINK_STRENGTH._def_;
100 }
101
Sean Condon0c577f62018-11-18 22:40:05 +0000102 initSimulation(options: Options) {
103 if (!options || !options.width || !options.height) {
104 throw new Error('missing options when initializing simulation');
105 }
106
107 /** Creating the simulation */
108 if (!this.simulation) {
109 const ticker = this.ticker;
110
111 // Creating the force simulation and defining the charges
112 this.simulation = d3.forceSimulation()
113 .force('charge',
Sean Condon50855cf2018-12-23 15:37:42 +0000114 d3.forceManyBody().strength(this.charges.bind(this)))
115 // .distanceMin(100).distanceMax(500))
116 .force('gravity',
117 d3.forceManyBody().strength(FORCES.GRAVITY))
118 .force('friction',
119 d3.forceManyBody().strength(FORCES.FRICTION));
Sean Condon0c577f62018-11-18 22:40:05 +0000120
121 // Connecting the d3 ticker to an angular event emitter
122 this.simulation.on('tick', function () {
123 ticker.emit(this);
124 });
125
126 this.initNodes();
Sean Condon50855cf2018-12-23 15:37:42 +0000127 // this.initLinks();
Sean Condon0c577f62018-11-18 22:40:05 +0000128 }
129
130 /** Updating the central force of the simulation */
131 this.simulation.force('centers', d3.forceCenter(options.width / 2, options.height / 2));
132
133 /** Restarting the simulation internal timer */
134 this.simulation.restart();
135 }
136
137 stopSimulation() {
138 this.simulation.stop();
Sean Condon50855cf2018-12-23 15:37:42 +0000139 this.log.debug('Simulation stopped');
140 }
141
142 restartSimulation() {
143 this.simulation.restart();
144 this.log.debug('Simulation restarted');
Sean Condon0c577f62018-11-18 22:40:05 +0000145 }
146}