All files elk-layout.ts

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 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375                                1x           1x 1x         1x 1x 1x           1x         1x 1x 1x     2x 2x     2x 2x 2x   2x 2x 2x 2x       10x 10x   2x 2x       2x 2x 6x 4x 2x 6x 2x   2x       4x 4x       4x 2x 2x   2x 2x   2x 2x   2x 2x 2x   4x 4x       2x 2x           2x 2x 1x 1x 1x 1x     2x 2x 1x 1x 1x 1x     2x         2x 2x         2x                             2x 2x       2x         2x 2x               1x 6x       6x 4x 4x       1x 6x 6x 4x   6x 4x 4x     6x 4x 2x 2x 2x       6x 2x 2x 2x 2x           1x 6x 6x 6x 6x   6x 2x                 2x 2x 2x 2x 2x 2x 2x   2x 2x                   2x   2x                   1x                                   1x   1x 8x   4x   2x       2x           1x 6x     1x 2x 2x   2x 2x     2x 2x   2x 2x     2x     1x       1x 4x     1x                   1x   1x 10x   2x   4x   2x       2x           1x 2x     1x 4x     1x 2x     1x       1x 2x     1x  
/********************************************************************************
 * Copyright (c) 2018 TypeFox and others.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v. 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0.
 *
 * This Source Code may also be made available under the following Secondary
 * Licenses when the conditions for such availability set forth in the Eclipse
 * Public License v. 2.0 are satisfied: GNU General Public License, version 2
 * with the GNU Classpath Exception which is available at
 * https://www.gnu.org/software/classpath/license.html.
 *
 * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
 ********************************************************************************/
 
import { injectable, inject } from 'inversify';
import {
    ELK, ElkNode, ElkGraphElement, ElkEdge, ElkLabel, ElkPort, ElkShape, ElkPrimitiveEdge,
    ElkExtendedEdge, LayoutOptions
} from 'elkjs/lib/elk-api';
import { SGraphSchema, SNodeSchema, SEdgeSchema, SLabelSchema, SPortSchema } from 'sprotty/lib/graph/sgraph';
import { SModelElementSchema, SModelIndex } from 'sprotty/lib/base/model/smodel';
import { getBasicType } from 'sprotty/lib/base/model/smodel-utils';
import { IModelLayoutEngine } from 'sprotty/lib/model-source/local-model-source';
import { SShapeElementSchema } from 'sprotty/lib/features/bounds/model';
import { Point } from 'sprotty/lib/utils/geometry';
 
export const ElkFactory = Symbol('ElkFactory');
export const IElementFilter = Symbol('IElementFilter');
export const ILayoutConfigurator = Symbol('ILayoutConfigurator');
 
/**
 * Layout engine that delegates to ELK by transforming the Sprotty graph into an ELK graph.
 */
@injectable()
export class ElkLayoutEngine implements IModelLayoutEngine {
 
    protected readonly elk: ELK;
 
    constructor(@inject(ElkFactory) elkFactory: ElkFactory,
                @inject(IElementFilter) protected readonly filter: IElementFilter,
                @inject(ILayoutConfigurator) protected readonly configurator: ILayoutConfigurator) {
        this.elk = elkFactory();
    }
 
    layout(graph: SGraphSchema, index?: SModelIndex<SModelElementSchema>): SGraphSchema | Promise<SGraphSchema> {
        Iif (getBasicType(graph) !== 'graph') {
            return graph;
        }
        Eif (!index) {
            index = new SModelIndex();
            index.add(graph);
        }
        const elkGraph = this.transformToElk(graph, index) as ElkNode;
        return this.elk.layout(elkGraph).then(result => {
            this.applyLayout(result, index!);
            return graph;
        });
    }
 
