From 2dcb366ffa42787f1e462e86ff43239f3d912715 Mon Sep 17 00:00:00 2001 From: Thomas Walker Lynch Date: Wed, 15 Jan 2025 12:35:26 +0000 Subject: [PATCH] adds entanglment copy and rewind to tm sr nd --- ...Array_CLI.java => TM_SR_ND_Array_CLI.java} | 0 ...ript.txt => TM_SR_ND_Array_transcript.txt} | 0 ...M_List_CLI.java => TM_SR_ND_List_CLI.java} | 4 +- ...cript.txt => TM_SR_ND_List_transcript.txt} | 0 .../example/TM_SR_ND/TM_SR_ND_Print_CLI.java | 21 ++++ .../TM_SR_ND/TM_SR_ND_Print_transcript.txt | 2 + ..._TM_Set_CLI.java => TM_SR_ND_Set_CLI.java} | 0 .../Ariadne_TM_SR_ND.java" | 106 +++++++++++++----- .../Ariadne_TM_SR_ND_Array.java" | 43 +++---- .../Ariadne_TM_SR_ND_List.java" | 77 ++++++++----- .../javac\360\237\226\211/Ariadne_Test.java" | 4 + 11 files changed, 178 insertions(+), 79 deletions(-) rename developer/example/TM_SR_ND/{ND_SR_TM_Array_CLI.java => TM_SR_ND_Array_CLI.java} (100%) rename developer/example/TM_SR_ND/{ND_SR_TM_Array_transcript.txt => TM_SR_ND_Array_transcript.txt} (100%) rename developer/example/TM_SR_ND/{ND_SR_TM_List_CLI.java => TM_SR_ND_List_CLI.java} (100%) rename developer/example/TM_SR_ND/{ND_SR_TM_List_transcript.txt => TM_SR_ND_List_transcript.txt} (100%) create mode 100644 developer/example/TM_SR_ND/TM_SR_ND_Print_CLI.java create mode 100644 developer/example/TM_SR_ND/TM_SR_ND_Print_transcript.txt rename developer/example/TM_SR_ND/{ND_SR_TM_Set_CLI.java => TM_SR_ND_Set_CLI.java} (100%) diff --git a/developer/example/TM_SR_ND/ND_SR_TM_Array_CLI.java b/developer/example/TM_SR_ND/TM_SR_ND_Array_CLI.java similarity index 100% rename from developer/example/TM_SR_ND/ND_SR_TM_Array_CLI.java rename to developer/example/TM_SR_ND/TM_SR_ND_Array_CLI.java diff --git a/developer/example/TM_SR_ND/ND_SR_TM_Array_transcript.txt b/developer/example/TM_SR_ND/TM_SR_ND_Array_transcript.txt similarity index 100% rename from developer/example/TM_SR_ND/ND_SR_TM_Array_transcript.txt rename to developer/example/TM_SR_ND/TM_SR_ND_Array_transcript.txt diff --git a/developer/example/TM_SR_ND/ND_SR_TM_List_CLI.java b/developer/example/TM_SR_ND/TM_SR_ND_List_CLI.java similarity index 100% rename from developer/example/TM_SR_ND/ND_SR_TM_List_CLI.java rename to developer/example/TM_SR_ND/TM_SR_ND_List_CLI.java index 127d510..8852f37 100644 --- a/developer/example/TM_SR_ND/ND_SR_TM_List_CLI.java +++ b/developer/example/TM_SR_ND/TM_SR_ND_List_CLI.java @@ -1,8 +1,8 @@ +import java.util.LinkedList; + import com.ReasoningTechnology.Ariadne.Ariadne_TM_SR_ND; import com.ReasoningTechnology.Ariadne.Ariadne_TM_SR_ND_List; -import java.util.LinkedList; - public class TM_SR_ND_List_CLI { public static void main( String[] args ){ // Create a linked list diff --git a/developer/example/TM_SR_ND/ND_SR_TM_List_transcript.txt b/developer/example/TM_SR_ND/TM_SR_ND_List_transcript.txt similarity index 100% rename from developer/example/TM_SR_ND/ND_SR_TM_List_transcript.txt rename to developer/example/TM_SR_ND/TM_SR_ND_List_transcript.txt diff --git a/developer/example/TM_SR_ND/TM_SR_ND_Print_CLI.java b/developer/example/TM_SR_ND/TM_SR_ND_Print_CLI.java new file mode 100644 index 0000000..4e24e17 --- /dev/null +++ b/developer/example/TM_SR_ND/TM_SR_ND_Print_CLI.java @@ -0,0 +1,21 @@ +/* +Example of the TM_SR_ND's toString function. + +*/ + +import java.util.Arrays; +import java.util.List; + +import com.ReasoningTechnology.Ariadne.Ariadne_TM_SR_ND; +import com.ReasoningTechnology.Ariadne.Ariadne_TM_SR_ND_List; + +public class TM_SR_ND_Print_CLI{ + + public static void main(String[] args){ + List data = Arrays.asList(42 ,null ,"" ,"World" ,1000); + Ariadne_TM_SR_ND tm = Ariadne_TM_SR_ND_List.make(data); + tm.step(); + tm.step(); + System.out.println(tm.toString()); + } +} diff --git a/developer/example/TM_SR_ND/TM_SR_ND_Print_transcript.txt b/developer/example/TM_SR_ND/TM_SR_ND_Print_transcript.txt new file mode 100644 index 0000000..6ff8231 --- /dev/null +++ b/developer/example/TM_SR_ND/TM_SR_ND_Print_transcript.txt @@ -0,0 +1,2 @@ +TM_SR_ND(SEGMENT( 42 World 1000 ) + |dd||-||ddddd||dddd| diff --git a/developer/example/TM_SR_ND/ND_SR_TM_Set_CLI.java b/developer/example/TM_SR_ND/TM_SR_ND_Set_CLI.java similarity index 100% rename from developer/example/TM_SR_ND/ND_SR_TM_Set_CLI.java rename to developer/example/TM_SR_ND/TM_SR_ND_Set_CLI.java diff --git "a/developer/javac\360\237\226\211/Ariadne_TM_SR_ND.java" "b/developer/javac\360\237\226\211/Ariadne_TM_SR_ND.java" index a53a211..4d53353 100644 --- "a/developer/javac\360\237\226\211/Ariadne_TM_SR_ND.java" +++ "b/developer/javac\360\237\226\211/Ariadne_TM_SR_ND.java" @@ -17,6 +17,8 @@ public class Ariadne_TM_SR_ND{ // static // + protected static int id_well = 100; + public enum Topology{ NULL ,CYCLIC @@ -32,14 +34,18 @@ public class Ariadne_TM_SR_ND{ // instance data // + private int id; + Ariadne_Test test = null; protected TopoIface current_topology; - public final TopoIface not_mounted = new NotMounted(); protected BigInteger index; // constructor(s) // public Ariadne_TM_SR_ND(){ + id = id_well++; + test = Ariadne_Test.make("Ariadne_TM_SR_ND::" + id + "::"); + test.switch_test(false); set_topology( not_mounted ); this.index = BigInteger.ZERO; } @@ -47,6 +53,10 @@ public class Ariadne_TM_SR_ND{ // Implementation of instance interface. // + public int id(){ + return this.id; + } + public BigInteger head_address(){ return index; } @@ -56,43 +66,62 @@ public class Ariadne_TM_SR_ND{ } public boolean head_on_same_cell(Ariadne_TM_SR_ND tm){ - return this.index.equals(tm.index); + boolean p = this.index.equals(tm.index); + if( test.is_on() ){ + test.print("head_on_same_cell this id/index: " + this.id() + "/" + this.index ); + test.print("head_on_same_cell tm id/index: " + tm.id() + "/" + tm.index ); + test.print("head_on_same_cell returning: " + p ); + } + return p; } - public Ariadne_TM_SR_ND entangle(){ - Ariadne_TM_SR_ND copy = make(); - - // entangled copy shares the same tape - copy.current_topology = this.current_topology; // Shares the same reference - + protected void entangle(Ariadne_TM_SR_ND copy){ + copy.current_topology = this.current_topology; // Nuance here, BigInteger is immutable, so operation on the original // index, and the copy index, will be independent, which is what we want. copy.index = this.index; - - return copy; + } + public Ariadne_TM_SR_ND entangle(){ + throw new UnsupportedOperationException("Ariadne_TM_SR_ND::entangle not implemented."); } public boolean is_mounted(){ return current_topology != null - && current_topology != not_mounted; + && current_topology != not_mounted + ; + } + + public boolean can_rewind(){ + return false; + } + + public void rewind(){ + index = BigInteger.ZERO; } public boolean can_read(){ - return current_topology.can_read(); + boolean p = current_topology.can_read(); + if( test.is_on() ) test.print("can_read " + p); + return p; } public Object read(){ - return current_topology.read(); + Object o = current_topology.read(); + if( test.is_on() ) test.print("read: " + o); + return o; } public boolean can_step(){ - return current_topology.can_step(); + boolean p = current_topology.can_step(); + if( test.is_on() ) test.print("can_step " + p); + return p; } public void step(){ - current_topology.step(); increment(); + if( test.is_on() ) test.print("step: " + index); + current_topology.step(); } public Topology topology(){ @@ -102,6 +131,14 @@ public class Ariadne_TM_SR_ND{ // Sets the tape access methods to be used. protected void set_topology(TopoIface new_topology){ current_topology = new_topology; + if( test.is_on() ){ + test.print("set_topology i: " + index); + if(is_mounted()){ + test.print("set_topology to: " + new_topology.topology()); + }else{ + test.print("set_topology to: Unmounted"); + } + } } protected interface TopoIface{ @@ -112,8 +149,7 @@ public class Ariadne_TM_SR_ND{ Topology topology(); } - // Initially, the tape has not been mounted. - protected class NotMounted implements TopoIface{ + protected final TopoIface not_mounted = new TopoIface(){ @Override public boolean can_read(){ return false; } @@ -129,8 +165,25 @@ public class Ariadne_TM_SR_ND{ @Override public Topology topology(){ throw new UnsupportedOperationException("Ariadne_TM_SR_ND::NotMounted::topology."); } - } + }; + protected 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 over null topology." ); + } + @Override public Topology topology(){ + return Topology.NULL; + } + }; // good citizen // @@ -139,28 +192,31 @@ public class Ariadne_TM_SR_ND{ if(!is_mounted()) return "TM_SR_ND(NotMounted)"; if(!can_read()) return "TM_SR_ND(Null)"; - StringBuilder data_channel = new StringBuilder(" "); - StringBuilder control_channel = new StringBuilder("|"); + StringBuilder data_channel = new StringBuilder(""); + StringBuilder control_channel = new StringBuilder(""); data_channel.append( "TM_SR_ND(" ).append( topology().name()).append("( " ); control_channel.append( " ".repeat(data_channel.length()) ); String element = null; - Ariadne_TM_SR_ND copy = (Ariadne_TM_SR_ND) this.entangle(); + Ariadne_TM_SR_ND copy = this.entangle(); + if( copy.can_rewind() ) copy.rewind(); + Object o = null; do{ + o = copy.read(); if(o == null){ data_channel.append( " ".repeat(3) ); - if(head_on_same_cell(copy)){ + if( head_on_same_cell(copy) ){ control_channel.append("<->"); }else{ control_channel.append("|-|"); } }else if(o.toString().isEmpty()){ data_channel.append( " ".repeat(3) ); - if(head_on_same_cell(copy)){ + if( head_on_same_cell(copy) ){ control_channel.append(""); }else{ control_channel.append("|e|"); @@ -181,8 +237,9 @@ public class Ariadne_TM_SR_ND{ } } - if(!copy.can_step()) break; + if( !copy.can_step() ) break; copy.step(); + }while(true); data_channel.append(" )"); @@ -192,7 +249,6 @@ public class Ariadne_TM_SR_ND{ data_channel .append("\n") .append(control_channel) - .append("\n") .toString(); } diff --git "a/developer/javac\360\237\226\211/Ariadne_TM_SR_ND_Array.java" "b/developer/javac\360\237\226\211/Ariadne_TM_SR_ND_Array.java" index 32c5da2..62a88dc 100644 --- "a/developer/javac\360\237\226\211/Ariadne_TM_SR_ND_Array.java" +++ "b/developer/javac\360\237\226\211/Ariadne_TM_SR_ND_Array.java" @@ -17,10 +17,6 @@ public class Ariadne_TM_SR_ND_Array extends Ariadne_TM_SR_ND{ private final List array; - private final TopoIface topo_null = new TopoNull(); - private final TopoIface topo_segment = new TopoSegment(); - private final TopoIface topo_rightmost = new TopoRightmost(); - // Constructor protected Ariadne_TM_SR_ND_Array(List array){ super(); @@ -39,27 +35,22 @@ public class Ariadne_TM_SR_ND_Array extends Ariadne_TM_SR_ND{ set_topology( topo_segment ); } - // TopoNull - private class TopoNull implements TopoIface{ - @Override public boolean can_read(){ - return false; - } - @Override public Object read(){ - throw new UnsupportedOperationException( "Cannot read from NULL topo." ); - } - @Override public boolean can_step(){ - return false; - } - @Override public void step(){ - throw new UnsupportedOperationException( "Cannot step from NULL topo." ); - } - @Override public Topology topology(){ - return Topology.NULL; + // instance interface implementation + + @Override public boolean can_rewind(){ + return true; + } + + @Override public void rewind() { + super.rewind(); + if (array == null || array.isEmpty()) { + set_topology(topo_null); // Null topology for empty or null arrays + return; } + set_topology(array.size() == 1 ? topo_rightmost : topo_segment); // Adjust topology } - // TopoSegment - private class TopoSegment implements TopoIface{ + protected final TopoIface topo_segment = new TopoIface(){ @Override public boolean can_read(){ return true; } @@ -76,10 +67,9 @@ public class Ariadne_TM_SR_ND_Array extends Ariadne_TM_SR_ND{ @Override public Topology topology(){ return Topology.SEGMENT; } - } + }; - // TopoRightmost - private class TopoRightmost implements TopoIface{ + protected final TopoIface topo_rightmost = new TopoIface(){ @Override public boolean can_read(){ return true; } @@ -95,5 +85,6 @@ public class Ariadne_TM_SR_ND_Array extends Ariadne_TM_SR_ND{ @Override public Topology topology(){ return Topology.RIGHTMOST; } - } + }; + } diff --git "a/developer/javac\360\237\226\211/Ariadne_TM_SR_ND_List.java" "b/developer/javac\360\237\226\211/Ariadne_TM_SR_ND_List.java" index 85d9b1c..9163754 100644 --- "a/developer/javac\360\237\226\211/Ariadne_TM_SR_ND_List.java" +++ "b/developer/javac\360\237\226\211/Ariadne_TM_SR_ND_List.java" @@ -27,10 +27,6 @@ public class Ariadne_TM_SR_ND_List extends Ariadne_TM_SR_ND{ private ListIterator iterator; // Iterator for traversal private T read_value; // Stores the current cell value - private final TopoIface topo_null = new TopoNull(); - private final TopoIface topo_segment = new TopoSegment(); - private final TopoIface topo_rightmost = new TopoRightmost(); - // constructor(s) // @@ -57,31 +53,60 @@ public class Ariadne_TM_SR_ND_List extends Ariadne_TM_SR_ND{ // instance interface implementation // + protected void entangle(Ariadne_TM_SR_ND_List copy){ + super.entangle(copy); + copy.read_value = this.read_value; + copy.iterator = this.list.listIterator(this.iterator.nextIndex()); + } + + @Override public Ariadne_TM_SR_ND_List entangle(){ + Ariadne_TM_SR_ND_List copy = Ariadne_TM_SR_ND_List.make(this.list); + + // Copy shared fields + copy.index = this.index; // Copy the step count + copy.read_value = this.read_value; // Synchronize the current read value + copy.iterator = this.list.listIterator(this.iterator.nextIndex()); // Align iterator + + // Set the appropriate topology in the copy based on the current topology + switch (this.current_topology.topology()) { + case NULL: + copy.current_topology = copy.topo_null; + break; + case SEGMENT: + copy.current_topology = copy.topo_segment; + break; + case RIGHTMOST: + copy.current_topology = copy.topo_rightmost; + break; + default: + throw new IllegalStateException("Unexpected topology: " + this.current_topology.topology()); + } + + return copy; + } + + @Override public boolean can_rewind(){ + return true; + } + + @Override public void rewind(){ + super.rewind(); + if(list == null || list.isEmpty()){ + set_topology(topo_null); + return; + } + iterator = list.listIterator(); // Reset the iterator + read_value = iterator.next(); // Sync the read value + set_topology(list.size() == 1 ? topo_rightmost : topo_segment); // Adjust topology + } + @Override @SuppressWarnings("unchecked") public T read(){ return (T) current_topology.read(); // Cast to ensure T is returned } - private class TopoNull implements TopoIface{ - @Override public boolean can_read(){ - return false; - } - @Override public T read(){ - throw new UnsupportedOperationException( "Cannot read from NULL topo." ); - } - @Override public boolean can_step(){ - return false; - } - @Override public void step(){ - throw new UnsupportedOperationException( "Cannot step over NULL topo." ); - } - @Override public Topology topology(){ - return Topology.NULL; - } - } - - private class TopoSegment implements TopoIface{ + protected final TopoIface topo_segment = new TopoIface(){ @Override public boolean can_read(){ return true; } @@ -98,9 +123,9 @@ public class Ariadne_TM_SR_ND_List extends Ariadne_TM_SR_ND{ @Override public Topology topology(){ return Topology.SEGMENT; } - } + }; - private class TopoRightmost implements TopoIface{ + protected final TopoIface topo_rightmost = new TopoIface(){ @Override public boolean can_read(){ return true; } @@ -116,7 +141,7 @@ public class Ariadne_TM_SR_ND_List extends Ariadne_TM_SR_ND{ @Override public Topology topology(){ return Topology.RIGHTMOST; } - } + }; } diff --git "a/developer/javac\360\237\226\211/Ariadne_Test.java" "b/developer/javac\360\237\226\211/Ariadne_Test.java" index 5ce6637..fd6618d 100644 --- "a/developer/javac\360\237\226\211/Ariadne_Test.java" +++ "b/developer/javac\360\237\226\211/Ariadne_Test.java" @@ -14,6 +14,10 @@ public class Ariadne_Test { protected Ariadne_Test(){ } + public boolean is_on(){ + return test; + } + public void switch_test(boolean enable){ if( test && !enable ){ print("test messages off"); -- 2.20.1