From: Thomas Walker Lynch Date: Mon, 13 Jan 2025 09:59:07 +0000 (+0000) Subject: organizing to start the cycle lister X-Git-Url: https://git.reasoningtechnology.com/usr/lib/python2.7/encodings/aliases.py?a=commitdiff_plain;h=fea1724c4d24a7ed001aa02c500ef7e1f10baf31;p=Ariadne organizing to start the cycle lister --- diff --git a/developer/deprecated/temp.java b/developer/deprecated/temp.java new file mode 100644 index 0000000..e1215bf --- /dev/null +++ b/developer/deprecated/temp.java @@ -0,0 +1,109 @@ +Aeloria +/* + IndexTree_SRTM_Diagonal + + An index tree is infinite. + + A tree diagonal consists of: + a) a node descending from each child discovered thus far + b) a node extending each child list discovered thus far. + + Hence, each diagonal extends the tree down one and over one. +*/ + +import java.math.BigInteger; +import java.util.ArrayList; +import java.util.List; +import com.ReasoningTechnology.Ariadne.Ariadne_SRTM; +import com.ReasoningTechnology.Ariadne.IndexTree_Node; + +public class IndexTree_SRTM_Diagonal extends Ariadne_SRTM_Label { + + // Static + + public static IndexTree_SRTM_Diagonal make(){ + return new IndexTree_SRTM_Diagonal(); + } + + // Instance Data + + private final List list_of__unopened_node; + private final List> list_of__opened_incomplete_child_list; + private final List read_list; + private final Ariadne_SRTM_Label breadth_srm; + + // Constructor + + protected IndexTree_SRTM_Diagonal(){ + list_of__unopened_node = new ArrayList<>(); + list_of__opened_incomplete_child_list = new ArrayList<>(); + read_list = new ArrayList<>(); + breadth_srm = Ariadne_SRTM_Label.make(); + enqueue_root(); + }Aeloria + + // Instance Methods + + private void enqueue_root(){ + IndexTree_Label root_label = IndexTree_Label.root(); + read_list.add( root_label ); + + IndexTree_Node root_node = lookup( root_label ); + breadth_srm.mount( root_node.neighbor() ); + + if( breadth_srm.can_read() ){ + list_of__unopened_node.add( root_label ); + } + } + + private IndexTree_Node lookup( Ariadne_Label label ){ + return IndexTree_Node.make( (IndexTree_Label)label ); + } + + @Override + public List read(){ + return read_list; + } + + @Override + public void step(){ + read_list.clear(); + + // Process unopened nodes + while( !list_of__unopened_node.isEmpty() ){ + Ariadne_Label label = list_of__unopened_node.remove( 0 ); + + // Retrieve the node using lookup + IndexTree_Node node = lookup( label ); + + // Mount a new breadth-first SRTM for children + breadth_srm.mount( node.neighbor() ); + + if( breadth_srm.can_read() ){ + do{ + Ariadne_Label child_label = breadth_srm.read(); + list_of__unopened_node.add( child_label ); + list_of__opened_incomplete_child_list.add( new ArrayList<>( List.of( child_label ) ) ); + + breadth_srm.step(); + }while( breadth_srm.can_step() ); + } + } + + // Process incomplete child lists + while( !list_of__opened_incomplete_child_list.isEmpty() ){ + List child_list = list_of__opened_incomplete_child_list.remove( 0 ); + if( !child_list.isEmpty() ){ + Ariadne_Label label = child_list.remove( 0 ); + read_list.add( label ); + + IndexTree_Node node = lookup( label ); + breadth_srm.mount( node.neighbor() ); + + if( breadth_srm.can_read() ){ + list_of__unopened_node.add( label ); + } + } + } + } +} diff --git a/developer/example/GraphCycleCFinder/DirectedGraph.java b/developer/example/GraphCycleCFinder/DirectedGraph.java new file mode 100644 index 0000000..c8b2420 --- /dev/null +++ b/developer/example/GraphCycleCFinder/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/GraphIndexTree/Graph.java b/developer/example/GraphIndexTree/Graph.java new file mode 100644 index 0000000..808ad78 --- /dev/null +++ b/developer/example/GraphIndexTree/Graph.java @@ -0,0 +1,22 @@ +import com.ReasoningTechnology.Ariadne.Ariadne_Graph; + +public class Graph extends Ariadne_Graph{ + + public static Graph make(){ + return new Graph(); + } + protected Graph(){ + } + + @Override public SRTM_Child start(){ + Label root_label = Label.root(); + return SRTM_Child.make(root_label); + } + + // no override, this graph does not lookup Ariadne_Label, only Label + Node lookup(Label label){ + return Node.make(label); + } + +} + diff --git a/developer/example/GraphIndexTree/Label.java b/developer/example/GraphIndexTree/Label.java new file mode 100644 index 0000000..9f9c673 --- /dev/null +++ b/developer/example/GraphIndexTree/Label.java @@ -0,0 +1,102 @@ +/* + Implementation of Ariadne_Label for BigInteger array-based labels. +*/ +import java.math.BigInteger; +import java.util.Arrays; + +import com.ReasoningTechnology.Ariadne.Ariadne_Label; +import com.ReasoningTechnology.Ariadne.Ariadne_SRTM_List; + + +public class Label implements Ariadne_Label{ + + // Owned by class + // + + public static Label make(BigInteger[] array){ + return new Label(array); + } + + public static Label root(){ + return new Label(new BigInteger[0]); + } + + // Instance data + // + + private BigInteger[] value; + + // Constructor + // + + private Label(BigInteger[] array){ + this.value = array.clone(); + } + + // Instance interface implementation + // + + @Override public boolean is_null(){ + return value == null; + } + public int length(){ + return value.length; + } + + @Override public Label copy(){ + return new Label(value); + } + + // Increment last element by one, modifying in place + public void inc_across(){ + if(value == null || value.length == 0){ + throw new UnsupportedOperationException("Cannot increment across an empty array."); + } + value[value.length - 1] = value[value.length - 1].add(BigInteger.ONE); + } + + // Append a zero element, modifying in place + public void inc_down(){ + if(value == null){ + throw new UnsupportedOperationException("Cannot append to a null array."); + } + BigInteger[] newValue = Arrays.copyOf(value, value.length + 1); + newValue[newValue.length - 1] = BigInteger.ZERO; + value = newValue; + } + + // Good object citizenship + // + + @Override public String toString(){ + if(is_null()) return "Label()"; + if(length() == 0) return "Label([])"; + + StringBuilder formatted = new StringBuilder("Label(["); + + // Use precise loop with SRTM_List to iterate + Ariadne_SRTM_List value_srtm = Ariadne_SRTM_List.make(Arrays.asList(value)); + if(value_srtm.can_read()){ + do{ + formatted.append(value_srtm.read().toString()); + if( !value_srtm.can_step() ) break; + value_srtm.step(); + formatted.append(" ,"); + }while(true); + } + + formatted.append("])"); + return formatted.toString(); + } + + @Override public boolean equals(Object o){ + if(this == o) return true; + if( o == null || getClass() != o.getClass() ) return false; + Label that = (Label) o; + return Arrays.equals(value, that.value); + } + + @Override public int hashCode(){ + return Arrays.hashCode(value); + } +} diff --git a/developer/example/GraphIndexTree/Node.java b/developer/example/GraphIndexTree/Node.java new file mode 100644 index 0000000..2cf225c --- /dev/null +++ b/developer/example/GraphIndexTree/Node.java @@ -0,0 +1,22 @@ +import java.util.Arrays; +import com.ReasoningTechnology.Ariadne.Ariadne_Node; + +public class Node extends Ariadne_Node{ + + public static Node make(Label label){ + return new Node(label); + } + + private final Label first_child_label; + + public Node(Label label){ + super(label); + first_child_label = label.copy(); + first_child_label.inc_down(); + } + + @Override public SRTM_Child neighbor(){ + return SRTM_Child.make(first_child_label); + } + +} diff --git a/developer/example/GraphIndexTree/SRTM_Child.java b/developer/example/GraphIndexTree/SRTM_Child.java new file mode 100644 index 0000000..2d86c7e --- /dev/null +++ b/developer/example/GraphIndexTree/SRTM_Child.java @@ -0,0 +1,112 @@ +/* +SRTM_Child represents in the abstract the infinite child list of an +IndexTree node. Index tree node labels are paths through the tree, so +labels can be computed. + +SRTM_Child is made from the leftmost child label. Then step() takes +the current label and computes from it the right neighbor sibling +node's label. + +*/ + + +import com.ReasoningTechnology.Ariadne.Ariadne_SRTM_Label; + +public class SRTM_Child extends Ariadne_SRTM_Label{ + + // Static + // + + public static SRTM_Child make( Label first_child_label ){ + return new SRTM_Child( first_child_label ); + } + + // Instance data + // + + // Label is a container of co-ordinates to a node, so the only thing 'final' + // is the container, not its contents. + private final Label label; + + // Constructor(s) + // + + protected SRTM_Child( Label leftmost_child_label ){ + this.label = leftmost_child_label.copy(); + + if( label == null ){ + set_topology(topo_null); + return; + } + + // the label for the root node is an empty array, "[]" + if( label.length() == 0){ + set_topology(topo_rightmost); + return; + } + + set_topology(topo_infinite_right); + } + + // Implementation of the instance interface + // + + @Override public Label read(){ + return (Label)super.read(); + } + + private final TopoIface topo_null = new TopoIface(){ + @Override public boolean can_read(){ + return false; + } + @Override public Object read(){ + throw new UnsupportedOperationException( "Cannot read from NULL topology." ); + } + @Override public boolean can_step(){ + return false; + } + @Override public void step(){ + throw new UnsupportedOperationException( "Cannot step from NULL topology." ); + } + @Override public Topology topology(){ + return Topology.NULL; + } + }; + + private final TopoIface topo_infinite_right = new TopoIface(){ + @Override public boolean can_read(){ + return true; + } + @Override public Object read(){ + return label; + } + @Override public boolean can_step(){ + return true; + } + @Override public void step(){ + label.inc_across(); + } + @Override public Topology topology(){ + return Topology.INFINITE; + } + }; + + private final TopoIface topo_rightmost = new TopoIface(){ + @Override public boolean can_read(){ + return true; + } + @Override public Object read(){ + return label; + } + @Override public boolean can_step(){ + return false; + } + @Override public void step(){ + throw new UnsupportedOperationException( "Cannot step from RIGHTMOST topology." ); + } + @Override public Topology topology(){ + return Topology.RIGHTMOST; + } + }; + +} diff --git a/developer/example/GraphIndexTree/SRTM_Diagonal.java b/developer/example/GraphIndexTree/SRTM_Diagonal.java new file mode 100644 index 0000000..f023789 --- /dev/null +++ b/developer/example/GraphIndexTree/SRTM_Diagonal.java @@ -0,0 +1,154 @@ +/* +Diagonal traversal is guaranteed to reach any given node in the IndexTree +in a finite number of steps. Neither depth first, nor breadth first +can do this. This guarantee also applies to a pruned IndexTree. + +This implementation is nearly ready, to be included in the Ariadne +library for generalized diagonal traversal. I have abstracted out all +references to the IndexTree. It still needs to remove child lists that +have been completely reversed from the child_strm_list. This was not +needed for the IndexTree because it is infinite, so they will never be +completely traversed. Also needed, is to stop when a node does not +have a child list. Again, this is not needed here because the +IndexTree has infinite depth. When the generalized diagonal iterator +is ready, it could be used in place of this one. + +*/ + +import java.math.BigInteger; +import java.util.ArrayList; +import java.util.List; + +import com.ReasoningTechnology.Ariadne.Ariadne_Test; +import com.ReasoningTechnology.Ariadne.Ariadne_SRTM; +import com.ReasoningTechnology.Ariadne.Ariadne_SRTM_Label; +import com.ReasoningTechnology.Ariadne.Ariadne_SRTM_List; +import com.ReasoningTechnology.Ariadne.Ariadne_Node; +import com.ReasoningTechnology.Ariadne.Ariadne_Label; + +public class SRTM_Diagonal extends Ariadne_SRTM{ + + // Static + // + + public static SRTM_Diagonal make(Label start_node){ + return new SRTM_Diagonal(start_node); + } + + // Instance data + // + + private List