Skip to content

Instantly share code, notes, and snippets.

@alexeygrigorev
Created December 22, 2013 18:47
Show Gist options
  • Save alexeygrigorev/8086691 to your computer and use it in GitHub Desktop.
Save alexeygrigorev/8086691 to your computer and use it in GitHub Desktop.
Generating event logs from a graph for process mining
package graph;
import java.util.*;
import com.google.common.collect.*;
/**
* @author Alexey Grigorev
*/
public class Graph {
private static final int MAX_VISITS = 2;
private final Multimap<String, String> nodes = HashMultimap.create();
private final Set<String> end = Sets.newHashSet();
public void addEdge(String from, String to) {
nodes.put(from, to);
}
public void setEndPoints(String... ends) {
end.addAll(Arrays.asList(ends));
}
public Iterable<String> adjacent(String node) {
return nodes.get(node);
}
public List<String> explore(String first) {
List<String> result = Lists.newLinkedList();
Queue<History> q = Lists.newLinkedList();
q.add(new History(first));
while (!q.isEmpty()) {
History next = q.poll();
String vertex = next.node;
if (end.contains(vertex)) {
result.add(next.path);
continue;
}
for (String u : adjacent(vertex)) {
if (next.good(u)) {
q.add(next.cont(u));
}
}
}
return result;
}
private static class History {
private final String node;
private final String path;
private final Multiset<String> visited;
public History(String node, String path, Multiset<String> visited) {
this.node = node;
this.path = path;
this.visited = visited;
}
public History(String node) {
this(node, node, HashMultiset.<String> create());
}
public boolean good(String next) {
return visited.count(next) < MAX_VISITS;
}
public History cont(String next) {
HashMultiset<String> copy = HashMultiset.create(visited);
copy.add(next);
return new History(next, path + ' ' + next, copy);
}
@Override
public String toString() {
return "History [node=" + node + ", path=" + path + "]";
}
}
}
package graph;
import java.util.List;
import org.junit.Test;
/**
* @author Alexey Grigorev
*/
public class GraphTest {
@Test
public void test() {
Graph g = new Graph();
g.addEdge("A", "B");
g.addEdge("B", "C");
g.addEdge("C", "D");
g.addEdge("D", "F");
g.addEdge("D", "X");
g.addEdge("F", "B");
g.addEdge("C", "E");
g.addEdge("E", "GH");
g.addEdge("E", "HG");
g.addEdge("GH", "J");
g.addEdge("HG", "J");
g.addEdge("J", "K");
g.addEdge("K", "N");
g.addEdge("N", "R");
g.addEdge("N", "K");
g.addEdge("K", "L");
g.addEdge("K", "M");
g.addEdge("M", "L");
g.addEdge("L", "O");
g.addEdge("O", "P");
g.addEdge("P", "K");
g.addEdge("P", "R");
g.addEdge("M", "Q");
g.addEdge("Q", "Z");
g.addEdge("R", "S");
g.addEdge("S", "R");
g.addEdge("S", "K");
g.addEdge("R", "T");
g.addEdge("T", "W");
g.addEdge("W", "T");
g.addEdge("T", "Y");
g.addEdge("Y", "Z");
g.setEndPoints("X", "Z");
List<String> res = g.explore("A");
for (String path : res) {
System.out.println(path);
}
}
}
<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">
<modelVersion>4.0.0</modelVersion>
<groupId>it4bi.ulb.bpm</groupId>
<artifactId>sec-gen</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>15.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit-dep</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment