Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libGeocoding / src-test / org / gvsig / data / vectorial / filter / testLevenshteinFilter.java @ 22684

History | View | Annotate | Download (7.63 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  main development
26
 */
27

    
28
package org.gvsig.data.vectorial.filter;
29

    
30
import java.io.File;
31
import java.util.Iterator;
32
import java.util.List;
33

    
34
import junit.framework.TestCase;
35

    
36
import org.apache.log4j.Logger;
37
import org.apache.log4j.PropertyConfigurator;
38
import org.gvsig.data.CloseException;
39
import org.gvsig.data.DataCollection;
40
import org.gvsig.data.DataManager;
41
import org.gvsig.data.DataStoreParameters;
42
import org.gvsig.data.InitializeException;
43
import org.gvsig.data.OpenException;
44
import org.gvsig.data.ReadException;
45
import org.gvsig.data.datastores.vectorial.file.dbf.DBFFeature;
46
import org.gvsig.data.datastores.vectorial.file.dbf.DBFStore;
47
import org.gvsig.data.datastores.vectorial.file.dbf.DBFStoreParameters;
48
import org.gvsig.data.datastores.vectorial.file.dbf.Register;
49
import org.gvsig.data.vectorial.AttributeDescriptor;
50
import org.gvsig.data.vectorial.DefaultFeatureType;
51
import org.gvsig.data.vectorial.Feature;
52
import org.gvsig.data.vectorial.FeatureCollection;
53
import org.gvsig.data.vectorial.FeatureType;
54
import org.opengis.filter.Filter;
55
import org.opengis.filter.FilterFactory2;
56
import org.opengis.filter.expression.Expression;
57
import org.opengis.filter.sort.SortBy;
58

    
59
/**
60
 * @author jsanz
61
 * 
62
 * 
63
 * @author <a href="mailto:jsanz@prodevelop.es"> Jorge Gaspar Sanz Salinas</a>
64
 * @author <a href="mailto:vsanjaime@prodevelop.es"> Vicente Sanjaime Calvet</a>
65
 * 
66
 */
67
public class testLevenshteinFilter extends TestCase {
68

    
69
        private static final float THRESHOLD = 0.78f;
70
        static Logger log = null;
71
        static String TEST_DBF = "test-data/AL.dbf";
72
        static String SEARCH_FIELD = "ASCIINAME";
73
        static String SEARCH_TERM = "Okshtunit";
74

    
75
        DBFStore store;
76

    
77
        public testLevenshteinFilter() {
78
                super();
79
        }
80

    
81
        public testLevenshteinFilter(String name) {
82
                super(name);
83
                // TODO Auto-generated constructor stub
84
        }
85

    
86
        /**
87
         * @throws java.lang.Exception
88
         */
89
        protected void setUp() throws Exception {
90
                if (log == null) {
91
                        // Configurar el log4j
92
                        PropertyConfigurator.configure("log4j.properties");
93
                        log = Logger.getLogger(testLevenshteinFilter.class);
94
                        log.info("Test initialized");
95
                }
96

    
97
                Register.selfRegister();
98
                DataManager manager = DataManager.getManager();
99
                DBFStoreParameters parameters = null;
100
                parameters = (DBFStoreParameters) manager
101
                                .createDataStoreParameters(DBFStore.DATASTORE_NAME);
102
                parameters.setFile(new File(TEST_DBF));
103

    
104
                try {
105
                        store = (DBFStore) manager
106
                                        .createDataStore((DataStoreParameters) parameters);
107
                } catch (InitializeException e) {
108
                        // TODO Auto-generated catch block
109
                        e.printStackTrace();
110
                }
111
        }
112

    
113
        /**
114
         * @throws java.lang.Exception
115
         */
116
        protected void tearDown() throws Exception {
117
        }
118

    
119
        @SuppressWarnings("unchecked")
120
        public void testIterate() throws ReadException {
121
                log.info("-----------testIterate-----------");
122

    
123
                long t1 = System.currentTimeMillis();
124
                DataCollection data = store.getDataCollection();
125
                log.info("Get data in " + (System.currentTimeMillis() - t1) + " msecs");
126

    
127
                Iterator<DBFFeature> it = data.iterator();
128
                DBFFeature dbfFeature;
129
                t1 = System.currentTimeMillis();
130
                while (it.hasNext()) {
131
                        dbfFeature = it.next();
132
                        dbfFeature.get(SEARCH_FIELD);
133
                }
134
                log.info("Iterate data in " + (System.currentTimeMillis() - t1)
135
                                + " msecs");
136

    
137
                log.info("-----------testIterate-----------");
138
        }
139

    
140
        public void testGetFeatureTypes() {
141
                log.info("-----------testGetFeatureTypes-----------");
142

    
143
                List<FeatureType> types = store.getFeatureTypes();
144
                assertEquals(1, types.size());
145
                DefaultFeatureType ftype = (DefaultFeatureType) types.get(0);
146
                log.info("Type with " + ftype.size() + " attributes");
147
                Object att = ftype.get(SEARCH_FIELD);
148

    
149
                // for (int i=0;i<ftype.size();i++){
150
                // log.info(((AttributeDescriptor) ftype.get(i)).getName());
151
                // }
152

    
153
                assertNotNull(att);
154
                assertTrue(att instanceof AttributeDescriptor);
155
                AttributeDescriptor ad = (AttributeDescriptor) att;
156
                assertEquals(AttributeDescriptor.TYPE_STRING, ad.getDataType());
157
                log.info("-----------testGetFeatureTypes-----------");
158
        }
159

    
160
        public void testEqualsFilter() {
161
                log.info("-----------testEqualsFilter-----------");
162
                // long t1 = System.currentTimeMillis();
163
                FilterFactory2 fact = new DistanceFilterFactory2Impl();
164
                Expression exp1 = fact.property(SEARCH_FIELD);
165
                Expression exp2 = fact.literal(SEARCH_TERM);
166
                Filter filter = fact.equals(exp1, exp2);
167

    
168
                int results = testshp(filter, null);
169
                assertEquals(0, results);
170

    
171
                log.info("-----------testEqualsFilter-----------");
172
        }
173

    
174
        public void testLikeFilter() {
175
                log.info("-----------testLikeFilter-----------");
176
                // long t1 = System.currentTimeMillis();
177
                FilterFactory2 fact = new DistanceFilterFactory2Impl();
178
                Expression exp1 = fact.property(SEARCH_FIELD);
179
                Filter filter = fact.like(exp1, "%" + SEARCH_TERM + "%");
180

    
181
                int results = testshp(filter, null);
182
                assertEquals(2, results);
183

    
184
                log.info("-----------testLikeFilter-----------");
185
        }
186

    
187
        public void testTextDistanceFilter() {
188
                log.info("-----------testTextDistanceFilter-----------");
189
                // long t1 = System.currentTimeMillis();
190
                DistanceFilterFactory2Impl fact = new DistanceFilterFactory2Impl();
191
                Expression exp1 = fact.property(SEARCH_FIELD);
192
                Filter filter = fact.distance(exp1, SEARCH_TERM, THRESHOLD);
193
                // Filter filter = fact.equals(exp1, exp2);
194
                int results = testshp(filter, null);
195
                assertEquals(4, results);
196

    
197
                log.info("-----------testTextDistanceFilter-----------");
198
        }
199

    
200
        /**
201
         * Convenience method to test the filter
202
         * 
203
         * @param filter
204
         * @param order
205
         * @return
206
         */
207
        private int testshp(Filter filter, SortBy[] order) {
208
                try {
209
                        store.open();
210
                } catch (OpenException e2) {
211
                        // TODO Auto-generated catch block
212
                        e2.printStackTrace();
213
                }
214

    
215
                FeatureType ft = store.getDefaultFeatureType();
216
                FeatureCollection featureCollection = null;
217

    
218
                try {
219
                        long t1 = System.currentTimeMillis();
220
                        featureCollection = (FeatureCollection) store.getDataCollection(ft,
221
                                        filter, order);
222
                        log.info("Search retrieved in " + (System.currentTimeMillis() - t1)
223
                                        + " msecs");
224
                        log.info(featureCollection.size() + " results");
225
                } catch (ReadException e1) {
226
                        // TODO Auto-generated catch block
227
                        e1.printStackTrace();
228
                }
229

    
230
                for (Object obj : featureCollection) {
231
                        if (obj instanceof Feature) {
232
                                Feature feat = (Feature) obj;
233
                                log.info(SEARCH_FIELD + " : " + feat.get(SEARCH_FIELD) + "("
234
                                                + feat.get("GENAMEID") + ")");
235
                        }
236
                }
237

    
238
                int numFeat = featureCollection.size();
239
                featureCollection.dispose();
240

    
241
                try {
242
                        store.close();
243
                        store.dispose();
244
                } catch (CloseException e) {
245
                        // TODO Auto-generated catch block
246
                        e.printStackTrace();
247
                }
248

    
249
                return numFeat;
250
        }
251
}