1- #+TITLE : ~prolog_graph_export~
2- #+AUTHOR : Wouter Beek
3- #+HTML_HEAD : <link rel="stylesheet" type="text/css" href="https://www.pirilampo.org/styles/readtheorg/css/htmlize.css"/>
4- #+HTML_HEAD : <link rel="stylesheet" type="text/css" href="https://www.pirilampo.org/styles/readtheorg/css/readtheorg.css"/>
5- #+HTML_HEAD : <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
6- #+HTML_HEAD : <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
7- #+HTML_HEAD : <script type="text/javascript" src="https://www.pirilampo.org/styles/lib/js/jquery.stickytableheaders.js"></script>
8- #+HTML_HEAD : <script type="text/javascript" src="https://www.pirilampo.org/styles/readtheorg/js/readtheorg.js"></script>
1+ #+title : ~prolog_graph_export~
2+ #+author : Wouter Beek
3+ #+html_head : <link rel="stylesheet" type="text/css" href="https://www.pirilampo.org/styles/readtheorg/css/htmlize.css"/>
4+ #+html_head : <link rel="stylesheet" type="text/css" href="https://www.pirilampo.org/styles/readtheorg/css/readtheorg.css"/>
5+ #+html_head : <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
6+ #+html_head : <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
7+ #+html_head : <script type="text/javascript" src="https://www.pirilampo.org/styles/lib/js/jquery.stickytableheaders.js"></script>
8+ #+html_head : <script type="text/javascript" src="https://www.pirilampo.org/styles/readtheorg/js/readtheorg.js"></script>
99
1010A library for exporting graphs from SWI-Prolog. Currently the
1111following formats are supported:
@@ -17,35 +17,38 @@ Please report any issues that you find when using this library.
1717
1818* Installation
1919
20- 1. Install [[http://www.swipl-prolog.org][SWI-Prolog]].
20+ 1. Install [[http://www.swipl-prolog.org][SWI-Prolog]].
2121
22- 2. Install [[https://github.com/wouterbeek/Prolog-Library-Collection][Prolog-Library-Collection]].
22+ 2. Install [[https://github.com/wouterbeek/Prolog-Library-Collection][Prolog-Library-Collection]].
2323
24- 3. Clone this library: ~git clone
25- https://github.com/wouterbeek/graph_export~.
24+ 3. Clone this library:
2625
27- 4. Add the following line to your ~.swiplrc~ file:
26+ #+begin_src sh
27+ git clone https://github.com/wouterbeek/prolog_graph_export
28+ #+end_src
2829
29- #+BEGIN_SRC prolog
30- user:file_search_path(library, '/your/path/to/prolog_graph_export/prolog').
31- #+END_SRC
30+ 4. Add the following line to your ~.swiplrc~ file:
3231
33- 5. In order to generate DOT visualizations, install GraphViz:
32+ #+begin_src prolog
33+ user:file_search_path(library, '/your/path/to/prolog_graph_export/prolog').
34+ #+end_src
3435
35- - ~sudo apt install graphviz~ on Debian-based distro's (e.g.,
36- Ubuntu).
36+ 5. In order to generate DOT visualizations, install GraphViz:
3737
38- - ~sudo dnf install graphviz~ on Red Hat-based distro's (e.g.,
39- Fedora).
38+ - ~sudo apt install graphviz~ on Debian-based distro's (e.g.,
39+ Ubuntu).
40+
41+ - ~sudo dnf install graphviz~ on Red Hat-based distro's (e.g.,
42+ Fedora).
4043
4144* Usage
4245
4346Once installed, modules from this library can by imported in the
4447following way:
4548
46- #+BEGIN_SRC prolog
49+ #+begin_src prolog
4750?- [library(graph/gv)].
48- #+END_SRC
51+ #+end_src
4952
5053** Exporting a single node
5154
@@ -55,9 +58,9 @@ Prolog output stream. The following example shows how to write a
5558graph consisting of one node, and open the result in a GraphViz
5659viewer:
5760
58- #+BEGIN_SRC prolog
61+ #+begin_src prolog
5962?- gv_view([Out]>>format(Out, "x [label=<Hello,<BR/>world!>,shape=diamond];\n", [])).
60- #+END_SRC
63+ #+end_src
6164
6265This opens the following image inside a GraphViz-compatible viewer:
6366
@@ -67,9 +70,9 @@ This opens the following image inside a GraphViz-compatible viewer:
6770
6871In this example we write a graph that consists of a single edge:
6972
70- #+BEGIN_SRC prolog
73+ #+begin_src prolog
7174?- gv_export('loves.svg', [Out]>>format(Out, "John -- Mary [label=loves]", [])).
72- #+END_SRC
75+ #+end_src
7376
7477This writes the following image to an SVG file. See the table in
7578Section [[Output formats]] for a full list of supported output formats.
@@ -96,10 +99,10 @@ labels (see Section [[HTML-like labels]]).
9699Suppose your program returns proof trees that consist of an entailment
97100rule label, a conclusion, and an arbitrary number of premises:
98101
99- #+BEGIN_SRC prolog
102+ #+begin_src prolog
100103?- Proof = t(rdfs(3),isa(class,class),[t(axiom(rdfs),range(range,class),[]),
101104 t(axiom(rdfs),range(subClassOf,class),[])]).
102- #+END_SRC
105+ #+end_src
103106
104107The following program exports such proof trees. Notice that this
105108program uses the support predicates. This allows the nodes to be
@@ -108,7 +111,7 @@ this results in simplified code because. Since these support
108111predicates are idempotent, emitting the same node/edge multiple times
109112does not accidentally change the exported graph.
110113
111- #+BEGIN_SRC prolog
114+ #+begin_src prolog
112115:- use_module(library(apply)).
113116:- use_module(library(graph/gv)).
114117:- use_module(library(yall)).
@@ -128,17 +131,17 @@ export_subproof(Out, Node, Tree) :-
128131 dot_node(Out, Concl),
129132 dot_arc(Out, Node, Concl),
130133 export_proof(Out, Tree).
131- #+END_SRC
134+ #+end_src
132135
133136Since we use the predicate ~dot_arc/3~, we must specify option
134137~directed(true)~ (graphs are undirected by default).
135138
136139In order to open a specific proof tree, like ~$Proof~, in a
137140GraphViz-compatible viewer, we make the following call:
138141
139- #+BEGIN_SRC prolog
142+ #+begin_src prolog
140143?- view_proof($Proof).
141- #+END_SRC
144+ #+end_src
142145
143146This produces the following visualization:
144147
@@ -148,14 +151,14 @@ This produces the following visualization:
148151
149152Suppose your program returns syntactic parse trees like the following:
150153
151- #+BEGIN_SRC prolog
154+ #+begin_src prolog
152155?- Tree = s(np(det(the),n(cat)),vp(v(loves),np(det(the),n(dog))))
153- #+END_SRC
156+ #+end_src
154157
155158The following code exports such parse trees to SVG (see
156159~example/parse_tree.pl~):
157160
158- #+BEGIN_SRC prolog
161+ #+begin_src prolog
159162:- use_module(library(apply)).
160163:- use_module(library(graph/gv)).
161164:- use_module(library(yall)).
@@ -169,7 +172,7 @@ export_tree(Out, Tree, Id) :-
169172 dot_node_id(Out, Id, [label(Op)]),
170173 maplist(export_tree(Out), Trees, Ids),
171174 maplist(dot_edge_id(Out, Id), Ids).
172- #+END_SRC
175+ #+end_src
173176
174177Notice that in the above program we use ~dot_node_id/3~ instead of
175178~dot_node/3~ and ~dot_edge_id/3~ instead of ~dot_edge/3~. The ~*_id~
@@ -179,9 +182,9 @@ IDs are generated with ~dot_id/1~.
179182We can generate the visualization for the above syntax tree (~$Tree~),
180183by making the following call:
181184
182- #+BEGIN_SRC prolog
185+ #+begin_src prolog
183186?- export_tree($Tree).
184- #+END_SRC
187+ #+end_src
185188
186189This prodices the following result:
187190
@@ -309,7 +312,7 @@ string represents one line in a multi-line label.
309312Strings must adhere to the following BNF grammar for DOT HTML-like
310313labels:
311314
312- #+BEGIN_SRC bnf
315+ #+begin_src bnf
313316label : text
314317 | table
315318text : textitem
@@ -334,7 +337,7 @@ cells : cell
334337 | cells <VR/> cell
335338cell: <TD> label </TD>
336339 | <TD> <IMG/> </TD>
337- #+END_SRC
340+ #+end_src
338341
339342In addition to the above BNF grammar, tags are allowed to have
340343attributes that are formatted similar to HTML attributes. Different
0 commit comments