From 90bb7e959496c3a12bebe055f6344b9f06f22809 Mon Sep 17 00:00:00 2001 From: Nathanael Sensfelder Date: Thu, 3 Aug 2017 15:28:17 +0200 Subject: Improving clarity through better Makefiles. --- instr-to-kodkod/cfg-to-paths/src/Path.java | 102 +++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 instr-to-kodkod/cfg-to-paths/src/Path.java (limited to 'instr-to-kodkod/cfg-to-paths/src/Path.java') diff --git a/instr-to-kodkod/cfg-to-paths/src/Path.java b/instr-to-kodkod/cfg-to-paths/src/Path.java new file mode 100644 index 0000000..9e0897f --- /dev/null +++ b/instr-to-kodkod/cfg-to-paths/src/Path.java @@ -0,0 +1,102 @@ +import java.util.*; + +public class Path +{ + private final ArrayList nodes; + private final Node last_node; + + public static Collection get_all_paths_from (final String root) + { + final Collection result; + final Stack waiting_list; + final Node root_node; + + root_node = Node.get_node(root); + + if (root_node == null) + { + System.err.println + ( + "[E] Could not find root node \"" + + root + + "\"." + ); + + return null; + } + + result = new ArrayList(); + waiting_list = new Stack(); + + waiting_list.push((new Path(Node.get_node(root)))); + + while (!waiting_list.empty()) + { + final Path current_path; + final Node current_node; + final Collection next_nodes; + + current_path = waiting_list.pop(); + current_node = current_path.last_node; + next_nodes = current_node.next_nodes(); + + if (next_nodes.isEmpty()) + { + result.add(current_path); + } + else + { + if (current_node.is_terminal()) + { + result.add(current_path); + } + for (final Node next: next_nodes) + { + waiting_list.push(current_path.add_step(next)); + } + } + } + + return result; + } + + private Path (final Node start) + { + nodes = new ArrayList(); + + nodes.add(start); + + last_node = start; + } + + private Path (final ArrayList nodes, final Node last_node) + { + this.nodes = nodes; + this.last_node = last_node; + + this.nodes.add(last_node); + } + + @SuppressWarnings("unchecked") + /* 'nodes' is an ArrayList, and so should be its clone. */ + private Path add_step (final Node n) + { + return new Path((ArrayList) nodes.clone(), n); + } + + public Collection> get_all_subpaths () + { + final Collection> result; + final int path_length; + + result = new ArrayList>(); + path_length = nodes.size(); + + for (int i = 0; i < path_length; ++i) + { + result.add(nodes.subList(i, path_length)); + } + + return result; + } +} -- cgit v1.2.3-70-g09d2