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.Iterator;
32 import java.util.List;
33
34 import org.openrdf.concepts.rdfs.Resource;
35 import org.openrdf.elmo.sesame.SesameManager;
36 import org.openrdf.elmo.sesame.roles.SesameEntity;
37 import org.openrdf.elmo.smusher.listener.SmusherListener;
38
39 /** This smusher finds matching instances in a repository by compairing
40 * all instances of a given type using a given comparator. Note
41 * that simple comparators can be combined into more complex ones using
42 * ConjunctiveComparator and DisjunctiveComparator.
43 *
44 * @author Peter Mika (pmika@cs.vu.nl)
45 * @see org.openrdf.elmo.model.ResourceComparator
46 *
47 */
48 //public class ComparatorSmusher<T extends Class<? extends Resource>> extends AbstractSmusher {
49 public class ComparatorSmusher<T extends Resource> extends AbstractSmusher {
50
51 private ResourceComparator<T> _comparator = null;
52 private Class _type = null;
53
54 public ComparatorSmusher(ResourceComparator<T> comparator, Class type) {
55 if (comparator != null) {
56 _comparator = comparator;
57 }
58 else
59 throw new IllegalArgumentException("Comparator is null");
60 if (type != null && Resource.class.isAssignableFrom(type)) {
61 _type = type;
62 } else {
63 throw new IllegalArgumentException("Type is null or not a subclass of Resource");
64 }
65
66 }
67
68
69
70 public void smush(SesameManager firstRepository, SesameManager secondRepository)
71
72 {
73 //TODO: how do we make the connection that is used is closed?
74 //does it close automatically when we finished iterating over the list?
75
76 List<T> firstList = Util.getAllInstances(firstRepository, _type);
77
78 List<T> secondList = firstList;
79 if (!secondRepository.equals(firstRepository)) {
80 secondList = Util.getAllInstances(secondRepository, _type);
81 }
82
83
84 Iterator it = _listeners.iterator();
85 while (it.hasNext()) {
86 SmusherListener nextListener = (SmusherListener) it.next();
87 nextListener.start();
88 }
89
90 for (int i = 0; i < firstList.size(); i++) {
91
92 T first = firstList.get(i);
93 for (int j = i + 1; j < secondList.size(); j++) {
94 T second = secondList.get(j);
95
96 if (_comparator.compare(first, second))
97 {
98 Iterator subit = _listeners.iterator();
99 while (subit.hasNext()) {
100 SmusherListener nextListener = (SmusherListener) subit.next();
101 nextListener.matchFound(
102 ((SesameEntity) first).getSesameResource(),
103 ((SesameEntity) second).getSesameResource());
104 }
105
106 }
107 }
108 }
109
110 it = _listeners.iterator();
111 while (it.hasNext()) {
112 SmusherListener nextListener = (SmusherListener) it.next();
113 nextListener.finish();
114 }
115
116 }
117
118 }