From: Thomas Walker Lynch Date: Tue, 14 Jan 2025 15:14:24 +0000 (+0000) Subject: strm typos -> srtm X-Git-Url: https://git.reasoningtechnology.com/home/twl/Documents/subuser/test_env.py?a=commitdiff_plain;h=37a7d2350a6734d12b853432c0ab6db922258108;p=Ariadne strm typos -> srtm --- diff --git a/developer/deprecated/DirectedGraph.java b/developer/deprecated/DirectedGraph.java new file mode 100644 index 0000000..c8b2420 --- /dev/null +++ b/developer/deprecated/DirectedGraph.java @@ -0,0 +1,234 @@ +package com.ReasoningTechnology.Ariadne; + +import java.util.HashMap; +import java.util.Map; +import java.util.List; +import java.util.ArrayList; + +public class Ariadne_GraphDirectedAcyclic extends Ariadne_Graph{ + + // test messaging + // + private static boolean test = false; + public static void test_switch(boolean test){ + if(Ariadne_Graph.test && !test){ + test_print("Ariadne_Graph:: test messages off"); + } + if(!Ariadne_Graph.test && test){ + test_print("Ariadne_Graph:: test messages on"); + } + } + private static void test_print(String message){ + if(test){ + System.out.println(message); + } + } + + // class data + // + + // marks this class might put on a node + public static Ariadne_TokenSet node_marks = new Ariadne_TokenSet(){{ + add(new Ariadne_Token("empty_root_label_list")); + add(new Ariadne_Token("cycle_exists")); + add(new Ariadne_Token("undefined_node_exists")); + add(new Ariadne_Token("bad_descent_termination")); + add(new Ariadne_Token("max_depth_reached")); + }}; + + // graph descend method return values + private static Ariadne_TokenSet graph_descend_set = new Ariadne_TokenSet(){{ + add(new Ariadne_Token("empty_path_stack")); + add(new Ariadne_Token("cycle_found")); + add(new Ariadne_Token("undefined_node")); + add(new Ariadne_Token("leaf")); + add(new Ariadne_Token("max_depth_reached")); + }}; + + + // instance data + + // constructors + // + public Ariadne_GraphDirectedAcyclic(){ + super(new HashMap<>(), null); + } + + public Ariadne_GraphDirectedAcyclic(Map node_map, Ariadne_DefinitionList recognizer_f_list, Ariadne_LabelList root_node_list, int max_depth, boolean verbose){ + super(node_map, recognizer_f_list); + Ariadne_TokenSet cycle_detection_result = graph_mark_cycles(root_node_list, max_depth, verbose); + } + + public Ariadne_GraphDirectedAcyclic(Map node_map, Ariadne_DefinitionList recognizer_f_list, Ariadne_LabelList root_node_list){ + super(node_map, recognizer_f_list); + Ariadne_TokenSet cycle_detection_result = graph_mark_cycles(root_node_list); + } + + // interface functions + // + private List path_find_cycle(Ariadne_LabelList path){ + if(path.size() <= 1) return null; + + int rightmost_index = path.size() - 1; + Ariadne_Label rightmost_node_label = path.get(rightmost_index); + + int cycle_leftmost_index = path.indexOf(rightmost_node_label); + Boolean has_cycle = cycle_leftmost_index < rightmost_index; + if(!has_cycle) return null; + + List result = new ArrayList<>(); + result.add(cycle_leftmost_index); + result.add(rightmost_index); + return result; + } + + private boolean graph_descend_cycle_case(Ariadne_LabelList left_path, List path_stack, boolean verbose){ + + List cycle_index_interval = path_find_cycle(left_path); + if(cycle_index_interval == null){ + return false; + } + + int cycle_i0 = cycle_index_interval.get(0); + int cycle_n = cycle_index_interval.get(1); + + if(verbose) Ariadne_Util.print_list( + "Found cycle:", + left_path.subList(cycle_i0, cycle_n + 1) + ); + + Ariadne_LabelList undefined_node_list = new Ariadne_LabelList(); + for (int i = cycle_i0; i <= cycle_n; i++){ + Ariadne_Label node_label = left_path.get(i); + Ariadne_Node node = super.lookup(node_label); + if(node != null){ + node.mark(new Ariadne_Token("cycle_member")); + } else{ + undefined_node_list.add(node_label); + } + } + + if(verbose) Ariadne_Util.print_list( + "Each undefined node could not be marked as a cycle member:", + undefined_node_list + ); + + path_stack.subList(cycle_i0 + 1, cycle_n + 1).clear(); + + return true; + } + + private Ariadne_TokenSet graph_descend(List path_stack, int max_depth, boolean verbose){ + Ariadne_TokenSet ret_value = new Ariadne_TokenSet(); + + if(path_stack.isEmpty()){ + ret_value.add(new Ariadne_Token("empty_path_stack")); + return ret_value; + } + + Ariadne_LabelList left_path = new Ariadne_LabelList(); + for (Ariadne_LabelList neighbor_list : path_stack){ + left_path.add(neighbor_list.get(0)); + } + + do{ + + if(graph_descend_cycle_case(left_path, path_stack, verbose)){ + ret_value.add(new Ariadne_Token("cycle_found")); + return ret_value; + } + + Ariadne_Label it_node_label = path_stack.get(path_stack.size() - 1).get(0); + Ariadne_Node it_node = super.lookup(it_node_label); + if(it_node == null){ + ret_value.add(new Ariadne_Token("undefined_node")); + return ret_value; + } + + Ariadne_LabelList neighbor_list = it_node.neighbor_LabelList(); + if(neighbor_list.isEmpty()){ + ret_value.add(new Ariadne_Token("leaf")); + return ret_value; + } + + path_stack.add(new Ariadne_LabelList(neighbor_list)); + Ariadne_Label it_next_label = neighbor_list.get(0); + left_path.add(it_next_label); + + if(max_depth > 0){ + max_depth--; + if(max_depth == 0){ + if(verbose){ + Ariadne_Util.print_list("GraphDirectedAcyclic.GraphDescend:: max_depth reached, ending descent:", path_stack); + } + ret_value.add(new Ariadne_Token("max_depth_reached")); + return ret_value; + } + } + + } while (true); + } + + + public Ariadne_TokenSet graph_mark_cycles(Ariadne_LabelList root_node_LabelList, int max_depth, boolean verbose){ + Ariadne_TokenSet ret_value = new Ariadne_TokenSet(); + boolean exists_malformed = false; + Ariadne_TokenSet result; + + if(root_node_LabelList.isEmpty()){ + ret_value.add(new Ariadne_Token("empty_root_label_list")); + return ret_value; + } + + List path_stack = new ArrayList<>(); + path_stack.add(new Ariadne_LabelList(root_node_LabelList)); + + do{ + result = graph_descend(path_stack, max_depth, verbose); + if(result.contains(new Ariadne_Token("cycle_found"))) ret_value.add(new Ariadne_Token("cycle_exists")); + if(result.contains(new Ariadne_Token("undefined_node"))) ret_value.add(new Ariadne_Token("undefined_node_exists")); + if(result.contains(new Ariadne_Token("max_depth_reached"))) ret_value.add(new Ariadne_Token("max_depth_reached")); + if(!result.contains(new Ariadne_Token("leaf")) && !result.contains(new Ariadne_Token("cycle_found"))) ret_value.add(new Ariadne_Token("bad_descent_termination")); + + Ariadne_LabelList top_list = path_stack.get(path_stack.size() - 1); + top_list.remove(0); + if(top_list.isEmpty()) path_stack.remove(path_stack.size() - 1); + + } while (!path_stack.isEmpty()); + + if(verbose){ + if(ret_value.contains("bad_descent_termination")){ + System.out.println("GraphDirectedAcyclic.graph_mark_cycles:: terminated with unexpected condition."); + } + if(ret_value.contains("cycle_exists")){ + System.out.println("GraphDirectedAcyclic.graph_mark_cycles:: One or more cycles detected."); + } + if(ret_value.contains("undefined_node_exists")){ + System.out.println("GraphDirectedAcyclic.graph_mark_cycles:: Undefined nodes exist."); + } + } + + return ret_value; + } + + public Ariadne_TokenSet graph_mark_cycles(Ariadne_LabelList root_node_LabelList){ + return graph_mark_cycles(root_node_LabelList, this.debug ? 40 : -1, this.debug); + } + + @Override + public Ariadne_Node lookup(Ariadne_Label node_label, boolean verbose){ + Ariadne_Node node = super.lookup(node_label, verbose); + if(node != null && node.has_mark(new Ariadne_Token("cycle_member"))){ + if(verbose){ + System.out.println("GraphDirectedAcyclic.lookup:: Node is part of a cycle, not returned: " + node_label); + } + return null; + } + return node; + } + + public Ariadne_Node lookup(Ariadne_Label node_label){ + return lookup(node_label, this.debug); + } + +} diff --git a/developer/example/GraphCycleCFinder/#SRTM_Depth.java# b/developer/example/GraphCycleCFinder/#SRTM_Depth.java# new file mode 100644 index 0000000..78de5f7 --- /dev/null +++ b/developer/example/GraphCycleCFinder/#SRTM_Depth.java# @@ -0,0 +1,121 @@ +/* +SRTM_Depth is a list of neighbor SRTMs going down the leftmost side of +a graph traversal. A leftmost traversal is one characterized by +following leftmost not yet visited node on the most recently visited +node's neighbor list. + +Depth traversal from a start node, ends when reaching a node that has +no neighbors, or when reaching encountering a cycle. + +The `Node::neighbor()` function returns an SRTM for iterating over the +node's neighbors. An SRTM is returned rather than a list, because in +general a neighbor list is allowed to be unbounded. Though with a +finite graph, that can not happen. (See IndexTree for an example of an +infinite depth and infinite breadth graph traveral.) + +It is possible to construct and infinite graph such that the +`SRTM_Depth::make()` function would never return. + +For a finite graph, this depth traversal will provably terminate, due +to running out unique (non cycle) nodes to visit. More generally, if +graph traversal from a start node is guaranteed to reach a leaf node +(has no neighbors), or a a cycle, within a finite number of node +traversal steps, then `SRTM_Depth::make()` will always return. + +Each call to step causes the TM read head to move to the next lowest +depth, leftmost unvisited node. This might require backtracking and +descending again. + +Nodes are referenced by label. + +A null valued label is flagged as an error, though we still look it up +in the graph. It is a fatal error, `exit(1)` if `lookup` returns a +null node. + +A path is a list of nodes found while traversing a tree. A road is a +list of child_srtm found while traversing the tree. + +*/ + +import com.ReasoningTechnology.Ariadne.Ariadne_SRTM; +import com.ReasoningTechnology.Ariadne.Ariadne_Graph; + + +class SRTM_Depth{ + + // static + // + + PathStack make(Graph g){ + return new SRTM_Depth(g); + } + + // instance data + // + protected List path; + protected Graph graph; + + // constructor + // + protected SRTM_Depth(Graph g){ + set_topography(topo_null); + if(g == null) return; + graph = g; + path.add( g.start() ); + descend(); + } + + // instance interface implementation + // + + + // A path is terminated by a leaf node or discovering a cycle. + protected boolean extend_path_to_termination(){ + + ArrayList