01: /*
02: * Copyright 2006 JBoss Inc
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.drools.rule.builder;
18:
19: import java.util.Iterator;
20:
21: import org.drools.lang.descr.BaseDescr;
22: import org.drools.lang.descr.ForallDescr;
23: import org.drools.lang.descr.PatternDescr;
24: import org.drools.rule.Forall;
25: import org.drools.rule.Pattern;
26: import org.drools.rule.RuleConditionElement;
27:
28: /**
29: * @author etirelli
30: *
31: */
32: public class ForallBuilder implements RuleConditionBuilder {
33:
34: public RuleConditionElement build(final RuleBuildContext context,
35: final BaseDescr descr) {
36: return build(context, descr, null);
37: }
38:
39: public RuleConditionElement build(final RuleBuildContext context,
40: final BaseDescr descr, final Pattern prefixPattern) {
41: final ForallDescr forallDescr = (ForallDescr) descr;
42:
43: final PatternBuilder patternBuilder = (PatternBuilder) context
44: .getDialect().getBuilder(PatternDescr.class);
45: final Pattern basePattern = (Pattern) patternBuilder.build(
46: context, forallDescr.getBasePattern());
47:
48: if (basePattern == null) {
49: return null;
50: }
51:
52: final Forall forall = new Forall(basePattern);
53:
54: // adding the newly created forall CE to the build stack
55: // this is necessary in case of local declaration usage
56: context.getBuildStack().push(forall);
57:
58: for (final Iterator it = forallDescr.getRemainingPatterns()
59: .iterator(); it.hasNext();) {
60: final Pattern anotherPattern = (Pattern) patternBuilder
61: .build(context, (PatternDescr) it.next());
62: forall.addRemainingPattern(anotherPattern);
63: }
64:
65: // poping the forall
66: context.getBuildStack().pop();
67:
68: return forall;
69: }
70:
71: }
|