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.openrdf.repository.Repository;
43  import org.slf4j.Logger;
44  import org.slf4j.LoggerFactory;
45  
46  /** A version of the IterativeSmusher for smushers that are independent, i.e.
47   * the matches found using one smusher do not influence the matches found with 
48   * another smusher. In such case, every smusher is executed only once and only
49   * the TransitivitySmusher is iterated.
50   * 
51   * @see TransitivitySmusher
52   * @author Peter Mika (pmika@cs.vu.nl)
53   *
54   */
55  public class FastIterativeSmusher extends AbstractSmusher {
56      
57      protected List<Smusher> _smushers = new ArrayList<Smusher>();
58       protected int _lastCount = -1;
59      protected final static Logger _logger = LoggerFactory.getLogger(FastIterativeSmusher.class);
60     
61      public FastIterativeSmusher() {
62          
63      }
64         
65      public void addSmusher(Smusher smusher) {
66          if (smusher != null) {
67              _smushers.add(smusher);
68          }
69      }
70      
71      public void removeSmusher(Smusher smusher) {
72          if (smusher != null) {
73              _smushers.remove(smusher);
74          }
75      }
76      
77      private class MetaListener implements SmusherListener {
78          
79          Collection _listeners = new HashSet();
80          
81          public MetaListener(Collection listeners) {
82              _listeners = listeners;
83          }
84          public void start() {          
85          }
86          public void matchFound(Resource first, Resource second) {
87              Iterator subit = _listeners.iterator();
88              while (subit.hasNext()) {
89                  SmusherListener nextListener = (SmusherListener) subit.next();
90                  nextListener.matchFound(first, second);
91              }
92          }
93          
94          public void commit() {
95          }
96          
97          public void finish() {                
98          }
99          
100     }
101     
102     public void smush(SesameManager firstRepository, SesameManager secondRepository) throws OpenRDFException {
103         
104       
105         
106         //Register our own listener with all of the smushers
107         MetaListener metaListener = new MetaListener(_listeners);
108         CounterListener counterListener = new CounterListener();
109         
110         for (int i = 0; i < _smushers.size(); i++) {
111             Smusher nextSmusher = (Smusher) _smushers.get(i);
112             nextSmusher.addListener(metaListener);
113             nextSmusher.addListener(counterListener);
114         }
115         
116         //Start all the registered listeners
117         Iterator it = _listeners.iterator();
118         while (it.hasNext()) {
119             SmusherListener nextListener = (SmusherListener) it.next();
120             nextListener.start();
121         }
122         
123         //Run all the smushers once
124         for (int i = 0; i < _smushers.size(); i++) {
125             Smusher nextSmusher = (Smusher) _smushers.get(i);
126             nextSmusher.smush(firstRepository, secondRepository);   
127         }
128         
129         //Commit all the registered listeners
130         it = _listeners.iterator();
131         while (it.hasNext()) {
132             SmusherListener nextListener = (SmusherListener) it.next();
133             nextListener.commit();
134         }
135        
136         _logger.debug("First iteration found " + counterListener.getCount() + " matches");
137         
138         
139         //Iterate the SameAsTransitivitySmusher
140         Smusher transSmusher = new TransitivitySmusher();
141         transSmusher.addListener(metaListener);
142         transSmusher.addListener(counterListener);
143         
144         _lastCount = -1;
145         int newCount = 0;
146         //Run all the smushers until they collectively produce more hits
147         //than in the previous iteration
148         while (newCount > _lastCount) {
149             
150             _lastCount = newCount;
151             counterListener.reset();
152             //Apply the transitivity rule of owl:sameAs
153             transSmusher.smush(firstRepository, secondRepository);
154            
155             newCount = counterListener.getCount();
156             _logger.debug("Last iteration found " + newCount + " matches");
157             
158             //Commit all the registered listeners
159             it = _listeners.iterator();
160             while (it.hasNext()) {
161                 SmusherListener nextListener = (SmusherListener) it.next();
162                 nextListener.commit();
163             }
164         }
165         _lastCount = newCount;
166         
167         //Finish all the registered listeners
168         it = _listeners.iterator();
169         while (it.hasNext()) {
170             SmusherListener nextListener = (SmusherListener) it.next();
171             nextListener.finish();
172         }
173        
174     }
175 
176     /**
177      * @return Returns the _lastCount.
178      */
179     public int getLastCount() {
180         return _lastCount;
181     }
182 
183     
184 }
185