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

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

started to rework algo for partially matched subscriptions (multi-valued attributes etc)

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