001/* An attribute that extends its container's scope.
002
003 Copyright (c) 2001-2015 The Regents of the University of California.
004 All rights reserved.
005 Permission is hereby granted, without written agreement and without
006 license or royalty fees, to use, copy, modify, and distribute this
007 software and its documentation for any purpose, provided that the above
008 copyright notice and the following two paragraphs appear in all copies
009 of this software.
010
011 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
012 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
013 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
014 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
015 SUCH DAMAGE.
016
017 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
018 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
019 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
020 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
021 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
022 ENHANCEMENTS, OR MODIFICATIONS.
023
024 PT_COPYRIGHT_VERSION_2
025 COPYRIGHTENDKEY
026
027 @ProposedRating Red (liuxj)
028 @AcceptedRating Red (liuxj)
029
030 */
031package ptolemy.data.expr;
032
033import java.util.Iterator;
034import java.util.List;
035
036import ptolemy.kernel.util.Attribute;
037import ptolemy.kernel.util.IllegalActionException;
038import ptolemy.kernel.util.NameDuplicationException;
039import ptolemy.kernel.util.Nameable;
040import ptolemy.kernel.util.NamedObj;
041import ptolemy.kernel.util.ScopeExtender;
042import ptolemy.kernel.util.Settable;
043
044///////////////////////////////////////////////////////////////////
045//// ScopeExtendingAttribute
046
047/**
048 An attribute that extends its container's scope. Any
049 parameter contained by such an attribute has the same
050 visibility as parameters of the container of the attribute.
051 They are shadowed, however, by parameters of the container.
052 That is, if the container has a parameter with the same name
053 as one in the parameter set, the one in the container provides
054 the value to any observer.
055
056 @author Xiaojun Liu
057 @version $Id$
058 @see ptolemy.data.expr.Variable
059 */
060public class ScopeExtendingAttribute extends Attribute
061        implements ScopeExtender {
062    /** Construct an attribute with the given name contained by the specified
063     *  entity. The container argument must not be null, or a
064     *  NullPointerException will be thrown.  This attribute will use the
065     *  workspace of the container for synchronization and version counts.
066     *  If the name argument is null, then the name is set to the empty string.
067     *  Increment the version of the workspace.
068     *  @param container The container.
069     *  @param name The name of this attribute.
070     *  @exception IllegalActionException If the attribute is not of an
071     *   acceptable class for the container, or if the name contains a period.
072     *  @exception NameDuplicationException If the name coincides with
073     *   an attribute already in the container.
074     */
075    public ScopeExtendingAttribute(NamedObj container, String name)
076            throws IllegalActionException, NameDuplicationException {
077        super(container, name);
078    }
079
080    ///////////////////////////////////////////////////////////////////
081    ////                         public methods                    ////
082
083    /** Expand the scope of the container by creating any required attributes.
084     *  This method does nothing, assuming that the derived classes will
085     *  create the attributes in their constructor.
086     *  @exception IllegalActionException If any required attribute cannot be
087     *   created.
088     */
089    @Override
090    public void expand() throws IllegalActionException {
091    }
092
093    /** Specify the container NamedObj, adding this attribute to the
094     *  list of attributes in the container.  Notify parameters that
095     *  depends on any parameter of this attribute about the change in
096     *  scope.  If the container already
097     *  contains an attribute with the same name, then throw an exception
098     *  and do not make any changes.  Similarly, if the container is
099     *  not in the same workspace as this attribute, throw an exception.
100     *  If this attribute is already contained by the NamedObj, do nothing.
101     *  This method is write-synchronized on the workspace and increments
102     *  its version number.
103     *  @param container The container to attach this attribute to..
104     *  @exception IllegalActionException If this attribute is not of the
105     *   expected class for the container, or it has no name,
106     *   or the attribute and container are not in the same workspace, or
107     *   the proposed container would result in recursive containment.
108     *  @exception NameDuplicationException If the container already has
109     *   an attribute with the name of this attribute.
110     */
111    @Override
112    public void setContainer(NamedObj container)
113            throws IllegalActionException, NameDuplicationException {
114        Nameable oldContainer = getContainer();
115        super.setContainer(container);
116
117        if (oldContainer != container) {
118            // Every variable in the new scope that may be shadowed by
119            // a variable inside this attribute must be invalidated.
120            // This does not include variables inside the container itself,
121            // which take precedence.
122            if (container != null) {
123                _invalidateShadowedSettables(container.getContainer());
124            }
125
126            // Every variable inside this attribute, and anything that
127            // had been depending on them, must still be valid.
128            validate();
129        }
130    }
131
132    /** Validate contained settables.
133     *  @exception IllegalActionException If any required attribute cannot be
134     *   created.
135     */
136    @Override
137    public void validate() throws IllegalActionException {
138        List<Settable> settables = attributeList(Settable.class);
139        for (Settable settable : settables) {
140            settable.validate();
141        }
142    }
143
144    ///////////////////////////////////////////////////////////////////
145    ////                         private methods                   ////
146
147    private void _invalidateShadowedSettables(NamedObj object)
148            throws IllegalActionException {
149        if (object == null) {
150            // Nothing to do.
151            return;
152        }
153
154        for (Object element : object.attributeList(Variable.class)) {
155            Variable variable = (Variable) element;
156
157            if (getAttribute(variable.getName()) != null) {
158                variable.invalidate();
159            }
160        }
161
162        // Also invalidate the variables inside any
163        // scopeExtendingAttributes.
164        Iterator scopeAttributes = object
165                .attributeList(ScopeExtendingAttribute.class).iterator();
166
167        while (scopeAttributes.hasNext()) {
168            ScopeExtendingAttribute attribute = (ScopeExtendingAttribute) scopeAttributes
169                    .next();
170            Iterator variables = attribute.attributeList(Variable.class)
171                    .iterator();
172
173            while (variables.hasNext()) {
174                Variable variable = (Variable) variables.next();
175
176                if (getAttribute(variable.getName()) != null) {
177                    variable.invalidate();
178                }
179            }
180        }
181
182        NamedObj container = object.getContainer();
183
184        if (container != null) {
185            _invalidateShadowedSettables(container);
186        }
187    }
188}