Skip to content

Commit d69692d

Browse files
committed
Improvements to documentation and examples.
- Turned examples into modules with `export/0' and `view/0' predicates. - Link to the example files from the main README. - Added a README to the examples subdirectory. - Updated images generated by a more recent GraphViz version.
1 parent c64fdf9 commit d69692d

File tree

12 files changed

+280
-214
lines changed

12 files changed

+280
-214
lines changed

README.md

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,30 +32,32 @@ following way:
3232

3333
The basic feature of this library is that it allows you to write to a
3434
GraphViz output file or to a GraphViz viewer by simply writing to a
35-
Prolog output stream. The following example shows how to write a
36-
graph consisting of one node, and open the result in a GraphViz
37-
viewer:
35+
Prolog output stream. The following example (see
36+
[`example/hello.pl`](example/hello.pl)) shows how to write a graph
37+
consisting of one node, and open the result in a GraphViz viewer:
3838

3939
```pl
4040
?- gv_view([Out]>>format(Out, "x [label=<Hello,<BR/>world!>,shape=diamond];\n", [])).
4141
```
4242

4343
This opens the following image inside a GraphViz-compatible viewer:
4444

45-
![](./example/hello.svg)
45+
![](example/hello.svg)
4646

4747
### Exporting a single edge
4848

49-
In this example we write a graph that consists of a single edge:
49+
In this example (see [`example/loves.pl`](example/loves.pl)) we write
50+
a graph that consists of a single edge:
5051

5152
```pl
52-
?- gv_export('loves.svg', [Out]>>format(Out, "John -- Mary [label=loves]", [])).
53+
?- gv_export('loves.svg', [Out]>>format(Out, "John -- Mary [label=loves];\n", [])).
5354
```
5455

