graphviz.pl 4.08 KB
Newer Older
1 2 3
:- module(
  graphviz,
  [
4
    % Commands
5
    draw_graph/0,
MARTINEZ Thierry 's avatar
MARTINEZ Thierry committed
6
    export_graph/1,
7 8 9
    % Public API
    draw_graph/1,
    export_graph/2,
MARTINEZ Thierry 's avatar
MARTINEZ Thierry committed
10
    set_draw_graph_driver/1
11 12 13
  ]
).

SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
14
:- use_module(doc).
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
15
:- use_module(biocham).
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
16

17 18 19 20 21 22

:- devdoc('\\section{Commands}').


draw_graph :-
  biocham_command,
MARTINEZ Thierry 's avatar
MARTINEZ Thierry committed
23
  doc('Draws the current graph.'),
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
24 25
  option(left_to_right, yesno, _LeftToRight, 'Draws the graph from left to
    right instead of the default top to bottom.'),
26 27
  get_current_graph(Id),
  draw_graph(Id).
28 29


FAGES Francois's avatar
FAGES Francois committed
30
:- initial(option(left_to_right: yes)).
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
31 32


MARTINEZ Thierry 's avatar
MARTINEZ Thierry committed
33
export_graph(OutputFile) :-
34 35
  biocham_command,
  type(OutputFile, output_file),
MARTINEZ Thierry 's avatar
MARTINEZ Thierry committed
36 37 38
  doc('
    Exports the current graph in a file.
    The format is chosen from the suffix:
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
39 40
    \\texttt{.dot}, \\texttt{.pdf}, \\texttt{.eps}, \\texttt{.ps},
    \\texttt{.png} or \\texttt{.svg}
MARTINEZ Thierry 's avatar
MARTINEZ Thierry committed
41 42
    -- assuming no extension is \\texttt{.dot}.
  '),
43 44
  get_current_graph(Id),
  export_graph(Id, OutputFile).
45 46


MARTINEZ Thierry 's avatar
MARTINEZ Thierry committed
47 48 49
:- devdoc('\\section{Public API}').


50 51 52 53 54 55 56 57 58 59
draw_graph(Id) :-
  nb_getval(draw_graph_driver, Driver),
  call(Driver, Id).


export_graph(Id, OutputFile) :-
  file_name_extension(_, Suffix, OutputFile),
  export_graph(Suffix, Id, OutputFile).


MARTINEZ Thierry 's avatar
MARTINEZ Thierry committed
60 61 62 63 64 65 66
set_draw_graph_driver(Driver) :-
  nb_setval(draw_graph_driver, Driver).


:- devdoc('\\section{Internal predicates}').


67 68
export_graph('', Id, OutputFile) :-
  !,
MARTINEZ Thierry 's avatar
MARTINEZ Thierry committed
69
  atom_concat(OutputFile, '.dot', FilenameDot),
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
70
  export_graph('dot', Id, FilenameDot).
MARTINEZ Thierry 's avatar
MARTINEZ Thierry committed
71

72 73 74
export_graph('dot', Id, OutputFile) :-
  !,
  create_cgraph(Id, Graph),
75 76 77
  agwrite(Graph, OutputFile),
  agclose(Graph).

78 79 80 81 82 83 84 85
export_graph(Format, Id, OutputFile) :-
  graphviz_format(Format),
  !,
  create_cgraph(Id, Graph),
  gvLayout(Graph, dot),
  gvRenderFilename(Graph, Format, OutputFile),
  gvFreeLayout(Graph),
  agclose(Graph).
MARTINEZ Thierry 's avatar
MARTINEZ Thierry committed
86 87


88
graphviz_format('pdf').
MARTINEZ Thierry 's avatar
MARTINEZ Thierry committed
89

90
graphviz_format('eps').
MARTINEZ Thierry 's avatar
MARTINEZ Thierry committed
91

92
graphviz_format('ps').
MARTINEZ Thierry 's avatar
MARTINEZ Thierry committed
93

94
graphviz_format('png').
MARTINEZ Thierry 's avatar
MARTINEZ Thierry committed
95

96
graphviz_format('svg').
97 98


99 100
create_cgraph(Id, Graph) :-
  get_graph_name(Id, GraphName),
101 102
  agopen(GraphName, directed, Graph),
  agattr(Graph, graph, ratio, fill, _),
MARTINEZ Thierry 's avatar
MARTINEZ Thierry committed
103
  agattr(Graph, node, shape, ellipse, _),
104 105
  agattr(Graph, edge, arrowhead, 'normal', _),
  agattr(Graph, edge, color, 'black', _),
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
106
  agattr(Graph, edge, dir, 'forward', _),
107
  agsubg(Graph, 'source', true, Source),
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
108 109 110 111
  get_option(left_to_right, LR),
  (
    LR == yes
  ->
112 113
    agattr(Graph, graph, rankdir, 'LR', _),
    agattr(Graph, graph, size, '11,7.5', _)
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
114
  ;
115
    agattr(Graph, graph, size, '7.5,11', _)
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
116
  ),
117
  agattr(Source, graph, rank, 'min', _),
118
  \+ (
119
    item([parent: Id, kind: vertex, item: VertexName, id: VertexId]),
120 121 122
    \+ (
      agnode(Graph, VertexName, true, Node),
      (
123
        get_attribute(VertexId, kind = transition)
124 125
      ->
        agset(Node, shape, box)
126 127 128 129
      ;
        get_attribute(VertexId, source = true)
      ->
        agsubnode(Source, Node, true)
130 131 132 133 134 135
      ;
        true
      )
    )
  ),
  \+ (
136
    item([parent: Id, kind: edge, item: Edge, id: EdgeId]),
137 138 139 140
    \+ (
      Edge = (VertexA -> VertexB),
      agnode(Graph, VertexA, false, NodeA),
      agnode(Graph, VertexB, false, NodeB),
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
141
      agedge(Graph, NodeA, NodeB, true, GraphvizEdge),
142 143 144 145 146 147 148 149 150
      (
        get_attribute(EdgeId, sign='-')
      ->
        agset(GraphvizEdge, arrowhead, 'tee'),
        agset(GraphvizEdge, color, 'red')
      ;
        get_attribute(EdgeId, sign='+')
      ->
        agset(GraphvizEdge, color, 'green4')
151 152 153 154
      ;
        get_attribute(EdgeId, sign='±')
      ->
        agset(GraphvizEdge, color, 'green4'),
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
155
        agedge(Graph, NodeA, NodeB, true, GraphvizEdge2),
156 157
        agset(GraphvizEdge2, arrowhead, 'tee'),
        agset(GraphvizEdge2, color, 'red')
SOLIMAN Sylvain's avatar
SOLIMAN Sylvain committed
158 159 160 161
      ;
        get_attribute(EdgeId, dir='none')
      ->
        agset(GraphvizEdge, dir, 'none')
162 163 164
      ;
        true
      )
165 166
    )
  ).
MARTINEZ Thierry 's avatar
MARTINEZ Thierry committed
167 168


169
graph_png(Id) :-
MARTINEZ Thierry 's avatar
MARTINEZ Thierry committed
170 171
  count(graph_png, Index),
  format(atom(Filename), 'graph~d.png', [Index]),
172
  export_graph(Id, Filename),
MARTINEZ Thierry 's avatar
MARTINEZ Thierry committed
173 174 175
  view_image(Filename).


176
graph_svg(Id) :-
MARTINEZ Thierry 's avatar
MARTINEZ Thierry committed
177 178
  count(graph_svg, Index),
  format(atom(Filename), 'graph~d.svg', [Index]),
179
  export_graph(Id, Filename),
MARTINEZ Thierry 's avatar
MARTINEZ Thierry committed
180 181 182
  view_image(Filename).


183
graph_pdf(Id) :-
MARTINEZ Thierry 's avatar
MARTINEZ Thierry committed
184 185
  count(graph_pdf, Index),
  format(atom(Filename), 'graph~d.pdf', [Index]),
186
  export_graph(Id, Filename),
MARTINEZ Thierry 's avatar
MARTINEZ Thierry committed
187
  open_file(Filename).