svn-gvsig-desktop / branches / v2_0_0_prep / libraries / org.gvsig.geocoding / src-test / org / gvsig / geocoding / dataaccess / StreetsDBFTest.java @ 32479
History | View | Annotate | Download (9.04 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
*/
|
22 |
|
23 |
/*
|
24 |
* AUTHORS (In addition to CIT):
|
25 |
* 2008 Prodevelop S.L vsanjaime programador
|
26 |
*/
|
27 |
|
28 |
package org.gvsig.geocoding.dataaccess; |
29 |
|
30 |
import java.io.File; |
31 |
import java.io.IOException; |
32 |
import java.util.ArrayList; |
33 |
import java.util.List; |
34 |
|
35 |
import junit.framework.TestCase; |
36 |
|
37 |
import org.apache.lucene.analysis.Analyzer; |
38 |
import org.apache.lucene.analysis.standard.StandardAnalyzer; |
39 |
import org.apache.lucene.document.Document; |
40 |
import org.apache.lucene.document.Field; |
41 |
import org.apache.lucene.index.CorruptIndexException; |
42 |
import org.apache.lucene.index.IndexWriter; |
43 |
import org.apache.lucene.index.Term; |
44 |
import org.apache.lucene.search.BooleanClause; |
45 |
import org.apache.lucene.search.BooleanQuery; |
46 |
import org.apache.lucene.search.FuzzyQuery; |
47 |
import org.apache.lucene.search.IndexSearcher; |
48 |
import org.apache.lucene.search.Query; |
49 |
import org.slf4j.Logger; |
50 |
import org.slf4j.LoggerFactory; |
51 |
|
52 |
/**
|
53 |
* Test
|
54 |
*
|
55 |
* @author <a href="mailto:jsanz@prodevelop.es"> Jorge Gaspar Sanz Salinas</a>
|
56 |
* @author <a href="mailto:vsanjaime@prodevelop.es"> Vicente Sanjaime Calvet</a>
|
57 |
*/
|
58 |
public class StreetsDBFTest extends TestCase { |
59 |
// private static final Logger log =
|
60 |
// LoggerFactory.getLogger(StreetsDBFTest.class);
|
61 |
//
|
62 |
// private static final float SENSIBILITY = 0.8f;
|
63 |
//
|
64 |
// private static final String SEARCH_TERM = "DON JUAN";
|
65 |
// private static final String TEST_FILE = "test/Horta.dbf";
|
66 |
//
|
67 |
// String INDEX_DIRPATH = new File("test/dbf").getAbsolutePath();
|
68 |
//
|
69 |
// DBFDriver driverdbf = null;
|
70 |
// int nFields;
|
71 |
// int nRows;
|
72 |
//
|
73 |
// int indID = 1;
|
74 |
// int indNAME = 8;
|
75 |
//
|
76 |
// String strID, strNAME, strTIMEZONE;
|
77 |
//
|
78 |
// private FieldDescription[] descs;
|
79 |
//
|
80 |
// public void setUp() {
|
81 |
// File f = new File(TEST_FILE);
|
82 |
//
|
83 |
// driverdbf = new DBFDriver();
|
84 |
//
|
85 |
// try {
|
86 |
// driverdbf.open(f);
|
87 |
// nFields = driverdbf.getFieldCount();
|
88 |
// nRows = (int) driverdbf.getRowCount();
|
89 |
//
|
90 |
// strID = driverdbf.getFieldName(indID);
|
91 |
// strNAME = driverdbf.getFieldName(indNAME);
|
92 |
//
|
93 |
// } catch (Exception e) {
|
94 |
// e.printStackTrace();
|
95 |
// }
|
96 |
//
|
97 |
// log.info("Fichero DBF abierto (" + nRows + " filas)");
|
98 |
// }
|
99 |
//
|
100 |
// // public void testFields() throws ReadDriverException{
|
101 |
// //
|
102 |
// // for (int i=0;i<nFields;i++){
|
103 |
// // log.info(i + " " + driverdbf.getFieldName(i) + " :" +
|
104 |
// // driverdbf.getFieldValue(0, i));
|
105 |
// // }
|
106 |
// // }
|
107 |
//
|
108 |
// /* Este test crea los indices */
|
109 |
// public void testGenerateIndex() {
|
110 |
// log.info("Creando ?ndice...");
|
111 |
//
|
112 |
// long t1 = System.currentTimeMillis();
|
113 |
//
|
114 |
// IndexWriter indexWriter = null;
|
115 |
// try {
|
116 |
// indexWriter = new IndexWriter(INDEX_DIRPATH,
|
117 |
// new StandardAnalyzer(), true);
|
118 |
// this.createIndex(indexWriter);
|
119 |
// indexWriter.optimize();
|
120 |
// } catch (Exception e) {
|
121 |
//
|
122 |
// }
|
123 |
// log.info("?ndice creado en " + (System.currentTimeMillis() - t1)
|
124 |
// + " msecs");
|
125 |
//
|
126 |
// }
|
127 |
//
|
128 |
// private void createIndex(IndexWriter indexWriter)
|
129 |
// throws CorruptIndexException, ReadDriverException, IOException {
|
130 |
// for (int i = 0; i < nRows; i++) {
|
131 |
//
|
132 |
// if (i % (nRows / 4 + 1) == 0)
|
133 |
// log.info(i * 1.0 / nRows * 100 + "%");
|
134 |
//
|
135 |
// indexWriter.addDocument(createDoc(i));
|
136 |
// }
|
137 |
// }
|
138 |
//
|
139 |
// /* Busqueda de cadenas */
|
140 |
// public void testLuceneRAMDirectorySearch() throws CorruptIndexException,
|
141 |
// LockObtainFailedException, IOException, ReadDriverException {
|
142 |
// Analyzer an = null;
|
143 |
//
|
144 |
// ArrayList<String> cadenas = new ArrayList<String>();
|
145 |
//
|
146 |
// cadenas.add(SEARCH_TERM);
|
147 |
// log
|
148 |
// .info("--------------TEST DE B?SQUEDA LUCENE CON RAMDirectory--------------");
|
149 |
// IndexWriter indexWriter = null;
|
150 |
// Directory dir = null;
|
151 |
// log.info("----Crear ?ndice----");
|
152 |
// long id1 = System.currentTimeMillis();
|
153 |
// dir = new RAMDirectory();
|
154 |
// indexWriter = new IndexWriter(dir, new StandardAnalyzer(), true);
|
155 |
// createIndex(indexWriter);
|
156 |
// indexWriter.optimize();
|
157 |
// log.info("?ndice creado en " + (System.currentTimeMillis() - id1)
|
158 |
// + " msecs");
|
159 |
//
|
160 |
// log.info("----Buscando...----");
|
161 |
// id1 = System.currentTimeMillis();
|
162 |
// an = new StandardAnalyzer();
|
163 |
// buscar(new IndexSearcher(dir), an, cadenas);
|
164 |
// long id2 = System.currentTimeMillis();
|
165 |
//
|
166 |
// log.info("B?squeda realizada en " + (id2 - id1) + "msecs");
|
167 |
// log
|
168 |
// .info("--------------TEST DE B?SQUEDA LUCENE CON RAMDirectory--------------");
|
169 |
// }
|
170 |
//
|
171 |
// /* Busqueda de cadenas */
|
172 |
// public void testLuceneSearch() throws CorruptIndexException, IOException
|
173 |
// {
|
174 |
// Analyzer an = null;
|
175 |
//
|
176 |
// ArrayList<String> cadenas = new ArrayList<String>();
|
177 |
//
|
178 |
// cadenas.add(SEARCH_TERM);
|
179 |
// log.info("--------------TEST DE B?SQUEDA LUCENE--------------");
|
180 |
// long id1 = System.currentTimeMillis();
|
181 |
// an = new StandardAnalyzer();
|
182 |
// buscar(new IndexSearcher(INDEX_DIRPATH), an, cadenas);
|
183 |
// long id2 = System.currentTimeMillis();
|
184 |
//
|
185 |
// log.info("B?squeda realizada en " + (id2 - id1) + "msecs");
|
186 |
//
|
187 |
// log.info("--------------TEST DE B?SQUEDA LUCENE--------------");
|
188 |
// }
|
189 |
//
|
190 |
// public void testDBFSearch() throws ReadDriverException {
|
191 |
// log.info("--------------TEST DE B?SQUEDA ITERANDO--------------");
|
192 |
// long id1 = System.currentTimeMillis();
|
193 |
// int results = 0;
|
194 |
// String fieldValue;
|
195 |
// for (int i = 0; i < nRows; i++) {
|
196 |
// fieldValue = driverdbf.getFieldValue(i, indNAME).toString();
|
197 |
// if (fieldValue.toLowerCase().indexOf(SEARCH_TERM.toLowerCase()) > -1) {
|
198 |
// log.info(driverdbf.getFieldValue(i, indID) + "\t" + fieldValue);
|
199 |
// results++;
|
200 |
// }
|
201 |
// }
|
202 |
//
|
203 |
// long id2 = System.currentTimeMillis();
|
204 |
//
|
205 |
// log.info(results + " resultados");
|
206 |
// log.info("B?squeda realizada en " + (id2 - id1) + "msecs");
|
207 |
//
|
208 |
// log.info("--------------TEST DE B?SQUEDA ITERANDO--------------");
|
209 |
// }
|
210 |
//
|
211 |
// private void testDataAccessFilters() {
|
212 |
// log.info("--------------TEST DE B?SQUEDA FILTROS--------------");
|
213 |
// // TODO
|
214 |
// log.info("--------------TEST DE B?SQUEDA FILTROS--------------");
|
215 |
// }
|
216 |
//
|
217 |
// private void buscar(IndexSearcher indexSearcher, Analyzer an,
|
218 |
// List<String> cadenas) {
|
219 |
// int results = 0;
|
220 |
// try {
|
221 |
// for (String cadena : cadenas) {
|
222 |
// log.info("Buscando ``" + cadena + "`` con el analizador "
|
223 |
// + an.getClass().getName());
|
224 |
// // Se crear la consulta indicando el nombre del campo donde
|
225 |
// // estan
|
226 |
// // las cadenas del indices
|
227 |
// // junto con el analizador
|
228 |
// Query consulta = null;
|
229 |
//
|
230 |
// // consulta = new QueryParser(strNAME, an).parse(cadena);
|
231 |
// // consulta = new FuzzyQuery(new Term(strNAME, "don"),
|
232 |
// // SENSIBILITY);
|
233 |
//
|
234 |
// consulta = new BooleanQuery();
|
235 |
// BooleanQuery boolConsulta = (BooleanQuery) consulta;
|
236 |
// boolConsulta.add(
|
237 |
// new FuzzyQuery(new Term(strNAME, "don"), 0.5f),
|
238 |
// BooleanClause.Occur.MUST);
|
239 |
// boolConsulta.add(
|
240 |
// new FuzzyQuery(new Term(strNAME, "joan"), 0.5f),
|
241 |
// BooleanClause.Occur.MUST);
|
242 |
//
|
243 |
// // se realiza la busqueda de una cadena y se obtinen los
|
244 |
// // resultados
|
245 |
// Hits hits = indexSearcher.search(consulta);
|
246 |
//
|
247 |
// results = hits.length();
|
248 |
// for (int i = 0; i < hits.length(); i++) {
|
249 |
// Document docu = hits.doc(i);
|
250 |
//
|
251 |
// log.info(docu.get(strID) + "\t " + docu.get(strNAME) + "("
|
252 |
// + hits.score(i) + ")");
|
253 |
// }
|
254 |
// }
|
255 |
//
|
256 |
// log.info(results + " resultados");
|
257 |
// } catch (Exception e) {
|
258 |
// e.printStackTrace();
|
259 |
// } finally {
|
260 |
// try {
|
261 |
// indexSearcher.close();
|
262 |
// } catch (IOException e) {
|
263 |
// log.error("Error cerrando ?ndice", e);
|
264 |
// }
|
265 |
//
|
266 |
// }
|
267 |
// }
|
268 |
//
|
269 |
// private Document createDoc(int i) throws ReadDriverException {
|
270 |
// Document doc = new Document();
|
271 |
//
|
272 |
// // Add ID
|
273 |
// Field fi;
|
274 |
// String fieldValue = driverdbf.getFieldValue(i, indID).toString();
|
275 |
// fi = new Field(strID, fieldValue, Field.Store.YES,
|
276 |
// Field.Index.UN_TOKENIZED);
|
277 |
// doc.add(fi);
|
278 |
//
|
279 |
// // Add NANME
|
280 |
// fieldValue = driverdbf.getFieldValue(i, indNAME).toString();
|
281 |
// fi = new Field(strNAME, fieldValue, Field.Store.YES,
|
282 |
// Field.Index.TOKENIZED);
|
283 |
// doc.add(fi);
|
284 |
//
|
285 |
// return doc;
|
286 |
// }
|
287 |
//
|
288 |
// public void tearDown() {
|
289 |
// try {
|
290 |
// driverdbf.close();
|
291 |
// } catch (CloseDriverException e) {
|
292 |
// // TODO Auto-generated catch block
|
293 |
// e.printStackTrace();
|
294 |
// }
|
295 |
// }
|
296 |
|
297 |
} |