001: //
002: // This file is part of the prose package.
003: //
004: // The contents of this file are subject to the Mozilla Public License
005: // Version 1.1 (the "License"); you may not use this file except in
006: // compliance with the License. You may obtain a copy of the License at
007: // http://www.mozilla.org/MPL/
008: //
009: // Software distributed under the License is distributed on an "AS IS" basis,
010: // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
011: // for the specific language governing rights and limitations under the
012: // License.
013: //
014: // The Original Code is prose.
015: //
016: // The Initial Developer of the Original Code is Andrei Popovici. Portions
017: // created by Andrei Popovici are Copyright (C) 2002 Andrei Popovici.
018: // All Rights Reserved.
019: //
020: // Contributor(s):
021: // $Id: QueryManager.java,v 1.1.1.1 2003/07/02 15:30:52 apopovic Exp $
022: // =====================================================================
023: //
024: // (history at end)
025: //
026:
027: package ch.ethz.prose.query;
028:
029: // used packages
030: import ch.ethz.prose.AspectManager;
031: import ch.ethz.prose.Aspect;
032: import ch.ethz.prose.crosscut.Crosscut;
033:
034: import java.util.List;
035: import java.util.Vector;
036: import java.util.Iterator;
037: import java.util.Comparator;
038: import java.util.Collections;
039: import java.util.Set;
040: import java.util.HashSet;
041:
042: import ch.ethz.prose.engine.JoinPointRequest;
043: import ch.ethz.prose.engine.JoinPointManager;
044:
045: /**
046: * Class
047: *
048: * @version $Revision: 1.1.1.1 $
049: * @author Andrei Popovici
050: */
051: public class QueryManager {
052:
053: public static final int SELECT_ASPECT = 0x01; // must be the minimum value of the 'SELECT' constants
054: public static final int SELECT_CROSSCUT = 0x02;
055: public static final int SELECT_JOINPOINT = 0x04;
056: public static final int GROUP_BY_ASPECT = 0x01;
057: public static final int GROUP_BY_CROSSCUT = 0x02;
058: public static final int GROUP_BY_JOINPOINT = 0x04;
059: public static final int SELECT_ALL = SELECT_ASPECT
060: | SELECT_CROSSCUT | SELECT_JOINPOINT;
061:
062: AspectManager aspectMgr = null;
063: JoinPointManager jpMgr = null;
064:
065: public QueryManager(AspectManager am) {
066: if (am == null)
067: throw new IllegalArgumentException(
068: "QueryManager.init: null am");
069: aspectMgr = am;
070: jpMgr = am.getJoinPointManager();
071: }
072:
073: /**
074: * This method processes the system (of its <code>AspectManager</code>) for finding a duplicate free
075: * result <code>List</code> consisting of three columns:
076: * <ul>
077: * <li><code>Aspect</code>
078: * <li><code>Crosscut</code>
079: * <li><code>JoinPointRequest</code>
080: * </ul>
081: * As input it takes a <code>List</code> of <code>Aspect</code> and two <code>int</code>'s indicating
082: * which columns are selected and by which column the result is grouped.
083: *
084: * Through the logical <code>OR</code> combination of the three member constants <code>SELECT_ASPECT</code>,
085: * <code>SELECT_CROSSCUT</code> and <code>SELECT_JOINPOINT</code> every possible opportunity of selecting the columns is
086: * reached.
087: *
088: * The value of the <code>groupBy</code> variable should be in accordance with one of the codes <code>GROUP_BY_ASPECT</code>,
089: * <code>GROUP_BY_CROSSCUT</code> or <code>GROUP_BY_JOINPOINT</code>. If the value differs from one of these member constants, the
090: * default constant <code>GROUP_BY_ASPECT</code> is assigned.
091: *
092: * @param aspectList The list that is processed for searching all corresponding crosscuts and their joinpoints.
093: * @param selectFields The constant that indicates which columns are selected in the result. The non-selected
094: * columns are set to <code>null</code>. An input value other than explained above raises an Exception.
095: * @param groupBy Indicates if the result table is grouped by aspects, crosscuts or joinpoints. By default
096: * they are grouped by aspects.
097: */
098: public List queryAspects(List aspectList, int selectFields,
099: int groupBy) {
100: if (selectFields == 0)
101: return new Vector();
102:
103: HashSet result = new HashSet();
104: Iterator i = aspectList.iterator();
105: while (i.hasNext()) {
106: AspectSurrogate as = (AspectSurrogate) i.next();
107: Aspect crtAspect = reconstructAspect(as);
108: if (crtAspect != null) {
109: Iterator j = crtAspect.getCrosscuts().iterator();
110: if (!j.hasNext())
111: result
112: .add(createTuple(as, null, null,
113: selectFields));
114: while (j.hasNext()) {
115: Crosscut crtCrosscut = (Crosscut) j.next();
116: Iterator k = jpMgr.getJoinpoints(crtCrosscut)
117: .iterator();
118: CrosscutSurrogate cs = new CrosscutSurrogate(as,
119: crtCrosscut);
120: if (!k.hasNext())
121: result.add(createTuple(as, cs, null,
122: selectFields));
123: while (k.hasNext()) {
124: JoinPointRequest crtJpr = (JoinPointRequest) k
125: .next();
126: result.add(createTuple(as, cs, crtJpr,
127: selectFields));
128: }
129:
130: }
131: }
132: }
133: return sortTupleList(new Vector(result), groupBy);
134: }
135:
136: public List queryCrosscuts(List crosscutList, int selectFields,
137: int groupBy) {
138: if (selectFields == 0)
139: return new Vector();
140:
141: HashSet result = new HashSet();
142: Iterator i = crosscutList.iterator();
143: while (i.hasNext()) {
144: try {
145: CrosscutSurrogate cs = (CrosscutSurrogate) i.next();
146: AspectSurrogate as = cs.getOwnerSurrogate();
147:
148: Crosscut crtCrosscut = reconstructCrosscut(cs);
149: Iterator k = jpMgr.getJoinpoints(crtCrosscut)
150: .iterator();
151: if (!k.hasNext())
152: result.add(createTuple(as, cs, null, selectFields));
153: while (k.hasNext()) {
154: JoinPointRequest crtJpr = (JoinPointRequest) k
155: .next();
156: result
157: .add(createTuple(as, cs, crtJpr,
158: selectFields));
159: }
160: } catch (ClassNotFoundException e) {
161: // The following exception are CAUGHT and not propagated. The
162: // reason is that a surrogate may come from a remote machine
163: // and may attempt to instantiate a method or a class (etc)
164: // which does not exist on our VM. This being the case,
165: // we skip this iteration
166: }
167:
168: }
169: return sortTupleList(new Vector(result), groupBy);
170: }
171:
172: public List queryJoinpoints(List jpList, int selectFields,
173: int groupBy) {
174: if (selectFields == 0)
175: return new Vector();
176:
177: HashSet result = new HashSet();
178: Iterator i = new HashSet(jpList).iterator();
179: while (i.hasNext()) {
180: try {
181: JoinPointRequestSurrogate jprs = (JoinPointRequestSurrogate) i
182: .next();
183: JoinPointRequest jpr = reconstructRequest(jprs);
184: Iterator j = jpMgr.getCrosscuts(jpr).iterator();
185: while (j.hasNext()) {
186: Crosscut crtCrosscut = (Crosscut) j.next();
187: Aspect crtAspect = crtCrosscut.getOwner();
188: result.add(createTuple(crtAspect, crtCrosscut,
189: jprs, selectFields));
190: }
191: }
192:
193: // The following exception are CAUGHT and not propagated. The
194: // reason is that a surrogate may come from a remote machine
195: // and may attempt to instantiate a method or a class (etc)
196: // which does not exist on our VM. This being the case,
197: // we skip this iteration
198: catch (ClassNotFoundException e) {
199: } catch (NoSuchMethodException e) {
200: } catch (NoSuchFieldException e) {
201: }
202:
203: }
204: return sortTupleList(new Vector(result), groupBy);
205: }
206:
207: public List queryAllJoinpoints() {
208: Set jpSur = new HashSet();
209: Iterator i = jpMgr.allJoinpoints().iterator();
210: while (i.hasNext())
211: jpSur.add(new JoinPointRequestSurrogate(
212: (JoinPointRequest) i.next()));
213:
214: List jpSurList = new Vector(jpSur);
215: return jpSurList;
216: }
217:
218: public List queryAllAspects() {
219: Set apSur = new HashSet();
220: Iterator i = aspectMgr.getAllAspects().iterator();
221: while (i.hasNext())
222: apSur.add(new AspectSurrogate((Aspect) i.next()));
223:
224: return new Vector(apSur);
225: }
226:
227: private List sortTupleList(List lst, int groupBy) {
228: if (groupBy != 0)
229: Collections.sort(lst, new TupleComparator(groupBy));
230:
231: return lst;
232: }
233:
234: static class TupleComparator implements Comparator {
235: int groupBy;
236:
237: TupleComparator(int groupBy) {
238: this .groupBy = groupBy;
239: }
240:
241: private int doHashCode(Object o) {
242: if (o == null)
243: return 0;
244: else
245: return o.hashCode();
246: }
247:
248: private int doEquals(Object o1, Object o2) {
249: int compareVal = doHashCode(o1) - doHashCode(o2);
250: if (compareVal != 0)
251: return compareVal;
252:
253: if (o1 != null && o1.equals(o2))
254: return 0;
255: else
256: return 1;
257: }
258:
259: public int compare(Object o1, Object o2) {
260: int hvalCompare;
261: Tuple t1 = (Tuple) o1;
262: Tuple t2 = (Tuple) o2;
263: switch (groupBy) {
264: case GROUP_BY_ASPECT:
265: hvalCompare = doEquals(t1.getAspectSurrogate(), t2
266: .getAspectSurrogate());
267: break;
268: case GROUP_BY_JOINPOINT:
269: hvalCompare = doEquals(t1.getRequestSurrogate(), t2
270: .getRequestSurrogate());
271: break;
272: case GROUP_BY_CROSSCUT:
273: hvalCompare = doEquals(t1.getAspectSurrogate(), t2
274: .getAspectSurrogate());
275: if (hvalCompare == 0)
276: hvalCompare = doEquals(t1.getCrosscutSurrogate(),
277: t2.getCrosscutSurrogate());
278: break;
279: default:
280: throw new IllegalArgumentException(
281: "Illegal groupByValue");
282: }
283: // System.err.println("\nt1: " + t1 + "\n" + t2 + " ->RESULT: " + hvalCompare);
284: return hvalCompare;
285: }
286: }
287:
288: protected Tuple createTuple(AspectSurrogate as,
289: CrosscutSurrogate cs, JoinPointRequest jpr, int selectFields) {
290: Tuple result = new Tuple();
291: if ((selectFields & SELECT_ASPECT) != 0)
292: result.setAspectSurrogate(as);
293: if ((selectFields & SELECT_CROSSCUT) != 0)
294: result.setCrosscutSurrogate(cs);
295: if ((selectFields & SELECT_JOINPOINT) != 0) {
296: if (jpr == null)
297: result.setRequestSurrogate(null);
298: else
299: result
300: .setRequestSurrogate(new JoinPointRequestSurrogate(
301: jpr));
302: }
303:
304: return result;
305: }
306:
307: protected Tuple createTuple(Aspect a, Crosscut c,
308: JoinPointRequestSurrogate jprs, int selectFields) {
309: Tuple result = new Tuple();
310: AspectSurrogate as = null;
311: if ((selectFields & SELECT_ASPECT) != 0) {
312: if (a != null)
313: as = new AspectSurrogate(a);
314: else
315: as = null;
316: result.setAspectSurrogate(as);
317: }
318:
319: if ((selectFields & SELECT_CROSSCUT) != 0) {
320: if (as == null) {
321: if (a != null)
322: as = new AspectSurrogate(a);
323: else
324: as = null;
325: }
326: result.setCrosscutSurrogate(new CrosscutSurrogate(as, c));
327: }
328:
329: if ((selectFields & SELECT_JOINPOINT) != 0)
330: result.setRequestSurrogate(jprs);
331: return result;
332: }
333:
334: public Aspect reconstructAspect(AspectSurrogate as) {
335: List lst = aspectMgr.getAllAspects();
336: Iterator i = lst.iterator();
337: while (i.hasNext()) {
338: Aspect crtAsp = (Aspect) (i.next());
339: if (crtAsp.getAssociatedObject().equals(
340: as.getAssociatedObject())
341: && crtAsp.getClass().getName().equals(
342: as.getAspectClassName()))
343: return crtAsp;
344: }
345: return null;
346: }
347:
348: protected Crosscut reconstructCrosscut(CrosscutSurrogate cs)
349: throws ClassNotFoundException {
350: Aspect a = reconstructAspect(cs.getOwnerSurrogate());
351: if (a == null)
352: return null;
353: return (Crosscut) a.getCrosscuts().get(cs.getIndex());
354: }
355:
356: /**
357: * Returns a <code>JoinPointRequest</code> Object that is represented by this
358: * <code>JoinPointRequestSurrogate</code>.
359: *
360: * @exception ClassNotFoundException can be thrown if the class of a required name or type or is not available.
361: * @exception NoSuchMethodException can be thrown while the creation of a <code>MethodEntryRequest</code> or a
362: * <code>MethodExitRequest</code>.
363: * @exception NoSuchFieldException can be thrown while the creation of a <code>FieldAccessRequest</code> or a
364: * <code>FieldModificationRequest</code>.
365: */
366: protected JoinPointRequest reconstructRequest(
367: JoinPointRequestSurrogate jprs)
368: throws ClassNotFoundException, NoSuchMethodException,
369: NoSuchFieldException {
370: return jpMgr.createJoinPointRequest(jprs.getKind(), jprs
371: .getMember().toRealInstance());
372: }
373:
374: }
375:
376: //======================================================================
377: //
378: // $Log: QueryManager.java,v $
379: // Revision 1.1.1.1 2003/07/02 15:30:52 apopovic
380: // Imported from ETH Zurich
381: //
382: // Revision 1.2 2003/05/25 11:37:13 popovici
383: // Redefinitions: Aspects surrogates do not need to have the class to
384: // be recreated; aspects surrogates can now be created without a real aspect
385: //
386: // Revision 1.1 2003/05/20 16:05:09 popovici
387: //
388: // New QueryManager replaces functionality in AspectManager (better Soc)
389: // New 'Surrogate' classes for usage in the QueryManager
390: // The 'RemoteAspectManager' and tools modified to use the Surrogates and the QueryManager
391: //
392: //
|