001: package org.geotools.caching.quatree;
002:
003: import com.vividsolutions.jts.geom.Coordinate;
004: import com.vividsolutions.jts.geom.Envelope;
005:
006: import junit.framework.Test;
007: import junit.framework.TestCase;
008: import junit.framework.TestSuite;
009:
010: import org.geotools.caching.Generator;
011: import org.geotools.caching.spatialindex.spatialindex.Region;
012:
013: import org.geotools.data.DefaultQuery;
014: import org.geotools.data.Query;
015:
016: import org.geotools.feature.FeatureType;
017:
018: import org.geotools.filter.FilterFactoryImpl;
019: import org.geotools.filter.spatial.BBOXImpl;
020:
021: import org.opengis.filter.Filter;
022:
023: import java.util.ArrayList;
024: import java.util.Iterator;
025: import java.util.List;
026:
027: public class QuadTreeQueryTrackerTest extends TestCase {
028: private final static short ERROR = -1;
029: private final static short UNCHANGED = 0;
030: private final static short OPTIMIZED = 1;
031: private final static short EMPTYQUERY = 2;
032: private final static short EXPANDED = 3;
033: private final List querySet = new ArrayList();
034: private FeatureType type;
035: private QuadTreeQueryTracker tracker;
036:
037: protected void setUp() {
038: Generator gen = new Generator(1000, 1000);
039: type = gen.getFeatureType();
040: tracker = new QuadTreeQueryTracker(new Region(new double[] { 0,
041: 0 }, new double[] { 1000, 1000 }), type);
042: }
043:
044: public static Test suite() {
045: return new TestSuite(QuadTreeQueryTrackerTest.class);
046: }
047:
048: public void testTracker() {
049: createQuerySet(50);
050:
051: int unchanged = 0;
052: int optimized = 0;
053: int empty = 0;
054: int expanded = 0;
055:
056: for (Iterator i = querySet.iterator(); i.hasNext();) {
057: Query q = (Query) i.next();
058: Query m = tracker.match(q);
059: short comp = compareQuery(q, m);
060:
061: //String msg = "" ;
062: if (comp == UNCHANGED) {
063: //msg = "Unchanged" ;
064: unchanged++;
065: tracker.register(m);
066: } else if (comp == OPTIMIZED) {
067: //msg = "Optimized" ;
068: optimized++;
069: tracker.register(m);
070: } else if (comp == EMPTYQUERY) {
071: //msg = "" ;
072: empty++;
073: } else if (comp == EXPANDED) {
074: expanded++;
075: tracker.register(m);
076: }
077: }
078:
079: System.out.println("Unchanged=" + unchanged + ", Optimized="
080: + optimized + ", Empty=" + empty + ", Expanded="
081: + expanded);
082:
083: //assertTrue(unchanged > 0);
084: //assertTrue(optimized > 0);
085: //assertTrue(empty > ((2 / 3) * querySet.size()));
086: }
087:
088: public void testRegister() {
089: createQuerySet(1);
090:
091: Query control = (Query) querySet.get(0);
092: Query m = tracker.match(control);
093: short comp = compareQuery(control, m);
094: assertTrue((comp == UNCHANGED) || (comp == EXPANDED));
095: tracker.register(m);
096: m = tracker.match(control);
097: assertEquals(EMPTYQUERY, compareQuery(control, m));
098: // test for repetibility
099: m = tracker.match(control);
100: assertEquals(EMPTYQUERY, compareQuery(control, m));
101: }
102:
103: public void testUnregister() {
104: /*FilterFactoryImpl ff = new FilterFactoryImpl() ;
105: Filter f = ff.bbox(type.getTypeName(), 10, 20, 15, 25, "") ;
106: Query q = new DefaultQuery(type.getTypeName(), f) ; */
107: createQuerySet(1);
108:
109: Query q = (Query) querySet.get(0);
110: Query m = tracker.match(q);
111: short comp = compareQuery(q, m);
112: assertTrue((comp == UNCHANGED) || (comp == EXPANDED));
113: tracker.register(m);
114: m = tracker.match(q);
115: assertEquals(EMPTYQUERY, compareQuery(q, m));
116: tracker.unregister(q);
117: m = tracker.match(q);
118: comp = compareQuery(q, m);
119: assertTrue((comp == UNCHANGED) || (comp == EXPANDED));
120: // test for repetibility
121: tracker.unregister(q);
122: m = tracker.match(q);
123: comp = compareQuery(q, m);
124: assertTrue((comp == UNCHANGED) || (comp == EXPANDED));
125: }
126:
127: //
128: // Utilities //
129: //
130: private void createQuerySet(int numberOfQueries) {
131: System.out.println("=== Creating Query Set");
132:
133: Coordinate p = Generator.pickRandomPoint(new Coordinate(500,
134: 500), 950, 950);
135: Coordinate last = p;
136:
137: for (int i = 0; i < numberOfQueries; i++) {
138: querySet.add(Generator.createBboxQuery(p, 100, 100));
139: p = Generator.pickRandomPoint(p, 50, 50);
140: querySet.add(Generator.createBboxQuery(p, 50, 50));
141: p = Generator.pickRandomPoint(p, 20, 20);
142: querySet.add(Generator.createBboxQuery(p, 20, 20));
143:
144: Coordinate temp = p;
145: p = last;
146: last = temp;
147: }
148: }
149:
150: private short compareQuery(Query q1, Query q2) {
151: if (q1.equals(q2)) {
152: return UNCHANGED;
153: }
154:
155: if ((q1.getFilter() instanceof BBOXImpl)
156: && (q2.getFilter() instanceof BBOXImpl)) {
157: BBOXImpl bb = (BBOXImpl) q1.getFilter();
158: Envelope env1 = new Envelope(bb.getMinX(), bb.getMaxX(), bb
159: .getMinY(), bb.getMaxY());
160: bb = (BBOXImpl) q2.getFilter();
161:
162: Envelope env2 = new Envelope(bb.getMinX(), bb.getMaxX(), bb
163: .getMinY(), bb.getMaxY());
164:
165: if (env1.equals(env2)) {
166: return UNCHANGED;
167: } else if (env1.contains(env2)) {
168: return OPTIMIZED;
169: } else if (env2.contains(env1)) {
170: return EXPANDED;
171: } else {
172: return ERROR;
173: }
174: } else {
175: return EMPTYQUERY;
176: }
177: }
178: }
|