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.lang.descr;
018:
019: import java.util.ArrayList;
020: import java.util.Collections;
021: import java.util.List;
022:
023: /**
024: * @author etirelli
025: *
026: */
027: public class ForallDescr extends BaseDescr implements
028: ConditionalElementDescr {
029:
030: private static final long serialVersionUID = 400L;
031:
032: private static final String BASE_IDENTIFIER = "$__forallBaseIdentifier";
033:
034: private List patterns;
035:
036: public ForallDescr() {
037: this .patterns = new ArrayList(2);
038: }
039:
040: /* (non-Javadoc)
041: * @see org.drools.lang.descr.ConditionalElementDescr#addDescr(org.drools.lang.descr.BaseDescr)
042: */
043: public void addDescr(final BaseDescr baseDescr) {
044: // cast to make sure we are adding a pattern descriptor
045: this .patterns.add(baseDescr);
046: }
047:
048: public void insertBeforeLast(final Class clazz,
049: final BaseDescr baseDescr) {
050: throw new UnsupportedOperationException(
051: "Can't add descriptors to " + this .getClass().getName());
052: }
053:
054: /* (non-Javadoc)
055: * @see org.drools.lang.descr.ConditionalElementDescr#getDescrs()
056: */
057: public List getDescrs() {
058: return this .patterns;
059: }
060:
061: /**
062: * Returns the base pattern from the forall CE
063: * @return
064: */
065: public PatternDescr getBasePattern() {
066: if (this .patterns.size() > 1) {
067: return (PatternDescr) this .patterns.get(0);
068: } else if (this .patterns.size() == 1) {
069: // in case there is only one pattern, we do a rewrite, so:
070: // forall( Cheese( type == "stilton" ) )
071: // becomes
072: // forall( BASE_IDENTIFIER : Cheese() Cheese( this == BASE_IDENTIFIER, type == "stilton" ) )
073: PatternDescr original = (PatternDescr) this .patterns.get(0);
074: PatternDescr base = (PatternDescr) original.clone();
075: base.getDescrs().clear();
076: base.setIdentifier(BASE_IDENTIFIER);
077: return base;
078: }
079: return null;
080: }
081:
082: /**
083: * Returns the remaining patterns from the forall CE
084: * @return
085: */
086: public List getRemainingPatterns() {
087: if (this .patterns.size() > 1) {
088: return this .patterns.subList(1, this .patterns.size());
089: } else if (this .patterns.size() == 1) {
090: // in case there is only one pattern, we do a rewrite, so:
091: // forall( Cheese( type == "stilton" ) )
092: // becomes
093: // forall( BASE_IDENTIFIER : Cheese() Cheese( this == BASE_IDENTIFIER, type == "stilton" ) )
094: PatternDescr original = (PatternDescr) this .patterns.get(0);
095: PatternDescr remaining = (PatternDescr) original.clone();
096: VariableRestrictionDescr restr = new VariableRestrictionDescr(
097: "==", BASE_IDENTIFIER);
098: FieldConstraintDescr constr = new FieldConstraintDescr(
099: "this");
100: constr.addRestriction(restr);
101: remaining.addConstraint(constr);
102: return Collections.singletonList(remaining);
103: }
104: return Collections.EMPTY_LIST;
105: }
106:
107: public void addOrMerge(BaseDescr baseDescr) {
108: this.patterns.add(baseDescr);
109: }
110: }
|