-----------
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
#!/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.
"""
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
# 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))
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()
# 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
# -*- 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 = """
+<html>
+<head>
+<style>
+body {
+ background-color: black;
+ color: white;
+}
+</style>
+
+<!--
+<script type="text/javascript" language="javascript" src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
+<script>
+function clicked(x) {
+ alert(x.childNodes);
+}
+</script>
+-->
+
+</head><body>
+"""
+
+FOOTER = """
+</body></html>
+"""
def circuit(edges):
# create hypergraph 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, """
- <html>
- <head>
- <style>
- body {
- background-color: black;
- color: white;
- }
- </style>
- </head>
- <body>
- """
+ print >> f, HEADER
x = StringIO()
interp.dump_charts(x)
print >> f, '<div style="position:absolute;">%s</div>' \
- % '<h1>Charts</h1>%s' \
+ % '<h1>Solution</h1>%s' \
% '<pre style="width: 500px;">%s</pre>' \
% 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 '<g id="%s" class="%s"><title>%s</title>%s</g>' % (i, cls, x, q)
+
+ svg = re.sub('<g (id="([^"]+)" class="(node|edge)"><title>([^<>]+)</title>([\w\W]+?)</g>)', foo, svg)
+
print >> f, """
<div style="width: 800px; position:absolute; left: 550px">
<h1>Hypergraph</h1>
%s
</div>
- """ % c.render('circuit')
+ """ % svg
- print >> f, '</body></html>'
+ print >> f, FOOTER
if open:
webbrowser.open(f.name)