|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2018 Fraunhofer IEM, Paderborn, Germany. |
| 3 | + * This program and the accompanying materials are made available under the |
| 4 | + * terms of the Eclipse Public License 2.0 which is available at |
| 5 | + * http://www.eclipse.org/legal/epl-2.0. |
| 6 | + * |
| 7 | + * SPDX-License-Identifier: EPL-2.0 |
| 8 | + * |
| 9 | + * Contributors: |
| 10 | + * Johannes Spaeth - initial API and implementation |
| 11 | + *******************************************************************************/ |
| 12 | + |
| 13 | +package pathexpression; |
| 14 | + |
| 15 | +import com.google.common.collect.BiMap; |
| 16 | +import com.google.common.collect.HashBasedTable; |
| 17 | +import com.google.common.collect.HashBiMap; |
| 18 | +import com.google.common.collect.Table; |
| 19 | +import pathexpression.RegEx.EmptySet; |
| 20 | + |
| 21 | +import java.util.LinkedList; |
| 22 | +import java.util.List; |
| 23 | + |
| 24 | +public class PathExpressionComputer<N, V> { |
| 25 | + |
| 26 | + private LabeledGraph<N, V> graph; |
| 27 | + private BiMap<N, Integer> nodeToIntMap = HashBiMap.create(); |
| 28 | + private Table<Integer, Integer, IRegEx<V>> table = HashBasedTable.create(); |
| 29 | + private IRegEx<V> emptyRegEx = new RegEx.EmptySet<V>(); |
| 30 | + |
| 31 | + public PathExpressionComputer(LabeledGraph<N, V> graph) { |
| 32 | + this.graph = graph; |
| 33 | + initNodesToIntMap(); |
| 34 | + } |
| 35 | + |
| 36 | + |
| 37 | + private void initNodesToIntMap() { |
| 38 | + int size = nodeToIntMap.size(); |
| 39 | + for (N node : graph.getNodes()) { |
| 40 | + System.out.println(node); |
| 41 | + nodeToIntMap.put(node, (++size)); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + private Integer getIntegerFor(N node) { |
| 46 | + assert nodeToIntMap.get(node) != null; |
| 47 | + return nodeToIntMap.get(node); |
| 48 | + } |
| 49 | + |
| 50 | + public IRegEx<V> getExpressionBetween(N a, N b) { |
| 51 | + if (!graph.getNodes().contains(a)) { |
| 52 | + return emptyRegEx; |
| 53 | + } |
| 54 | + List<IRegEx<V>> allExpr = computeAllPathFrom(a); |
| 55 | + return allExpr.get(getIntegerFor(b) - 1); |
| 56 | + } |
| 57 | + |
| 58 | + private List<IRegEx<V>> computeAllPathFrom(N a) { |
| 59 | + assert graph.getNodes().contains(a); |
| 60 | + eliminate(); |
| 61 | + List<PathExpression<V>> extractPathSequence = extractPathSequence(); |
| 62 | + List<IRegEx<V>> regEx = new LinkedList<>(); |
| 63 | + for (int i = 0; i < graph.getNodes().size(); i++) { |
| 64 | + regEx.add(emptyRegEx); |
| 65 | + } |
| 66 | + regEx.set(getIntegerFor(a) - 1, new Epsilon<V>()); |
| 67 | + for (int i = 0; i < extractPathSequence.size(); i++) { |
| 68 | + PathExpression<V> tri = extractPathSequence.get(i); |
| 69 | + if (tri.getSource() == tri.getTarget()) { |
| 70 | + IRegEx<V> expression = tri.getExpression(); |
| 71 | + |
| 72 | + int vi = tri.getSource(); |
| 73 | + IRegEx<V> regExVi = regEx.get(vi - 1); |
| 74 | + regEx.set(vi - 1, RegEx.<V>concatenate(regExVi, expression)); |
| 75 | + |
| 76 | + } else { |
| 77 | + IRegEx<V> expression = tri.getExpression(); |
| 78 | + int vi = tri.getSource(); |
| 79 | + int wi = tri.getTarget(); |
| 80 | + IRegEx<V> inter; |
| 81 | + IRegEx<V> regExVi = regEx.get(vi - 1); |
| 82 | + inter = RegEx.<V>concatenate(regExVi, expression); |
| 83 | + |
| 84 | + IRegEx<V> regExWi = regEx.get(wi - 1); |
| 85 | + regEx.set(wi - 1, RegEx.<V>union(regExWi, inter)); |
| 86 | + } |
| 87 | + } |
| 88 | + return regEx; |
| 89 | + } |
| 90 | + |
| 91 | + private List<PathExpression<V>> extractPathSequence() { |
| 92 | + int n = graph.getNodes().size(); |
| 93 | + List<PathExpression<V>> list = new LinkedList<PathExpression<V>>(); |
| 94 | + for (int u = 1; u <= n; u++) { |
| 95 | + for (int w = u; w <= n; w++) { |
| 96 | + IRegEx<V> reg = table.get(u, w); |
| 97 | + if (!(reg instanceof EmptySet) && !(reg instanceof Epsilon)) { |
| 98 | + list.add(new PathExpression<V>(reg, u, w)); |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + for (int u = n; u > 0; u--) { |
| 103 | + for (int w = 1; w < u; w++) { |
| 104 | + IRegEx<V> reg = table.get(u, w); |
| 105 | + if (!(reg instanceof EmptySet)) { |
| 106 | + list.add(new PathExpression<V>(reg, u, w)); |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + return list; |
| 111 | + } |
| 112 | + |
| 113 | + private void eliminate() { |
| 114 | + int numberOfNodes = graph.getNodes().size(); |
| 115 | + for (int v = 1; v <= numberOfNodes; v++) { |
| 116 | + for (int w = 1; w <= numberOfNodes; w++) { |
| 117 | + if (v == w) { |
| 118 | + updateTable(v, w, new Epsilon()); |
| 119 | + } else { |
| 120 | + updateTable(v, w, emptyRegEx); |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + for (Edge<N, V> e : graph.getEdges()) { |
| 125 | + Integer head = getIntegerFor(e.getStart()); |
| 126 | + Integer tail = getIntegerFor(e.getTarget()); |
| 127 | + IRegEx<V> pht = table.get(head, tail); |
| 128 | + pht = RegEx.<V>union(new RegEx.Plain<V>(e.getLabel()), pht); |
| 129 | + updateTable(head, tail, pht); |
| 130 | + } |
| 131 | + for (int v = 1; v <= numberOfNodes; v++) { |
| 132 | + IRegEx<V> pvv = table.get(v, v); |
| 133 | + pvv = RegEx.<V>star(pvv); |
| 134 | + updateTable(v, v, pvv); |
| 135 | + for (int u = v + 1; u <= numberOfNodes; u++) { |
| 136 | + IRegEx<V> puv = table.get(u, v); |
| 137 | + if (puv instanceof EmptySet) { |
| 138 | + continue; |
| 139 | + } |
| 140 | + puv = RegEx.<V>concatenate(puv, pvv); |
| 141 | + updateTable(u, v, puv); |
| 142 | + for (int w = v + 1; w <= numberOfNodes; w++) { |
| 143 | + IRegEx<V> pvw = table.get(v, w); |
| 144 | + if (pvw instanceof EmptySet) { |
| 145 | + continue; |
| 146 | + } |
| 147 | + |
| 148 | + IRegEx<V> old_puw = table.get(u, w); |
| 149 | + IRegEx<V> a = RegEx.<V>concatenate(puv, pvw); |
| 150 | + IRegEx<V> puw = RegEx.<V>union(old_puw, a); |
| 151 | + updateTable(u, w, puw); |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + System.out.println(); |
| 156 | + } |
| 157 | + |
| 158 | + |
| 159 | + private void updateTable(Integer i, Integer j, IRegEx<V> reg) { |
| 160 | + table.put(i, j, reg); |
| 161 | + } |
| 162 | + |
| 163 | +} |
0 commit comments