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.rule.builder;
018:
019: import java.util.ArrayList;
020: import java.util.Calendar;
021: import java.util.HashMap;
022: import java.util.Iterator;
023: import java.util.List;
024: import java.util.Map;
025: import java.util.Stack;
026:
027: import org.drools.base.evaluators.DateFactory;
028: import org.drools.compiler.Dialect;
029: import org.drools.compiler.DialectConfiguration;
030: import org.drools.compiler.DialectRegistry;
031: import org.drools.compiler.PackageBuilderConfiguration;
032: import org.drools.lang.descr.AttributeDescr;
033: import org.drools.lang.descr.QueryDescr;
034: import org.drools.lang.descr.RuleDescr;
035: import org.drools.rule.Package;
036: import org.drools.rule.Query;
037: import org.drools.rule.Rule;
038: import org.drools.spi.DeclarationScopeResolver;
039:
040: /**
041: * A context for the current build
042: *
043: * @author etirelli
044: */
045: public class RuleBuildContext {
046:
047: // current package
048: private Package pkg;
049:
050: // configuration
051: private PackageBuilderConfiguration configuration;
052:
053: // current rule
054: private Rule rule;
055:
056: // a stack for the rule building used
057: // for declarations resolution
058: private Stack buildStack;
059:
060: // current Rule descriptor
061: private RuleDescr ruleDescr;
062:
063: // available declarationResolver
064: private DeclarationScopeResolver declarationResolver;
065:
066: // a simple counter for patterns
067: private int patternId = -1;
068:
069: // errors found when building the current context
070: private List errors;
071:
072: // list of generated methods
073: private List methods;
074:
075: // map<String invokerClassName, String invokerCode> of generated invokers
076: private Map invokers;
077:
078: // map<String invokerClassName, ConditionalElement ce> of generated invoker lookups
079: private Map invokerLookups;
080:
081: // map<String invokerClassName, BaseDescr descr> of descriptor lookups
082: private Map descrLookups;
083:
084: // a simple counter for generated names
085: private int counter;
086:
087: private DialectRegistry dialectRegistry;
088:
089: private Dialect dialect;
090:
091: /**
092: * Default constructor
093: */
094: public RuleBuildContext(
095: final PackageBuilderConfiguration configuration,
096: final Package pkg, final RuleDescr ruleDescr,
097: final DialectRegistry dialectRegistry,
098: final Dialect defaultDialect) {
099: this .configuration = configuration;
100: this .pkg = pkg;
101:
102: this .methods = new ArrayList();
103: this .invokers = new HashMap();
104: this .invokerLookups = new HashMap();
105: this .descrLookups = new HashMap();
106: this .errors = new ArrayList();
107: this .buildStack = new Stack();
108: this .declarationResolver = new DeclarationScopeResolver(
109: new Map[] { this .pkg.getGlobals() }, this .buildStack);
110: this .ruleDescr = ruleDescr;
111:
112: if (ruleDescr instanceof QueryDescr) {
113: this .rule = new Query(ruleDescr.getName());
114: } else {
115: this .rule = new Rule(ruleDescr.getName());
116: }
117:
118: // Assign attributes
119: setAttributes(this .rule, ruleDescr, ruleDescr.getAttributes());
120:
121: this .dialectRegistry = dialectRegistry;
122: this .dialect = (this .rule.getDialect() != null) ? this .dialectRegistry
123: .getDialectConfiguration(this .rule.getDialect())
124: .getDialect()
125: : defaultDialect;
126:
127: getDialect().init(ruleDescr);
128: }
129:
130: public Dialect getDialect() {
131: return dialect;
132: }
133:
134: /**
135: * Allows the change of the current dialect in the context
136: */
137: public void setDialect(Dialect dialect) {
138: this .dialect = dialect;
139: }
140:
141: public Dialect getDialect(String dialectName) {
142: return ((DialectConfiguration) this .dialectRegistry
143: .getDialectConfiguration(dialectName)).getDialect();
144: }
145:
146: /**
147: * Returns the list of errors found while building the current context
148: * @return
149: */
150: public List getErrors() {
151: return this .errors;
152: }
153:
154: /**
155: * Returns the current package being built
156: * @return
157: */
158: public Package getPkg() {
159: return this .pkg;
160: }
161:
162: /**
163: * Returns the current Rule being built
164: * @return
165: */
166: public Rule getRule() {
167: return this .rule;
168: }
169:
170: /**
171: * Returns the current RuleDescriptor
172: * @return
173: */
174: public RuleDescr getRuleDescr() {
175: return this .ruleDescr;
176: }
177:
178: /**
179: * Returns the available declarationResolver instance
180: * @return
181: */
182: public DeclarationScopeResolver getDeclarationResolver() {
183: return this .declarationResolver;
184: }
185:
186: /**
187: * Sets the available declarationResolver instance
188: * @param declarationResolver
189: */
190: public void setDeclarationResolver(
191: final DeclarationScopeResolver variables) {
192: this .declarationResolver = variables;
193: }
194:
195: /**
196: * Returns the Map<String invokerClassName, BaseDescr descr> of descriptor lookups
197: * @return
198: */
199: public Map getDescrLookups() {
200: return this .descrLookups;
201: }
202:
203: public void setDescrLookups(final Map descrLookups) {
204: this .descrLookups = descrLookups;
205: }
206:
207: /**
208: * Returns the Map<String invokerClassName, ConditionalElement ce> of generated invoker lookups
209: * @return
210: */
211: public Map getInvokerLookups() {
212: return this .invokerLookups;
213: }
214:
215: public void setInvokerLookups(final Map invokerLookups) {
216: this .invokerLookups = invokerLookups;
217: }
218:
219: /**
220: * Returns the Map<String invokerClassName, String invokerCode> of generated invokers
221: * @return
222: */
223: public Map getInvokers() {
224: return this .invokers;
225: }
226:
227: public void setInvokers(final Map invokers) {
228: this .invokers = invokers;
229: }
230:
231: /**
232: * Returns the list of generated methods
233: * @return
234: */
235: public List getMethods() {
236: return this .methods;
237: }
238:
239: public void setMethods(final List methods) {
240: this .methods = methods;
241: }
242:
243: /**
244: * Returns current counter value for generated method names
245: * @return
246: */
247: public int getCurrentId() {
248: return this .counter;
249: }
250:
251: public int getNextId() {
252: return this .counter++;
253: }
254:
255: public int getPatternId() {
256: return this .patternId;
257: }
258:
259: public int getNextPatternId() {
260: return ++this .patternId;
261: }
262:
263: public void setPatternId(final int patternId) {
264: this .patternId = patternId;
265: }
266:
267: public Stack getBuildStack() {
268: return this .buildStack;
269: }
270:
271: /**
272: * Sets rule Attributes
273: *
274: * @param rule
275: * @param attributes
276: */
277: public static void setAttributes(final Rule rule,
278: final RuleDescr ruleDescr, final List attributes) {
279:
280: for (final Iterator it = attributes.iterator(); it.hasNext();) {
281: final AttributeDescr attributeDescr = (AttributeDescr) it
282: .next();
283: final String name = attributeDescr.getName();
284: if (name.equals("salience")) {
285: try {
286: ruleDescr.setSalience(attributeDescr.getValue());
287: } catch (Exception e) {
288:
289: }
290: } else if (name.equals("no-loop")) {
291: if (attributeDescr.getValue() == null) {
292: rule.setNoLoop(true);
293: } else {
294: rule.setNoLoop(Boolean.valueOf(
295: attributeDescr.getValue()).booleanValue());
296: }
297: } else if (name.equals("auto-focus")) {
298: if (attributeDescr.getValue() == null) {
299: rule.setAutoFocus(true);
300: } else {
301: rule.setAutoFocus(Boolean.valueOf(
302: attributeDescr.getValue()).booleanValue());
303: }
304: } else if (name.equals("agenda-group")) {
305: rule.setAgendaGroup(attributeDescr.getValue());
306: } else if (name.equals("activation-group")) {
307: rule.setActivationGroup(attributeDescr.getValue());
308: } else if (name.equals("ruleflow-group")) {
309: rule.setRuleFlowGroup(attributeDescr.getValue());
310: } else if (name.equals("lock-on-active")) {
311: if (attributeDescr.getValue() == null) {
312: rule.setLockOnActive(true);
313: } else {
314: rule.setLockOnActive(Boolean.valueOf(
315: attributeDescr.getValue()).booleanValue());
316: }
317: } else if (name.equals("duration")) {
318: rule.setDuration(Long.parseLong(attributeDescr
319: .getValue()));
320: rule.setAgendaGroup("");
321: } else if (name.equals("enabled")) {
322: if (attributeDescr.getValue() == null) {
323: rule.setEnabled(true);
324: } else {
325: rule.setEnabled(Boolean.valueOf(
326: attributeDescr.getValue()).booleanValue());
327: }
328: } else if (name.equals("date-effective")) {
329: final Calendar cal = Calendar.getInstance();
330: cal.setTime(DateFactory.parseDate(attributeDescr
331: .getValue()));
332: rule.setDateEffective(cal);
333: } else if (name.equals("date-expires")) {
334: final Calendar cal = Calendar.getInstance();
335: cal.setTime(DateFactory.parseDate(attributeDescr
336: .getValue()));
337: rule.setDateExpires(cal);
338: } else if (name.equals("dialect")) {
339: rule.setDialect(attributeDescr.getValue());
340: }
341: }
342: }
343:
344: public PackageBuilderConfiguration getConfiguration() {
345: return configuration;
346: }
347:
348: }
|