1 |
/********************************************************************* |
2 |
* * |
3 |
* Copyright (c) 1997,1998, 1999 * |
4 |
* Multimedia DB Group and DEIS - CSITE-CNR, * |
5 |
* University of Bologna, Bologna, ITALY. * |
6 |
* * |
7 |
* All Rights Reserved. * |
8 |
* * |
9 |
* Permission to use, copy, and distribute this software and its * |
10 |
* documentation for NON-COMMERCIAL purposes and without fee is * |
11 |
* hereby granted provided that this copyright notice appears in * |
12 |
* all copies. * |
13 |
* * |
14 |
* THE AUTHORS MAKE NO REPRESENTATIONS OR WARRANTIES ABOUT THE * |
15 |
* SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING * |
16 |
* BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, * |
17 |
* FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. THE AUTHOR * |
18 |
* SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A * |
19 |
* RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS * |
20 |
* DERIVATIVES. * |
21 |
* * |
22 |
*********************************************************************/ |
23 |
|
24 |
#ifndef MTPREDICATE_H |
25 |
#define MTPREDICATE_H |
26 |
|
27 |
#include "MTobject.h" |
28 |
|
29 |
//class GiSTentry; |
30 |
|
31 |
typedef enum { |
32 |
FUZZY_STANDARD, |
33 |
FUZZY_ALGEBRAIC |
34 |
} language; |
35 |
|
36 |
typedef enum { |
37 |
LINEAR, |
38 |
EXPONENTIAL, |
39 |
DISTR |
40 |
} dist2sim; |
41 |
|
42 |
extern language query_language; |
43 |
|
44 |
double Dist2Sim(double dist); |
45 |
double Sim2Dist(double sim); |
46 |
|
47 |
class MTpred: public GiSTobject { // the base class for predicates |
48 |
public: |
49 |
virtual GiSTobjid IsA() { return MTPREDICATE_CLASS; } |
50 |
virtual double distance(const Object &obj) const=0; |
51 |
virtual ~MTpred() {} |
52 |
|
53 |
#ifdef PRINTING_OBJECTS |
54 |
virtual void Print(ostream& os) const=0; |
55 |
#endif |
56 |
}; |
57 |
|
58 |
class Pred: public MTpred { // a simple predicate |
59 |
public: |
60 |
// constructors, destructors, etc. |
61 |
Pred(const Object &obj): object(obj) {} |
62 |
Pred(const Pred& p) : object(p.object) {} |
63 |
GiSTobject *Copy() const { return new Pred(*this); } |
64 |
|
65 |
// virtual methods |
66 |
GiSTobjid IsA() { return MTPREDICATE_CLASS; } |
67 |
double distance(const Object &obj) const { |
68 |
return object.distance(obj); |
69 |
} |
70 |
|
71 |
// access to private methods |
72 |
Object obj() const { return object; } |
73 |
|
74 |
#ifdef PRINTING_OBJECTS |
75 |
void Print(ostream& os) const { |
76 |
os << object; |
77 |
} |
78 |
#endif |
79 |
|
80 |
private: |
81 |
Object object; |
82 |
}; |
83 |
|
84 |
class AndPred: public MTpred { // a conjunction of two predicates |
85 |
public: |
86 |
// constructors, destructors, etc. |
87 |
AndPred(const MTpred *p1, const MTpred *p2): pred1((MTpred *)p1->Copy()), pred2((MTpred *)p2->Copy()) {} |
88 |
GiSTobject *Copy() const { |
89 |
return new AndPred(pred1, pred2); |
90 |
} |
91 |
~AndPred() { |
92 |
delete pred1; |
93 |
delete pred2; |
94 |
} |
95 |
|
96 |
// virtual methods |
97 |
GiSTobjid IsA() { return MTPREDICATE_AND_CLASS; } |
98 |
double distance(const Object &obj) const { |
99 |
switch(query_language) { |
100 |
case FUZZY_STANDARD: { |
101 |
double s1=Dist2Sim(pred1->distance(obj)), s2=Dist2Sim(pred2->distance(obj)); |
102 |
|
103 |
return Sim2Dist(MIN(s1, s2)); |
104 |
} |
105 |
case FUZZY_ALGEBRAIC: return Sim2Dist(Dist2Sim(pred1->distance(obj))*Dist2Sim(pred2->distance(obj))); |
106 |
default: return maxDist(); |
107 |
} |
108 |
} |
109 |
|
110 |
// access to private members |
111 |
MTpred *Pred1() const { return pred1; } |
112 |
MTpred *Pred2() const { return pred2; } |
113 |
|
114 |
#ifdef PRINTING_OBJECTS |
115 |
void Print(ostream& os) const { |
116 |
os << "(" << *pred1 << "&&" << *pred2 << ")"; |
117 |
} |
118 |
#endif |
119 |
|
120 |
private: |
121 |
MTpred *pred1, *pred2; |
122 |
}; |
123 |
|
124 |
class OrPred: public MTpred { // a disjunction of two predicates |
125 |
public: |
126 |
// constructors, destructors, etc. |
127 |
OrPred(const MTpred *p1, const MTpred *p2): pred1((MTpred *)p1->Copy()), pred2((MTpred *)p2->Copy()) {} |
128 |
GiSTobject *Copy() const { |
129 |
return new OrPred(pred1, pred2); |
130 |
} |
131 |
~OrPred() { |
132 |
delete pred1; |
133 |
delete pred2; |
134 |
} |
135 |
|
136 |
// virtual methods |
137 |
GiSTobjid IsA() { return MTPREDICATE_OR_CLASS; } |
138 |
double distance(const Object &obj) const { |
139 |
switch(query_language) { |
140 |
case FUZZY_STANDARD: { |
141 |
double s1=Dist2Sim(pred1->distance(obj)), s2=Dist2Sim(pred2->distance(obj)); |
142 |
|
143 |
return Sim2Dist(MAX(s1, s2)); |
144 |
} |
145 |
case FUZZY_ALGEBRAIC: { |
146 |
double s1=Dist2Sim(pred1->distance(obj)), s2=Dist2Sim(pred2->distance(obj)); |
147 |
|
148 |
return Sim2Dist(s1+s2-s1*s2); |
149 |
} |
150 |
default: return maxDist(); |
151 |
} |
152 |
} |
153 |
|
154 |
// access to private members |
155 |
MTpred *Pred1() const { return pred1; } |
156 |
MTpred *Pred2() const { return pred2; } |
157 |
|
158 |
#ifdef PRINTING_OBJECTS |
159 |
void Print(ostream& os) const { |
160 |
os << "(" << *pred1 << "||" << *pred2 << ")"; |
161 |
} |
162 |
#endif |
163 |
|
164 |
private: |
165 |
MTpred *pred1, *pred2; |
166 |
}; |
167 |
|
168 |
class NotPred: public MTpred { // a negated predicate |
169 |
public: |
170 |
// constructors, destructors, etc. |
171 |
NotPred(const MTpred *p): pred((MTpred *)p->Copy()) {} |
172 |
GiSTobject *Copy() const { |
173 |
return new NotPred(pred); |
174 |
} |
175 |
~NotPred() { delete pred; } |
176 |
|
177 |
// virtual methods |
178 |
GiSTobjid IsA() { return MTPREDICATE_NOT_CLASS; } |
179 |
double distance(const Object &obj) const { |
180 |
return Sim2Dist(1-Dist2Sim(pred->distance(obj))); |
181 |
} |
182 |
|
183 |
// access to private members |
184 |
MTpred *Pred() const { return pred; } |
185 |
|
186 |
#ifdef PRINTING_OBJECTS |
187 |
void Print(ostream& os) const { |
188 |
os << "(!" << *pred << ")"; |
189 |
} |
190 |
#endif |
191 |
|
192 |
private: |
193 |
MTpred *pred; |
194 |
}; |
195 |
|
196 |
// for the queries, the conjunction corresponds to intersection, the disjunction to union, and the negation to difference |
197 |
|
198 |
class MTquery: public GiSTobject { // the base class for queries |
199 |
public: |
200 |
// constructors, destructors, etc. |
201 |
MTquery() : grade(0), isOpen(FALSE) {} |
202 |
MTquery(const double g, const BOOL o=FALSE) : grade(g), isOpen(o) {} |
203 |
virtual ~MTquery() {} |
204 |
|
205 |
// pure virtual methods |
206 |
virtual int Consistent(const GiSTentry& entry)=0; |
207 |
virtual int NonConsistent(const GiSTentry& entry)=0; |
208 |
|
209 |
// access to private members |
210 |
double Grade() const { return grade; } |
211 |
void SetGrade(double p_grade) { grade=p_grade; } |
212 |
|
213 |
protected: |
214 |
double grade; |
215 |
BOOL isOpen; |
216 |
}; |
217 |
|
218 |
class SimpleQuery : public MTquery { // a simple query |
219 |
public: |
220 |
// constructors, destructors, etc. |
221 |
SimpleQuery(const MTpred *p, const double r, const BOOL o=FALSE) : MTquery(0,o), pred((MTpred *)p->Copy()), radius(r) {} |
222 |
SimpleQuery(const SimpleQuery& q) : MTquery(q.grade, q.isOpen), pred((MTpred *)q.pred->Copy()), radius(q.radius) {} |
223 |
GiSTobject *Copy() const { |
224 |
return new SimpleQuery(*this); |
225 |
} |
226 |
~SimpleQuery() { delete pred; } |
227 |
|
228 |
// basic consistency methods |
229 |
int Consistent(const GiSTentry& entry); |
230 |
int NonConsistent(const GiSTentry& entry); |
231 |
|
232 |
// access to private members |
233 |
double Radius() const { return radius; } |
234 |
void SetRadius(double p_radius) { radius=p_radius; } |
235 |
|
236 |
private: |
237 |
MTpred *pred; |
238 |
double radius; |
239 |
}; |
240 |
|
241 |
class AndQuery : public MTquery { // the conjunction of two queries |
242 |
public: |
243 |
// constructors, destructors, etc. |
244 |
AndQuery(const MTquery *q1, const MTquery *q2): q1((MTquery *)q1->Copy()), q2((MTquery *)q2->Copy()) {} |
245 |
GiSTobject *Copy() const { |
246 |
return new AndQuery(q1, q2); |
247 |
} |
248 |
~AndQuery() { |
249 |
delete q1; |
250 |
delete q2; |
251 |
} |
252 |
|
253 |
// basic consistency methods |
254 |
int Consistent(const GiSTentry& entry) { |
255 |
int response=q1->Consistent(entry)&&q2->Consistent(entry); |
256 |
|
257 |
grade=MAX(q1->Grade(), q2->Grade()); |
258 |
return response; |
259 |
} |
260 |
int NonConsistent(const GiSTentry& entry) { |
261 |
int response=q1->NonConsistent(entry)||q2->NonConsistent(entry); |
262 |
|
263 |
grade=MAX(q1->Grade(), q2->Grade()); // or what? |
264 |
return response; |
265 |
} |
266 |
|
267 |
#ifdef PRINTING_OBJECTS |
268 |
void Print(ostream& os) const { |
269 |
os << "(" << *q1 << " and " << *q2 << ")"; |
270 |
} |
271 |
#endif |
272 |
|
273 |
private: |
274 |
MTquery *q1, *q2; |
275 |
}; |
276 |
|
277 |
class OrQuery : public MTquery { // the disjunction of two queries |
278 |
public: |
279 |
// constructors, destructors, etc. |
280 |
OrQuery(const MTquery *q1, const MTquery *q2): q1((MTquery *)q1->Copy()), q2((MTquery *)q2->Copy()) {} |
281 |
GiSTobject *Copy() const { |
282 |
return new OrQuery(q1, q2); |
283 |
} |
284 |
~OrQuery() { |
285 |
delete q1; |
286 |
delete q2; |
287 |
} |
288 |
|
289 |
// basic consistency methods |
290 |
int Consistent(const GiSTentry& entry) { |
291 |
int response=q1->Consistent(entry)||q2->Consistent(entry); |
292 |
|
293 |
grade=MIN(q1->Grade(), q2->Grade()); |
294 |
return response; |
295 |
} |
296 |
int NonConsistent(const GiSTentry& entry) { |
297 |
int response=q1->NonConsistent(entry)&&q2->NonConsistent(entry); |
298 |
|
299 |
grade=MIN(q1->Grade(), q2->Grade()); // or what? |
300 |
return response; |
301 |
} |
302 |
|
303 |
#ifdef PRINTING_OBJECTS |
304 |
void Print(ostream& os) const { |
305 |
os << "(" << *q1 << " or " << *q2 << ")"; |
306 |
} |
307 |
#endif |
308 |
|
309 |
private: |
310 |
MTquery *q1, *q2; |
311 |
}; |
312 |
|
313 |
class NotQuery: public MTquery { // the negation of a query |
314 |
public: |
315 |
// constructors, destructors, etc. |
316 |
NotQuery(const MTquery *q): q((MTquery *)q->Copy()) {} |
317 |
GiSTobject *Copy() const { return new NotQuery(q); } |
318 |
~NotQuery() { delete q; } |
319 |
|
320 |
// basic consistency methods |
321 |
int Consistent(const GiSTentry& entry) { |
322 |
int response=q->NonConsistent(entry); |
323 |
|
324 |
grade=q->Grade(); |
325 |
return response; |
326 |
} |
327 |
int NonConsistent(const GiSTentry& entry) { |
328 |
int response=q->Consistent(entry); |
329 |
|
330 |
grade=q->Grade(); // or what? |
331 |
return response; |
332 |
} |
333 |
|
334 |
#ifdef PRINTING_OBJECTS |
335 |
void Print(ostream& os) const { |
336 |
os << "(not " << *q << ")"; |
337 |
} |
338 |
#endif |
339 |
|
340 |
private: |
341 |
MTquery *q; |
342 |
}; |
343 |
|
344 |
class TopQuery: public GiSTobject { // a simple k-NN query |
345 |
public: |
346 |
// constructors, destructors, etc. |
347 |
TopQuery(const MTpred *p, const int n) : k(n), pred((MTpred *)p->Copy()) {} |
348 |
TopQuery(const TopQuery& q) : k(q.k), pred((MTpred *)q.pred->Copy()) {} |
349 |
GiSTobject *Copy() const { return new TopQuery(*this); } |
350 |
~TopQuery() { delete pred; } |
351 |
|
352 |
// access to private members |
353 |
const MTpred *Pred() const { return pred; } |
354 |
|
355 |
int k; |
356 |
private: |
357 |
MTpred *pred; |
358 |
}; |
359 |
|
360 |
#endif |