View Javadoc

1   /*******************************************************************************
2    * SAT4J: a SATisfiability library for Java Copyright (C) 2004, 2012 Artois University and CNRS
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   * Contributors:
28   *   CRIL - initial API and implementation
29   *******************************************************************************/
30  package org.sat4j.tools;
31  
32  import java.util.ArrayList;
33  import java.util.Arrays;
34  import java.util.Collection;
35  import java.util.List;
36  
37  import org.sat4j.specs.IConstr;
38  import org.sat4j.specs.ISolverService;
39  import org.sat4j.specs.Lbool;
40  import org.sat4j.specs.RandomAccessModel;
41  import org.sat4j.specs.SearchListener;
42  
43  /**
44   * Allow to feed the solver with several SearchListener.
45   * 
46   * @author leberre
47   * 
48   */
49  public class MultiTracing<T extends ISolverService> implements
50          SearchListener<T> {
51  
52      /**
53  	 * 
54  	 */
55      private static final long serialVersionUID = 1L;
56  
57      private final Collection<SearchListener<T>> listeners = new ArrayList<SearchListener<T>>();
58  
59      public MultiTracing(SearchListener<T>... listeners) {
60          this.listeners.addAll(Arrays.asList(listeners));
61      }
62  
63      public MultiTracing(List<SearchListener<T>> listenersList) {
64          this.listeners.addAll(listenersList);
65      }
66  
67      public void assuming(int p) {
68          for (SearchListener<T> sl : this.listeners) {
69              sl.assuming(p);
70          }
71  
72      }
73  
74      public void propagating(int p, IConstr reason) {
75          for (SearchListener<T> sl : this.listeners) {
76              sl.propagating(p, reason);
77          }
78  
79      }
80  
81      public void backtracking(int p) {
82          for (SearchListener<T> sl : this.listeners) {
83              sl.backtracking(p);
84          }
85      }
86  
87      public void adding(int p) {
88          for (SearchListener<T> sl : this.listeners) {
89              sl.adding(p);
90          }
91  
92      }
93  
94      public void learn(IConstr c) {
95          for (SearchListener<T> sl : this.listeners) {
96              sl.learn(c);
97          }
98  
99      }
100 
101     public void delete(int[] clause) {
102         for (SearchListener<T> sl : this.listeners) {
103             sl.delete(clause);
104         }
105 
106     }
107 
108     public void conflictFound(IConstr confl, int dlevel, int trailLevel) {
109         for (SearchListener<T> sl : this.listeners) {
110             sl.conflictFound(confl, dlevel, trailLevel);
111         }
112 
113     }
114 
115     public void conflictFound(int p) {
116         for (SearchListener<T> sl : this.listeners) {
117             sl.conflictFound(p);
118         }
119 
120     }
121 
122     public void solutionFound(int[] model, RandomAccessModel lazyModel) {
123         for (SearchListener<T> sl : this.listeners) {
124             sl.solutionFound(model, lazyModel);
125         }
126 
127     }
128 
129     public void beginLoop() {
130         for (SearchListener<T> sl : this.listeners) {
131             sl.beginLoop();
132         }
133     }
134 
135     public void start() {
136         for (SearchListener<T> sl : this.listeners) {
137             sl.start();
138         }
139 
140     }
141 
142     public void end(Lbool result) {
143         for (SearchListener<T> sl : this.listeners) {
144             sl.end(result);
145         }
146     }
147 
148     public void restarting() {
149         for (SearchListener<T> sl : this.listeners) {
150             sl.restarting();
151         }
152 
153     }
154 
155     public void backjump(int backjumpLevel) {
156         for (SearchListener<T> sl : this.listeners) {
157             sl.backjump(backjumpLevel);
158         }
159 
160     }
161 
162     public void init(T solverService) {
163         for (SearchListener<T> sl : this.listeners) {
164             sl.init(solverService);
165         }
166     }
167 
168     public void cleaning() {
169         for (SearchListener<T> sl : this.listeners) {
170             sl.cleaning();
171         }
172     }
173 
174 }