source: trunk/greenstone3-extensions/gsdl-as/src/org/greenstone/gsdlas/ProfileStore.java@ 8753

Last change on this file since 8753 was 8753, checked in by schweer, 19 years ago

removed unnecessary TODO's & added an important one ;)

  • Property svn:keywords set to Author Date Id Revision
File size: 10.2 KB
Line 
1/*
2 * Created on Oct 27, 2004
3 * Copyright (C) Andrea Schweer, 2004
4 *
5 * This file is part of the Greenstone Alerting Service.
6 * Refer to the COPYING file in the base directory of this package
7 * for licensing information.
8 */
9package org.greenstone.gsdlas;
10
11import java.util.*;
12
13import javax.servlet.http.HttpSession;
14
15import org.greenstone.gsdlas.profiles.*;
16
17/**
18 * Storage for all Profiles. Singleton.
19 *
20 * @author schweer
21 *
22 */
23public class ProfileStore {
24
25 private static ProfileStore instance;
26
27 private Map docIdEqualsSubscriptions = new TreeMap();
28 private Map collectionIdEqualsSubscriptions = new TreeMap();
29 private Map docCollectionIdEqualsSubscriptions = new TreeMap();
30 private Set noEqualsSubscriptions = new TreeSet();
31
32 private ProfileStore() {
33 }
34
35 /**
36 * @return the one existing instance of this class.
37 */
38 public static ProfileStore getInstance() {
39 if (instance == null) {
40 instance = new ProfileStore();
41 }
42 return instance;
43 }
44
45 /**
46 * @param subscription
47 * the subscription to add to the profile store.
48 */
49 private void addSubscription(Subscription subscription) {
50 Predicate documentIdPredicate = subscription.getPredicate("documentID");
51 Predicate collectionIdPredicate = subscription.getPredicate("collectionID");
52
53 if (documentIdPredicate != null) {
54 IdEqualsPredicate docIdPredicate
55 = (IdEqualsPredicate) documentIdPredicate;
56 if (collectionIdPredicate != null) {
57 IdEqualsPredicate collIdPredicate
58 = (IdEqualsPredicate) collectionIdPredicate;
59
60 String[] values = new String[2];
61 values[0] = docIdPredicate.getValue();
62 values[1] = collIdPredicate.getValue();
63
64 Set subList = (Set) docCollectionIdEqualsSubscriptions.get(values);
65 if (subList == null) {
66 subList = new TreeSet();
67 docCollectionIdEqualsSubscriptions.put(values, subList);
68 }
69 subList.add(subscription);
70 } else {
71 String value = docIdPredicate.getValue();
72 Set subList = (Set) docIdEqualsSubscriptions.get(value);
73 if (subList == null) {
74 subList = new TreeSet();
75 docIdEqualsSubscriptions.put(value, subList);
76 }
77 subList.add(subscription);
78 }
79 } else if (collectionIdPredicate != null) {
80 IdEqualsPredicate collIdPredicate
81 = (IdEqualsPredicate) collectionIdPredicate;
82
83 String value = collIdPredicate.getValue();
84 Set subList = (Set) collectionIdEqualsSubscriptions.get(value);
85 if (subList == null) {
86 subList = new TreeSet();
87 collectionIdEqualsSubscriptions.put(value, subList);
88 }
89 subList.add(subscription);
90 } else {
91 noEqualsSubscriptions.add(subscription);
92 }
93 }
94
95 /**
96 * @return
97 */
98 public Set getSubscriptionsWithoutEqualsPredicates() {
99 return Collections.unmodifiableSet(noEqualsSubscriptions);
100 }
101
102 /**
103 * Computes the list of equality subscriptions satisfied by the event.
104 * @param event
105 * @return
106 */
107 public Set getPartiallyMatchedSubscriptions(Map event) {
108 Set result = new TreeSet();
109 if (event.get("documentID") != null) {
110 String documentID = (String) event.get("documentID");
111 if (event.get("collectionID") != null) {
112 String collectionID = (String) event.get("collectionID");
113 String[] values = new String[] {documentID, collectionID};
114 Set fromDocAndCollectionId = (Set) docCollectionIdEqualsSubscriptions.get(values);
115 if (fromDocAndCollectionId != null) {
116 result.addAll(fromDocAndCollectionId);
117 }
118 Set fromCollectionId = (Set) collectionIdEqualsSubscriptions.get(collectionID);
119 if (fromCollectionId != null) {
120 result.addAll(fromCollectionId);
121 }
122 }
123 Set fromDocId = (Set) collectionIdEqualsSubscriptions.get(documentID);
124 if (fromDocId != null) {
125 result.addAll(fromDocId);
126 }
127 } else if (event.get("collectionID") != null){
128 String collectionID = (String) event.get("collectionID");
129 Set fromCollectionId = (Set) collectionIdEqualsSubscriptions.get(collectionID);
130 if (fromCollectionId != null) {
131 result.addAll(fromCollectionId);
132 }
133 }
134 return result;
135 }
136
137 /**
138 *
139 * @param gsComm
140 * @return
141 * @throws UnsupportedFieldException
142 */
143 private Set findMatchedQueryPredicates(Map event, GreenstoneCommunicator gsComm) {
144 Set matchedPreds = new TreeSet();
145
146 // iterate through all document content predicates
147 // execute the query stored in the predicate
148 // predicate is matched iff the docID occurs in the query result
149
150 for (Iterator iter = PredicateFactory.getQueryPredicates("document_content").iterator(); iter.hasNext();) {
151 Predicate predicate = (Predicate) iter.next();
152 String collection = (String) event.get("collectionID");
153 String query = predicate.getValue();
154 Set results;
155 try {
156 results = gsComm.fullTextSearch(collection, query);
157 if (results.contains(event.get("documentID"))) {
158 matchedPreds.add(predicate);
159 }
160 } catch (Exception e) {
161 }
162 }
163
164 return matchedPreds;
165 }
166
167 /**
168 * Computes the set of SubstringMatchPredicates satisfied by the event.
169 * @param event
170 *
171 * @return
172 */
173 private Set findMatchedSubstringMatchPredicates(Map event) {
174 Set result = new TreeSet();
175
176 for (Iterator iter = PredicateFactory.getAllSubstringMatchPredicates().iterator(); iter.hasNext();) {
177 Predicate predicate = (Predicate) iter.next();
178 if (predicate.isSatisfied(event)) {
179 result.add(predicate);
180 }
181 }
182 return result;
183 }
184
185 /**
186 * Filters the event against the profiles. It uses the equality-preferred
187 * algorithm as described in Fabret, Llirbat, Pereira and Shasha:
188 * <em>Efficient Matching for Content-based Publish/Subscribe
189 * Systems</em>.
190 * @param event
191 * @param gsComm
192 */
193 public Set filter(Map event, GreenstoneCommunicator gsComm) {
194 Set matchedSubscriptions = new TreeSet();
195
196 Set partiallyMatchedSubscriptions
197 = getPartiallyMatchedSubscriptions(event);
198
199 Set matchedPredicates = findMatchedSubstringMatchPredicates(event);
200 matchedPredicates.addAll(findMatchedQueryPredicates(event, gsComm));
201
202 Map numMatchedPredicates = new TreeMap();
203 for (Iterator iter = matchedPredicates.iterator(); iter.hasNext();) {
204 Predicate pred = (Predicate) iter.next();
205 for (Iterator iterator = pred.getSubscriptionIDs().iterator(); iterator
206 .hasNext();) {
207 // TODO change to subscription IDs
208 Subscription sub = (Subscription) iterator.next();
209 Integer count = (Integer) numMatchedPredicates.get(sub);
210 int newCountValue = (count == null ? 1 : count.intValue() + 1);
211 numMatchedPredicates.put(sub,
212 new Integer(newCountValue));
213 }
214 }
215
216 Set notUnmatchedSubscriptions = partiallyMatchedSubscriptions;
217 notUnmatchedSubscriptions.addAll(noEqualsSubscriptions);
218 for (Iterator iter = notUnmatchedSubscriptions.iterator(); iter
219 .hasNext();) {
220 Subscription sub = (Subscription) iter.next();
221 int matchedPredicatesCount = 0;
222 if (numMatchedPredicates.get(sub) != null) {
223 matchedPredicatesCount = ((Integer) numMatchedPredicates.get(sub)).intValue();
224 }
225 if (matchedPredicatesCount == sub.getNumOfNonEqualsPredicates()) {
226 matchedSubscriptions.add(sub);
227 }
228 }
229 return matchedSubscriptions;
230 }
231
232 public String toString() {
233 Set allSubscriptions = getAllSubscriptions();
234
235 StringBuffer buffer = new StringBuffer();
236
237 for (Iterator iter = allSubscriptions.iterator(); iter.hasNext();) {
238 Subscription sub = (Subscription) iter.next();
239 buffer.append(sub);
240 buffer.append("\n");
241 }
242
243 return buffer.toString();
244 }
245
246 /**
247 * @return
248 */
249 public Set getAllSubscriptions() {
250 Set allSubscriptions = new TreeSet();
251 allSubscriptions.addAll(noEqualsSubscriptions);
252
253 for(Iterator iter = docIdEqualsSubscriptions.values().iterator(); iter.hasNext();) {
254 Set values = (Set) iter.next();
255 allSubscriptions.addAll(values);
256 }
257
258 for(Iterator iter = docCollectionIdEqualsSubscriptions.values().iterator(); iter.hasNext();) {
259 Set values = (Set) iter.next();
260 allSubscriptions.addAll(values);
261 }
262
263 for(Iterator iter = collectionIdEqualsSubscriptions.values().iterator(); iter.hasNext();) {
264 Set values = (Set) iter.next();
265 allSubscriptions.addAll(values);
266 }
267 return Collections.unmodifiableSet(allSubscriptions);
268 }
269
270
271 /**
272 * @param arguments
273 * @throws Exception
274 */
275 public void createSubscription(Map arguments) throws Exception {
276 Subscription sub = new Subscription(arguments);
277 addSubscription(sub);
278 }
279
280
281 /**
282 * @param subscriptionID
283 */
284 public void deleteSubscription(String subscriptionID) {
285 // TODO Auto-generated method stub
286
287 }
288
289 /**
290 * @param arguments
291 * @param session
292 */
293 public void changeSubscription(Map arguments, HttpSession session) {
294 String subscriptionID = (String) arguments.get("subscriptionID");
295 // TODO Auto-generated method stub
296
297 }
298
299}
Note: See TracBrowser for help on using the repository browser.