@@ -26,72 +26,78 @@ import {
2626 calculateLayerNumbersLongestPath ,
2727 IntermediatesCreationResult ,
2828} from './model/horizontal-grouping' ;
29- import { SvgResult , Dimensions } from '../public_api' ;
3029import { LayoutModel , LayoutModelBuilder } from './model/layout-model' ;
31- import { DerivedDimensions , getDerivedDimensions } from './dimensions' ;
30+ import { DerivedDimensions , Dimensions , getDerivedDimensions } from './dimensions' ;
3231
33- export class Flow2svgService {
34- private cache = new AsynchronousCache < SvgResult > ( ) ;
32+ export interface LayoutStatisticsResult {
33+ layout : Layout ;
34+ numNodes : number ;
35+ numEdges : number ;
36+ numNodeVisitsDuringLayerCalculation : number ;
37+ }
38+
39+ export class FlowLayoutService {
40+ private cache = new AsynchronousCache < LayoutStatisticsResult > ( ) ;
3541
3642 private _numSvgCalculations = 0 ;
3743 get numSvgCalculations ( ) : number {
3844 return this . _numSvgCalculations ;
3945 }
4046
4147 getHashes ( ) : string [ ] {
42- return [ ... this . cache . getSortedKeys ( ) ] ;
48+ return this . cache . getSortedKeys ( ) ;
4349 }
4450
45- private dimensions : DerivedDimensions ;
51+ private readonly dimensions : DerivedDimensions ;
4652
4753 constructor ( dimensions : Dimensions ) {
4854 this . dimensions = getDerivedDimensions ( dimensions ) ;
4955 }
5056
5157 async flow2svg ( flow : string ) : Promise < string > {
52- const statistics = await this . flow2svgStatistics ( flow ) ;
53- return statistics . svg ;
58+ const statistics = await this . flow2LayoutStatistics ( flow ) ;
59+ return generateSvg ( statistics . layout , this . dimensions ) ;
60+ }
61+
62+ async flow2Layout ( flow : string ) : Promise < Layout > {
63+ const statistics = await this . flow2LayoutStatistics ( flow ) ;
64+ return statistics . layout ;
5465 }
5566
56- async flow2svgStatistics ( flow : string ) : Promise < SvgResult > {
57- let hash : string ;
67+ async flow2LayoutStatistics ( flow : string ) : Promise < LayoutStatisticsResult > {
5868 try {
59- hash = await sha256 ( flow ) ;
69+ const hash = await sha256 ( flow ) ;
70+ return this . cache . get ( hash , ( ) => this . flow2LayoutStatisticsImpl ( flow ) ) ;
6071 } catch ( error ) {
61- console . log ( error ) ;
62- throw new Error ( 'Could not calculate hash' ) ;
72+ throw new Error ( 'Could not calculate hash' , error as Error ) ;
6373 }
64- return await this . cache . get ( hash , ( ) => this . flow2svgStatisticsImpl ( flow ) ) ;
6574 }
6675
67- private async flow2svgStatisticsImpl ( flow : string ) : Promise < SvgResult > {
76+ private async flow2LayoutStatisticsImpl ( flow : string ) : Promise < LayoutStatisticsResult > {
6877 ++ this . _numSvgCalculations ;
69- const b : FlowGraph = getGraphFromFlow ( flow , this . dimensions ) ;
70- const g : OriginalGraph = findErrorFlow ( b ) ;
78+ const flowGraph : FlowGraph = getGraphFromFlow ( flow , this . dimensions ) ;
79+ const graph : OriginalGraph = findErrorFlow ( flowGraph ) ;
7180 let numNodeVisits = 0 ;
72- const nodeIdToLayer : Map < string , number > = calculateLayerNumbersLongestPath ( g , ( ) => ++ numNodeVisits ) ;
73- const intermediates : IntermediatesCreationResult = introduceIntermediateNodesAndEdges ( g , nodeIdToLayer ) ;
74- let lb : LayoutBase ;
75- try {
76- lb = LayoutBase . create (
77- intermediates . intermediate . nodes . map ( ( n ) => n . id ) ,
78- intermediates . intermediate ,
79- ) ;
80- } catch ( error ) {
81- throw error ;
82- }
83- lb = minimizeNumCrossings ( lb ) ;
84- const layoutModel : LayoutModel = new LayoutModelBuilder ( lb , intermediates . intermediate ) . run ( ) ;
81+ const nodeIdToLayer : Map < string , number > = calculateLayerNumbersLongestPath ( graph , ( ) => ++ numNodeVisits ) ;
82+ const intermediates : IntermediatesCreationResult = introduceIntermediateNodesAndEdges ( graph , nodeIdToLayer ) ;
83+ const layoutBase = LayoutBase . create (
84+ intermediates . intermediate . nodes . map ( ( n ) => n . id ) ,
85+ intermediates . intermediate ,
86+ ) ;
87+ const layoutModel : LayoutModel = new LayoutModelBuilder (
88+ minimizeNumCrossings ( layoutBase ) ,
89+ intermediates . intermediate ,
90+ ) . run ( ) ;
8591 const layout : Layout = new LayoutBuilder (
8692 layoutModel ,
8793 intermediates . original ,
8894 this . dimensions ,
8995 this . dimensions ,
9096 ) . run ( ) ;
9197 return {
92- svg : generateSvg ( layout , this . dimensions ) ,
93- numNodes : g . nodes . length ,
94- numEdges : g . edges . length ,
98+ layout,
99+ numNodes : graph . nodes . length ,
100+ numEdges : graph . edges . length ,
95101 numNodeVisitsDuringLayerCalculation : numNodeVisits ,
96102 } ;
97103 }
0 commit comments