From c0dee8c6e3b88b5a4b600dfb2d2b48cd22d50e8e Mon Sep 17 00:00:00 2001
From: Jake <jake.read@cba.mit.edu>
Date: Sat, 20 Jul 2019 13:45:04 -0400
Subject: [PATCH] bork, unbork route search

---
 save/systems/00-sys-midroutesearch.json | 634 ++++++++++++++++++++++++
 view/vtoplevel.js                       |  39 +-
 2 files changed, 658 insertions(+), 15 deletions(-)
 create mode 100644 save/systems/00-sys-midroutesearch.json

diff --git a/save/systems/00-sys-midroutesearch.json b/save/systems/00-sys-midroutesearch.json
new file mode 100644
index 0000000..00eede8
--- /dev/null
+++ b/save/systems/00-sys-midroutesearch.json
@@ -0,0 +1,634 @@
+{
+  "interpreterName": "cuttlefish",
+  "interpreterVersion": "v0.1",
+  "hunks": [
+    {
+      "type": "manager",
+      "name": "nrol",
+      "inputs": [
+        {
+          "name": "msgs",
+          "type": "byteArray"
+        }
+      ],
+      "outputs": [
+        {
+          "name": "msgs",
+          "type": "byteArray",
+          "connections": [
+            {
+              "inHunkIndex": 1,
+              "inHunkInput": 0
+            }
+          ]
+        }
+      ],
+      "states": []
+    },
+    {
+      "type": "view",
+      "name": "tlview",
+      "inputs": [
+        {
+          "name": "msgs",
+          "type": "byteArray"
+        }
+      ],
+      "outputs": [
+        {
+          "name": "msgs",
+          "type": "byteArray",
+          "connections": [
+            {
+              "inHunkIndex": 0,
+              "inHunkInput": 0
+            }
+          ]
+        }
+      ],
+      "states": []
+    },
+    {
+      "type": "link",
+      "name": "link_2",
+      "inputs": [
+        {
+          "name": "data",
+          "type": "byteArray"
+        },
+        {
+          "name": "mgrMsgs",
+          "type": "byteArray"
+        },
+        {
+          "name": "auto_0_3",
+          "type": "byteArray"
+        }
+      ],
+      "outputs": [
+        {
+          "name": "data",
+          "type": "byteArray",
+          "connections": [
+            {
+              "inHunkIndex": 3,
+              "inHunkInput": 0
+            }
+          ]
+        },
+        {
+          "name": "mgrMsgs",
+          "type": "byteArray",
+          "connections": [
+            {
+              "inHunkIndex": 4,
+              "inHunkInput": 0
+            }
+          ]
+        },
+        {
+          "name": "auto_0_2",
+          "type": "byteArray",
+          "connections": [
+            {
+              "inHunkIndex": 5,
+              "inHunkInput": 0
+            }
+          ]
+        }
+      ],
+      "states": [
+        {
+          "name": "isActive",
+          "type": "boolean",
+          "value": true
+        },
+        {
+          "name": "otherLink",
+          "type": "uint16",
+          "value": 1
+        },
+        {
+          "name": "inputList",
+          "type": "string",
+          "value": "mgrMsgs (byteArray), auto_0_3 (byteArray)"
+        },
+        {
+          "name": "outputList",
+          "type": "string",
+          "value": "mgrMsgs (byteArray), auto_0_2 (byteArray)"
+        }
+      ],
+      "contains": {
+        "interpreterName": "nautilus",
+        "interpreterVersion": "v0.1",
+        "hunks": [
+          {
+            "type": "manager",
+            "name": "nautilus",
+            "inputs": [
+              {
+                "name": "msgs",
+                "type": "byteArray"
+              }
+            ],
+            "outputs": [
+              {
+                "name": "msgs",
+                "type": "byteArray",
+                "connections": [
+                  {
+                    "inHunkIndex": 1,
+                    "inHunkInput": 1
+                  }
+                ]
+              }
+            ],
+            "states": []
+          },
+          {
+            "type": "link",
+            "name": "link_1",
+            "inputs": [
+              {
+                "name": "data",
+                "type": "byteArray"
+              },
+              {
+                "name": "mgrMsgs",
+                "type": "byteArray"
+              },
+              {
+                "name": "auto_0_3",
+                "type": "byteArray"
+              }
+            ],
+            "outputs": [
+              {
+                "name": "data",
+                "type": "byteArray",
+                "connections": [
+                  {
+                    "inHunkIndex": 2,
+                    "inHunkInput": 0
+                  }
+                ]
+              },
+              {
+                "name": "mgrMsgs",
+                "type": "byteArray",
+                "connections": [
+                  {
+                    "inHunkIndex": 0,
+                    "inHunkInput": 0
+                  }
+                ]
+              },
+              {
+                "name": "auto_0_2",
+                "type": "byteArray",
+                "connections": [
+                  {
+                    "inHunkIndex": 3,
+                    "inHunkInput": 1
+                  }
+                ]
+              }
+            ],
+            "states": [
+              {
+                "name": "isActive",
+                "type": "boolean",
+                "value": true
+              },
+              {
+                "name": "otherLink",
+                "type": "uint16",
+                "value": 2
+              },
+              {
+                "name": "inputList",
+                "type": "string",
+                "value": "mgrMsgs (byteArray), auto_0_3 (byteArray)"
+              },
+              {
+                "name": "outputList",
+                "type": "string",
+                "value": "mgrMsgs (byteArray), auto_0_2 (byteArray)"
+              }
+            ]
+          },
+          {
+            "type": "comm/websocketserver",
+            "name": "comm/websocketserver_2",
+            "inputs": [
+              {
+                "name": "data",
+                "type": "byteArray"
+              }
+            ],
+            "outputs": [
+              {
+                "name": "data",
+                "type": "byteArray",
+                "connections": [
+                  {
+                    "inHunkIndex": 1,
+                    "inHunkInput": 0
+                  }
+                ]
+              }
+            ],
+            "states": [
+              {
+                "name": "status",
+                "type": "string",
+                "value": "connected"
+              },
+              {
+                "name": "port",
+                "type": "number",
+                "value": 2042
+              }
+            ]
+          },
+          {
+            "type": "link",
+            "name": "link_3",
+            "inputs": [
+              {
+                "name": "data",
+                "type": "byteArray"
+              },
+              {
+                "name": "mgrMsgs",
+                "type": "byteArray"
+              }
+            ],
+            "outputs": [
+              {
+                "name": "data",
+                "type": "byteArray",
+                "connections": [
+                  {
+                    "inHunkIndex": 4,
+                    "inHunkInput": 0
+                  }
+                ]
+              },
+              {
+                "name": "mgrMsgs",
+                "type": "byteArray",
+                "connections": [
+                  {
+                    "inHunkIndex": 1,
+                    "inHunkInput": 2
+                  }
+                ]
+              }
+            ],
+            "states": [
+              {
+                "name": "isActive",
+                "type": "boolean",
+                "value": true
+              },
+              {
+                "name": "otherLink",
+                "type": "uint16",
+                "value": 1
+              },
+              {
+                "name": "inputList",
+                "type": "string",
+                "value": "mgrMsgs (byteArray)"
+              },
+              {
+                "name": "outputList",
+                "type": "string",
+                "value": "mgrMsgs (byteArray)"
+              }
+            ],
+            "contains": {
+              "interpreterName": "ponyo",
+              "interpreterVersion": "v0.1",
+              "hunks": [
+                {
+                  "type": "manager",
+                  "name": "ponyo_one",
+                  "inputs": [
+                    {
+                      "name": "mgrMsgs_1024",
+                      "type": "byteArray"
+                    }
+                  ],
+                  "outputs": [
+                    {
+                      "name": "mgrMsgs_1024",
+                      "type": "byteArray",
+                      "connections": [
+                        {
+                          "inHunkIndex": 1,
+                          "inHunkInput": 1
+                        }
+                      ]
+                    }
+                  ],
+                  "states": []
+                },
+                {
+                  "type": "link",
+                  "name": "link_1",
+                  "inputs": [
+                    {
+                      "name": "data_1024",
+                      "type": "byteArray"
+                    },
+                    {
+                      "name": "mgrMsgs_1024",
+                      "type": "byteArray"
+                    }
+                  ],
+                  "outputs": [
+                    {
+                      "name": "data_1024",
+                      "type": "byteArray",
+                      "connections": [
+                        {
+                          "inHunkIndex": 2,
+                          "inHunkInput": 0
+                        }
+                      ]
+                    },
+                    {
+                      "name": "mgrMsgs_1024",
+                      "type": "byteArray",
+                      "connections": [
+                        {
+                          "inHunkIndex": 0,
+                          "inHunkInput": 0
+                        }
+                      ]
+                    }
+                  ],
+                  "states": [
+                    {
+                      "name": "isActive",
+                      "type": "boolean",
+                      "value": true
+                    },
+                    {
+                      "name": "otherLink",
+                      "type": "uint16",
+                      "value": 3
+                    },
+                    {
+                      "name": "inputList",
+                      "type": "string",
+                      "value": "mgrMsgs (byteArray)"
+                    },
+                    {
+                      "name": "outputList",
+                      "type": "string",
+                      "value": "mgrMsgs (byteArray)"
+                    }
+                  ]
+                },
+                {
+                  "type": "comm/COBSerialUSB",
+                  "name": "comm/COBSerialUSB_2",
+                  "inputs": [
+                    {
+                      "name": "data_1024",
+                      "type": "byteArray"
+                    }
+                  ],
+                  "outputs": [
+                    {
+                      "name": "data_1024",
+                      "type": "byteArray",
+                      "connections": [
+                        {
+                          "inHunkIndex": 1,
+                          "inHunkInput": 0
+                        }
+                      ]
+                    }
+                  ],
+                  "states": []
+                },
+                {
+                  "type": "stepper",
+                  "name": "stepper_3",
+                  "inputs": [
+                    {
+                      "name": "increment",
+                      "type": "int32"
+                    }
+                  ],
+                  "outputs": [
+                    {
+                      "name": "increment",
+                      "type": "int32"
+                    },
+                    {
+                      "name": "stallGuard",
+                      "type": "uint32"
+                    }
+                  ],
+                  "states": [
+                    {
+                      "name": "current (6-24)",
+                      "type": "uint16",
+                      "value": 14
+                    }
+                  ]
+                }
+              ]
+            }
+          },
+          {
+            "type": "comm/cobserial",
+            "name": "comm/cobserial_4",
+            "inputs": [
+              {
+                "name": "data",
+                "type": "byteArray"
+              }
+            ],
+            "outputs": [
+              {
+                "name": "data",
+                "type": "byteArray",
+                "connections": [
+                  {
+                    "inHunkIndex": 3,
+                    "inHunkInput": 0
+                  }
+                ]
+              }
+            ],
+            "states": [
+              {
+                "name": "status",
+                "type": "string",
+                "value": "closed"
+              },
+              {
+                "name": "pid",
+                "type": "string",
+                "value": "8022"
+              },
+              {
+                "name": "refresh",
+                "type": "boolean",
+                "value": false
+              }
+            ]
+          },
+          {
+            "type": "control/saturnjog",
+            "name": "control/saturnjog_5",
+            "inputs": [
+              {
+                "name": "xNegPressure",
+                "type": "boolean"
+              },
+              {
+                "name": "xPosPressure",
+                "type": "boolean"
+              },
+              {
+                "name": "xStepsMade",
+                "type": "int32"
+              }
+            ],
+            "outputs": [
+              {
+                "name": "xStepsNextIncrement",
+                "type": "int32"
+              }
+            ],
+            "states": [
+              {
+                "name": "intervalRunning",
+                "type": "boolean",
+                "value": false
+              },
+              {
+                "name": "msPerInterval",
+                "type": "number",
+                "value": 20
+              },
+              {
+                "name": "accel (s/s^2)",
+                "type": "number",
+                "value": 2000
+              },
+              {
+                "name": "max (s/s)",
+                "type": "number",
+                "value": 2000
+              }
+            ]
+          }
+        ]
+      }
+    },
+    {
+      "type": "comm/websocketclient",
+      "name": "comm/websocketclient_3",
+      "inputs": [
+        {
+          "name": "data",
+          "type": "byteArray"
+        }
+      ],
+      "outputs": [
+        {
+          "name": "data",
+          "type": "byteArray",
+          "connections": [
+            {
+              "inHunkIndex": 2,
+              "inHunkInput": 0
+            }
+          ]
+        }
+      ],
+      "states": [
+        {
+          "name": "status",
+          "type": "string",
+          "value": "not connected"
+        },
+        {
+          "name": "retrycount",
+          "type": "number",
+          "value": 0
+        },
+        {
+          "name": "retryreset",
+          "type": "boolean",
+          "value": false
+        },
+        {
+          "name": "address",
+          "type": "string",
+          "value": "127.0.0.1"
+        },
+        {
+          "name": "port",
+          "type": "number",
+          "value": 2042
+        }
+      ]
+    },
+    {
+      "type": "view",
+      "name": "view_4",
+      "inputs": [
+        {
+          "name": "msgs",
+          "type": "byteArray"
+        }
+      ],
+      "outputs": [
+        {
+          "name": "msgs",
+          "type": "byteArray",
+          "connections": [
+            {
+              "inHunkIndex": 2,
+              "inHunkInput": 1
+            }
+          ]
+        }
+      ],
+      "states": []
+    },
+    {
+      "type": "view",
+      "name": "view_5",
+      "inputs": [
+        {
+          "name": "msgs",
+          "type": "byteArray"
+        }
+      ],
+      "outputs": [
+        {
+          "name": "msgs",
+          "type": "byteArray",
+          "connections": [
+            {
+              "inHunkIndex": 2,
+              "inHunkInput": 2
+            }
+          ]
+        }
+      ],
+      "states": []
+    }
+  ]
+}
\ No newline at end of file
diff --git a/view/vtoplevel.js b/view/vtoplevel.js
index c443f61..6dbbbfe 100644
--- a/view/vtoplevel.js
+++ b/view/vtoplevel.js
@@ -265,7 +265,7 @@ function makeTopLevel(view) {
           return false
         }
       } else {
-        if (debug) console.log(`TRACE: finally returns`, next)
+        if (debug) console.log(`TRACE: finally returns ${next.name} in ${next.parent.name} from ${next.parent.parentView.name} of ${next.parent.parentView.interpreterName}`)
         return next
       }
     } catch (err) {
@@ -284,6 +284,8 @@ function makeTopLevel(view) {
    - operate when possible paths (unoccupied & same-type) or partial paths already exist
    - operate across multiple levels!
    - in deep future: choose shortest (& least busy?) path through existing graph
+   - also: atm if links are not already spread (i.e. if link outputs don't match opposite inputs) ....
+    - then we add one side at, say, index 2, and the other at, say. index 5 ... no bueno
 
   */
 
@@ -299,6 +301,7 @@ function makeTopLevel(view) {
       // ok, first off, are these things in the same view? can I find a view from an outputs?
       let opv = output.parent.parentView
       let ipv = input.parent.parentView
+      if(debug) console.log(`output parentview is ${opv.name} and input parent view is ${ipv.name}`)
       if (opv === ipv) {
         // we r on par
         if (debug) console.log(`BR: inputs are the same to ${view.name}`)
@@ -311,14 +314,19 @@ function makeTopLevel(view) {
         // to find another bug (courtesy of the def-replace-polymorphism bugfarm)
         // we should run a .go before this...
         view.globalOrganize()
+        // now,
+        if(debug) console.log(`BR: GO completes, now build for:`)
+        if(debug) console.log(`BR: from ${output.name} in ${output.parent.name} to ${input.name} in ${input.parent.name}`)
+        let finroute = []
         // we are the top level ... but we should hunt by link,
-        let recurse = (view, entrance, trace) => {
+        var recurse = (view, entrance, trace) => {
           if (debug) console.log(`BR: recurse to ${view.name}`)
-          let ntrace = JSON.parse(JSON.stringify(trace))
           for (let df of view.defs) {
             if (df.type === 'link' && df !== entrance) {
               if (df.reciprocalLink) {
-                ntrace.push({
+                if (debug) console.log(`BR: pushes to ntrace of len ${trace.length} exit for ${df.reciprocalLink.name}`)
+                let borkit = JSON.parse(JSON.stringify(trace))
+                borkit.push({
                   entrance: {
                     link: df.name,
                     view: view.name
@@ -329,25 +337,26 @@ function makeTopLevel(view) {
                   }
                 })
                 if (df.reciprocalLink.parentView === ipv) {
-                  if (debug) console.log("BR: Makes")
-                  return ntrace;
+                  if (debug) console.log(`BR: Makes view ${df.reciprocalLink.parentView.name} and ${ipv.name}`)
+                  finroute = JSON.parse(JSON.stringify(borkit));
                 } else {
-                  return recurse(df.reciprocalLink.parentView, df.reciprocalLink, ntrace)
+                  recurse(df.reciprocalLink.parentView, df.reciprocalLink, JSON.parse(JSON.stringify(borkit)))
                 }
               } else {
                 if (debug) console.log(`BR: no reciprocal link for ${df.name} within ${view.name}`)
-                reject("no reciprocal link")
-              } // no reciprocalLink
+              }
             } // not a link
           }
         }
-        let route = recurse(opv, null, [])
-        if (!route) {
+        recurse(opv, null, [])
+        // ...
+        if(debug) console.log(`BR: recursion ran to completion, route is len ${finroute.length}`)
+        if(debug) console.log(`BR: the route:`, finroute)
+        if (finroute.length < 1) {
           if (debug) console.log(`BR: no route returned...`)
           console.error("no route to build...")
           reject()
         }
-        if (debug) console.log("BR Recurse Return: ", route)
         // should we resolve those objects? probably safe to assume that view names are unique,
         // these are the kinds of code snippets that happen when jake is betwixt cpp and js
         let resolver = (obj) => {
@@ -363,14 +372,14 @@ function makeTopLevel(view) {
           obj.view = handle
           obj.link = realLink
         }
-        for (let item of route) {
+        for (let item of finroute) {
           resolver(item.entrance)
           resolver(item.exit)
         }
-        if (debug) console.log("BR: resolved to", route)
+        if (debug) console.log("BR: resolved to", finroute)
         // OK: we gotem
         // so!
-        view.constructRoute(output, input, route).then(() => {
+        view.constructRoute(output, input, finroute).then(() => {
           resolve()
         })
       } // end not-same-view case,
-- 
GitLab