|
| 1 | +/****************************************************************************** |
| 2 | + * Copyright 2025 TypeFox GmbH |
| 3 | + * This program and the accompanying materials are made available under the |
| 4 | + * terms of the MIT License, which is available in the project root. |
| 5 | + ******************************************************************************/ |
| 6 | + |
| 7 | +import { TypirServices } from '../typir.js'; |
| 8 | +import { TypeEdge } from './type-edge.js'; |
| 9 | +import { TypeGraph } from './type-graph.js'; |
| 10 | +import { Type } from './type-node.js'; |
| 11 | + |
| 12 | +/** |
| 13 | + * Graph algorithms to do calculations on the type graph. |
| 14 | + * All algorithms are robust regarding cycles. |
| 15 | + */ |
| 16 | +export interface GraphAlgorithms { |
| 17 | + collectReachableTypes(from: Type, $relations: Array<TypeEdge['$relation']>, filterEdges?: (edgr: TypeEdge) => boolean): Set<Type>; |
| 18 | + existsEdgePath(from: Type, to: Type, $relations: Array<TypeEdge['$relation']>, filterEdges?: (edgr: TypeEdge) => boolean): boolean; |
| 19 | + getEdgePath(from: Type, to: Type, $relations: Array<TypeEdge['$relation']>, filterEdges?: (edgr: TypeEdge) => boolean): TypeEdge[]; |
| 20 | +} |
| 21 | + |
| 22 | +export class DefaultGraphAlgorithms implements GraphAlgorithms { |
| 23 | + protected readonly graph: TypeGraph; |
| 24 | + |
| 25 | + constructor(services: TypirServices) { |
| 26 | + this.graph = services.infrastructure.Graph; |
| 27 | + } |
| 28 | + |
| 29 | + collectReachableTypes(from: Type, $relations: Array<TypeEdge['$relation']>, filterEdges?: (edgr: TypeEdge) => boolean): Set<Type> { |
| 30 | + const result: Set<Type> = new Set(); |
| 31 | + const remainingToCheck: Type[] = [from]; |
| 32 | + |
| 33 | + while (remainingToCheck.length > 0) { |
| 34 | + const current = remainingToCheck.pop()!; |
| 35 | + const outgoingEdges = $relations.flatMap(r => current.getOutgoingEdges(r)); |
| 36 | + for (const edge of outgoingEdges) { |
| 37 | + if (edge.cachingInformation === 'LINK_EXISTS' && (filterEdges === undefined || filterEdges(edge))) { |
| 38 | + if (result.has(edge.to)) { |
| 39 | + // already checked |
| 40 | + } else { |
| 41 | + result.add(edge.to); // this type is reachable |
| 42 | + remainingToCheck.push(edge.to); // check it for recursive conversions |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + return result; |
| 49 | + } |
| 50 | + |
| 51 | + existsEdgePath(from: Type, to: Type, $relations: Array<TypeEdge['$relation']>, filterEdges?: (edgr: TypeEdge) => boolean): boolean { |
| 52 | + const visited: Set<Type> = new Set(); |
| 53 | + const stack: Type[] = [from]; |
| 54 | + |
| 55 | + while (stack.length > 0) { |
| 56 | + const current = stack.pop()!; |
| 57 | + visited.add(current); |
| 58 | + |
| 59 | + const outgoingEdges = $relations.flatMap(r => current.getOutgoingEdges(r)); |
| 60 | + for (const edge of outgoingEdges) { |
| 61 | + if (edge.cachingInformation === 'LINK_EXISTS' && (filterEdges === undefined || filterEdges(edge))) { |
| 62 | + if (edge.to === to) { |
| 63 | + /* It was possible to reach our goal type using this path. |
| 64 | + * Base case that also catches the case in which start and end are the same |
| 65 | + * (is there a cycle?). Therefore it is allowed to have been "visited". |
| 66 | + * True will only be returned if there is a real path (cycle) made up of edges |
| 67 | + */ |
| 68 | + return true; |
| 69 | + } |
| 70 | + if (!visited.has(edge.to)) { |
| 71 | + /* The target node of this edge has not been visited before and is also not our goal node |
| 72 | + * Add it to the stack and investigate this path later. |
| 73 | + */ |
| 74 | + stack.push(edge.to); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + // Fall through means that we could not reach the goal type |
| 81 | + return false; |
| 82 | + } |
| 83 | + |
| 84 | + getEdgePath(from: Type, to: Type, $relations: Array<TypeEdge['$relation']>, filterEdges?: (edgr: TypeEdge) => boolean): TypeEdge[] { |
| 85 | + const visited: Map<Type, TypeEdge|undefined> = new Map(); // the edge from the parent to the current node |
| 86 | + visited.set(from, undefined); |
| 87 | + const stack: Type[] = [from]; |
| 88 | + |
| 89 | + while (stack.length > 0) { |
| 90 | + const current = stack.pop()!; |
| 91 | + |
| 92 | + const outgoingEdges = $relations.flatMap(r => current.getOutgoingEdges(r)); |
| 93 | + for (const edge of outgoingEdges) { |
| 94 | + if (edge.cachingInformation === 'LINK_EXISTS' && (filterEdges === undefined || filterEdges(edge))) { |
| 95 | + if (edge.to === to) { |
| 96 | + /* It was possible to reach our goal type using this path. |
| 97 | + * Base case that also catches the case in which start and end are the same |
| 98 | + * (is there a cycle?). Therefore it is allowed to have been "visited". |
| 99 | + * True will only be returned if there is a real path (cycle) made up of edges |
| 100 | + */ |
| 101 | + const result: TypeEdge[] = [edge]; |
| 102 | + // collect the path of used edges, from "to" back to "from" |
| 103 | + let backNode = edge.from; |
| 104 | + while (backNode !== from) { |
| 105 | + const backEdge = visited.get(backNode)!; |
| 106 | + result.unshift(backEdge); |
| 107 | + backNode = backEdge.from; |
| 108 | + } |
| 109 | + return result; |
| 110 | + } |
| 111 | + if (!visited.has(edge.to)) { |
| 112 | + /* The target node of this edge has not been visited before and is also not our goal node |
| 113 | + * Add it to the stack and investigate this path later. |
| 114 | + */ |
| 115 | + stack.push(edge.to); |
| 116 | + visited.set(edge.to, edge); |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + // Fall through means that we could not reach the goal type |
| 123 | + return []; |
| 124 | + } |
| 125 | + |
| 126 | +} |
0 commit comments