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.Map;
020:
021: import org.drools.base.ShadowProxy;
022: import org.drools.common.InternalFactHandle;
023: import org.drools.common.InternalWorkingMemory;
024: import org.drools.rule.Declaration;
025: import org.drools.spi.Tuple;
026:
027: public class QueryResult {
028:
029: protected Tuple tuple;
030: private WorkingMemory workingMemory;
031: private QueryResults queryResults;
032:
033: public QueryResult(final Tuple tuple,
034: final WorkingMemory workingMemory,
035: final QueryResults queryResults) {
036: this .tuple = tuple;
037: this .workingMemory = workingMemory;
038: this .queryResults = queryResults;
039: }
040:
041: /**
042: * Return a map of Declarations where the key is the identifier and the value
043: * is the Declaration.
044: *
045: * @return
046: * The Map of Declarations.
047: */
048: public Map getDeclarations() {
049: return this .queryResults.getDeclarations();
050: }
051:
052: /**
053: * Returns the Object for int position in the Tuple
054: *
055: * @param i
056: * @return
057: * The Object
058: */
059: public Object get(final int i) {
060: //adjust for the DroolsQuery object
061: return getObject(this .tuple.get(i + 1));
062: }
063:
064: /**
065: * Return the Object for the given Declaration identifer.
066: * @param identifier
067: * @return
068: * The Object
069: */
070: public Object get(final String identifier) {
071: return get((Declaration) this .queryResults.getDeclarations()
072: .get(identifier));
073: }
074:
075: /**
076: * Return the Object for the given Declaration.
077: * @param identifier
078: * @return
079: * The Object
080: */
081: public Object get(final Declaration declaration) {
082: return declaration.getValue(
083: (InternalWorkingMemory) workingMemory,
084: getObject(this .tuple.get(declaration)));
085: }
086:
087: /**
088: * Return the FactHandles for the Tuple.
089: * @return
090: */
091: public FactHandle[] getFactHandles() {
092: // Strip the DroolsQuery fact
093: final FactHandle[] src = this .tuple.getFactHandles();
094: final FactHandle[] dst = new FactHandle[src.length - 1];
095: System.arraycopy(src, 1, dst, 0, dst.length);
096: return dst;
097: }
098:
099: /**
100: * The size of the Tuple; i.e. the number of columns (FactHandles) in this row result.
101: * @return
102: */
103: public int size() {
104: // Adjust for the DroolsQuery object
105: return this .tuple.getFactHandles().length - 1;
106: }
107:
108: /**
109: * Get the Object for the given FactHandle
110: * @param handle
111: * @return
112: */
113: private Object getObject(FactHandle factHandle) {
114: InternalFactHandle handle = (InternalFactHandle) factHandle;
115: if (handle.isShadowFact()) {
116: return ((ShadowProxy) handle.getObject())
117: .getShadowedObject();
118: } else {
119: return handle.getObject();
120: }
121: }
122: }
|