001/* Bindings is used to represent (variable, value) pairs. 002 003 Copyright (c) 2003-2013 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_3 025 COPYRIGHTENDKEY 026 */ 027package ptolemy.moml.unit; 028 029import java.util.Hashtable; 030import java.util.Iterator; 031import java.util.LinkedHashSet; 032import java.util.Vector; 033 034import ptolemy.actor.IOPort; 035import ptolemy.kernel.ComponentEntity; 036 037/////////////////////////////////////////////////////////////////// 038//// Bindings 039 040/** 041 Represents a set of bindings. Each binding is a (variable, Unit) pair where 042 variable is a String. If the value of Unit is null, then the binding for the 043 variable exists but its value is null. Since null is an allowable value the 044 Hashtable class is not adequate. 045 046 @author Rowland R Johnson 047 @version $Id$ 048 @since Ptolemy II 8.0 049 @Pt.ProposedRating Red (rowland) 050 @Pt.AcceptedRating Red (rowland) 051 */ 052public class Bindings { 053 /** 054 * Construct Bindings with no members. 055 * 056 */ 057 public Bindings() { 058 super(); 059 } 060 061 /** 062 * Create bindings for a set of nodes in a CompositeEntity. The set of 063 * nodes can be a subset of the nodes in the CompositeEntity. Each port on 064 * each node yields a binding. 065 * 066 * @param nodes 067 * The set of nodes(ComponentEntities). 068 */ 069 public Bindings(Vector nodes) { 070 for (int i = 0; i < nodes.size(); i++) { 071 ComponentEntity actor = (ComponentEntity) nodes.elementAt(i); 072 Iterator iter = actor.portList().iterator(); 073 074 while (iter.hasNext()) { 075 IOPort actorPort = (IOPort) iter.next(); 076 String varLabel = actor.getName() + "." + actorPort.getName(); 077 put(varLabel, null); 078 } 079 } 080 } 081 082 /////////////////////////////////////////////////////////////////// 083 //// public methods //// 084 085 /** 086 * Return true if the binding exists. 087 * 088 * @param vLabel A String that represents the variable. 089 * @return True if there exists a binding for the variable. 090 */ 091 public boolean bindingExists(String vLabel) { 092 Iterator iter = _keys.iterator(); 093 094 while (iter.hasNext()) { 095 String key = (String) iter.next(); 096 097 if (key.equals(vLabel)) { 098 return true; 099 } 100 } 101 102 return false; 103 } 104 105 /** 106 * Get the value for a variable. 107 * 108 * @param vLabel A String that represents the variable. 109 * @return The value for the variable. 110 */ 111 public Unit get(String vLabel) { 112 Unit u = (Unit) _VarLabel2Unit.get(vLabel); 113 return u; 114 } 115 116 /** 117 * A human readable form (more or less) of the bindings. 118 */ 119 public String humanReadableForm() { 120 StringBuffer retv = new StringBuffer("Bindings\n"); 121 Iterator keys = _keys.iterator(); 122 123 while (keys.hasNext()) { 124 String varLabel = (String) keys.next(); 125 Unit unit = (Unit) _VarLabel2Unit.get(varLabel); 126 String unitExpr = "null"; 127 128 if (unit != null) { 129 unitExpr = unit.descriptiveForm(); 130 } 131 132 retv.append(" " + varLabel + " = " + unitExpr + "\n"); 133 } 134 135 retv.append("\\Bindings\n"); 136 return retv.toString(); 137 } 138 139 /** 140 * Create a binding for a variable and Unit. If a binding already exists 141 * for the variable, then update the Unit 142 * 143 * @param varLabel A String that represents the variable. 144 * @param U The Unit. 145 */ 146 public void put(String varLabel, Unit U) { 147 _keys.add(varLabel); 148 149 if (U != null) { 150 _VarLabel2Unit.put(varLabel, U); 151 } 152 } 153 154 /** 155 * Create an array of Strings that contains all of the variables. 156 * 157 * @return An array of Strings containing the variables. 158 */ 159 public String[] variableLabels() { 160 String[] retv = new String[_keys.size()]; 161 Iterator iter = _keys.iterator(); 162 int i = 0; 163 164 while (iter.hasNext()) { 165 retv[i++] = (String) iter.next(); 166 } 167 168 return retv; 169 } 170 171 /////////////////////////////////////////////////////////////////// 172 //// private variables //// 173 Hashtable _VarLabel2Unit = new Hashtable(); 174 175 LinkedHashSet _keys = new LinkedHashSet(); 176}