View Javadoc

1   /*******************************************************************************
2   * SAT4J: a SATisfiability library for Java Copyright (C) 2004-2008 Daniel Le Berre
3   *
4   * All rights reserved. This program and the accompanying materials
5   * are made available under the terms of the Eclipse Public License v1.0
6   * which accompanies this distribution, and is available at
7   * http://www.eclipse.org/legal/epl-v10.html
8   *
9   * Alternatively, the contents of this file may be used under the terms of
10  * either the GNU Lesser General Public License Version 2.1 or later (the
11  * "LGPL"), in which case the provisions of the LGPL are applicable instead
12  * of those above. If you wish to allow use of your version of this file only
13  * under the terms of the LGPL, and not to allow others to use your version of
14  * this file under the terms of the EPL, indicate your decision by deleting
15  * the provisions above and replace them with the notice and other provisions
16  * required by the LGPL. If you do not delete the provisions above, a recipient
17  * may use your version of this file under the terms of the EPL or the LGPL.
18  * 
19  * Based on the original MiniSat specification from:
20  * 
21  * An extensible SAT solver. Niklas Een and Niklas Sorensson. Proceedings of the
22  * Sixth International Conference on Theory and Applications of Satisfiability
23  * Testing, LNCS 2919, pp 502-518, 2003.
24  *
25  * See www.minisat.se for the original solver in C++.
26  * 
27  *******************************************************************************/
28  package org.sat4j.minisat.core;
29  
30  import java.io.FileWriter;
31  import java.io.IOException;
32  import java.io.ObjectInputStream;
33  import java.io.PrintWriter;
34  import java.io.Writer;
35  
36  import org.sat4j.core.Vec;
37  
38  /**
39   * Class allowing to express the search as a tree in the dot language. The
40   * resulting file can be viewed in a tool like <a
41   * href="http://www.graphviz.org/">Graphviz</a>
42   * 
43   * To use only on small benchmarks.
44   * 
45   * Note that also does not make sense to use such a listener on a distributed or
46   * remote solver.
47   * 
48   * @author daniel
49   * 
50   */
51  public class DotSearchListener implements SearchListener {
52  
53      /**
54       * 
55       */
56      private static final long serialVersionUID = 1L;
57  
58      private final Vec<String> pile;
59  
60      private String currentNodeName = null;
61  
62      private transient Writer out;
63  
64      private boolean estOrange = false;
65  
66      public DotSearchListener(final String fileNameToSave) {
67          pile = new Vec<String>();
68          try {
69              out = new FileWriter(fileNameToSave);
70          } catch (IOException e) {
71              System.err.println("Problem when created file.");
72          }
73      }
74  
75      public final void assuming(final int p) {
76          final int absP = Math.abs(p);
77          String newName;
78  
79          if (currentNodeName == null) {
80              newName = ""+absP;
81              pile.push(newName);
82              saveLine(lineTab("\"" + newName + "\"" + "[label=\"" + newName
83                      + "\", shape=circle, color=blue, style=filled]"));
84          } else {
85              newName = currentNodeName;
86              pile.push(newName);
87              saveLine(lineTab("\"" + newName + "\"" + "[label=\""
88                      + absP
89                      + "\", shape=circle, color=blue, style=filled]"));
90          }
91          currentNodeName = newName;
92      }
93  
94      public final void propagating(final int p) {
95          String newName = currentNodeName + "." + p;
96  
97          if (currentNodeName == null) {
98              saveLine(lineTab("\"null\" [label=\"\", shape=point]"));
99          }
100         final String couleur = estOrange ? "orange" : "green";
101 
102         saveLine(lineTab("\"" + newName + "\"" + "[label=\""
103                 + Integer.toString(p) + "\",shape=point, color=black]"));
104         saveLine(lineTab("\"" + currentNodeName + "\"" + " -- " + "\""
105                 + newName + "\"" + "[label=" + "\" " + Integer.toString(p)
106                 + "\", fontcolor =" + couleur + ", color = " + couleur
107                 + ", style = bold]"));
108         currentNodeName = newName;
109         estOrange = false;
110     }
111 
112     public final void backtracking(final int p) {
113         final String temp = pile.last();
114         pile.pop();
115         saveLine("\"" + temp + "\"" + "--" + "\"" + currentNodeName + "\""
116                 + "[label=\"\", color=red, style=dotted]");
117         currentNodeName = temp;
118     }
119 
120     public final void adding(final int p) {
121         estOrange = true;
122     }
123 
124     public final void learn(final Constr clause) {
125     }
126 
127     public final void delete(final int[] clause) {
128     }
129 
130     public final void conflictFound() {
131         saveLine(lineTab("\"" + currentNodeName
132                 + "\" [label=\"\", shape=box, color=\"red\", style=filled]"));
133     }
134 
135     public final void solutionFound() {
136         saveLine(lineTab("\"" + currentNodeName
137                 + "\" [label=\"\", shape=box, color=\"green\", style=filled]"));
138     }
139 
140     public final void beginLoop() {
141     }
142 
143     public final void start() {
144         saveLine("graph G {");
145     }
146 
147     public final void end(Lbool result) {
148         saveLine("}");
149     }
150 
151     private final String lineTab(final String line) {
152         return "\t" + line;
153     }
154 
155     private final void saveLine(final String line) {
156         try {
157             out.write(line + '\n');
158             if ("}".equals(line)) {
159                 out.close();
160             }
161         } catch (IOException e) {
162             e.printStackTrace();
163         }
164     }
165     
166     private void readObject(ObjectInputStream stream) throws IOException,
167             ClassNotFoundException {
168         // if the solver is serialized, out is linked to stdout
169         stream.defaultReadObject();
170         out = new PrintWriter(System.out);
171     }
172 }