001/* A port that mirrors the properties of an associated port. 002 003 Copyright (c) 2003-2014 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 */ 028package ptolemy.actor.lib.hoc; 029 030import ptolemy.actor.TypedIOPort; 031import ptolemy.kernel.ComponentEntity; 032import ptolemy.kernel.Entity; 033import ptolemy.kernel.util.IllegalActionException; 034import ptolemy.kernel.util.NameDuplicationException; 035import ptolemy.kernel.util.Workspace; 036import ptolemy.moml.MoMLChangeRequest; 037 038/////////////////////////////////////////////////////////////////// 039//// MirrorPort 040 041/** 042 This port mirrors the properties of an associated port. That is, 043 if either this port or the associated port is changed by 044 setInput(), setOutput(), setMultiport(), or if it is removed 045 by setContainer(null), or its name is changed by setName(), 046 then the mirror port gets the same 047 changes. The changes are also applied to derived ports 048 (ports that mirror this one because of the class mechanism). 049 <p> 050 Users of this class must override their clone(Workspace) 051 method to re-establish appropriate port associations in 052 the clone. Cloning this port results in a clone with 053 no associations. 054 055 @author Edward A. Lee 056 @version $Id$ 057 @since Ptolemy II 4.0 058 @Pt.ProposedRating Yellow (eal) 059 @Pt.AcceptedRating Red (neuendor) 060 */ 061public class MirrorPort extends TypedIOPort { 062 /** Construct a port in the specified workspace with an empty 063 * string as a name. You can then change the name with setName(). 064 * If the workspace argument 065 * is null, then use the default workspace. 066 * The object is added to the workspace directory. 067 * Increment the version number of the workspace. 068 * @param workspace The workspace that will list the port. 069 * @exception IllegalActionException If port parameters cannot be initialized. 070 */ 071 public MirrorPort(Workspace workspace) throws IllegalActionException { 072 // This constructor is needed for Shallow codgen. 073 super(workspace); 074 } 075 076 /** Create a new instance of a port. 077 * @param container The container for the port. 078 * @param name The name of the port. 079 * @exception IllegalActionException If the container does not 080 * implement TypedActor. 081 * @exception NameDuplicationException If the name matches that 082 * of a port already in the container. 083 */ 084 public MirrorPort(ComponentEntity container, String name) 085 throws IllegalActionException, NameDuplicationException { 086 super(container, name); 087 } 088 089 /////////////////////////////////////////////////////////////////// 090 //// public methods //// 091 092 /** Clone the object into the specified workspace. This overrides 093 * the base class to unset the associated port. Users of this 094 * class are responsible for resetting it in their clone methods. 095 * @param workspace The workspace for the new object. 096 * @return A new NamedObj. 097 * @exception CloneNotSupportedException If any of the attributes 098 * cannot be cloned. 099 * @see #exportMoML(java.io.Writer, int, String) 100 */ 101 @Override 102 public Object clone(Workspace workspace) throws CloneNotSupportedException { 103 MirrorPort result = (MirrorPort) super.clone(workspace); 104 result._associatedPort = null; 105 result._settingAssociatedPort = false; 106 return result; 107 } 108 109 /** Return the associated port, or null if there is none. 110 * @return The associated port, or null if there is none. 111 * @see #setAssociatedPort(MirrorPort) 112 */ 113 public MirrorPort getAssociatedPort() { 114 return _associatedPort; 115 } 116 117 /** Specify an associated port. Once this is specified, 118 * then any changes made to this port (its name, whether it 119 * is an input or output, and whether it is a multiport) are 120 * mirrored in the associated port, and any changes made in 121 * the associated port are mirrored here. 122 * @param port The associated port. 123 * @see #getAssociatedPort() 124 */ 125 public void setAssociatedPort(MirrorPort port) { 126 _associatedPort = port; 127 port._associatedPort = this; 128 129 // NOTE: The association is not propagated to derived 130 // objects because we explicitly propagate all the changes. 131 } 132 133 /** Override the base class so that if the container is being 134 * set to null, then the associated port is also deleted 135 * (via a change request). Note that if the container 136 * of this port is changed to something other than null, 137 * there is no reasonable basis for changing the container 138 * of the associated port, so it is left unchanged. 139 * @param container The proposed container. 140 * @exception IllegalActionException If the proposed container is not a 141 * ComponentEntity, doesn't implement Actor, or has no name, 142 * or the port and container are not in the same workspace. Or 143 * it's not null 144 * @exception NameDuplicationException If the container already has 145 * a port with the name of this port. 146 */ 147 @Override 148 public void setContainer(Entity container) 149 throws IllegalActionException, NameDuplicationException { 150 super.setContainer(container); 151 152 if (container == null && _associatedPort != null 153 && _associatedPort.getContainer() != null) { 154 // Use a MoML change request to ensure propagation. 155 // Note that when that change request is executed, 156 // this port will be the associated port, but no 157 // change request will be issued because its container 158 // is already null. 159 MoMLChangeRequest request = new MoMLChangeRequest(this, 160 _associatedPort.getContainer(), 161 "<deletePort name=\"" + _associatedPort.getName() + "\"/>"); 162 _associatedPort.getContainer().requestChange(request); 163 } 164 } 165 166 /** Override the base class to also set the associated port, 167 * if there is one. 168 * @param isInput True to make this an input port. 169 * @exception IllegalActionException If changing the port status is 170 * not permitted. 171 */ 172 @Override 173 public void setInput(boolean isInput) throws IllegalActionException { 174 super.setInput(isInput); 175 176 if (_associatedPort != null && _associatedPort.isInput() != isInput) { 177 // Use a MoML change request to ensure propagation. 178 // Note that when that change request is executed, 179 // this port will be the associated port, but no 180 // change request will be issued because it already 181 // has matching status. 182 String value = isInput ? "true" : "false"; 183 MoMLChangeRequest request = new MoMLChangeRequest(this, 184 _associatedPort.getContainer(), 185 "<property name=\"input\" value=\"" + value + "\"/>"); 186 _associatedPort.getContainer().requestChange(request); 187 } 188 } 189 190 /** Override the base class to also set the associated port, 191 * if there is one. 192 * @param isMultiport True to make this a multiport. 193 * @exception IllegalActionException If changing the port status is 194 * not permitted. 195 */ 196 @Override 197 public void setMultiport(boolean isMultiport) 198 throws IllegalActionException { 199 super.setMultiport(isMultiport); 200 201 if (_associatedPort != null 202 && _associatedPort.isMultiport() != isMultiport) { 203 // Use a MoML change request to ensure propagation. 204 // Note that when that change request is executed, 205 // this port will be the associated port, but no 206 // change request will be issued because it already 207 // has matching status. 208 String value = isMultiport ? "true" : "false"; 209 MoMLChangeRequest request = new MoMLChangeRequest(this, 210 _associatedPort.getContainer(), 211 "<property name=\"multiport\" value=\"" + value + "\"/>"); 212 _associatedPort.getContainer().requestChange(request); 213 } 214 215 // Set the associated port first, so that if it fails, we 216 // don't change the status of this one either. 217 if (!_settingAssociatedPort && _associatedPort != null) { 218 try { 219 _settingAssociatedPort = true; 220 _associatedPort.setMultiport(isMultiport); 221 } finally { 222 _settingAssociatedPort = false; 223 } 224 } 225 226 // Note that propagation is handled by the call below. 227 super.setMultiport(isMultiport); 228 } 229 230 /** Override the base class to also set the associated port, 231 * if there is one. 232 */ 233 @Override 234 public void setName(String name) 235 throws IllegalActionException, NameDuplicationException { 236 super.setName(name); 237 238 if (_associatedPort != null 239 && !_associatedPort.getName().equals(name)) { 240 // Use a MoML change request to ensure propagation. 241 // Note that when that change request is executed, 242 // this port will be the associated port, but no 243 // change request will be issued because it already 244 // has matching status. 245 MoMLChangeRequest request = new MoMLChangeRequest(this, 246 _associatedPort, "<rename name=\"" + name + "\"/>"); 247 _associatedPort.requestChange(request); 248 } 249 } 250 251 /** Override the base class to also set the associated port, 252 * if there is one. 253 * @param isOutput True to make this an output port. 254 * @exception IllegalActionException If changing the port status is 255 * not permitted. 256 */ 257 @Override 258 public void setOutput(boolean isOutput) throws IllegalActionException { 259 super.setOutput(isOutput); 260 261 if (_associatedPort != null && _associatedPort.isOutput() != isOutput) { 262 // Use a MoML change request to ensure propagation. 263 // Note that when that change request is executed, 264 // this port will be the associated port, but no 265 // change request will be issued because it already 266 // has matching status. 267 String value = isOutput ? "true" : "false"; 268 MoMLChangeRequest request = new MoMLChangeRequest(this, 269 _associatedPort.getContainer(), 270 "<property name=\"output\" value=\"" + value + "\"/>"); 271 _associatedPort.getContainer().requestChange(request); 272 } 273 } 274 275 /////////////////////////////////////////////////////////////////// 276 //// private variables //// 277 278 /** The associated port, if there is one. */ 279 private MirrorPort _associatedPort = null; 280 281 /** Flag indicating that we are setting the associated port. */ 282 private boolean _settingAssociatedPort = false; 283}