Skip to content
Snippets Groups Projects
Commit e4a2d007 authored by Johannes Mey's avatar Johannes Mey
Browse files

names aligned to paper

parent 10512962
No related branches found
No related tags found
No related merge requests found
......@@ -27,42 +27,33 @@ aspect Reachability {
* Kosaraju's algorithm
*/
syn Set<Set<Component>> DependencyGraph.SCC() {
Map<Component, Integer> visited = new HashMap<>();
Deque<Component> locked = new LinkedList<>();
Map<Component, Set> visited = new HashMap<>();
LinkedList<Component> locked = new LinkedList<>();
// visit nodes forward
for (Component n : getComponentList())
visit(n, visited, locked);
for (Component c : getComponentList())
if (!visited.containsKey(c))
c.visit(visited, locked); // forward search
// assign nodes to SCCs backward
int scc = 0;
for (Component n : locked) {
assign(n, visited, scc);
scc++;
}
for (Component c : locked)
if (visited.get(c) == null)
c.assign(visited, new HashSet()); // backward search
Map<Integer, Set<Component>> result = visited.entrySet().stream().collect(
Collectors.groupingBy(
e -> e.getValue(),
Collectors.mapping(e -> e.getKey(), Collectors.toSet())
)
);
return result.values().stream().collect(Collectors.toSet());
return new HashSet(visited.values());
}
private void DependencyGraph.visit(Component n, Map<Component, Integer> visited, Deque<Component> locked) {
if (visited.containsKey(n)) return;
visited.put(n, -1);
for (Component s : n.getFromList())
visit(s, visited, locked);
locked.addFirst(n);
void Component.visit(Map<Component, Set> visited, LinkedList<Component> locked) {
visited.put(this, null);
for (Component c : getFromList())
if (!visited.containsKey(c))
c.visit(visited, locked);
locked.addFirst(this);
}
private void DependencyGraph.assign(Component n, Map<Component, Integer> visited, int root) {
if (visited.get(n) > -1) return;
visited.put(n, root);
for (Component p : n.getToList())
assign(p, visited, root);
void Component.assign(Map<Component, Set> visited, Set scc) {
scc.add(this);
visited.put(this, scc);
for (Component c : getToList())
if (visited.get(c) == null)
c.assign(visited, scc);
}
}
Subproject commit 9192bc5fbffdac3fdf397fa1b86275cb2573309c
Subproject commit 13916e03792aa69f92369b139819addb6128e73d
aspect Reachability {
syn Set<State> State.successors() circular [new HashSet<>()] {
Set<State> result = new HashSet<>();
for (Transition t : getOutgoingList()) {
State s = t.getTo();
result.add(s);
result.addAll(s.successors());
}
return result;
}
// syn Set<State> State.successors() circular [new HashSet<>()] {
// Set<State> result = new HashSet<>();
// for (Transition t : getOutgoingList()) {
// State s = t.getTo();
// result.add(s);
// result.addAll(s.successors());
// }
// return result;
// }
//
// syn Set<State> State.predecessors() circular [new HashSet<>()] {
// Set<State> result = new HashSet<>();
// for (Transition t : getIncomingList()) {
// State s = t.getFrom();
// result.add(s);
// result.addAll(s.predecessors());
// }
// return result;
// }
//
// syn boolean State.hasCycle() = successors().contains(this);
//
// syn Set<State> State.SCC() {
// Set<State> result = new HashSet<>(successors());
// result.retainAll(predecessors());
// return result;
// }
//
// coll HashSet<Set<State>> StateMachine.SCC() with add root StateMachine;
// State contributes SCC() when SCC().size() > 0 to StateMachine.SCC();
syn Set<State> State.predecessors() circular [new HashSet<>()] {
Set<State> result = new HashSet<>();
for (Transition t : getIncomingList()) {
State s = t.getFrom();
result.add(s);
result.addAll(s.predecessors());
}
return result;
}
// syn Set<Set<State>> StateMachine.SCC() {
// Map<State, Integer> visited = new HashMap<>();
// Deque<State> locked = new LinkedList<>();
//
// // visit nodes forward
// for (State n : states())
// n.visit(visited, locked);
//
// // assign nodes to SCCs backward
// int scc = 0;
// for (State n : locked) {
// n.assign(visited, scc);
// scc++;
// }
//
// Map<Integer, Set<State>> result = visited.entrySet().stream().collect(
// Collectors.groupingBy(
// e -> e.getValue(),
// Collectors.mapping(e -> e.getKey(), Collectors.toSet())
// )
// );
// return result.values().stream().collect(Collectors.toSet());
// }
//
// void State.visit(Map<State, Integer> visited, Deque<State> locked) {
// if (visited.containsKey(this)) return;
// visited.put(this, -1);
// for (Transition t : getOutgoingList())
// t.getTo().visit(visited, locked);
// locked.addFirst(this);
// }
//
// void State.assign(Map<State, Integer> visited, int root) {
// if (visited.get(this) > -1) return;
// visited.put(this, root);
// for (Transition t : getIncomingList())
// t.getFrom().assign(visited, root);
// }
syn Set<Set<State>> StateMachine.SCC() {
Map<State, Set> visited = new HashMap<>();
LinkedList<State> locked = new LinkedList<>();
syn boolean State.hasCycle() = successors().contains(this);
for (State n : states())
if (!visited.containsKey(n))
n.visit(visited, locked); // forward search
syn Set<State> State.SCC() {
Set<State> result = new HashSet<>(successors());
result.retainAll(predecessors());
return result;
for (State n : locked)
if (visited.get(n) == null)
n.assign(visited, new HashSet()); // backward search
return new HashSet(visited.values());
}
void State.visit(Map<State, Set> visited, LinkedList<State> locked) {
visited.put(this, null);
for (Transition t : getOutgoingList())
if (!visited.containsKey(t.getTo()))
t.getTo().visit(visited, locked);
locked.addFirst(this);
}
coll HashSet<Set<State>> StateMachine.SCC() with add root StateMachine;
State contributes SCC() when SCC().size() > 0 to StateMachine.SCC();
void State.assign(Map<State, Set> visited, Set root) {
root.add(this);
visited.put(this, root);
for (Transition t : getIncomingList())
if (visited.get(t.getFrom()) == null)
t.getFrom().assign(visited, root);
}
}
......@@ -119,52 +119,55 @@ public class Main {
stateMachine.addElement(gg);
{
Set<Set<State>> smSCC = stateMachine.SCC();
Set<Set<Component>> dgComponentSCCType = stateMachine.dependencyGraph().SCC();
Set<Set<State>> dgSCCType = dgComponentSCCType.stream()
Set<Set<State>> dgSCC = dgComponentSCCType.stream()
.map(scc -> scc.stream().map(Component::getState).collect(Collectors.toSet()))
.collect(Collectors.toSet());
List<Set<Component>> dgJGraphTSCCType = jgraphtComputeSCC(stateMachine.dependencyGraph());
Set<Set<State>> dgSCCType2 = dgJGraphTSCCType.stream()
Set<Set<State>> jgSCC = dgJGraphTSCCType.stream()
.map(scc -> scc.stream().map(Component::getState).collect(Collectors.toSet()))
.collect(Collectors.toSet());
System.out.println("\nType Dependency analysis:");
System.out.println(" DepGrap SCC: " + dgSCCType);
System.out.println(" JGraphT SCC: " + dgSCCType2);
System.out.println(" SCCs are equal: " + dgSCCType.equals(dgSCCType2));
System.out.println(" StateMachine SCC: " + smSCC);
System.out.println(" DepGrap SCC: " + dgSCC);
System.out.println(" JGraphT SCC: " + jgSCC);
System.out.println(" SCCs are equal: " + smSCC.equals(dgSCC));
System.out.println(" SCCs are equal: " + smSCC.equals(jgSCC));
System.out.println("\n\n");
try {
new File("graph/statemachine/dot/").mkdirs();
int sccIndex = 0;
for (Set<Component> scc : dgComponentSCCType) {
if (scc.size() > 1) {
PrintWriter writer = new PrintWriter("graph/statemachine/dot/scc" + sccIndex + ".dot");
writer.println(stateMachine.dependencyGraph().dotGraph(scc).toDot());
writer.flush();
writer.close();
sccIndex++;
}
}
new File("graph/statemachine/puml/").mkdirs();
sccIndex = 0;
for (Set<Component> scc : dgComponentSCCType) {
if (scc.size() > 1) {
PrintWriter writer = new PrintWriter("graph/statemachine/puml/scc" + sccIndex + ".puml");
writer.println(stateMachine.dependencyGraph().dotGraph(scc).toPlant("class", ".."));
writer.flush();
writer.close();
sccIndex++;
}
}
} catch (FileNotFoundException exception) {
exception.printStackTrace();
}
// try {
//
// new File("graph/statemachine/dot/").mkdirs();
// int sccIndex = 0;
// for (Set<Component> scc : dgComponentSCCType) {
// if (scc.size() > 1) {
// PrintWriter writer = new PrintWriter("graph/statemachine/dot/scc" + sccIndex + ".dot");
// writer.println(stateMachine.dependencyGraph().dotGraph(scc).toDot());
// writer.flush();
// writer.close();
// sccIndex++;
// }
// }
//
// new File("graph/statemachine/puml/").mkdirs();
// sccIndex = 0;
// for (Set<Component> scc : dgComponentSCCType) {
// if (scc.size() > 1) {
// PrintWriter writer = new PrintWriter("graph/statemachine/puml/scc" + sccIndex + ".puml");
// writer.println(stateMachine.dependencyGraph().dotGraph(scc).toPlant("class", ".."));
// writer.flush();
// writer.close();
// sccIndex++;
// }
// }
//
// } catch (FileNotFoundException exception) {
// exception.printStackTrace();
// }
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment