1/* 2 * Copyright (c) 2024-2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import { BaseEdge, BaseExplicitGraph, BaseNode, Kind, NodeID } from './BaseExplicitGraph'; 17interface Attributes { 18 [name: string]: any; 19} 20 21interface NodeAttributes extends Attributes { 22 name: string; 23 kind: Kind; 24} 25 26interface EdgeAttributes extends Attributes { 27 kind: Kind; 28} 29 30export class DependsNode<NodeAttr extends NodeAttributes> extends BaseNode { 31 private attr: NodeAttr; 32 33 public constructor(id: NodeID, attr: NodeAttr) { 34 super(id, attr.kind); 35 this.attr = attr; 36 } 37 38 public getNodeAttr(): NodeAttr { 39 return this.attr; 40 } 41 42 public setNodeAttr(attr: NodeAttr): void { 43 this.attr = attr; 44 } 45 46 public getDotLabel(): string { 47 return this.attr.name; 48 } 49} 50 51export class DependsEdge<NodeAttr extends NodeAttributes, EdgeAttr extends EdgeAttributes> extends BaseEdge { 52 private attr: EdgeAttr; 53 54 public constructor(s: DependsNode<NodeAttr>, d: DependsNode<NodeAttr>, attr: EdgeAttr) { 55 super(s, d, attr.kind); 56 this.attr = attr; 57 } 58 59 public getEdgeAttr(): EdgeAttr { 60 return this.attr; 61 } 62 63 public setEdgeAttr(attr: EdgeAttr): void { 64 this.attr = attr; 65 } 66 67 public getKey(): string { 68 return `${this.getSrcID()}-${this.getDstID()}-${this.getKind()}`; 69 } 70} 71 72export class DependsGraph<NodeAttr extends NodeAttributes, EdgeAttr extends EdgeAttributes> extends BaseExplicitGraph { 73 protected depsMap: Map<string, NodeID>; 74 protected edgesMap: Map<string, DependsEdge<NodeAttr, EdgeAttr>>; 75 76 constructor() { 77 super(); 78 this.depsMap = new Map(); 79 this.edgesMap = new Map(); 80 } 81 82 public hasDepsNode(key: string): boolean { 83 return this.depsMap.has(key); 84 } 85 86 public addDepsNode(key: string, attr: NodeAttr): DependsNode<NodeAttr> { 87 if (this.depsMap.has(key)) { 88 // update attr 89 let node = this.getNode(this.depsMap.get(key)!) as DependsNode<NodeAttr>; 90 node.setNodeAttr(attr); 91 return node; 92 } 93 94 let node = new DependsNode(this.getNodeNum(), attr); 95 this.depsMap.set(key, node.getID()); 96 this.addNode(node); 97 return node; 98 } 99 100 public addEdge(src: DependsNode<NodeAttr>, dst: DependsNode<NodeAttr>, attr: EdgeAttr): DependsEdge<NodeAttr, EdgeAttr> { 101 let edge = new DependsEdge(src, dst, attr); 102 let key = edge.getKey(); 103 if (this.edgesMap.has(key)) { 104 return this.edgesMap.get(key)!; 105 } 106 this.edgesMap.set(key, edge); 107 src.addOutgoingEdge(edge); 108 dst.addIncomingEdge(edge); 109 return edge; 110 } 111 112 public getGraphName(): string { 113 return 'DependsGraph'; 114 } 115} 116