From 5765570617d7965ba1b300aef4821c08d651cd7e Mon Sep 17 00:00:00 2001 From: Marvin Sanchez Date: Fri, 19 Aug 2016 08:53:01 +0800 Subject: [PATCH] first upload --- data/.DS_Store | Bin 0 -> 6148 bytes data/canons.yaml | 31 ++ data/civpro.json | 819 +++++++++++++++++++++++++++++++++ data/civpro.yaml | 278 ++++++++++++ data/crimpro.json | 575 +++++++++++++++++++++++ data/crimpro.yaml | 216 +++++++++ data/ethics.json | 1058 +++++++++++++++++++++++++++++++++++++++++++ data/ethics.yaml | 440 ++++++++++++++++++ data/ibp.json | 246 ++++++++++ data/ibp.yaml | 103 +++++ data/insurance.yaml | 131 ++++++ data/judicial.json | 519 +++++++++++++++++++++ data/judicial.yaml | 217 +++++++++ data/nego.yaml | 324 +++++++++++++ graph.pde | 142 ++++++ layout.pde | 234 ++++++++++ parsey.py | 255 +++++++++++ rect.pde | 69 +++ render.pde | 185 ++++++++ style.pde | 111 +++++ yaml2mindmap.pde | 148 ++++++ 21 files changed, 6101 insertions(+) create mode 100644 data/.DS_Store create mode 100644 data/canons.yaml create mode 100644 data/civpro.json create mode 100644 data/civpro.yaml create mode 100644 data/crimpro.json create mode 100644 data/crimpro.yaml create mode 100644 data/ethics.json create mode 100644 data/ethics.yaml create mode 100644 data/ibp.json create mode 100644 data/ibp.yaml create mode 100644 data/insurance.yaml create mode 100644 data/judicial.json create mode 100644 data/judicial.yaml create mode 100644 data/nego.yaml create mode 100644 graph.pde create mode 100644 layout.pde create mode 100755 parsey.py create mode 100644 rect.pde create mode 100644 render.pde create mode 100644 style.pde create mode 100644 yaml2mindmap.pde diff --git a/data/.DS_Store b/data/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 GIT binary patch literal 6148 zcmeH~Jr2S!425mzP>H1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3 zem<@ulZcFPQ@L2!n>{z**++&mCkOWA81W14cNZlEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ zLs35+`xjp>T0 d) + d = dd; + } + return d; + } + + void _reset(Node n) { + n.layout = new LayoutInfo(); + n.layout.children = new ArrayList(); + all.add(n); + + for (Object c : n.children) { + Node nc = (Node)c; + + if (n.parent == null && mode != null) { + if (!nc.direction.equals(mode)) + continue; + } + + _reset(nc); + n.layout.children.add(nc); + } + } + + void _calculateSize(Node n) { + + Style style = n.style; + Rect exT = style.calculateTextExtents(n.text, style.textSize); + n.layout.width = exT.width; + n.layout.height = exT.height; + + ArrayList children = n.layout.children; + for (Object c : children) { + Node nc = (Node)c; + _calculateSize(nc); + } + } + + void _calculateBounds(Node n) { + + n.layout.bounds.reset(); + n.layout.bounds.addToBound(n.layout); + + ArrayList children = n.layout.children; + for (Object c : children) { + Node nc = (Node)c; + _calculateBounds(nc); + n.layout.bounds.addToBound(nc.layout.bounds); + } + } + + void _position(Node n) { + + Style style = n.style; + Rect exT = style.calculateTextExtents(n.text, style.textSize); + Rect exS = style.calculateTextExtents(" ", style.textSize); + + float margin = (exS.height * 0.25 * style.margin); + float spacing = (exS.width * 0.25 * style.spacing); + + n.layout.width = exT.width + margin; + n.layout.height = exT.height + (exS.height * 0) + margin; + + float xx = n.layout.x + n.layout.width + (spacing * 2); + float yy = n.layout.y; + + if (n.childrenDirection.equals("down")) { + xx = n.layout.x + (n.layout.width / 2) + spacing; + yy = n.layout.y + n.layout.height + (spacing * 2); + } + + ArrayList children = n.layout.children; + for (Object c : children) { + Node nc = (Node)c; + nc.layout.x = xx; + nc.layout.y = yy; + yy += nc.layout.bounds.height + spacing; + + if (mode == "left") { + nc.layout.x = n.layout.x - (nc.layout.width) - (spacing * 2); + if (n.childrenDirection.equals("down")) { + nc.layout.x += (n.layout.width / 2); + } + } + + _position(nc); + } + } + + void _calculatePorts(Node n) { + + n.layout.childrenPort.x = n.layout.x + n.layout.width; + n.layout.childrenPort.y = n.layout.y + (n.layout.height / 2); + + if (mode == "left") { + n.layout.childrenPort.x = n.layout.x; + } + + if (n.childrenDirection.equals("down")) { + n.layout.childrenPort.x = n.layout.x + (n.layout.width / 2); + n.layout.childrenPort.y = n.layout.y + (n.layout.height / 2); + } + + ArrayList children = n.layout.children; + for (Object c : children) { + Node nc = (Node)c; + _calculatePorts(nc); + nc.layout.port.x = nc.layout.x; + nc.layout.port.y = nc.layout.y + (nc.layout.height / 2); + + if (mode == "left") { + nc.layout.port.x = nc.layout.x + nc.layout.width; + } + + } + } + + void _calculateRootBounds(Node n, Rect b) { + if (n.parent == null) + b.reset(); + + // padd + Rect r = new Rect(n.layout); + r.expandRect(10); + b.addToBound(r); + + for (Object c : n.children) { + Node nc = (Node)c; + _calculateRootBounds(nc, b); + } + } + + void layoutBiTree(Node n) { + + //n.childrenDirection = "down"; + + mode = "right"; + + all = new ArrayList(); + + _reset(n); + _calculateSize(n); + + int d = _getDepth(); + for (int i=0; i' + print '
' + n['text'] + '
' + + cc = 'children' + if 'direction' in n: + if n['direction'] == 'left': + cc = cc + ' directionLeft' + + if 'framed' in n: + print n['framed'] + cc = cc + ' framed' + + if 'nodes' in n: + print '
' + for c in n['nodes']: + yamlHtml(c) + print '
' + print '' + + +# dumps the structure back to yaml format +def yamlDump(n, l=0): + + ret = '' + + tabs = ' ' + s = '' + for i in range(l): + s = s + tabs + + attribs = [ 'text' , 'name', 'style', 'direction', 'childrenDirection', 'framed', 'collapsed' ] + for nn in attribs: + if nn in n: + t = n[nn].replace("\n", "\\n") + if nn != 'text': + t = tabs + nn + ': ' + t + ret = ret + s + t + "\n" + + if 'nodes' in n: + ret = ret + "\n" + for c in n['nodes']: + ret = ret + yamlDump(c, l+1) + ret = ret + "\n" + + if 'edges' in n: + ret = ret + tabs + 'edges:' + "\n" + for e in n['edges']: + n1 = e['n1'] + n2 = e['n2'] + ret = ret + tabs + tabs + n1 + ', ' + n2 + "\n" + + return ret + +# cleans the parsed yaml file +def walkClean(n): + + n.pop('_level') + if '_text' in n: + n['text'] = n['_text'] + n.pop('_text') + + if len(n['nodes']) == 0: + n.pop('nodes', 0) + return + + edges = None + for c in n['nodes']: + + if c['_text'] == 'edges': + edges = c + continue + + walkClean(c) + + if edges != None: + n['nodes'].remove(edges) + n['edges'] = [] + for e in edges['nodes']: + m = e['_text'].split(',') + if len(m) > 1: + n['edges'].append( { 'n1': m[0].strip() , 'n2': m[1].strip() }) + # print e + +def applyStyle(css, n): + + if css == None: + return + + if 'nodes' not in n: + return + + classes = [] + if '_class' in n: + cc = [ c.strip() for c in n['_class'].split(' ') ] + for cname in cc: + for cs in css['nodes']: + if cs['text'] == cname: + classes.append(cs) + + style = [ c for c in n['nodes'] if c['text'] == '_style' ] + if len(style) > 0: + style = style[0] + classes.append(style) + else: + style = { 'text': '_style' } + n['nodes'].append(style) + + nn = {} + for cc in classes: + for a in cc: + nn[a] = cc[a] + + for a in nn: + style[a] = nn[a] + + style['text'] = '_' + + for c in n['nodes']: + applyStyle(css, c) + +def newNode(t, l, p): + + n = '_text' + v = t + v = v.replace("\\n", "\n") + + m = re.search('(.*):(.*)', t) + if m != None: + nn = m.group(1).strip() + vv = m.group(2).strip() + if len(nn) > 0 and len(vv) > 0: + p[nn] = vv + return None + + v = v.replace(':','').strip() + + child = { + '_level': l, + n: v, + 'nodes': [] + } + + if p != None: + p['nodes'].append(child) + + return child + +def getLevel(line): + level = 0 + m = re.search('^ *', line) + if m != None: + level = len(m.group(0)) + return level + +list = [] +lines = [] + +def parse(filepath): + + root = None + + fo = open(filepath, 'r') + for line in fo: + + obj = None + + # print line + # print '----' + + if len(list) > 0 : + obj = list[ len(list) - 1 ] + + if len(line.strip()) == 0: + continue + + text = line.strip() + + if obj == None: + root = newNode(text, 0, None) + list.append(root) + continue + + line = line.replace('\t',' ') + + # get level + level = getLevel(line) + # print level + + while(level <= obj['_level']): + if len(list) == 1: + break + list.pop() + obj = list[ len(list) - 1 ] + + child = newNode(text, level, obj) + if child != None: + list.append(child) + obj = child + + walkClean(root) + + # find css + css = None + if 'nodes' in root: + cc = [ c for c in root['nodes'] if c['text'] == '_css' ] + if len(cc) > 0: + css = cc[0] + + applyStyle(css, root) + return root + +if sys.argv[0] == __file__: + + source = '' + toHtml = '-html' in sys.argv + toJson = '-yaml' not in sys.argv and not toHtml + + if len(sys.argv) > 1: + source = sys.argv[len(sys.argv) - 1] + + if 'yaml' in source: + root = parse(source) + + if toJson: + target = source.replace('yaml','json') + with io.open(target, 'w', encoding='utf-8') as f: + f.write(unicode(json.dumps(root, indent=1, sort_keys=True))) + # print json.dumps(root, indent=1, sort_keys=True) + + # print css + + elif toHtml: + + print ' \ + \ + ' + + yamlHtml(root) + else: + print yamlDump(root) diff --git a/rect.pde b/rect.pde new file mode 100644 index 0000000..6adc841 --- /dev/null +++ b/rect.pde @@ -0,0 +1,69 @@ +class Position { + float x; + float y; +} + +class Rect { + float x = 0; + float y = 0; + float width = 0; + float height = 0; + + Rect() { + } + + Rect(Rect r) { + setRect(r); + } + + void setRect(Rect r) { + x = r.x; + y = r.y; + width = r.width; + height = r.height; + } + + void expandRect(int size) { + x -= size; + width += (size*2); + y -= size; + height += (size*2); + } + + void addToBound(Rect r) { + + if (x == 0 && y == 0 && width == 0 && height == 0) { + x = r.x; + y = r.y; + width = r.width; + height = r.height; + return; + } + + float x0 = x; + float y0 = y; + float x1 = x + width; + float y1 = y + height; + + if (x0 > r.x) + x0 = r.x; + if (y0 > r.y) + y0 = r.y; + if (x1 < r.x + r.width) + x1 = r.x + r.width; + if (y1 < r.y + r.height) + y1 = r.y + r.height; + + x = x0; + y = y0; + width = x1 - x; + height = y1 - y; + } + + void reset() { + x = 0; + y = 0; + width = 0; + height = 0; + } +} \ No newline at end of file diff --git a/render.pde b/render.pde new file mode 100644 index 0000000..6122899 --- /dev/null +++ b/render.pde @@ -0,0 +1,185 @@ + +class Renderer { + + PGraphics gfx = null; + + void begin(PGraphics g) { + gfx = g; + } + + void end() { + gfx = null; + } + + void _drawEdge(float x0, float y0, float w0, float x1, float y1, float w1, int edgeStyle) { + + PGraphics g = gfx; + if (g == null) + g = getGraphics(); + + float d0 = 1; + float d1 = -1; + + float xA = x0 + (w0/2); + float xB = x1 + (w1/2); + + x0 += (w0/2); + x1 -= (w1/2); + + if (x1 < x0) { + x0 -= w0; + x1 += (w1); + d0 = -1; + d1 = 1; + } + + float dX = (x1-x0)*0.5; + float dY = (y1-y0)*0.5; + + float ddx = sqrt(dX * dX) * 0.5; + + if (edgeStyle == 1) { + float ctl = 1.6; + g.bezier( + x0, y0, + x0+(ddx*d0)*ctl, y0, + x1+(ddx*d1)*ctl, y1, + x1, y1 + ); + return; + } + + g.line(x0, y0, x0+dX, y0); + g.line(x1, y1, x1-dX, y1); + if (dY != 0) { + g.line(x0+dX, y0, x1-dX, y1); + } + } + + void _drawNode(Node n) { + + PGraphics g = gfx; + if (g == null) + g = getGraphics(); + + g.pushMatrix(); + g.stroke(0); + + Style style = n.style; + + // ports + g.strokeWeight(0.5 * style.link); + g.stroke(style.linkColor); + g.noFill(); + for (Object c : n.children) { + Node nc = (Node)c; + + if (n.parent != null) { + drawEdge(n.layout.childrenPort.x, n.layout.childrenPort.y, nc.layout.port.x, nc.layout.port.y); + continue; + } + + Rect n1 = n.layout; + Rect n2 = nc.layout; + _drawEdge(n1.x+(n1.width/2), n1.y+(n1.height/2), n1.width * 0, n2.x+(n2.width/2), n2.y+(n2.height/2), n2.width, 1); + } + + g.strokeWeight(0.5 * style.border); + Rect r = new Rect((Rect)n.layout); + g.fill(style.fillColor); + g.stroke(style.borderColor); + g.rect(r.x, r.y, r.width, r.height, 4); + + Style gs = globalStyle; + + Rect ew = gs.calculateTextExtents(n.text, style.textSize); + Rect eh = gs.calculateTextExtents(" ", style.textSize); + float tx = (eh.height / 2) + (r.width / 2) - (ew.width / 2); + float ty = (eh.height / 1.6) + (r.height / 2) - (ew.height / 2); + + g.textFont(gs.font, style.textSize); + g.textAlign(style.textAlign); + g.fill(style.textColor); + g.text(n.text, r.x + tx, r.y + ty); + + for (Object c : n.children) { + Node nc = (Node)c; + _drawNode(nc); + } + + g.popMatrix(); + } + + void drawEdge(float x, float y, float xx, float yy) { + PGraphics g = gfx; + if (g == null) + g = getGraphics(); + + float tx = (xx - x)/2; + float ty = (yy - y)/2; + + if (y == yy) { + g.line(x, y, xx, yy); + } else { + g.line(x, y, x+tx, y); + g.line(x+tx, y, xx-tx, yy); + g.line(xx, yy, xx-tx, yy); + } + } + + void draw(Node root) { + + for (Object c : root.edges) { + Edge e = (Edge)c; + + if (e.from == null || e.to == null) { + continue; + } + + Rect n1 = e.from.layout; + Rect n2 = e.to.layout; + + PGraphics g = gfx; + if (g == null) + g = getGraphics(); + + g.strokeWeight(0.5 * root.style.edge); + g.stroke(root.style.edgeColor); + g.noFill(); + _drawEdge(n1.x+(n1.width/2), n1.y+(n1.height/2), n1.width, n2.x+(n2.width/2), n2.y+(n2.height/2), n2.width, 1); + } + + _drawNode(root); + } + + void saveImage(Node root, float scale) { + + Rect r = root.layout.bounds; + int gw = (int)(r.width * scale); + int gh = (int)(r.height * scale); + PGraphics g = createGraphics(gw, gh, JAVA2D); + + float xx = r.x * scale; + float yy = r.y * scale; + float ww = (r.width * scale) - g.width; + float hh = (r.height * scale) - g.height; + + g.beginDraw(); + g.clear(); + g.background(255); + + g.translate(-xx, -yy); + g.scale(scale); + + begin(g); + draw(root); + end(); + + g.endDraw(); + g.save(project + ".png"); + + println("done"); + } +} + +Renderer globalRenderer = new Renderer(); \ No newline at end of file diff --git a/style.pde b/style.pde new file mode 100644 index 0000000..711316c --- /dev/null +++ b/style.pde @@ -0,0 +1,111 @@ +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.lang.reflect.Type; +import java.awt.Color; + +class Style { + + public String name; + public String fontName = "menlo"; + + public int margin = 2; + public int spacing = 2; + + public int border = 1; + public color borderColor = color(0x80); + public int link = 2; + public color linkColor = color(0x40); + public int edge = 1; + public color edgeColor = color(0xff,0x00,0x00); + + public int roundCorner = 8; + public color textColor = color(0x0); //color(0xff, 0xff, 0xff); + public color fillColor = color(0xff); //color(0x50, 0x50, 0x50); + public int textSize = 12; + int textAlign = LEFT; + + PFont font; + + Style() { + } + + Style(String name) { + this.name = name; + } + + void _prep() { + + if (font == null) { + font = createFont(fontName, textSize, true); + } + + } + + int _stringLines(String l) { + String lines[] = l.split("\n"); + return lines.length; + } + + float _stringMaxWidth(String l) { + String lines[] = l.split("\n"); + float tw = textWidth(" "); + for (String s : lines) { + float tt = textWidth(s + " "); + if (tt > tw) + tw = tt; + } + return tw; + } + + Rect calculateTextExtents(String text, int size) { + + _prep(); + + Rect r = new Rect(); + + textFont(font, size); + + r.width = _stringMaxWidth(text) + (this.margin * 2); + r.height = _stringLines(text) * (this.textSize + 6); + + return r; + } + + void loadStyle(JSONObject s) { + + try { + Field fields[] = Style.class.getFields(); + for(Field f : fields) { + String sfield = f.toString(); + String stype = f.getGenericType().toString(); + String sname = sfield.substring(sfield.lastIndexOf(".")+1); + if (stype.equals("int")) { + if (sname.indexOf("color") >= 0 || sname.indexOf("Color") >= 0) { + try { + String sv = s.getString(sname); + String rr = sv.substring(0,2); + String gg = sv.substring(2,4); + String bb = sv.substring(4,6); + color v = color(Integer.parseInt(rr, 16), Integer.parseInt(gg, 16), Integer.parseInt(bb, 16)); + f.set(this, v); + } catch(Exception e) { + } + } else { + try { + int v = s.getInt(sname); + f.set(this, v); + } catch(Exception e) { + } + } + } + } + } catch(Exception e) { + println(e); + } + + //println(s); + //for( + } +} + +Style globalStyle = new Style("global"); \ No newline at end of file diff --git a/yaml2mindmap.pde b/yaml2mindmap.pde new file mode 100644 index 0000000..548acc2 --- /dev/null +++ b/yaml2mindmap.pde @@ -0,0 +1,148 @@ +String project = "civpro"; + +Node root; + +int defaultBx = 10; +int defaultBy = 10; +int xOffset = 0; +int yOffset = 0; +int bx = defaultBx; +int by = defaultBy; +float scale = 1.4; +float windowScale = 1.0; + +float fileScale = 2.0; + +boolean locked = false; + +int redrawCount = 0; +void doRedraw() { + redrawCount = 20; + loop(); +} + +void settings() +{ + size((int)(1280*windowScale), (int)(720*windowScale)); +} + +void setup() { + frameRate(32); + + root = new Node(); + + execCommand("python " + dataPath("../parsey.py") + " " + dataPath(project) + ".yaml"); + root.load(dataPath(project + ".json")); + + Layout l = new Layout(); + l.layoutBiTree(root); + + centerNode(root.layout); + doRedraw(); +} + +void draw() { + + background(255); + pushMatrix(); + + translate(bx, by); + scale(scale); + + globalRenderer.begin(getGraphics()); + globalRenderer.draw(root); + globalRenderer.end(); + + popMatrix(); + + if (redrawCount > 0) { + redrawCount--; + noStroke(); + fill(0, 255, 0); + rect(10, 10, 5, 5); + } else { + noLoop(); + } +} + +void mousePressed() { + locked = true; + + xOffset = mouseX-bx; + yOffset = mouseY-by; + + doRedraw(); +} + +void mouseDragged() { + if (locked) { + bx = mouseX-xOffset; + by = mouseY-yOffset; + } + + doRedraw(); +} + +void mouseReleased() { + locked = false; + doRedraw(); +} + +void centerNode(Rect n) { + if (n == null) + return; + float fbx = (width/2) - ((n.x + (n.width/2))* scale); + float fby = (height/2) - ((n.y + (n.height/2))* scale); + bx = (int)fbx; + by = (int)fby; +} + +void keyReleased() { + + // scale + { + if (key == '-') { + scale -= 0.2; + } + if (key == '=') { + scale += 0.2; + } + if (scale < 0.4) + scale = 0.4; + if (scale > 2.0) + scale = 2.0; + + centerNode(root.layout); + } + + { + if (key == 'i') { + println("saving image"); + globalRenderer.saveImage(root, fileScale); + } + } + + + if (key == ESC) + key = 0; + + doRedraw(); +} + +void execCommand(String cmd) { + + try { + println("run " + cmd); + Process p = Runtime.getRuntime().exec(cmd); + try { + p.waitFor(); + } + catch(Exception e) { + println("error running command " + cmd); + } + println("done"); + } + catch(java.io.IOException e) { + println(e); + } + } \ No newline at end of file