Commit 89958b82 authored by Jake Read's avatar Jake Read

appears to save / and load to a representation consistently

parent 52d686fe
......@@ -75,7 +75,7 @@ function State() {
// within state ... or not ?
function Button(label) {
var button = {
isButton: true,
type: 'ui-button',
isPressed: false,
label: label
}
......
......@@ -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 ?
......
{
"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
{
"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
......@@ -18,7 +18,7 @@ function Delay() {
delay.state = State()
// alias !
var state = delay.state
var state = delay.state
state.ms = 100
......
......@@ -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)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment