Catalog of Variables

So long as blocks document what they read and write in a consistent way, we will cross-reference these state variable names as a guide to blocks that work together.

digraph catalog { rankdir=LR node [shape=block style=filled fillcolor=palegreen] FROM PREVIEW SOLO node [shape=oval fillcolor=lightblue] FROM -> page -> blocks -> items -> PREVIEW blocks -> aspects -> SOLO }

CLICK FROM mech.fed.wiki/catalog-of-mech-blocks CODE blocks CODE variables CLICK PREVIEW synopsis items CLICK SOLO CLICK POPUP state

We export two functions to be called in sequence above.

export function blocks() { let page = this.page let blocks = this.blocks = page.story .map(item => item.text.match(/^\[\[([A-Z]+)\]\]/)) .filter(m => m) .map(m => ({title:m[1],in:[],out:[]})) return `${blocks.length} blocks` }

export async function variables() { let uniq = (value, index, self) => self.indexOf(value) === index let id = this.context.page.title let blocks = this.blocks await dofetch(blocks) doparse(blocks) let items = blocks.map(block => ({ type:`paragraph`, text:`${block.title}: ${block.io}`})) this.items = items let states = blocks .map(block => [block.in,block.out]) .flat(2) .filter(uniq) .sort((a,b) => a < b ? -1 : 1) let result = mkaspect(blocks,states) this.aspect = [formats(),{id,result}] return `${states.length} state variables` }

We code each transformation as individual functions called in turn by the "CODE variables" block above.

function dofetch(blocks) { let site = `http://mech.fed.wiki` return Promise.all( blocks.map(block => fetch(`${site}/${block.title.toLowerCase()}.json`) .then(res => res.json()) .then(page => { block.io = page.story.slice(-1)[0].text }) )) }

function doparse(blocks) { let regex = /\b(reads?|inputs?)? ?state\.(\w+)\b/g blocks.forEach(block => { [...block.io.matchAll(regex)] .forEach(m => { if(['reads','read','input','inputs'].includes(m[1])) block.in.push(m[2]) else block.out.push(m[2]) }) }) }

import {Graph} from 'https://wardcunningham.github.io/graph/graph.js' function mkaspect(blocks,states) { let site = `mech.fed.wiki` return states.map(state => { let graph = new Graph() let vid = graph.addNode('state',{name:state}) blocks.forEach(block => { if(block.out.includes(state)) { let name = block.title let bid = graph.addNode('block',{name,site}) graph.addRel('write',bid,vid) } if(block.in.includes(state)) { let name = block.title let bid = graph.addNode('block',{name,site}) graph.addRel('read',bid,vid) } }) return {name:state,graph} }) }

function formats() { let name = 'State as Ovals' let graph = new Graph() let emphasis = { state: "shape=oval fillcolor=lightblue", Graphviz: "fillcolor=white"} graph.addNode('Graphviz',{name:'ovals',emphasis}) let id = 'Preferred Formats' let result = [{name, graph}] return {id,result} }