Skip to content

Commit 5d1daeb

Browse files
committed
Initial commit
1 parent be26b9e commit 5d1daeb

File tree

11 files changed

+1094
-0
lines changed

11 files changed

+1094
-0
lines changed

pom.xml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<modelVersion>4.0.0</modelVersion>
3+
<groupId>de.fraunhofer.iem</groupId>
4+
<artifactId>PathExpression</artifactId>
5+
<version>1.0.0</version>
6+
<build>
7+
<sourceDirectory>src</sourceDirectory>
8+
<testSourceDirectory>test</testSourceDirectory>
9+
<plugins>
10+
<plugin>
11+
<artifactId>maven-compiler-plugin</artifactId>
12+
<version>3.3</version>
13+
<configuration>
14+
<source>1.7</source>
15+
<target>1.7</target>
16+
</configuration>
17+
</plugin>
18+
</plugins>
19+
</build>
20+
21+
<dependencies>
22+
<dependency>
23+
<groupId>com.google.guava</groupId>
24+
<artifactId>guava</artifactId>
25+
<version>23.5-jre</version>
26+
</dependency>
27+
<dependency>
28+
<groupId>junit</groupId>
29+
<artifactId>junit</artifactId>
30+
<version>4.12</version>
31+
<scope>test</scope>
32+
</dependency>
33+
</dependencies>
34+
</project>

src/pathexpression/Edge.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
package pathexpression;
13+
14+
public interface Edge<N, V> {
15+
public N getStart();
16+
17+
public N getTarget();
18+
19+
public V getLabel();
20+
}

src/pathexpression/Epsilon.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
public class Epsilon<V> implements IRegEx<V> {
16+
17+
public Epsilon() {
18+
}
19+
20+
@Override
21+
public String toString() {
22+
return "EPS";
23+
}
24+
}

src/pathexpression/IRegEx.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
package pathexpression;
13+
14+
public interface IRegEx<V> {
15+
16+
}

src/pathexpression/LabeledGraph.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
package pathexpression;
13+
14+
import java.util.Set;
15+
16+
public interface LabeledGraph<N, V> {
17+
Set<Edge<N, V>> getEdges();
18+
19+
Set<N> getNodes();
20+
21+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
package pathexpression;
13+
14+
class PathExpression<V> {
15+
private IRegEx<V> ex;
16+
private int w;
17+
private int u;
18+
19+
public IRegEx<V> getExpression() {
20+
return ex;
21+
}
22+
23+
public int getTarget() {
24+
return w;
25+
}
26+
27+
public int getSource() {
28+
return u;
29+
}
30+
31+
32+
public PathExpression(IRegEx<V> reg, int u, int w) {
33+
this.ex = reg;
34+
this.u = u;
35+
this.w = w;
36+
}
37+
38+
public String toString() {
39+
return "{" + u + "," + ex.toString() + "," + w + "}";
40+
}
41+
42+
@Override
43+
public int hashCode() {
44+
final int prime = 31;
45+
int result = 1;
46+
result = prime * result + ((ex == null) ? 0 : ex.hashCode());
47+
result = prime * result + u;
48+
result = prime * result + w;
49+
return result;
50+
}
51+
52+
@Override
53+
public boolean equals(Object obj) {
54+
if (this == obj)
55+
return true;
56+
if (obj == null)
57+
return false;
58+
if (getClass() != obj.getClass())
59+
return false;
60+
PathExpression other = (PathExpression) obj;
61+
if (ex == null) {
62+
if (other.ex != null)
63+
return false;
64+
} else if (!ex.equals(other.ex))
65+
return false;
66+
if (u != other.u)
67+
return false;
68+
if (w != other.w)
69+
return false;
70+
return true;
71+
}
72+
73+
74+
75+
}
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
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

Comments
 (0)