Skip to content
Browse files

now distributing example that includes Lua format helper

  • Loading branch information...
1 parent 1d0fdba commit 8b0ad29157028b4e5339c5c91447ace674043f11 @rndmcnlly committed Oct 1, 2011
Showing with 69 additions and 9 deletions.
  1. +13 −1 README
  2. +39 −0 example.lp
  3. +17 −8 lonsdaleite
View
14 README
@@ -17,9 +17,12 @@ USAGE
facts, one per line, in AnsProlog syntax (in the format you would feed to a
solver).
- cat some_logical_graph.lp | lonsdaleite | dot
+ $ cat some_ground_graph_instance.lp | lonsdaleite | dot
+ or
+ $ clingo example.lp | lonsdaleite -dcub
OPTIONS
+ -d dump graph after parsing
-c expect input piped from Clasp's output
-u construct URL for Google's GraphViz Charts API
-b open that URL in the default web browser
@@ -41,6 +44,15 @@ MODELING INTERFACE
graphviz_global_node_attr(K,V)
graphviz_global_edge_attr(K,V)
+HINTS
+ Look at the Lua code embedded in example.lp for a helper function that can be
+ used to easily build complex strings for use as attribute values.
+
+ INPUT: graphviz_node_attr(N,pos,@format("%s,%s!",X,Y)) :- at(N,X,Y).
+ OUTPUT: graphviz_node_attr(b,pos,"5,5!")
+ NOTE: Use the neato engine if you are generating the pos attribute with ASP.
+
+
CAVEATS
When using the -c option, Lonsdaleite splits the answer set line on spaces
before parsing any of the terms. Avoid embedded spaces in your terms when
View
39 example.lp
@@ -0,0 +1,39 @@
+
+
+#begin_lua
+ function format(template, ...)
+ strings = {}
+ for i, v in ipairs(arg) do
+ table.insert(strings,tostring(v))
+ end
+ return Val.new(Val.ID,[["]]..string.format(template,unpack(strings))..[["]])
+ end
+#end_lua.
+
+node(a;b;c;d;e).
+
+edge(a,b).
+edge(b,c).
+edge(c,d).
+edge(d,e).
+edge(c,e).
+edge(e,a).
+
+at(a,0,0).
+at(b,5,5).
+
+label(a,"origin").
+label(e,777).
+
+label(b,c,"beecee").
+
+graphviz_type(digraph).
+graphviz_engine(neato).
+graphviz_global_node_attr(shape,circle).
+graphviz_global_edge_attr(color,red).
+graphviz_node(N) :- node(N).
+graphviz_edge(N1,N2) :- edge(N1,N2).
+graphviz_edge_attr(N1,N2,label,L) :- label(N1,N2,L).
+graphviz_node_attr(N,label,@format("%s[%s]",N,L)) :- label(N,L).
+graphviz_node_attr(N,shape,square) :- label(N,_).
+graphviz_node_attr(N,pos,@format("%s,%s!",X,Y)) :- at(N,X,Y).
View
25 lonsdaleite
@@ -1,10 +1,11 @@
#!/bin/env python
# vim: ft=python
-import sys, getopt, urllib, webbrowser, re
+import sys, getopt, webbrowser, re
def usage():
print("""
+ -d dump graph after parsing
-c expect input piped from Clasp's output
-u construct URL for Google's GraphViz Charts API
-b open that URL in the default web browser
@@ -14,18 +15,21 @@ def main():
expectClasp = False
constructUrl = False
showInBrowser = False
+ dumpGraph = False
try:
- opts, args = getopt.getopt(sys.argv[1:], "cub")
+ opts, args = getopt.getopt(sys.argv[1:], "dcub")
except getopt.GetoptError, err:
print str(err)
usage()
sys.exit(2)
for o, a in opts:
- if o == "-c":
+ if o == "-d":
+ dumpGraph = True
+ elif o == "-c":
expectClasp = True
- if o == "-u":
+ elif o == "-u":
constructUrl = True
elif o == "-b":
showInBrowser = True
@@ -75,6 +79,9 @@ def main():
for line in sys.stdin.readlines():
consider(line.strip())
+ if dumpGraph:
+ print(graph)
+
a_list = lambda seq: "[" + (",".join("%s=%s" % kv for kv in seq)) + "]"
@@ -87,20 +94,21 @@ def main():
if graph['graph_attr']:
graphvizCode += "graph" + a_list(graph['graph_attr'])
+ graphvizCode += ';'
if graph['global_node_attr']:
graphvizCode += "node" + a_list(graph['global_node_attr'])
+ graphvizCode += ';'
if graph['global_edge_attr']:
graphvizCode += "edge" + a_list(graph['global_edge_attr'])
+ graphvizCode += ';'
nodes = []
for (n1,) in graph['node']:
attrs = [(k,v) for n1t,k,v in graph['node_attr'] if n1t == n1]
- print n1, attrs
nodes.append(n1 + a_list(attrs) if attrs else n1)
graphvizCode += ';'.join(nodes)
-
graphvizCode += ';'
edges = []
@@ -113,8 +121,9 @@ def main():
graphvizCode += '}'
if constructUrl:
- query = {'cht': "gv:%s" % graph['engine'][-1][0], 'chl': graphvizCode}
- url = "http://chart.googleapis.com/chart?" + urllib.urlencode(query)
+ cht = "gv:%s" % graph['engine'][-1][0]
+ chl = graphvizCode
+ url = "http://chart.googleapis.com/chart?chl=" + chl + "&cht=" + cht
if showInBrowser:
webbrowser.open(url)
else:

0 comments on commit 8b0ad29

Please sign in to comment.
Something went wrong with that request. Please try again.