001/* A test suite parameter that is shared globally in a model. 002 003 Copyright (c) 2006-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 028 */ 029package ptolemy.actor.parameters.test; 030 031import java.util.Collection; 032 033import ptolemy.actor.parameters.SharedParameter; 034import ptolemy.kernel.util.IllegalActionException; 035import ptolemy.kernel.util.InternalErrorException; 036import ptolemy.kernel.util.NameDuplicationException; 037import ptolemy.kernel.util.NamedObj; 038 039/////////////////////////////////////////////////////////////////// 040//// SharedParameter 041 042/** 043 A test suite parameter that is shared globally in a model. 044 @author Christopher Brooks 045 @version $Id$ 046 @since Ptolemy II 8.0 047 @Pt.ProposedRating Green (eal) 048 @Pt.AcceptedRating Green (acataldo) 049 */ 050public class TestSharedParameter extends SharedParameter { 051 /** Construct a parameter with the given container and name. 052 * The container class will be used to determine which other 053 * instances of TestSharedParameter are shared with this one. 054 * NOTE: Do not use this constructor if you plan to set 055 * a default value. Use the four argument constructor instead. 056 * @param container The container. 057 * @param name The name of the parameter. 058 * @exception IllegalActionException If the parameter is not of an 059 * acceptable class for the container. 060 * @exception NameDuplicationException If the name coincides with 061 * a parameter already in the container. 062 */ 063 public TestSharedParameter(NamedObj container, String name) 064 throws IllegalActionException, NameDuplicationException { 065 this(container, name, null, ""); 066 } 067 068 /** Construct a parameter with the given container, name, and 069 * container class. The specified class will be used to determine 070 * which other instances of TestSharedParameter are shared with this one. 071 * NOTE: Do not use this constructor if you plan to set 072 * a default value. Use the four argument constructor instead. 073 * @param container The container. 074 * @param name The name of the parameter. 075 * @param containerClass The class used to determine shared instances. 076 * @exception IllegalActionException If the parameter is not of an 077 * acceptable class for the container. 078 * @exception NameDuplicationException If the name coincides with 079 * a parameter already in the container. 080 */ 081 public TestSharedParameter(NamedObj container, String name, 082 Class containerClass) 083 throws IllegalActionException, NameDuplicationException { 084 this(container, name, containerClass, ""); 085 } 086 087 /** Construct a parameter with the given container, name, 088 * container class, and default value. This is the preferred 089 * constructor to use. 090 * The specified class will be used to determine 091 * which other instances of TestSharedParameter are shared with this one. 092 * @param container The container. 093 * @param name The name of the parameter. 094 * @param containerClass The class used to determine shared instances. 095 * @param defaultValue The default value to use if the container's 096 * model has no shared parameters. 097 * @exception IllegalActionException If the parameter is not of an 098 * acceptable class for the container, or an empty string to specify no 099 * default value. 100 * @exception NameDuplicationException If the name coincides with 101 * a parameter already in the container. 102 */ 103 public TestSharedParameter(NamedObj container, String name, 104 Class containerClass, String defaultValue) 105 throws IllegalActionException, NameDuplicationException { 106 super(container, name, containerClass, defaultValue); 107 } 108 109 /////////////////////////////////////////////////////////////////// 110 //// public methods //// 111 112 /** Infer the value of this parameter from the container 113 * context. That is, search for parameters that are 114 * shared with this one, and set the value of this parameter 115 * to match the last one encountered. 116 * If there are no shared parameters, then assign the 117 * default value given as an argument. 118 * @param defaultValue The default parameter value to give. 119 * @exception InternalErrorException If there are multiple 120 * shared parameters in the model, but their values 121 * do not match. 122 */ 123 @Override 124 public void inferValueFromContext(String defaultValue) { 125 super.inferValueFromContext(defaultValue); 126 inferValueFromContextCount++; 127 } 128 129 /** Return true if this instance is suppressing propagation. 130 * Unless setSuppressingPropagation() has been called, this 131 * returns false. 132 * @return Returns whether this instance is suppressing propagation. 133 * @see #setSuppressingPropagation(boolean) 134 */ 135 @Override 136 public boolean isSuppressingPropagation() { 137 isSuppressingPropagationCount++; 138 return super.isSuppressingPropagation(); 139 } 140 141 /** Override the base class to also set the expression of shared 142 * parameters. 143 * @param expression The expression. 144 */ 145 @Override 146 public void setExpression(String expression) { 147 setExpressionCount++; 148 super.setExpression(expression); 149 } 150 151 /** Return a collection of all the shared parameters within the 152 * same model as this parameter. If there are no such parameters 153 * or if this parameter is deeply contained within an EntityLibrary, then 154 * return an empty collection. The list will include this instance if 155 * this instance. 156 * A shared parameter is one that is an instance of TestSharedParameter, 157 * has the same name as this one, and is contained by the container 158 * class specified in the constructor. 159 * @return A list of parameters. 160 */ 161 @Override 162 public synchronized Collection sharedParameterSet() { 163 sharedParameterSetCount++; 164 //new Exception("TestSharedParameter.sharedParameterSet(): " 165 // + getFullName() + " " + sharedParameterSetCount).printStackTrace(); 166 167 return super.sharedParameterSet(); 168 } 169 170 /** Override the base class to also validate the shared instances. 171 * @exception IllegalActionException If this variable or a 172 * variable dependent on this variable cannot be evaluated (and is 173 * not lazy) and the model error handler throws an exception. 174 * Also thrown if the change is not acceptable to the container. 175 */ 176 @Override 177 public Collection validate() throws IllegalActionException { 178 validateCount++; 179 //new Exception("TestSharedParameter.validate(): " 180 // + getFullName() + " " + validateCount).printStackTrace(); 181 return super.validate(); 182 } 183 184 /////////////////////////////////////////////////////////////////// 185 //// protected methods //// 186 187 /** Override the base class to do the propagation only if 188 * the specified destination is not shared. 189 * @param destination Object to which to propagate the 190 * value. 191 * @exception IllegalActionException If the value cannot 192 * be propagated. 193 */ 194 @Override 195 protected void _propagateValue(NamedObj destination) 196 throws IllegalActionException { 197 propagateValueCount++; 198 super._propagateValue(destination); 199 } 200 201 /** Return the current counts. 202 * @return The current counts. 203 */ 204 public String getCounts() { 205 return "inferValueFromContextCount: " + inferValueFromContextCount 206 + "\nisSuppressingPropagationCount: " 207 + isSuppressingPropagationCount + "\nsetExpressionCount: " 208 + setExpressionCount + "\nsharedParameterSetCount: " 209 + sharedParameterSetCount + "\nvalidateCount: " + validateCount 210 + "\npropagateValueCount: " + propagateValueCount; 211 } 212 213 /////////////////////////////////////////////////////////////////// 214 //// public variables //// 215 216 public int inferValueFromContextCount; 217 218 public int isSuppressingPropagationCount; 219 220 public int setExpressionCount; 221 222 public int sharedParameterSetCount; 223 224 public int validateCount; 225 226 public int propagateValueCount; 227}