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.reteoo.builder;
18:
19: import java.util.Iterator;
20:
21: import org.drools.rule.Pattern;
22: import org.drools.rule.Forall;
23: import org.drools.rule.GroupElement;
24: import org.drools.rule.GroupElementFactory;
25: import org.drools.rule.RuleConditionElement;
26:
27: /**
28: * The Reteoo component builder for forall CE
29: *
30: * @author etirelli
31: */
32: public class ForallBuilder implements ReteooComponentBuilder {
33:
34: /**
35: * @inheritDoc
36: */
37: public void build(final BuildContext context,
38: final BuildUtils utils, final RuleConditionElement rce) {
39: final Forall forall = (Forall) rce;
40:
41: // forall can be translated into
42: // not( basePattern and not( <remaining_patterns>+ ) )
43: // so we just do that:
44:
45: final GroupElement and = GroupElementFactory.newAndInstance();
46: and.addChild(forall.getBasePattern());
47:
48: final GroupElement not2 = GroupElementFactory.newNotInstance();
49: if (forall.getRemainingPatterns().size() == 1) {
50: not2.addChild((Pattern) forall.getRemainingPatterns()
51: .get(0));
52: and.addChild(not2);
53: } else if (forall.getRemainingPatterns().size() > 1) {
54: final GroupElement and2 = GroupElementFactory
55: .newAndInstance();
56: for (final Iterator it = forall.getRemainingPatterns()
57: .iterator(); it.hasNext();) {
58: and2.addChild((Pattern) it.next());
59: }
60: not2.addChild(and2);
61: and.addChild(not2);
62: }
63:
64: final GroupElement not = GroupElementFactory.newNotInstance();
65: not.addChild(and);
66:
67: // get builder for the CEs
68: final ReteooComponentBuilder builder = utils.getBuilderFor(not);
69:
70: // builds the CEs
71: builder.build(context, utils, not);
72: }
73:
74: /**
75: * @inheritDoc
76: */
77: public boolean requiresLeftActivation(final BuildUtils utils,
78: final RuleConditionElement rce) {
79: return true;
80: }
81:
82: }
|