001: /*
002: * Copyright 2006 JBoss Inc
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.drools.reteoo.builder;
018:
019: import java.util.HashMap;
020: import java.util.LinkedList;
021: import java.util.List;
022: import java.util.ListIterator;
023: import java.util.Map;
024:
025: import org.drools.common.BetaConstraints;
026: import org.drools.common.InternalRuleBase;
027: import org.drools.common.InternalWorkingMemory;
028: import org.drools.reteoo.ObjectSource;
029: import org.drools.reteoo.ReteooBuilder;
030: import org.drools.reteoo.ReteooRuleBase;
031: import org.drools.reteoo.TupleSource;
032: import org.drools.rule.RuleConditionElement;
033:
034: /**
035: * A build context for Reteoo Builder
036: *
037: * @author etirelli
038: */
039: public class BuildContext {
040:
041: // tuple source to attach next node to
042: private TupleSource tupleSource;
043:
044: // object source to attach next node to
045: private ObjectSource objectSource;
046:
047: // object type cache to check for cross products
048: private LinkedList objectType;
049:
050: // offset of the pattern
051: private int currentPatternOffset;
052:
053: // rule base to add rules to
054: private InternalRuleBase rulebase;
055:
056: // working memories attached to the given rulebase
057: private InternalWorkingMemory[] workingMemories;
058:
059: // id generator
060: private ReteooBuilder.IdGenerator idGenerator;
061:
062: // a build stack to track nested elements
063: private LinkedList buildstack;
064:
065: // beta constraints from the last pattern attached
066: private List betaconstraints;
067:
068: // alpha constraints from the last pattern attached
069: private List alphaConstraints;
070:
071: public BuildContext(final InternalRuleBase rulebase,
072: final ReteooBuilder.IdGenerator idGenerator) {
073: this .rulebase = rulebase;
074: this .workingMemories = (InternalWorkingMemory[]) this .rulebase
075: .getWorkingMemories();
076: this .idGenerator = idGenerator;
077:
078: this .objectType = new LinkedList();
079: this .buildstack = new LinkedList();
080:
081: this .tupleSource = null;
082: this .objectSource = null;
083:
084: this .currentPatternOffset = 0;
085: }
086:
087: /**
088: * @return the currentPatternOffset
089: */
090: public int getCurrentPatternOffset() {
091: return this .currentPatternOffset;
092: }
093:
094: /**
095: * @param currentPatternOffset the currentPatternOffset to set
096: */
097: public void setCurrentPatternOffset(final int currentPatternIndex) {
098: this .currentPatternOffset = currentPatternIndex;
099: this .syncObjectTypesWithPatternOffset();
100: }
101:
102: public void syncObjectTypesWithPatternOffset() {
103: while (this .objectType.size() > this .currentPatternOffset) {
104: this .objectType.removeLast();
105: }
106: }
107:
108: /**
109: * @return the objectSource
110: */
111: public ObjectSource getObjectSource() {
112: return this .objectSource;
113: }
114:
115: /**
116: * @param objectSource the objectSource to set
117: */
118: public void setObjectSource(final ObjectSource objectSource) {
119: this .objectSource = objectSource;
120: }
121:
122: /**
123: * @return the objectType
124: */
125: public LinkedList getObjectType() {
126: return this .objectType;
127: }
128:
129: /**
130: * @param objectType the objectType to set
131: */
132: public void setObjectType(final LinkedList objectType) {
133: this .objectType = objectType;
134: }
135:
136: /**
137: * @return the tupleSource
138: */
139: public TupleSource getTupleSource() {
140: return this .tupleSource;
141: }
142:
143: /**
144: * @param tupleSource the tupleSource to set
145: */
146: public void setTupleSource(final TupleSource tupleSource) {
147: this .tupleSource = tupleSource;
148: }
149:
150: public void incrementCurrentPatternOffset() {
151: this .currentPatternOffset++;
152: }
153:
154: public void decrementCurrentPatternOffset() {
155: this .currentPatternOffset--;
156: this .syncObjectTypesWithPatternOffset();
157: }
158:
159: /**
160: * Returns context rulebase
161: * @return
162: */
163: public InternalRuleBase getRuleBase() {
164: return this .rulebase;
165: }
166:
167: /**
168: * Return the array of working memories associated with the given
169: * rulebase.
170: *
171: * @return
172: */
173: public InternalWorkingMemory[] getWorkingMemories() {
174: return this .workingMemories;
175: }
176:
177: /**
178: * Returns an Id for the next node
179: * @return
180: */
181: public int getNextId() {
182: return this .idGenerator.getNextId();
183: }
184:
185: /**
186: * Method used to undo previous id assignment
187: */
188: public void releaseLastId() {
189: this .idGenerator.releaseLastId();
190: }
191:
192: /**
193: * Adds the rce to the build stack
194: * @param rce
195: */
196: public void push(final RuleConditionElement rce) {
197: this .buildstack.addLast(rce);
198: }
199:
200: /**
201: * Removes the top stack element
202: * @return
203: */
204: public RuleConditionElement pop() {
205: return (RuleConditionElement) this .buildstack.removeLast();
206: }
207:
208: /**
209: * Returns the top stack element without removing it
210: * @return
211: */
212: public RuleConditionElement peek() {
213: return (RuleConditionElement) this .buildstack.getLast();
214: }
215:
216: /**
217: * Returns a list iterator to iterate over the stacked elements
218: * @return
219: */
220: public ListIterator stackIterator() {
221: return this .buildstack.listIterator();
222: }
223:
224: /**
225: * @return the betaconstraints
226: */
227: public List getBetaconstraints() {
228: return this .betaconstraints;
229: }
230:
231: /**
232: * @param betaconstraints the betaconstraints to set
233: */
234: public void setBetaconstraints(final List betaconstraints) {
235: this .betaconstraints = betaconstraints;
236: }
237:
238: public int getNextSequence(String groupName) {
239: //List list = new ArrayList();
240:
241: Integer seq = (Integer) this .rulebase
242: .getAgendaGroupRuleTotals().get(groupName);
243: if (seq == null) {
244: seq = new Integer(0);
245: }
246: Integer newSeq = new Integer(seq.intValue() + 1);
247: this .rulebase.getAgendaGroupRuleTotals().put(groupName, newSeq);
248:
249: return newSeq.intValue();
250: }
251:
252: /**
253: * @return
254: */
255: public List getAlphaConstraints() {
256: return alphaConstraints;
257: }
258:
259: public void setAlphaConstraints(List alphaConstraints) {
260: this.alphaConstraints = alphaConstraints;
261: }
262:
263: }
|