001/*Attributes for ports decorated by a communication aspect.
002
003@Copyright (c) 2011-2014 The Regents of the University of California.
004All rights reserved.
005
006Permission is hereby granted, without written agreement and without
007license or royalty fees, to use, copy, modify, and distribute this
008software and its documentation for any purpose, provided that the
009above copyright notice and the following two paragraphs appear in all
010copies of this software.
011
012IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
013FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
014ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
015THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
016SUCH DAMAGE.
017
018THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
019INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
020MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
021PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
022CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
023ENHANCEMENTS, OR MODIFICATIONS.
024
025                                                PT_COPYRIGHT_VERSION_2
026                                                COPYRIGHTENDKEY
027
028
029 */
030package ptolemy.actor;
031
032import ptolemy.data.IntToken;
033import ptolemy.data.expr.Parameter;
034import ptolemy.kernel.util.Attribute;
035import ptolemy.kernel.util.Decorator;
036import ptolemy.kernel.util.IllegalActionException;
037import ptolemy.kernel.util.NameDuplicationException;
038import ptolemy.kernel.util.NamedObj;
039import ptolemy.kernel.util.Settable;
040
041/** Attributes for ports decorated by a communication aspect.
042 *  A port on an actor decorated by a composite communication aspect must
043 *  specify the request port that input tokens are routed to.
044 *
045 *  @author Patricia Derler
046 *  @version $Id$
047 *  @since Ptolemy II 10.0
048 *  @Pt.ProposedRating Yellow (derler)
049 *  @Pt.AcceptedRating Red (derler)
050 */
051public class CommunicationAspectAttributes extends ExecutionAttributes {
052
053    /** Constructor to use when editing a model.
054     *  @param target The object being decorated.
055     *  @param decorator The decorator.
056     *  @exception IllegalActionException If the superclass throws it.
057     *  @exception NameDuplicationException If the superclass throws it.
058     */
059    public CommunicationAspectAttributes(NamedObj target, Decorator decorator)
060            throws IllegalActionException, NameDuplicationException {
061        super(target, decorator);
062        _init();
063    }
064
065    /** Constructor to use when parsing a MoML file.
066     *  @param target The object being decorated.
067     *  @param name The name of this attribute.
068     *  @exception IllegalActionException If the superclass throws it.
069     *  @exception NameDuplicationException If the superclass throws it.
070     */
071    public CommunicationAspectAttributes(NamedObj target, String name)
072            throws IllegalActionException, NameDuplicationException {
073        super(target, name);
074        _init();
075    }
076
077    ///////////////////////////////////////////////////////////////////
078    ////                         parameters                        ////
079
080    /** The sequenceNumber indicates the order in which communication aspects
081     *  are used. It defaults to the integer value -1 to indicate that
082     *  this communication aspect is not used. After enabling the quantity
083     *  manager, this sequence number gets updated automatically.
084     */
085    public Parameter sequenceNumber;
086
087    ///////////////////////////////////////////////////////////////////
088    ////                         public methods                    ////
089
090    /** If attribute is <i>messageLength</i> report the new value
091     *  to the communication aspect.
092     *  @param attribute The changed parameter.
093     *  @exception IllegalActionException If the parameter set is not valid.
094     *  Not thrown in this class.
095     */
096    @Override
097    public void attributeChanged(Attribute attribute)
098            throws IllegalActionException {
099        IOPort port = (IOPort) getContainer();
100        if (attribute == enable) {
101            // When cloning, the contanier might be an EntityLibrary.
102            // See vergil/test/VergilConfiguration.tcl.
103            NamedObj container = port.getContainer().getContainer();
104            if (container instanceof CompositeActor) {
105                if (((CompositeActor) container).isOpaque()) {
106                    port.createReceivers();
107                }
108                port.invalidateCommunicationAspects();
109            }
110        }
111        super.attributeChanged(attribute);
112    }
113
114    private void _init()
115            throws IllegalActionException, NameDuplicationException {
116        sequenceNumber = new Parameter(this, "sequenceNumber",
117                new IntToken(-1));
118        sequenceNumber.setPersistent(true);
119        sequenceNumber.setVisibility(Settable.EXPERT);
120    }
121}