001/* 002 * Copyright (c) 2007-2010 The Regents of the University of California. 003 * All rights reserved. 004 * 005 * '$Author: crawl $' 006 * '$Date: 2010-07-26 17:45:03 +0000 (Mon, 26 Jul 2010) $' 007 * '$Revision: 25155 $' 008 * 009 * Permission is hereby granted, without written agreement and without 010 * license or royalty fees, to use, copy, modify, and distribute this 011 * software and its documentation for any purpose, provided that the above 012 * copyright notice and the following two paragraphs appear in all copies 013 * of this software. 014 * 015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY 016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF 018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF 019 * SUCH DAMAGE. 020 * 021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, 022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE 024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF 025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, 026 * ENHANCEMENTS, OR MODIFICATIONS. 027 * 028 */ 029 030package org.kepler.provenance; 031 032/** A set of Parameters used by provenance Recording types. 033 * 034 * @author Karen Schuchardt, Daniel Crawl 035 * @version $Id: RecordingParameters.java 25155 2010-07-26 17:45:03Z crawl $ 036 * 037 */ 038 039import java.util.Collection; 040import java.util.LinkedHashMap; 041import java.util.LinkedHashSet; 042import java.util.Map; 043 044import ptolemy.data.BooleanToken; 045import ptolemy.data.IntToken; 046import ptolemy.data.StringToken; 047import ptolemy.data.expr.FileParameter; 048import ptolemy.data.expr.Parameter; 049import ptolemy.data.expr.StringParameter; 050import ptolemy.data.type.BaseType; 051import ptolemy.kernel.util.Attribute; 052import ptolemy.kernel.util.IllegalActionException; 053import ptolemy.kernel.util.NameDuplicationException; 054import ptolemy.kernel.util.NamedObj; 055 056public class RecordingParameters 057{ 058 /** Create a new set of parameters. New Parameters will use no 059 * for their container. */ 060 public RecordingParameters(NamedObj no) 061 { 062 _dummy_no = no; 063 _params = new LinkedHashMap<String,Parameter>(); 064 } 065 066 /** Check if the set contains a specific name. */ 067 public boolean containsName(String name) 068 { 069 return _params.containsKey(name); 070 } 071 072 /** See if set contains a specific parameter. */ 073 public boolean containsParameter(Attribute attribute) 074 { 075 return _params.containsValue(attribute); 076 } 077 078 /** Get all the parameter name-value pairs. */ 079 public Map<String,String> getNamesValues() 080 { 081 LinkedHashMap<String,String> retval = new LinkedHashMap<String,String>(); 082 for(Map.Entry<String, Parameter> entry : _params.entrySet()) 083 { 084 retval.put(entry.getKey(), entry.getValue().getExpression()); 085 } 086 return retval; 087 } 088 089 /** Get all the names as a Collection. */ 090 public Collection<String> names() 091 { 092 // return a copy since of the names so that the caller 093 // can iterate and remove items. 094 // 095 // use a LinkedHashSet so the order is predictable 096 // (the parameter order in the config dialog is the same as 097 // the order they were added to _params). 098 return new LinkedHashSet<String>(_params.keySet()); 099 } 100 101 /** Replace a specific Parameter's container. */ 102 public void replaceContainer(String name, NamedObj no) 103 throws IllegalActionException, NameDuplicationException 104 { 105 Parameter p = _params.get(name); 106 p.setContainer(no); 107 } 108 109 /** Replace a specific Parameter. */ 110 public void replaceParameter(String name, Parameter parameter) 111 throws IllegalActionException 112 { 113 Parameter old = _params.remove(name); 114 _params.put(name, parameter); 115 116 // if the old parameter was a boolean, set the new one 117 // to be boolean so that it appears as a check box in 118 // the listener's config dialog. 119 if(old.getType() == BaseType.BOOLEAN) 120 { 121 parameter.setTypeEquals(BaseType.BOOLEAN); 122 } 123 124 // if the old parameter had several choices for its 125 // value, copy the choices to the new parameter. 126 String choices[] = old.getChoices(); 127 if(choices != null) 128 { 129 for(String choiceStr : choices) 130 { 131 parameter.addChoice(choiceStr); 132 } 133 } 134 } 135 136 /** Set the persistency of all parameters. */ 137 public void setPersistent(boolean persistent) 138 { 139 for(Parameter parameter : _params.values()) 140 { 141 parameter.setPersistent(persistent); 142 } 143 } 144 145 ////////////////////////////////////////////////////////////////////// 146 //// protected methods //// 147 148 /** Add an integer parameter. */ 149 protected void addIntParameter(String name, int value) 150 throws IllegalActionException, NameDuplicationException 151 { 152 Parameter p = new Parameter(_dummy_no, name, new IntToken(value)); 153 p.setTypeEquals(BaseType.INT); 154 _params.put(name, p); 155 } 156 157 /** Add a string parameter. */ 158 protected void addStringParameter(String name) 159 throws IllegalActionException, NameDuplicationException 160 { 161 addStringParameter(name, null); 162 } 163 164 /** Add a string parameter with default value. */ 165 protected void addStringParameter(String name, String value) 166 throws IllegalActionException, NameDuplicationException 167 { 168 Parameter p = new StringParameter(_dummy_no, name); 169 if(value != null) 170 { 171 p.setExpression(value); 172 } 173 _params.put(name, p); 174 } 175 176 /** Add a choice to a string parameter. */ 177 protected void addStringParameterChoice(String name, String choice) 178 throws IllegalActionException 179 { 180 Parameter p = _params.get(name); 181 if(p == null) 182 { 183 throw new IllegalActionException("Could not find parameter " + 184 "named " + name); 185 } 186 p.addChoice(choice); 187 } 188 189 /** Add a boolean parameter with a default value. */ 190 protected void addBooleanParameter(String name, boolean value) 191 throws IllegalActionException, NameDuplicationException 192 { 193 Parameter p = new Parameter(_dummy_no, name, new BooleanToken(value)); 194 p.setTypeEquals(BaseType.BOOLEAN); 195 _params.put(name, p); 196 } 197 198 /** Add a file parameter with default value. */ 199 protected void addFileParameter(String name, String value) 200 throws IllegalActionException, NameDuplicationException 201 { 202 Parameter p = new FileParameter(_dummy_no, name); 203 p.setExpression(value); 204 _params.put(name, p); 205 } 206 207 /** Get the value of a string parameter. */ 208 public String getStringValue(String name) 209 throws IllegalActionException 210 { 211 return ((StringToken)_params.get(name).getToken()).stringValue(); 212 } 213 214 /** Get the value of a boolean parameter. */ 215 protected Boolean getBooleanValue(String name) throws IllegalActionException 216 { 217 Parameter p = _params.get(name); 218 return ((BooleanToken)p.getToken()).booleanValue(); 219 } 220 221 ////////////////////////////////////////////////////////////////////// 222 //// private variables //// 223 224 /** The NamedObj container for new Parameters. */ 225 private NamedObj _dummy_no = null; 226 227 /** A mapping of parameter name to Parameter. */ 228 protected Map<String,Parameter> _params = null; 229}