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.Iterator;
020:
021: import org.drools.RuntimeDroolsException;
022: import org.drools.lang.descr.AndDescr;
023: import org.drools.lang.descr.BaseDescr;
024: import org.drools.lang.descr.ConditionalElementDescr;
025: import org.drools.lang.descr.ExistsDescr;
026: import org.drools.lang.descr.NotDescr;
027: import org.drools.lang.descr.OrDescr;
028: import org.drools.rule.GroupElement;
029: import org.drools.rule.GroupElementFactory;
030: import org.drools.rule.Pattern;
031: import org.drools.rule.RuleConditionElement;
032:
033: /**
034: * @author etirelli
035: *
036: */
037: public class GroupElementBuilder implements RuleConditionBuilder {
038:
039: public RuleConditionElement build(final RuleBuildContext context,
040: final BaseDescr descr) {
041: return build(context, descr, null);
042: }
043:
044: public RuleConditionElement build(final RuleBuildContext context,
045: final BaseDescr descr, final Pattern prefixPattern) {
046: final ConditionalElementDescr cedescr = (ConditionalElementDescr) descr;
047:
048: final GroupElement ge = newGroupElementFor(cedescr.getClass());
049: context.getBuildStack().push(ge);
050:
051: if (prefixPattern != null) {
052: ge.addChild(prefixPattern);
053: }
054:
055: // iterate over child descriptors
056: for (final Iterator it = cedescr.getDescrs().iterator(); it
057: .hasNext();) {
058: // gets child to build
059: final BaseDescr child = (BaseDescr) it.next();
060:
061: // gets corresponding builder
062: final RuleConditionBuilder builder = context.getDialect()
063: .getBuilder(child.getClass());
064:
065: if (builder != null) {
066: final RuleConditionElement element = builder.build(
067: context, child);
068: // in case there is a problem with the building,
069: // builder will return null. Ex: ClassNotFound for the pattern type
070: if (element != null) {
071: ge.addChild(element);
072: }
073: } else {
074: throw new RuntimeDroolsException(
075: "BUG: no builder found for descriptor class "
076: + child.getClass());
077: }
078:
079: }
080:
081: context.getBuildStack().pop();
082:
083: return ge;
084: }
085:
086: private GroupElement newGroupElementFor(final Class descr) {
087: if (AndDescr.class.isAssignableFrom(descr)) {
088: return GroupElementFactory.newAndInstance();
089: } else if (OrDescr.class.isAssignableFrom(descr)) {
090: return GroupElementFactory.newOrInstance();
091: } else if (NotDescr.class.isAssignableFrom(descr)) {
092: return GroupElementFactory.newNotInstance();
093: } else if (ExistsDescr.class.isAssignableFrom(descr)) {
094: return GroupElementFactory.newExistsInstance();
095: } else {
096: throw new RuntimeDroolsException(
097: "BUG: Not able to create a group element for descriptor: "
098: + descr.getName());
099: }
100: }
101:
102: }
|