    protected transformToElk(smodel: SModelElementSchema, index: SModelIndex<SModelElementSchema>): ElkGraphElement {
        switch (getBasicType(smodel)) {
            case 'graph': {
                const sgraph = smodel as SGraphSchema;
                const elkGraph: ElkNode = {
                    id: sgraph.id,
                    layoutOptions: this.configurator.apply(sgraph, index)
                };
                Eif (sgraph.children) {
                    elkGraph.children = sgraph.children
                        .filter(c => getBasicType(c) === 'node' && this.filter.apply(c, index))
                        .map(c => this.transformToElk(c, index)) as ElkNode[];
                    elkGraph.edges = sgraph.children
                        .filter(c => getBasicType(c) === 'edge' && this.filter.apply(c, index))
                        .map(c => this.transformToElk(c, index)) as ElkEdge[];
                }
                return elkGraph;
            }
 
            case 'node': {
                const snode = smodel as SNodeSchema;
                const elkNode: ElkNode = {
                    id: snode.id,
                    layoutOptions: this.configurator.apply(snode, index)
                };
                if (snode.children) {
                    elkNode.children = snode.children
                        .filter(c => getBasicType(c) === 'node' && this.filter.apply(c, index))
                        .map(c => this.transformToElk(c, index)) as ElkNode[];
                    elkNode.edges = snode.children
                        .filter(c => getBasicType(c) === 'edge' && this.filter.apply(c, index))
                        .map(c => this.transformToElk(c, index)) as ElkEdge[];
                    elkNode.labels = snode.children
                        .filter(c => getBasicType(c) === 'label' && this.filter.apply(c, index))
                        .map(c => this.transformToElk(c, index)) as ElkLabel[];
                    elkNode.ports = snode.children
                        .filter(c => getBasicType(c) === 'port' && this.filter.apply(c, index))
                        .map(c => this.transformToElk(c, index)) as ElkPort[];
                }
                this.transformShape(elkNode, snode);
                return elkNode;
            }
 
            case 'edge': {
                const sedge = smodel as SEdgeSchema;
                const elkEdge: ElkPrimitiveEdge = {
                    id: sedge.id,
                    source: sedge.sourceId,
                    target: sedge.targetId,
                    layoutOptions: this.configurator.apply(sedge, index)
                };
                const sourceElement = index.getById(sedge.sourceId);
                if (sourceElement && getBasicType(sourceElement) === 'port') {
                    const parent = index.getParent(sourceElement.id);
                    Eif (parent && getBasicType(parent) === 'node') {
                        elkEdge.source = parent.id;
                        elkEdge.sourcePort = sourceElement.id;
                    }
                }
                const targetElement = index.getById(sedge.targetId);
                if (targetElement && getBasicType(targetElement) === 'port') {
                    const parent = index.getParent(targetElement.id);
                    Eif (parent && getBasicType(parent) === 'node') {
                        elkEdge.target = parent.id;
                        elkEdge.targetPort = targetElement.id;
                    }
                }
                Iif (sedge.children) {
                    elkEdge.labels = sedge.children
                        .filter(c => getBasicType(c) === 'label' && this.filter.apply(c, index))
                        .map(c => this.transformToElk(c, index)) as ElkLabel[];
                }
                const points = sedge.routingPoints;
                Iif (points && points.length >= 2) {
                    elkEdge.sourcePoint = points[0];
                    elkEdge.bendPoints = points.slice(1, points.length - 1);
                    elkEdge.targetPoint = points[points.length - 1];
                }
                return elkEdge;
            }
 
            case 'label': {
                const slabel = smodel as SLabelSchema;
                const elkLabel: ElkLabel = {
                    id: slabel.id,
                    text: slabel.text,
                    layoutOptions: this.configurator.apply(slabel, index)
                };
                this.transformShape(elkLabel, slabel);
                return elkLabel;
            }
 
            case 'port': {
                const sport = smodel as SPortSchema;
                const elkPort: ElkPort = {
                    id: sport.id,
                    layoutOptions: this.configurator.apply(sport, index)
                };
                Iif (sport.children) {
                    elkPort.labels = sport.children
                        .filter(c => getBasicType(c) === 'label' && this.filter.apply(c, index))
                        .map(c => this.transformToElk(c, index)) as ElkLabel[];
                }
                this.transformShape(elkPort, sport);
                return elkPort;
            }
 
            default:
                throw new Error('Type not supported: ' + smodel.type);
        }
    }
 
    protected transformShape(elkShape: ElkShape, sshape: SShapeElementSchema): void {
        Iif (sshape.position) {
            elkShape.x = sshape.position.x;
            elkShape.y = sshape.position.y;
        }
        if (sshape.size) {
            elkShape.width = sshape.size.width;
            elkShape.height = sshape.size.height;
        }
    }
 
    protected applyLayout(elkNode: ElkNode, index: SModelIndex<SModelElementSchema>): void {
        const snode = index.getById(elkNode.id);
        if (snode && getBasicType(snode) === 'node') {
            this.applyShape(snode as SNodeSchema, elkNode, index);
        }
        if (elkNode.children) {
            for (const child of elkNode.children) {
                this.applyLayout(child, index);
            }
        }
        if (elkNode.edges) {
            for (const elkEdge of elkNode.edges) {
                const sedge = index.getById(elkEdge.id);
                Eif (sedge && getBasicType(sedge) === 'edge') {
                    this.applyEdge(sedge as SEdgeSchema, elkEdge, index);
                }
            }
        }
        if (elkNode.ports) {
            for (const elkPort of elkNode.ports) {
                const sport = index.getById(elkPort.id);
                Eif (sport && getBasicType(sport) === 'port') {
                    this.applyShape(sport as SPortSchema, elkPort, index);
                }
            }
        }
    }
 
    protected applyShape(sshape: SShapeElementSchema, elkShape: ElkShape, index: SModelIndex<SModelElementSchema>): void {
        Eif (elkShape.x !== undefined && elkShape.y !== undefined)
            sshape.position = { x: elkShape.x, y: elkShape.y };
        Eif (elkShape.width !== undefined && elkShape.height !== undefined)
            sshape.size = { width: elkShape.width, height: elkShape.height };
 
        if (elkShape.labels) {
            for (const elkLabel of elkShape.labels) {
                const slabel = index.getById(elkLabel.id);
                if (slabel) {
                    this.applyShape(slabel as SLabelSchema, elkLabel, index);
                }
            }
        }
    }
 
