View Javadoc

1   /*
2    * Copyright (c) 2007, Peter Mika All rights reserved.
3    * 
4    * Redistribution and use in source and binary forms, with or without
5    * modification, are permitted provided that the following conditions are met:
6    * 
7    * - Redistributions of source code must retain the above copyright notice, this
8    *   list of conditions and the following disclaimer.
9    * - Redistributions in binary form must reproduce the above copyright notice,
10   *   this list of conditions and the following disclaimer in the documentation
11   *   and/or other materials provided with the distribution. 
12   * - Neither the name of the openrdf.org nor the names of its contributors may
13   *   be used to endorse or promote products derived from this software without
14   *   specific prior written permission.
15   * 
16   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26   * POSSIBILITY OF SUCH DAMAGE.
27   * 
28   */
29  package org.openrdf.elmo.smusher;
30  
31  import java.util.ArrayList;
32  import java.util.Collection;
33  import java.util.HashSet;
34  import java.util.Iterator;
35  import java.util.List;
36  
37  import org.openrdf.OpenRDFException;
38  import org.openrdf.elmo.sesame.SesameManager;
39  import org.openrdf.elmo.smusher.listener.CounterListener;
40  import org.openrdf.elmo.smusher.listener.SmusherListener;
41  import org.openrdf.model.Resource;
42  import org.slf4j.Logger;
43  import org.slf4j.LoggerFactory;
44  
45  /** This smusher iterates a set of smushers until no more new matches can be found.
46   * 
47   * @author Peter Mika (pmika@cs.vu.nl)
48   *
49   */
50  public class IterativeSmusher extends AbstractSmusher {
51      
52      protected List<Smusher> _smushers = new ArrayList<Smusher>();
53      protected int _lastCount = -1;
54      protected final static Logger _logger = LoggerFactory.getLogger(IterativeSmusher.class);
55     
56      public IterativeSmusher() {
57          
58      }
59         
60      public void addSmusher(Smusher smusher) {
61          if (smusher != null) {
62              _smushers.add(smusher);
63          }
64      }
65      
66      public void removeSmusher(Smusher smusher) {
67          if (smusher != null) {
68              _smushers.remove(smusher);
69          }
70      }
71      
72      private class MetaListener implements SmusherListener {
73          
74          Collection<SmusherListener> _listeners = new HashSet<SmusherListener>();
75          
76          public MetaListener(Collection<SmusherListener> listeners) {
77              _listeners = listeners;
78          }
79          public void start() {          
80          }
81          
82          public void matchFound(Resource first, Resource second) {
83              Iterator subit = _listeners.iterator();
84              while (subit.hasNext()) {
85                  SmusherListener nextListener = (SmusherListener) subit.next();
86                  nextListener.matchFound(first, second);
87              }
88          }
89         
90          public void commit() {    
91          }
92          
93          public void finish() {                
94          }
95          
96      }
97      
98      public void smush(SesameManager firstRepository, SesameManager secondRepository) throws OpenRDFException {
99          
100       
101         
102         //Register our own listener with all of the smushers
103         MetaListener metaListener = new MetaListener(_listeners);
104         CounterListener counterListener = new CounterListener();
105    
106         for (int i = 0; i < _smushers.size(); i++) {
107             Smusher nextSmusher = (Smusher) _smushers.get(i);
108             nextSmusher.addListener(metaListener);
109             nextSmusher.addListener(counterListener);
110         }
111         
112         //Start all the registered listeners
113         Iterator it = _listeners.iterator();
114         while (it.hasNext()) {
115             SmusherListener nextListener = (SmusherListener) it.next();
116             nextListener.start();
117         }
118         
119         _lastCount = -1;
120         int newCount = 0;
121         //Run all the smushers until they collectively produce more hits
122         //than in the previous iteration
123         while (newCount > _lastCount) {
124             
125             _lastCount = newCount;
126             counterListener.reset();
127             //Run all the smushers
128             for (int i = 0; i < _smushers.size(); i++) {
129                 Smusher nextSmusher = (Smusher) _smushers.get(i);
130                 nextSmusher.smush(firstRepository, secondRepository); 
131             }
132            
133             //Commit all the registered listeners
134             it = _listeners.iterator();
135             while (it.hasNext()) {
136                 SmusherListener nextListener = (SmusherListener) it.next();
137                 nextListener.commit();
138             }
139             
140             newCount = counterListener.getCount();
141             _logger.debug("Last iteration found " + newCount + " matches");
142         }
143         _lastCount = newCount;
144         
145         //Finish all the registered listeners
146         it = _listeners.iterator();
147         while (it.hasNext()) {
148             SmusherListener nextListener = (SmusherListener) it.next();
149             nextListener.finish();
150         }
151        
152     }
153 
154     /**
155      * @return Returns the _lastCount.
156      */
157     public int getLastCount() {
158         return _lastCount;
159     }
160 
161     
162 }