001/* A class representing an evaluation scope that contains a set of named 002 constants. 003 004 Copyright (c) 2002-2014 The Regents of the University of California. 005 All rights reserved. 006 Permission is hereby granted, without written agreement and without 007 license or royalty fees, to use, copy, modify, and distribute this 008 software and its documentation for any purpose, provided that the above 009 copyright notice and the following two paragraphs appear in all copies 010 of this software. 011 012 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY 013 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 014 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF 015 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF 016 SUCH DAMAGE. 017 018 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, 019 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 020 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE 021 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF 022 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, 023 ENHANCEMENTS, OR MODIFICATIONS. 024 025 PT_COPYRIGHT_VERSION_2 026 COPYRIGHTENDKEY 027 028 029 */ 030package ptolemy.data.expr; 031 032import java.util.HashSet; 033import java.util.Map; 034import java.util.Set; 035 036import ptolemy.data.type.TypeConstant; 037 038////////////////////////////////////////////////////////////////////////// 039//// NamedConstantsScope 040 041/** 042 An implementation of ParserScope that contains a map from names to value 043 tokens. 044 045 @author Xiaojun Liu 046 @version $Id$ 047 @since Ptolemy II 2.1 048 @Pt.ProposedRating Red (liuxj) 049 @Pt.AcceptedRating Red (liuxj) 050 */ 051public class NamedConstantsScope implements ParserScope { 052 /** Construct a new scope that contains the given map from names to 053 * value tokens. 054 * @param map The map of names to values where the keys are Strings 055 * that name a variable and each key is a {@link ptolemy.data.Token}. 056 */ 057 public NamedConstantsScope(Map map) { 058 _map = map; 059 } 060 061 /** Look up and return the value with the specified name in the 062 * scope. Return null if the name is not defined in this scope. 063 * @param name The name of the variable to be looked up. 064 * @return The token associated with the given name in the scope. 065 */ 066 @Override 067 public ptolemy.data.Token get(String name) { 068 ptolemy.data.Token result = (ptolemy.data.Token) _map.get(name); 069 return result; 070 } 071 072 /** Look up and return the type of the value with the specified 073 * name in the scope. Return null if the name is not defined in 074 * this scope. 075 * @param name The name of the variable to be looked up. 076 * @return The token associated with the given name in the scope. 077 */ 078 @Override 079 public ptolemy.data.type.Type getType(String name) { 080 ptolemy.data.Token value = (ptolemy.data.Token) _map.get(name); 081 082 if (value == null) { 083 return null; 084 } else { 085 return value.getType(); 086 } 087 } 088 089 /** Look up and return the type term for the specified name 090 * in the scope. Return null if the name is not defined in this 091 * scope, or is a constant type. 092 * @param name The name of the variable to be looked up. 093 * @return The InequalityTerm associated with the given name in 094 * the scope. 095 */ 096 @Override 097 public ptolemy.graph.InequalityTerm getTypeTerm(String name) { 098 ptolemy.data.Token value = (ptolemy.data.Token) _map.get(name); 099 100 if (value == null) { 101 return null; 102 } else { 103 return new TypeConstant(value.getType()); 104 } 105 } 106 107 /** Return the set of identifiers defined in this scope. 108 * @return A set containing the key defined in the map. 109 */ 110 @Override 111 public Set identifierSet() { 112 Set set = new HashSet(); 113 set.addAll(_map.keySet()); 114 return set; 115 } 116 117 private Map _map; 118}