01: /*
02: * <copyright>
03: *
04: * Copyright 2003-2004 BBNT Solutions, LLC
05: * under sponsorship of the Defense Advanced Research Projects
06: * Agency (DARPA).
07: *
08: * You can redistribute this software and/or modify it under the
09: * terms of the Cougaar Open Source License as published on the
10: * Cougaar Open Source Website (www.cougaar.org).
11: *
12: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
13: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
14: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
15: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
16: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
17: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
18: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23: *
24: * </copyright>
25: */
26:
27: package org.cougaar.lib.aggagent.query;
28:
29: import java.util.HashMap;
30: import java.util.Iterator;
31: import java.util.LinkedList;
32: import java.util.List;
33: import java.util.Map;
34:
35: /**
36: * An implementation of the Aggregator interface that behavior most likely to
37: * be used by developers. In particular, a series of keys can be used to
38: * gather atoms into affinity classes, and a DataAtomMelder (q.v.) is used to
39: * meld the affinity classes into atoms.
40: */
41: public class BatchAggregator implements Aggregator {
42: private List aggIds = null;
43: private DataAtomMelder melder = null;
44:
45: /**
46: * Create a new BatchAggregator. A list of ids is used to collate a result
47: * set, and a DataAtomMelder is used to meld collections of atoms into the
48: * aggregated result set.
49: */
50: public BatchAggregator(List ids, DataAtomMelder m) {
51: aggIds = ids;
52: melder = m;
53: }
54:
55: /**
56: * Transform the raw result set into an aggregated result set.
57: */
58: public void aggregate(Iterator dataAtoms, List output) {
59: Map batches = collate(dataAtoms);
60: for (Iterator i = batches.entrySet().iterator(); i.hasNext();) {
61: Map.Entry e = (Map.Entry) i.next();
62: melder.meld(aggIds, (CompoundKey) e.getKey(), (List) e
63: .getValue(), output);
64: }
65: }
66:
67: private Map collate(Iterator atoms) {
68: Map ret = new HashMap();
69: while (atoms.hasNext()) {
70: ResultSetDataAtom a = (ResultSetDataAtom) atoms.next();
71: CompoundKey k = a.getKey(aggIds);
72: List l = (List) ret.get(k);
73: if (l == null)
74: ret.put(k, l = new LinkedList());
75: l.add(a);
76: }
77: return ret;
78: }
79: }
|