5556
This writes the following image to an SVG file. See the table in
56-
Section [[Output formats]] for a full list of supported output formats.
57+
Section [Output formats](#output-formats-option-format1) for a full
58+
list of supported output formats.
5759

58-
![](./example/loves.svg)
60+
![](example/loves.svg)
5961

6062
## Advanced use
6163

@@ -70,24 +72,26 @@ library's support predicates. The most important ones are:
7072
`dot_arc/4` and `dot_edge/4` take a list of options that are emitted
7173
as GraphViz attributes. Option `label(+or([string,list(string)]))`
7274
allows (multi-line) Unicode labels to be emitted using HTML-like
73-
labels (see Section [[HTML-like labels]]).
75+
labels (see Section [HTML-like labels](#html-like-labels)).
7476

7577
### Exporting a proof tree
7678

7779
Suppose your program returns proof trees that consist of an entailment
7880
rule label, a conclusion, and an arbitrary number of premises:
7981

8082
```pl
81-
?- Proof = t(rdfs(3),isa(class,class),[t(axiom(rdfs),range(range,class),[]),
82-
t(axiom(rdfs),range(subClassOf,class),[])]).
83+
?- Proof = t(rdfs(3),(class,class),[t(axiom(rdfs),range(range,class),[]),
84+
t(axiom(rdfs),range(,class),[])]).
8385
```
8486

85-
The following program exports such proof trees. Notice that this
86-
program uses the support predicates. This allows the nodes to be
87-
characterized by Prolog terms instead of DOT IDs. For most programs
88-
this results in simplified code because. Since these support
89-
predicates are idempotent, emitting the same node/edge multiple times
90-
does not accidentally change the exported graph.
87+
The following program (see
88+
[`example/proof_tree.pl`](example/proof_tree.pl)) exports such proof
89+
trees. Notice that this program uses the support predicates. This
90+
allows the nodes to be characterized by Prolog terms instead of DOT
91+
IDs. For most programs this results in simplified code because.
92+
Since these support predicates are idempotent, emitting the same
93+
node/edge multiple times does not accidentally change the exported
94+
graph.
9195

9296
```pl
9397
:- use_module(library(apply)).
@@ -123,7 +127,7 @@ GraphViz-compatible viewer, we make the following call:
123127

124128
This produces the following visualization:
125129

126-
![](./example/proof_tree.svg)
130+
![](example/proof_tree.svg)
127131

128132
### Exporting a parse trees
129133

@@ -134,7 +138,7 @@ Suppose your program returns syntactic parse trees like the following:
134138
```
135139

136140
The following code exports such parse trees to SVG (see
137-
`example/parse_tree.pl`):
141+
[`example/parse_tree.pl`](example/parse_tree.pl)):
138142

139143
```pl
140144
:- use_module(library(apply)).
@@ -166,7 +170,7 @@ by making the following call:
166170

167171
This prodices the following result:
168172

169-
![](./example/parse_tree.svg)
173+
![](example/parse_tree.svg)
170174

171175
Notice that we create a new DOT ID (`dot_id/1`) for each node in the
172176
tree. Because of this, the two occurrences of ‘the’ can be

example/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Run the examples
2+
3+
```sh
4+
$ swipl -s hello.pl -g view -t halt
5+
$ swipl -s loves.pl -g view -t halt
6+
$ swipl -s parse_tree.pl -g view -t halt
7+
$ swipl -s proof_tree.pl -g view -t halt
8+
```

example/hello.dot

Lines changed: 0 additions & 3 deletions
This file was deleted.

example/hello.pl

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
1-
:- use_module(library(gv)).
1+
:- module(hello, [export/0, view/0]).
2+
3+
/** <module> Hello, world! for GraphViz
4+
5+
*/
6+
27
:- use_module(library(yall)).
38

4-
run :-
5-
gv_view([Out]>>format(Out, "x [label=<Hello,<BR/>world!>]\n", [])).
9+
:- use_module(library(gv)).
10+
11+
dot("x [label=<Hello,<BR/>world!>,shape=diamond];\n").
12+
13+
export :-
14+
dot(String),
15+
gv_export('hello.svg', {String}/[Out]>>format(Out, String, [])).
16+
17+
view :-
18+
dot(String),
19+
gv_view({String}/[Out]>>format(Out, String, [])).

example/hello.svg

Lines changed: 12 additions & 12 deletions
Loading

example/loves.dot

Lines changed: 0 additions & 3 deletions
This file was deleted.

example/loves.pl

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
1-
:- use_module(library(gv)).
1+
:- module(loves, [export/0, view/0]).
2+
3+
/** <module> John loves Mary example
4+
5+
*/
6+
27
:- use_module(library(yall)).
38

4-
run :-
5-
gv_export('loves.svg', [Out]>>format(Out, "John -- Mary [label=loves]", [])).
9+
:- use_module(library(gv)).
10+
11+
dot("John -- Mary [label=loves];\n").
12+
13+
export :-
14+
dot(String),
15+
gv_export('loves.svg', {String}/[Out]>>format(Out, String, [])).
16+
17+
view :-
18+
dot(String),
19+
gv_view({String}/[Out]>>format(Out, String, [])).

example/loves.svg

Lines changed: 10 additions & 10 deletions
Loading

example/parse_tree.pl

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,33 @@
1+
:- module(parse_tree, [export/0, export/1, view/0, view/1]).
2+
3+
/** <module> Parse tree example
4+
5+
*/
6+
17
:- use_module(library(apply)).
2-
:- use_module(library(gv)).
38
:- use_module(library(yall)).
49

5-
run :-
6-
export_tree(s(np(det(the),n(cat)),vp(v(loves),np(det(the),n(dog))))).
10+
:- use_module(library(gv)).
11+
12+
export :-
13+
tree(Tree),
14+
export(Tree).
15+
16+
export(Tree) :-
17+
gv_export('parse_tree.svg', {Tree}/[Out]>>export_tree_(Out, Tree, _)).
18+
19+
tree(s(np(det(the),n(cat)),vp(v(loves),np(det(the),n(dog))))).
20+
21+
view :-
22+
tree(Tree),
23+
view(Tree).
724

8-
export_tree(Tree) :-
9-
gv_export('parse_tree.svg', {Tree}/[Out]>>export_tree(Out, Tree, _)).
25+
view(Tree) :-
26+
gv_view({Tree}/[Out]>>export_tree_(Out, Tree, _)).
1027

11-
export_tree(Out, Tree, Id) :-
28+
export_tree_(Out, Tree, Id) :-
1229
Tree =.. [Op|Trees],
1330
dot_id(Id),
1431
dot_node_id(Out, Id, [label(Op)]),
15-
maplist(export_tree(Out), Trees, Ids),
32+
maplist(export_tree_(Out), Trees, Ids),
1633
maplist(dot_edge_id(Out, Id), Ids).

0 commit comments

Comments
 (0)