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 learnUnit(int p) {
102         for (SearchListener<T> sl : this.listeners) {
103             sl.learnUnit(p);
104         }
105     }
106 
107     public void delete(int[] clause) {
108         for (SearchListener<T> sl : this.listeners) {
109             sl.delete(clause);
110         }
111 
112     }
113 
114     public void conflictFound(IConstr confl, int dlevel, int trailLevel) {
115         for (SearchListener<T> sl : this.listeners) {
116             sl.conflictFound(confl, dlevel, trailLevel);
117         }
118 
119     }
120 
121     public void conflictFound(int p) {
122         for (SearchListener<T> sl : this.listeners) {
123             sl.conflictFound(p);
124         }
125 
126     }
127 
128     public void solutionFound(int[] model, RandomAccessModel lazyModel) {
129         for (SearchListener<T> sl : this.listeners) {
130             sl.solutionFound(model, lazyModel);
131         }
132 
133     }
134 
135     public void beginLoop() {
136         for (SearchListener<T> sl : this.listeners) {
137             sl.beginLoop();
138         }
139     }
140 
141     public void start() {
142         for (SearchListener<T> sl : this.listeners) {
143             sl.start();
144         }
145 
146     }
147 
148     public void end(Lbool result) {
149         for (SearchListener<T> sl : this.listeners) {
150             sl.end(result);
151         }
152     }
153 
154     public void restarting() {
155         for (SearchListener<T> sl : this.listeners) {
156             sl.restarting();
157         }
158 
159     }
160 
161     public void backjump(int backjumpLevel) {
162         for (SearchListener<T> sl : this.listeners) {
163             sl.backjump(backjumpLevel);
164         }
165 
166     }
167 
168     public void init(T solverService) {
169         for (SearchListener<T> sl : this.listeners) {
170             sl.init(solverService);
171         }
172     }
173 
174     public void cleaning() {
175         for (SearchListener<T> sl : this.listeners) {
176             sl.cleaning();
177         }
178     }
179 
180 }