From e1b27b174d83e4fa0e6762d0be31a8cb267d0413 Mon Sep 17 00:00:00 2001 From: Tim Vieira Date: Sun, 7 Jul 2013 16:17:39 -0400 Subject: [PATCH] circle. Mouse over shows the rule source and rule index. minor tweaks to readme. --- README.md | 10 +-- src/Dyna/Backend/Python/debug.py | 51 ++++++------- src/Dyna/Backend/Python/post/draw_circuit.py | 79 ++++++++++++++++---- src/Dyna/Backend/Python/repl.py | 4 +- 4 files changed, 94 insertions(+), 50 deletions(-) diff --git a/README.md b/README.md index 1cbbeb0..a0431c6 100644 --- a/README.md +++ b/README.md @@ -15,16 +15,12 @@ Quick Start ----------- First, ensure that you have the Haskell platform 2012.2 or later installed -either through your favorite package manager (e,g, `apt-get install +either through your favorite package manager (e.g., `apt-get install haskell-platform`) or by installing it stand-alone from the haskell homepage. This is enough to get you the compiler up and running. In order execute -programs, you'll need to set up the Python backend up. For that you'll need to -have the following: - - * Python 2.7+ - -The python modules required +programs, you'll need to set up the Python backend. For that you'll need `Python +2.7+` and the following python modules $ easy_install ipython diff --git a/src/Dyna/Backend/Python/debug.py b/src/Dyna/Backend/Python/debug.py index 3aef59d..2aa120a 100644 --- a/src/Dyna/Backend/Python/debug.py +++ b/src/Dyna/Backend/Python/debug.py @@ -1,6 +1,6 @@ #!/usr/bin/env python """ -Generates a visual representation of a Dyna program rules after the +Generates a visual representation of a Dyna program's rules after the normalization process. """ @@ -16,7 +16,7 @@ try: from pygments.lexers import get_lexer_by_name from pygments.formatters import HtmlFormatter -except ImportError as e: +except ImportError: def format_code(code): warn('pygments not installed.') return code, 0 @@ -109,8 +109,8 @@ class Hypergraph(object): # connect body variables to edge crux for b in e.body: -# print >> f, '"%s" -> "%s" [arrowhead=none];' % (b, id(e)) - print >> f, '"%s" -> "%s";' % (b, id(e)) + print >> f, '"%s" -> "%s" [arrowhead=none];' % (b, id(e)) +# print >> f, '"%s" -> "%s";' % (b, id(e)) # connect head variables to edge # print >> f, ' "%s" [label="",shape=point];' % (id(e)) @@ -136,27 +136,27 @@ class Hypergraph(object): self.render(name) os.system('gnome-open %s.svg 2>/dev/null' % name) - def get_function(self, x): - """ - String of symbolic representation of ``x``, a variable or function, in - this expresion graph. - """ - - if isinstance(x, Edge): - label = re.sub('@\d+$', '', x.label) - if not x.body: # arity 0 - return label - - if not label[0].isalpha() and len(x.body) == 2: - [a,b] = map(self.get_function, x.body) - return '(%s %s %s)' % (a, label, b) - - return '%s(%s)' % (label, ', '.join(map(self.get_function, x.body))) - else: - if not self.incoming[x]: # input variable - return x - [e] = self.incoming[x] - return self.get_function(e) +# def get_function(self, x): +# """ +# String of symbolic representation of ``x``, a variable or function, in +# this expresion graph. +# """ +# +# if isinstance(x, Edge): +# label = re.sub('@\d+$', '', x.label) +# if not x.body: # arity 0 +# return label +# +# if not label[0].isalpha() and len(x.body) == 2: +# [a,b] = map(self.get_function, x.body) +# return '(%s %s %s)' % (a, label, b) +# +# return '%s(%s)' % (label, ', '.join(map(self.get_function, x.body))) +# else: +# if not self.incoming[x]: # input variable +# return x +# [e] = self.incoming[x] +# return self.get_function(e) def toposort(self, root): visited = set() @@ -208,6 +208,7 @@ def graph_styles(g): # edge styles for e in g.edges: + sty[e].update({'shape': 'rectangle'}) if e.label.startswith('&') \ or e.label.startswith('!') \ or e.label.startswith('=') : # distiguish unif edges diff --git a/src/Dyna/Backend/Python/post/draw_circuit.py b/src/Dyna/Backend/Python/post/draw_circuit.py index 6509e5b..194c507 100644 --- a/src/Dyna/Backend/Python/post/draw_circuit.py +++ b/src/Dyna/Backend/Python/post/draw_circuit.py @@ -1,10 +1,39 @@ # -*- coding: utf-8 -*- +""" +TODO: draw subgraph of nodes matching a query. +""" +import re import webbrowser from debug import Hypergraph from cStringIO import StringIO from utils import lexer, subst +HEADER = """ + + + + + + + +""" + +FOOTER = """ + +""" def circuit(edges): # create hypergraph object @@ -59,36 +88,56 @@ class draw_circuit(object): es = infer_edges(interp) c = circuit(es) + from collections import defaultdict + sty = defaultdict(dict) + for e in c.edges: + sty[e].update(dict(shape='circle', + width='.125', + fillcolor="yellow", + label='')) + with file(outfile, 'wb') as f: - print >> f, """ - - - - - - """ + print >> f, HEADER x = StringIO() interp.dump_charts(x) print >> f, '
%s
' \ - % '

Charts

%s' \ + % '

Solution

%s' \ % '
%s
' \ % x.getvalue() + svg = c.render('circuit', sty=sty) + + E = {str(id(e)): e for e in c.edges} + + def foo(m): + z, i, cls, x, q = m.groups() + + if cls == 'edge': + # split on arrow -> + u, v = x.split('->') + print 'edge:', i, u, v + else: + if x in E: + print 'crux', i, x, + rule = interp.rules[int(E[x].label)] + x = '%s %% rule %s' % (rule.src, rule.index) + else: + print 'node:', i, x + + return '%s%s' % (i, cls, x, q) + + svg = re.sub('([^<>]+)([\w\W]+?))', foo, svg) + print >> f, """

Hypergraph

%s
- """ % c.render('circuit') + """ % svg - print >> f, '' + print >> f, FOOTER if open: webbrowser.open(f.name) diff --git a/src/Dyna/Backend/Python/repl.py b/src/Dyna/Backend/Python/repl.py index 7ce7bbf..a9299ea 100644 --- a/src/Dyna/Backend/Python/repl.py +++ b/src/Dyna/Backend/Python/repl.py @@ -1,9 +1,7 @@ # -*- coding: utf-8 -*- """ -TODO: unsubscribe - -TODO: subscriptions probably should only show "changes" +TODO: subscriptions: unsubscribe, only show changes TODO: help should print call signature of loads and post-processors in addition to help. -- 2.50.1