diff options
Diffstat (limited to 'subprojects/viatra-runtime-base-itc/src/main/java/tools/refinery/viatra/runtime/base/itc/alg/misc/GraphHelper.java')
-rw-r--r-- | subprojects/viatra-runtime-base-itc/src/main/java/tools/refinery/viatra/runtime/base/itc/alg/misc/GraphHelper.java | 173 |
1 files changed, 173 insertions, 0 deletions
diff --git a/subprojects/viatra-runtime-base-itc/src/main/java/tools/refinery/viatra/runtime/base/itc/alg/misc/GraphHelper.java b/subprojects/viatra-runtime-base-itc/src/main/java/tools/refinery/viatra/runtime/base/itc/alg/misc/GraphHelper.java new file mode 100644 index 00000000..194e979b --- /dev/null +++ b/subprojects/viatra-runtime-base-itc/src/main/java/tools/refinery/viatra/runtime/base/itc/alg/misc/GraphHelper.java | |||
@@ -0,0 +1,173 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2013, Tamas Szabo, Istvan Rath and Daniel Varro | ||
3 | * This program and the accompanying materials are made available under the | ||
4 | * terms of the Eclipse Public License v. 2.0 which is available at | ||
5 | * http://www.eclipse.org/legal/epl-v20.html. | ||
6 | * | ||
7 | * SPDX-License-Identifier: EPL-2.0 | ||
8 | *******************************************************************************/ | ||
9 | package tools.refinery.viatra.runtime.base.itc.alg.misc; | ||
10 | |||
11 | import java.util.ArrayList; | ||
12 | import java.util.Collection; | ||
13 | import java.util.HashSet; | ||
14 | import java.util.List; | ||
15 | import java.util.Map.Entry; | ||
16 | import java.util.Set; | ||
17 | |||
18 | import tools.refinery.viatra.runtime.base.itc.graphimpl.Graph; | ||
19 | import tools.refinery.viatra.runtime.base.itc.igraph.IBiDirectionalGraphDataSource; | ||
20 | import tools.refinery.viatra.runtime.base.itc.igraph.IGraphDataSource; | ||
21 | import tools.refinery.viatra.runtime.matchers.util.IMemoryView; | ||
22 | |||
23 | /** | ||
24 | * Utility class for graph related operations. | ||
25 | * | ||
26 | * @author Tamas Szabo | ||
27 | */ | ||
28 | public class GraphHelper { | ||
29 | |||
30 | private GraphHelper() {/*Utility class constructor*/} | ||
31 | |||
32 | /** | ||
33 | * Returns the subgraph from the given {@link IBiDirectionalGraphDataSource} which contains the given set of nodes. | ||
34 | * | ||
35 | * @param nodesInSubGraph | ||
36 | * the nodes that are present in the subgraph | ||
37 | * @param graphDataSource | ||
38 | * the graph data source for the original graph | ||
39 | * @return the subgraph associated to the given nodes | ||
40 | */ | ||
41 | public static <V> Graph<V> getSubGraph(Collection<V> nodesInSubGraph, | ||
42 | IBiDirectionalGraphDataSource<V> graphDataSource) { | ||
43 | Graph<V> g = new Graph<V>(); | ||
44 | if (nodesInSubGraph != null) { | ||
45 | for (V node : nodesInSubGraph) { | ||
46 | g.insertNode(node); | ||
47 | } | ||
48 | |||
49 | for (V node : nodesInSubGraph) { | ||
50 | IMemoryView<V> sources = graphDataSource.getSourceNodes(node); | ||
51 | for (Entry<V, Integer> entry : sources.entriesWithMultiplicities()) { | ||
52 | for (int i = 0; i < entry.getValue(); i++) { | ||
53 | V s = entry.getKey(); | ||
54 | if (nodesInSubGraph.contains(s)) { | ||
55 | g.insertEdge(s, node); | ||
56 | } | ||
57 | } | ||
58 | } | ||
59 | } | ||
60 | } | ||
61 | |||
62 | return g; | ||
63 | } | ||
64 | |||
65 | /** | ||
66 | * Constructs a path between source and target in the given graph. Both the {@link IGraphDataSource} and the set of | ||
67 | * nodes are used, this way it is possible to construct a path in a given subgraph. | ||
68 | * | ||
69 | * The returned {@link List} contains the nodes along the path (this means that there is an edge in the graph | ||
70 | * between two consecutive nodes). A self loop (one edge) is indicated with the source node being present two times | ||
71 | * in the returned {@link List}. | ||
72 | * | ||
73 | * @param source | ||
74 | * the source node | ||
75 | * @param target | ||
76 | * the target node | ||
77 | * @param nodesInGraph | ||
78 | * the nodes that are present in the subgraph | ||
79 | * @param graphDataSource | ||
80 | * the graph data source | ||
81 | * @return the path between the two nodes | ||
82 | */ | ||
83 | public static <V> List<V> constructPath(V source, V target, Set<V> nodesInGraph, | ||
84 | IGraphDataSource<V> graphDataSource) { | ||
85 | Set<V> visitedNodes = new HashSet<V>(); | ||
86 | List<V> path = new ArrayList<V>(); | ||
87 | |||
88 | visitedNodes.add(source); | ||
89 | path.add(source); | ||
90 | V act = source; | ||
91 | |||
92 | // if source and target are the same node | ||
93 | if (source.equals(target) && graphDataSource.getTargetNodes(source).containsNonZero(target)) { | ||
94 | // the node will be present in the path two times | ||
95 | path.add(source); | ||
96 | return path; | ||
97 | } else { | ||
98 | while (act != null) { | ||
99 | V nextNode = getNextNodeToVisit(act, graphDataSource, nodesInGraph, visitedNodes); | ||
100 | if (nextNode == null && path.size() > 1) { | ||
101 | // needs to backtrack along path | ||
102 | // remove the last element in the path because we can't go | ||
103 | // anywhere from there | ||
104 | path.remove(path.size() - 1); | ||
105 | while (nextNode == null && path.size() > 0) { | ||
106 | V lastPathElement = path.get(path.size() - 1); | ||
107 | nextNode = getNextNodeToVisit(lastPathElement, graphDataSource, nodesInGraph, visitedNodes); | ||
108 | if (nextNode == null) { | ||
109 | path.remove(path.size() - 1); | ||
110 | } | ||
111 | } | ||
112 | } | ||
113 | |||
114 | if (nextNode != null) { | ||
115 | visitedNodes.add(nextNode); | ||
116 | path.add(nextNode); | ||
117 | if (nextNode.equals(target)) { | ||
118 | return path; | ||
119 | } | ||
120 | } | ||
121 | act = nextNode; | ||
122 | } | ||
123 | return null; | ||
124 | } | ||
125 | } | ||
126 | |||
127 | private static <V> V getNextNodeToVisit(V act, IGraphDataSource<V> graphDataSource, Set<V> nodesInSubGraph, | ||
128 | Set<V> visitedNodes) { | ||
129 | IMemoryView<V> targetNodes = graphDataSource.getTargetNodes(act); | ||
130 | for (Entry<V, Integer> entry : targetNodes.entriesWithMultiplicities()) { | ||
131 | for (int i = 0; i < entry.getValue(); i++) { | ||
132 | V node = entry.getKey(); | ||
133 | if (nodesInSubGraph.contains(node) && !visitedNodes.contains(node)) { | ||
134 | return node; | ||
135 | } | ||
136 | } | ||
137 | } | ||
138 | return null; | ||
139 | } | ||
140 | |||
141 | /** | ||
142 | * Returns the number of self-loop edges for the given node. | ||
143 | * | ||
144 | * @param node | ||
145 | * the node | ||
146 | * @param graphDataSource | ||
147 | * the graph data source | ||
148 | * @return the number of self-loop edges | ||
149 | */ | ||
150 | public static <V> int getEdgeCount(V node, IGraphDataSource<V> graphDataSource) { | ||
151 | return getEdgeCount(node, node, graphDataSource); | ||
152 | } | ||
153 | |||
154 | /** | ||
155 | * Returns the number of edges between the given source and target nodes. | ||
156 | * | ||
157 | * @param source | ||
158 | * the source node | ||
159 | * @param target | ||
160 | * the target node | ||
161 | * @param graphDataSource | ||
162 | * the graph data source | ||
163 | * @return the number of parallel edges between the two nodes | ||
164 | */ | ||
165 | public static <V> int getEdgeCount(V source, V target, IGraphDataSource<V> graphDataSource) { | ||
166 | Integer count = graphDataSource.getTargetNodes(source).getCount(target); | ||
167 | if (count == null) { | ||
168 | return 0; | ||
169 | } else { | ||
170 | return count; | ||
171 | } | ||
172 | } | ||
173 | } | ||