Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | import { EventEmitter } from '@angular/core'; |
| 17 | import { Link } from './link'; |
| 18 | import { Node } from './node'; |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 19 | import * as d3 from 'd3-force'; |
| 20 | import {LogService} from 'gui2-fw-lib'; |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 21 | |
| 22 | const FORCES = { |
| 23 | LINKS: 1 / 50, |
| 24 | COLLISION: 1, |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 25 | GRAVITY: 0.4, |
| 26 | FRICTION: 0.7 |
| 27 | }; |
| 28 | |
| 29 | const CHARGES = { |
| 30 | device: -80, |
| 31 | host: -200, |
| 32 | region: -80, |
| 33 | _def_: -120 |
| 34 | }; |
| 35 | |
| 36 | const LINK_DISTANCE = { |
| 37 | // note: key is link.type |
| 38 | direct: 100, |
| 39 | optical: 120, |
| 40 | UiEdgeLink: 100, |
| 41 | _def_: 50, |
| 42 | }; |
| 43 | |
| 44 | const LINK_STRENGTH = { |
| 45 | // note: key is link.type |
| 46 | // range: {0.0 ... 1.0} |
| 47 | _def_: 0.1 |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | export interface Options { |
| 51 | width: number; |
| 52 | height: number; |
| 53 | } |
| 54 | |
| 55 | export 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 Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 62 | constructor(options: Options, public log: LogService) { |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 63 | 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 Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 82 | .strength(this.strength.bind(this)) |
| 83 | .distance(this.distance.bind(this)) |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 84 | ); |
| 85 | } |
| 86 | |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 87 | 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 Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 102 | 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 Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 114 | 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 Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 120 | |
| 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 Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 127 | // this.initLinks(); |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 128 | } |
| 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 Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 139 | this.log.debug('Simulation stopped'); |
| 140 | } |
| 141 | |
| 142 | restartSimulation() { |
| 143 | this.simulation.restart(); |
| 144 | this.log.debug('Simulation restarted'); |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 145 | } |
| 146 | } |