KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jalisto > se > query > execution > ExecutionNode


1 /*
2  * Jalisto - JAva LIght STOrage
3  * Copyright (C) 2000-2005 Xcalia http://www.xcalia.com
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * Xcalia
20  * 71, rue Desnouettes
21  * 75014 Paris - France
22  * http://www.xcalia.com
23  */

24 package org.objectweb.jalisto.se.query.execution;
25
26 import org.objectweb.jalisto.se.api.Session;
27 import org.objectweb.jalisto.se.api.MetaRepository;
28 import org.objectweb.jalisto.se.api.query.IndexManager;
29 import org.objectweb.jalisto.se.query.constraint.BinaryBooleanOperatorConstraint;
30 import org.objectweb.jalisto.se.query.result.QueryResultWrapper;
31 import org.objectweb.jalisto.se.query.result.WrapperSet;
32
33 import java.util.List JavaDoc;
34
35 public class ExecutionNode extends ExecutionElement {
36
37     public ExecutionNode(ExecutionTree tree, ExecutionElement father,
38                          BinaryBooleanOperatorConstraint binaryConstraint, int depth) {
39         super(tree, father, depth);
40         this.andOp = binaryConstraint.isAndOperator();
41     }
42
43     public void setLeft(ExecutionElement left) {
44         this.left = left;
45     }
46
47     public void setRight(ExecutionElement right) {
48         this.right = right;
49     }
50
51     public boolean onlyOneToResolve() {
52         if (left.isResolved()) {
53             return !right.isResolved();
54         }
55         return right.isResolved();
56     }
57
58     public boolean isAndNode() {
59         return andOp;
60     }
61
62     public boolean isInOrBranch(IndexManager indexManager) {
63         return ((!andOp) && (left.isInOrBranch(indexManager) || right.isInOrBranch(indexManager)));
64     }
65
66     public void cleanSubExecutionTree() {
67         super.cleanSubExecutionTree();
68         left.cleanSubExecutionTree();
69         right.cleanSubExecutionTree();
70     }
71
72     public void cleanAll() {
73         super.cleanAll();
74         left.cleanAll();
75         right.cleanAll();
76     }
77
78     public void defineMeta(MetaRepository repository) {
79         left.defineMeta(repository);
80         right.defineMeta(repository);
81     }
82
83     /**
84      * ****************************** RESOLVE *******************************************
85      */

86
87     public ExecutionNode resolveBack() {
88         if (isResolved()) {
89             return getFather();
90         }
91
92         if (left.isResolved() && right.isResolved()) {
93             WrapperSet setLeft = left.getResolvedValues();
94             WrapperSet setRight = right.getResolvedValues();
95             if (andOp) {
96                 doIntersection(resolvedValues, setLeft, setRight);
97                 tree.getToResolveAndNodes().remove(this);
98             } else {
99                 resolvedValues.addAll(setLeft);
100                 resolvedValues.addAll(setRight);
101             }
102             isResolved = true;
103             if (andOp) {
104                 tree.getToResolveAndNodes().remove(this);
105             }
106             return getFather();
107         }
108
109         return null;
110     }
111
112     public void resolveBeforeAndNodeNearRoot(Session session, WrapperSet candidates, List JavaDoc toResolve) {
113         // one is resolved
114
ExecutionElement resolved;
115         ExecutionElement unresolved;
116         if (left.isResolved()) {
117             resolved = left;
118             unresolved = right;
119         } else {
120             resolved = right;
121             unresolved = left;
122         }
123
124         if (candidates != null) {
125             unresolved.resolveBeforeAndNodeNearRoot(
126                     session,
127                     doIntersection(new WrapperSet(), candidates, resolved.getResolvedValues()),
128                     toResolve);
129         } else {
130             unresolved.resolveBeforeAndNodeNearRoot(session, resolved.getResolvedValues(), toResolve);
131         }
132
133         toResolve.remove(this);
134     }
135
136     public boolean resolveOnElement(Session session, QueryResultWrapper wrapper) {
137         if (andOp) {
138             return left.resolveOnElement(session, wrapper) && right.resolveOnElement(session, wrapper);
139         } else {
140             return left.resolveOnElement(session, wrapper) || right.resolveOnElement(session, wrapper);
141         }
142     }
143
144     public WrapperSet resolveWithCandidates(Session session, WrapperSet candidates) {
145         if (isResolved()) {
146             return doIntersection(new WrapperSet(), candidates, resolvedValues);
147         }
148
149         // one is resolved
150
ExecutionElement resolved;
151         ExecutionElement unresolved;
152         if (left.isResolved()) {
153             resolved = left;
154             unresolved = right;
155         } else {
156             resolved = right;
157             unresolved = left;
158         }
159
160         WrapperSet wSet = doIntersection(new WrapperSet(), candidates, resolved.getResolvedValues());
161         resolvedValues = unresolved.resolveWithCandidates(session, wSet);
162         isResolved = true;
163
164         if (andOp) {
165             tree.getToResolveAndNodes().remove(this); // really needed ?
166
}
167
168         return resolvedValues;
169     }
170
171     public String JavaDoc toString() {
172         if (andOp) {
173             return "Enode&&";
174         } else {
175             return "Enode||";
176         }
177     }
178
179
180     private boolean andOp;
181     private ExecutionElement left;
182     private ExecutionElement right;
183
184 }
185
Popular Tags