    protected applyEdge(sedge: SEdgeSchema, elkEdge: ElkEdge, index: SModelIndex<SModelElementSchema>): void {
        const points: Point[] = [];
        Eif ((elkEdge as any).sections && (elkEdge as any).sections.length > 0) {
            const section = (elkEdge as ElkExtendedEdge).sections[0];
            Eif (section.startPoint)
                points.push(section.startPoint);
            Iif (section.bendPoints)
                points.push(...section.bendPoints);
            Eif (section.endPoint)
                points.push(section.endPoint);
        } else {
            const section = elkEdge as ElkPrimitiveEdge;
            if (section.sourcePoint)
                points.push(section.sourcePoint);
            if (section.bendPoints)
                points.push(...section.bendPoints);
            if (section.targetPoint)
                points.push(section.targetPoint);
        }
        sedge.routingPoints = points;
 
        Iif (elkEdge.labels) {
            elkEdge.labels.forEach((elkLabel) => {
                const sLabel = index.getById(elkLabel.id);
                if (sLabel) {
                    this.applyShape(sLabel, elkLabel, index);
                }
            });
        }
    }
 
}
 
/**
 * Factory for ELK instances. Follow the elkjs package documentation on how to configure ELK
 * instances. For example, the bundled version can be used by importing the ELK constructor
 * from `"elkjs/lib/elk.bundled"`. For the webworker version, import the constructor from
 * `"elkjs/lib/elk-api"` and add the option `workerUrl: "elk/elk-worker.min.js"`.
 */
export type ElkFactory = () => ELK;
 
/**
 * Filter used to determine which model elements should be included in the automatic layout.
 */
export interface IElementFilter {
    apply(element: SModelElementSchema, index: SModelIndex<SModelElementSchema>): boolean
}
 
@injectable()
export class DefaultElementFilter implements IElementFilter {
 
    apply(element: SModelElementSchema, index: SModelIndex<SModelElementSchema>): boolean {
        switch (getBasicType(element)) {
            case 'node':
                return this.filterNode(element as SNodeSchema, index);
            case 'edge':
                return this.filterEdge(element as SEdgeSchema, index);
            case 'label':
                return this.filterLabel(element as SLabelSchema, index);
            case 'port':
                return this.filterPort(element as SPortSchema, index);
            default:
                return true;
        }
    }
 
    protected filterNode(node: SNodeSchema, index: SModelIndex<SModelElementSchema>): boolean {
        return true;
    }
 
    protected filterEdge(edge: SEdgeSchema, index: SModelIndex<SModelElementSchema>): boolean {
        const source = index.getById(edge.sourceId);
        Iif (!source)
            return false;
        const sourceType = getBasicType(source);
        Iif (sourceType === 'node' && !this.filterNode(source, index)
            || sourceType === 'port' && !this.filterPort(source, index))
            return false;
        const target = index.getById(edge.targetId);
        Iif (!target)
            return false;
        const targetType = getBasicType(target);
        Iif (targetType === 'node' && !this.filterNode(target, index)
            || targetType === 'port' && !this.filterPort(target, index))
            return false;
        return true;
    }
 
    protected filterLabel(label: SLabelSchema, index: SModelIndex<SModelElementSchema>): boolean {
        return true;
    }
 
    protected filterPort(port: SPortSchema, index: SModelIndex<SModelElementSchema>): boolean {
        return true;
    }
 
}
 
/**
 * Configurator of layout algorithms; provides mappings of layout options for each model element.
 */
export interface ILayoutConfigurator {
    apply(element: SModelElementSchema, index: SModelIndex<SModelElementSchema>): LayoutOptions | undefined
}
 
@injectable()
export class DefaultLayoutConfigurator implements ILayoutConfigurator {
 
    apply(element: SModelElementSchema, index: SModelIndex<SModelElementSchema>): LayoutOptions | undefined {
        switch (getBasicType(element)) {
            case 'graph':
                return this.graphOptions(element as SGraphSchema, index);
            case 'node':
                return this.nodeOptions(element as SNodeSchema, index);
            case 'edge':
                return this.edgeOptions(element as SEdgeSchema, index);
            case 'label':
                return this.labelOptions(element as SLabelSchema, index);
            case 'port':
                return this.portOptions(element as SPortSchema, index);
            default:
                return undefined;
        }
    }
 
    protected graphOptions(sgraph: SGraphSchema, index: SModelIndex<SModelElementSchema>): LayoutOptions | undefined {
        return undefined;
    }
 
    protected nodeOptions(snode: SNodeSchema, index: SModelIndex<SModelElementSchema>): LayoutOptions | undefined {
        return undefined;
    }
 
    protected edgeOptions(sedge: SEdgeSchema, index: SModelIndex<SModelElementSchema>): LayoutOptions | undefined {
        return undefined;
    }
 
    protected labelOptions(slabel: SLabelSchema, index: SModelIndex<SModelElementSchema>): LayoutOptions | undefined {
        return undefined;
    }
 
    protected portOptions(sport: SPortSchema, index: SModelIndex<SModelElementSchema>): LayoutOptions | undefined {
        return undefined;
    }
 
}