001: package org.drools;
002:
003: /*
004: * Copyright 2005 JBoss Inc
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import java.util.HashMap;
020: import java.util.Iterator;
021: import java.util.List;
022: import java.util.Map;
023: import java.util.NoSuchElementException;
024:
025: import org.drools.rule.Declaration;
026: import org.drools.rule.Query;
027: import org.drools.spi.Tuple;
028:
029: /**
030: * Returned QueryResults instance for a requested named query. from here you can iterate the returned data, or
031: * get a specific row. All the available Declarations used in the query can also be accessed.
032: *
033: */
034: public class QueryResults {
035: private Query query;
036:
037: private Map declarations;
038:
039: protected List results;
040: protected WorkingMemory workingMemory;
041:
042: public QueryResults(final List results, final Query query,
043: final WorkingMemory workingMemory) {
044: this .results = results;
045: this .query = query;
046: this .workingMemory = workingMemory;
047: }
048:
049: public QueryResult get(final int i) {
050: if (i > this .results.size()) {
051: throw new NoSuchElementException();
052: }
053: return new QueryResult((Tuple) this .results.get(i),
054: this .workingMemory, this );
055: }
056:
057: /**
058: * Returns an Iterator for the results.
059: *
060: * @return
061: */
062: public Iterator iterator() {
063: return new QueryResultsIterator(this .results.iterator());
064: }
065:
066: /**
067: * Return a map of Declarations where the key is the identifier and the value
068: * is the Declaration.
069: *
070: * @return
071: * The Map of Declarations.
072: */
073: public Map getDeclarations() {
074:
075: final Declaration[] declarations = this .query.getDeclarations();
076: final Map map = new HashMap(declarations.length);
077: for (int i = 0, length = declarations.length; i < length; i++) {
078: map.put(declarations[i].getIdentifier(), declarations[i]);
079: }
080: this .declarations = map;
081:
082: return this .declarations;
083: }
084:
085: /**
086: * The results size
087: * @return
088: */
089: public int size() {
090: return this .results.size();
091: }
092:
093: private class QueryResultsIterator implements Iterator {
094: private Iterator iterator;
095:
096: public QueryResultsIterator(final Iterator iterator) {
097: this .iterator = iterator;
098: }
099:
100: public boolean hasNext() {
101: return this .iterator.hasNext();
102: }
103:
104: public Object next() {
105: return new QueryResult((Tuple) this .iterator.next(),
106: QueryResults.this .workingMemory, QueryResults.this );
107: }
108:
109: public void remove() {
110: this.iterator.remove();
111: }
112:
113: }
114: }
|