001 /*
002 * Copyright (C) 2008-2009 Holger Arndt, A. Naegele and M. Bundschus
003 *
004 * This file is part of the Java Data Mining Package (JDMP).
005 * See the NOTICE file distributed with this work for additional
006 * information regarding copyright ownership and licensing.
007 *
008 * JDMP is free software; you can redistribute it and/or modify
009 * it under the terms of the GNU Lesser General Public License as
010 * published by the Free Software Foundation; either version 2
011 * of the License, or (at your option) any later version.
012 *
013 * JDMP is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016 * GNU Lesser General Public License for more details.
017 *
018 * You should have received a copy of the GNU Lesser General Public
019 * License along with JDMP; if not, write to the
020 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
021 * Boston, MA 02110-1301 USA
022 */
023
024 package org.jdmp.lucene;
025
026 import java.io.File;
027 import java.io.IOException;
028 import java.util.Collection;
029 import java.util.Iterator;
030 import java.util.List;
031
032 import javax.swing.AbstractListModel;
033
034 import org.jdmp.core.AbstractCoreObject;
035 import org.jdmp.core.dataset.DataSet;
036 import org.jdmp.core.dataset.DefaultDataSet;
037 import org.jdmp.core.sample.Sample;
038 import org.jdmp.core.util.ObservableList;
039 import org.ujmp.core.listmatrix.ListMatrix;
040
041 public class LuceneDataSet extends DefaultDataSet {
042 private static final long serialVersionUID = -1783524677465942349L;
043
044 private LuceneIndex index = null;
045
046 public LuceneDataSet(DataSet ds) throws Exception {
047 this(null, ds);
048 }
049
050 public LuceneDataSet() throws Exception {
051 this(null, null);
052 }
053
054 public LuceneDataSet(File path) throws Exception {
055 this(path, null);
056 }
057
058 public DataSet search(String query) {
059 return search(query, 1000);
060 }
061
062 public DataSet search(String query, int count) {
063 try {
064 DataSet ds = index.search(query, count);
065 ds.setLabel("Results for " + query);
066 return ds;
067 } catch (Exception e) {
068 e.printStackTrace();
069 return new DefaultDataSet();
070 }
071 }
072
073 public DataSet search(List<String> query) {
074 return search(query, 1000);
075 }
076
077 public DataSet search(List<String> query, int count) {
078 try {
079 DataSet ds = index.search(query, count);
080 ds.setLabel("Results for " + query);
081 return ds;
082 } catch (Exception e) {
083 e.printStackTrace();
084 return new DefaultDataSet();
085 }
086 }
087
088 public LuceneDataSet(File path, DataSet ds) throws Exception {
089 index = new LuceneIndex(path);
090 setSamples(new LuceneSampleList());
091 if (ds != null) {
092 setVariables(ds.getVariables());
093 new AddSamplesThread(ds).start();
094 }
095 }
096
097 class AddSamplesThread extends Thread {
098 private DataSet ds = null;
099
100 public AddSamplesThread(DataSet ds) {
101 this.ds = ds;
102 setPriority(Thread.MIN_PRIORITY);
103 }
104
105 @Override
106 public void run() {
107 for (int i = 0; i < ds.getSamples().getSize(); i++) {
108 getSamples().add(ds.getSamples().getElementAt(i));
109 }
110 }
111 }
112
113 class LuceneSampleList extends AbstractListModel implements
114 ObservableList<Sample> {
115 private static final long serialVersionUID = -7189321183317113764L;
116
117 @Override
118 public void add(Sample sample) {
119 try {
120 index.add(sample);
121 fireContentsChanged();
122 } catch (Exception e) {
123 e.printStackTrace();
124 }
125 }
126
127 @Override
128 public void addAll(Collection<Sample> values) {
129 throw new RuntimeException("not implemented");
130 }
131
132 @Override
133 public boolean remove(Sample value) {
134 throw new RuntimeException("not implemented");
135 }
136
137 @Override
138 public void clear() {
139 throw new RuntimeException("not implemented");
140 }
141
142 @Override
143 public Sample getElementAt(int i) {
144 try {
145 return index.get(i);
146 } catch (Exception e) {
147 e.printStackTrace();
148 return null;
149 }
150 }
151
152 @Override
153 public void fireContentsChanged() {
154 fireContentsChanged(this, -1, -1);
155 }
156
157 @Override
158 public int indexOf(Sample value) {
159 throw new RuntimeException("not implemented");
160 }
161
162 @Override
163 public boolean isEmpty() {
164 return getSize() == 0;
165 }
166
167 @Override
168 public Collection<Sample> toCollection() {
169 throw new RuntimeException("not implemented");
170 }
171
172 @Override
173 public int getSize() {
174 try {
175 return index.getSize();
176 } catch (Exception e) {
177 e.printStackTrace();
178 return 0;
179 }
180 }
181
182 @Override
183 public Iterator<Sample> iterator() {
184 throw new RuntimeException("not implemented");
185 }
186
187 }
188
189 public synchronized void flush() {
190 try {
191 index.flush();
192 } catch (IOException e) {
193 e.printStackTrace();
194 }
195 }
196
197 public synchronized void optimize() {
198 try {
199 index.optimize();
200 } catch (IOException e) {
201 e.printStackTrace();
202 }
203
204 }
205
206 }