From 89958b828b19e32911ef32d1bb6e31bdc65b69e5 Mon Sep 17 00:00:00 2001
From: Jake <jake.read@cba.mit.edu>
Date: Fri, 9 Nov 2018 17:43:24 -0500
Subject: [PATCH] appears to save / and load to a representation consistently

---
 lib/jsunit.js     |   2 +-
 nnc.js => main.js |  84 ++++++++++++++++------
 save/onesave.json | 174 ++++++++++++++++++++++------------------------
 save/twosave.json | 162 +++++++++++++++++++++---------------------
 src/util/delay.js |   2 +-
 src/util/log.js   |   4 ++
 6 files changed, 232 insertions(+), 196 deletions(-)
 rename nnc.js => main.js (85%)

diff --git a/lib/jsunit.js b/lib/jsunit.js
index 324ff7c..d12559e 100644
--- a/lib/jsunit.js
+++ b/lib/jsunit.js
@@ -75,7 +75,7 @@ function State() {
 // within state ... or not ? 
 function Button(label) {
     var button = {
-        isButton: true,
+        type: 'ui-button',
         isPressed: false,
         label: label
     }
diff --git a/nnc.js b/main.js
similarity index 85%
rename from nnc.js
rename to main.js
index 9f25315..80b3763 100644
--- a/nnc.js
+++ b/main.js
@@ -38,7 +38,7 @@ const fs = require('fs')
 const app = require('express')()
 const http = require('http').Server(app)
 
-// websocket does websocket
+// websocket, to share program representations with the client (and back)
 const WebSocket = require('ws')
 
 /*
@@ -145,8 +145,9 @@ PROGRAM AS API
 var program = {
     description: {
         name: 'tstprgmem',
+        counter: 0, 
     },
-    modules: new Array()
+    modules: {}
 }
 
 // add things
@@ -161,6 +162,7 @@ gate.outputs.out.attach(delay.inputs.thru)
 delay.outputs.out.attach(log.inputs.thru)
 gate.outputs.out.attach(log.inputs.thru)
 
+console.log('saving onesave.json')
 saveProgram(program, 'save/onesave.json')
 
 program = null 
@@ -168,12 +170,20 @@ delete gate
 delete delay
 delete log 
 
-console.log('PROGRAM', program)
-
+console.log('opening onesave.json')
 var prgmem = openProgram('save/onesave.json')
 
 saveProgram(prgmem, 'save/twosave.json')
 
+// working through:
+/*
+
+ - add load-from-thing-connect-outputs-to-inputs 
+ - add new state loading / structure?
+ - UI / letters 
+
+*/
+
 
 /*
 ------------------------------------------------------
@@ -182,16 +192,24 @@ PROGRAM ASSEMBLY
 */
 
 function addModuleToProgram(program, path) {
+    // source -> heap
     // program is an object seeking heirarchy, has program.modules 
     if (fs.existsSync(path)) {
         var src = require(path)
         var mod = new src()
 
-        // add to list
-        program.modules.push(mod)
+        // wants unique module id's 
+        if(program.description.counter == null){
+            program.description.counter = 0
+        } else {
+            program.description.counter ++ 
+        }
+
         // make unique name 
-        mod.description.id = mod.description.name + '-' + program.modules.length
+        mod.description.id = mod.description.id = mod.description.name + '-' + program.description.counter
         mod.description.path = path
+        
+        program.modules[mod.description.id] = mod
 
         // input need references for later hookup
         for (key in mod.inputs) {
@@ -295,21 +313,19 @@ function saveProgram(prgmem, path) {
     // ok, and we're interested in just copying the relevant things ... 
     var svprgmem = {
         description: {
-            name: prgmem.description.name
+            name: prgmem.description.name,
+            counter: prgmem.description.counter
         },
-        modules: new Array()
+        modules: {}
     }
 
     var mdls = prgmem.modules
 
-    mdls.forEach(function(mdl) {
-        // basically, going to run a diff on this
-        console.log('mdl unit', mdl)
-        // and 
+    for(key in mdls){
+        var mdl = mdls[key]
         var og = makeRepFromModule(mdl)
-        console.log('rep uniiiit', og)
-        svprgmem.modules.push(og)
-    })
+        svprgmem.modules[mdl.description.id] = og 
+    }
 
     fs.writeFileSync(path, JSON.stringify(svprgmem, null, 2), 'utf8')
 }
@@ -319,19 +335,43 @@ function openProgram(path){
 
     // get the .json file as an object 
     var prgRep = JSON.parse(fs.readFileSync(path, 'utf8'))
-    console.log('OPENING THIS REP', prgRep)
+    console.log('OPENING THIS PRGRAM REP', prgRep)
 
+    // copy-pasta the program descro, 
     program.description = {
         name: prgRep.description.name,
-        id: prgRep.description.name + 1,
+        counter: 0,
+        id: prgRep.description.name + 1, // in another world, we count
         path: path
     }
 
-    program.modules = new Array()
+    // gonna get those modules from source
+    program.modules = {}
 
-    prgRep.modules.forEach(function(rep){
-        addModuleToProgram(program, rep.description.path)
-    })
+    for(key in prgRep.modules){
+        var mdlRep = prgRep.modules[key]
+        addModuleToProgram(program, mdlRep.description.path)
+    }
+
+    // gonna hook 'em up
+    for(modName in prgRep.modules){
+        // keys should be identical for rep and heap
+        var mdlRep = prgRep.modules[modName]
+        var mdl = program.modules[modName]
+
+        for(outName in mdlRep.outputs){
+            var outRep = mdlRep.outputs[outName]
+            // each has some caller ids 
+            for(nestedInputRep in outRep.calls){
+                // conn from tl program -> this hookup 
+                var nIRParent = outRep.calls[nestedInputRep].parentId
+                var nIRKey = outRep.calls[nestedInputRep].key 
+                var nI = program.modules[nIRParent].inputs[nIRKey]
+                console.log("ATTACHING", nI, nIRKey, 'to', mdl.outputs[outName])
+                mdl.outputs[outName].attach(nI)
+            }
+        }
+    }
 
     // once modules exist, link inputs / outputs / copy state ? 
 
diff --git a/save/onesave.json b/save/onesave.json
index aa0f880..1f5cd7f 100644
--- a/save/onesave.json
+++ b/save/onesave.json
@@ -1,104 +1,94 @@
 {
-    "description":
-    {
-        "name": "tstprgmem"
-    },
-    "modules": [
-    {
-        "description":
-        {
-            "id": "gate-1",
-            "name": "gate",
-            "alt": "in ... out",
-            "path": "./src/util/gate.js"
-        },
-        "inputs":
-        {
-            "thru":
-            {
-                "accepts": "any"
-            }
-        },
-        "outputs":
-        {
-            "out":
-            {
-                "emits": "any",
-                "calls": [
-                {
-                    "parentId": "delay-2",
-                    "key": "thru"
-                },
-                {
-                    "parentId": "logger-3",
-                    "key": "thru"
-                }]
-            }
-        },
-        "state":
-        {
-            "toggle":
+  "description": {
+    "name": "tstprgmem",
+    "counter": 3
+  },
+  "modules": {
+    "gate-1": {
+      "description": {
+        "id": "gate-1",
+        "name": "gate",
+        "alt": "in ... out",
+        "path": "./src/util/gate.js"
+      },
+      "inputs": {
+        "thru": {
+          "accepts": "any"
+        }
+      },
+      "outputs": {
+        "out": {
+          "emits": "any",
+          "calls": [
             {
-                "isButton": true,
-                "isPressed": false,
-                "label": "Open / Close"
+              "parentId": "delay-2",
+              "key": "thru"
             },
-            "message": "closed"
-        }
-    },
-    {
-        "description":
-        {
-            "id": "delay-2",
-            "name": "delay",
-            "alt": "in ... out",
-            "path": "./src/util/delay.js"
-        },
-        "inputs":
-        {
-            "thru":
             {
-                "accepts": "any"
+              "parentId": "logger-3",
+              "key": "thru"
             }
+          ]
+        }
+      },
+      "state": {
+        "toggle": {
+          "type": "ui-button",
+          "isPressed": false,
+          "label": "Open / Close"
         },
-        "outputs":
-        {
-            "out":
+        "message": "closed"
+      }
+    },
+    "delay-2": {
+      "description": {
+        "id": "delay-2",
+        "name": "delay",
+        "alt": "in ... out",
+        "path": "./src/util/delay.js"
+      },
+      "inputs": {
+        "thru": {
+          "accepts": "any"
+        }
+      },
+      "outputs": {
+        "out": {
+          "emits": "any",
+          "calls": [
             {
-                "emits": "any",
-                "calls": [
-                {
-                    "parentId": "logger-3",
-                    "key": "thru"
-                }]
+              "parentId": "logger-3",
+              "key": "thru"
             }
-        },
-        "state":
-        {
-            "ms": 100
+          ]
         }
+      },
+      "state": {
+        "ms": 100
+      }
     },
-    {
-        "description":
-        {
-            "id": "logger-3",
-            "name": "logger",
-            "alt": "in ... out to console",
-            "path": "./src/util/log.js"
-        },
-        "inputs":
-        {
-            "thru":
-            {
-                "accepts": "any"
-            }
-        },
-        "outputs":
-        {},
-        "state":
-        {
-            "prefix": "LOGGER:",
-            "message": "---"
+    "logger-3": {
+      "description": {
+        "id": "logger-3",
+        "name": "logger",
+        "alt": "in ... out to console",
+        "path": "./src/util/log.js"
+      },
+      "inputs": {
+        "thru": {
+          "accepts": "any"
+        }
+      },
+      "outputs": {
+        "throughput": {
+          "emits": "any",
+          "calls": []
         }
-    }]
+      },
+      "state": {
+        "prefix": "LOGGER:",
+        "message": "---"
+      }
+    }
+  }
 }
\ No newline at end of file
diff --git a/save/twosave.json b/save/twosave.json
index fd9d0a5..1f5cd7f 100644
--- a/save/twosave.json
+++ b/save/twosave.json
@@ -1,92 +1,94 @@
 {
-    "description":
-    {
-        "name": "tstprgmem"
-    },
-    "modules": [
-    {
-        "description":
-        {
-            "id": "gate-1",
-            "name": "gate",
-            "alt": "in ... out",
-            "path": "./src/util/gate.js"
-        },
-        "inputs":
-        {
-            "thru":
-            {
-                "accepts": "any"
-            }
-        },
-        "outputs":
-        {
-            "out":
-            {
-                "emits": "any",
-                "calls": []
-            }
-        },
-        "state":
-        {
-            "toggle":
+  "description": {
+    "name": "tstprgmem",
+    "counter": 3
+  },
+  "modules": {
+    "gate-1": {
+      "description": {
+        "id": "gate-1",
+        "name": "gate",
+        "alt": "in ... out",
+        "path": "./src/util/gate.js"
+      },
+      "inputs": {
+        "thru": {
+          "accepts": "any"
+        }
+      },
+      "outputs": {
+        "out": {
+          "emits": "any",
+          "calls": [
             {
-                "isButton": true,
-                "isPressed": false,
-                "label": "Open / Close"
+              "parentId": "delay-2",
+              "key": "thru"
             },
-            "message": "closed"
-        }
-    },
-    {
-        "description":
-        {
-            "id": "delay-2",
-            "name": "delay",
-            "alt": "in ... out",
-            "path": "./src/util/delay.js"
-        },
-        "inputs":
-        {
-            "thru":
             {
-                "accepts": "any"
+              "parentId": "logger-3",
+              "key": "thru"
             }
+          ]
+        }
+      },
+      "state": {
+        "toggle": {
+          "type": "ui-button",
+          "isPressed": false,
+          "label": "Open / Close"
         },
-        "outputs":
-        {
-            "out":
+        "message": "closed"
+      }
+    },
+    "delay-2": {
+      "description": {
+        "id": "delay-2",
+        "name": "delay",
+        "alt": "in ... out",
+        "path": "./src/util/delay.js"
+      },
+      "inputs": {
+        "thru": {
+          "accepts": "any"
+        }
+      },
+      "outputs": {
+        "out": {
+          "emits": "any",
+          "calls": [
             {
-                "emits": "any",
-                "calls": []
+              "parentId": "logger-3",
+              "key": "thru"
             }
-        },
-        "state":
-        {
-            "ms": 100
+          ]
         }
+      },
+      "state": {
+        "ms": 100
+      }
     },
-    {
-        "description":
-        {
-            "id": "logger-3",
-            "name": "logger",
-            "alt": "in ... out to console",
-            "path": "./src/util/log.js"
-        },
-        "inputs":
-        {
-            "thru":
-            {
-                "accepts": "any"
-            }
-        },
-        "outputs":
-        {},
-        "state":
-        {
-            "prefix": "LOGGER:",
-            "message": "---"
+    "logger-3": {
+      "description": {
+        "id": "logger-3",
+        "name": "logger",
+        "alt": "in ... out to console",
+        "path": "./src/util/log.js"
+      },
+      "inputs": {
+        "thru": {
+          "accepts": "any"
+        }
+      },
+      "outputs": {
+        "throughput": {
+          "emits": "any",
+          "calls": []
         }
-    }]
+      },
+      "state": {
+        "prefix": "LOGGER:",
+        "message": "---"
+      }
+    }
+  }
 }
\ No newline at end of file
diff --git a/src/util/delay.js b/src/util/delay.js
index 21e43be..d7f8500 100644
--- a/src/util/delay.js
+++ b/src/util/delay.js
@@ -18,7 +18,7 @@ function Delay() {
 
     delay.state = State()
     // alias !
-    var state = delay.state 
+    var state = delay.state
 
     state.ms = 100
 
diff --git a/src/util/log.js b/src/util/log.js
index 3af0f62..69c4f60 100644
--- a/src/util/log.js
+++ b/src/util/log.js
@@ -26,6 +26,10 @@ function Logger() {
         thru: Input('any', onInput) // makes anything into '1' event
     }
 
+    logger.outputs = {
+        throughput: Output('any')
+    }
+
     function onInput(input){
         state.message = input.toString()
         console.log(state.prefix, input)
-- 
GitLab