1/* Part of SWISH 2 3 Author: Markus Triska 4 E-mail: triska@metalevel.at 5 WWW: http://www.swi-prolog.org 6 Copyright (C): 2016 Markus Triska 7 All rights reserved. 8 9 Redistribution and use in source and binary forms, with or without 10 modification, are permitted provided that the following conditions 11 are met: 12 13 1. Redistributions of source code must retain the above copyright 14 notice, this list of conditions and the following disclaimer. 15 16 2. Redistributions in binary form must reproduce the above copyright 17 notice, this list of conditions and the following disclaimer in 18 the documentation and/or other materials provided with the 19 distribution. 20 21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 25 COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 27 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 28 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 29 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 31 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 POSSIBILITY OF SUCH DAMAGE. 33*/ 34 35:- module(swish_render_bdd, 36 [ term_rendering//3 % +Term, +Vars, +Options 37 ]). 38:- use_module('../render'). 39:- use_module(graphviz, []). 40:- use_module(library(clpb)). 41:- use_module(library(varnumbers)). 42 43:- register_renderer(bdd, "Render BDDs corresponding to CLP(B) constraints"). 44 45/** <module> Render Binary Decision Diagrams (BDDs) 46 47This renderer draws Binary Decision Diagrams (BDDs) that express 48CLP(B) constraints. 49 50Both kinds of residual goals that `library(bdd)` can emit are 51supported by this renderer: By default, `library(clpb)` emits `sat/1` 52residual goals. These are first translated to BDDs by reposting the 53goals while temporarily setting `clpb_residuals` to `bdd`. The 54translation of such BDDs to `dot/1` terms is straight-forward. 55Alternatively, you can set `clpb_residuals` to `bdd` yourself. In that 56case, residual goals are directly emitted as BDDs and are again 57translated to `dot/1` terms by this renderer. 58 59In both cases, the graphviz renderer is used for the final output. 60 61*/ 62 63%% term_rendering(+Term, +Vars, +Options)// 64% 65% Renders BDDs as emitted by library(clpb). 66 67term_rendering(Goal, Vars, Options) --> 68 ( { Goal = clpb:'$clpb_bdd'(Ns), 69 is_list(Ns) } -> [] 70 ; { Goal = sat(Sat0) } -> 71 { current_prolog_flag(clpb_residuals, OldFlag), 72 varnumbers_names(Sat0, Sat, VNames), 73 term_variables(Sat, Vs), 74 setup_call_cleanup(set_prolog_flag(clpb_residuals, bdd), 75 catch((sat(Sat), 76 copy_term(Vs, Vs, [clpb:'$clpb_bdd'(Ns)]), 77 % reset all attributes 78 maplist(del_attrs, Vs), 79 throw(nodes(Vs,Ns))), 80 % Vs recreate the bindings in nodes Ns 81 nodes(Vs,Ns), 82 true), 83 set_prolog_flag(clpb_residuals, OldFlag)), 84 % for displaying the labels, unify each variable with its name 85 maplist(eq, VNames) } 86 ), 87 { nodes_dot_digraph(Ns, Dot) }, 88 swish_render_graphviz:term_rendering(Dot, Vars, Options). 89 90eq(Name=Var) :- Name = Var. 91 92nodes_dot_digraph(Nodes, dot(digraph(Stmts))) :- 93 maplist(node_then_else, Nodes, Thens, Elses), 94 phrase((nodes_labels(Nodes), 95 [node(false, [fontname='Palatino-Bold']), 96 node(true, [fontname='Palatino-Bold'])], 97 [edge([style='filled'])], 98 list(Thens), 99 [edge([style='dotted'])], 100 list(Elses)), Stmts). 101 102nodes_labels([]) --> []. 103nodes_labels([node(N)-(v(Var,_) -> _ ; _)|Nodes]) --> 104 [node(N, [label=Var])], 105 nodes_labels(Nodes). 106 107node_then_else(Node-(_->Then0;Else0), (ID->Then), (ID->Else)) :- 108 maplist(node_id, [Node,Then0,Else0], [ID,Then,Else]). 109 110node_id(node(N), N). 111node_id(true, true). 112node_id(false, false). 113 114list([]) --> []. 115list([L|Ls]) --> [L], list(Ls)