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

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

started to work on multi-valued predicates; started to rework the filter algorithm

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