001/* A class representing an explict evaluation scope.
002
003 Copyright (c) 2002-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.data.expr;
030
031import java.util.HashSet;
032import java.util.Iterator;
033import java.util.Set;
034
035import ptolemy.kernel.util.IllegalActionException;
036import ptolemy.kernel.util.NamedList;
037
038//////////////////////////////////////////////////////////////////////////
039//// ExplicitScope
040
041/**
042 An implementation of ParserScope that includes an explicit list of
043 Variables in the scope.
044
045 @author Steve Neuendorffer
046 @version $Id$
047 @since Ptolemy II 2.1
048 @Pt.ProposedRating Red (liuxj)
049 @Pt.AcceptedRating Red (liuxj)
050 */
051public class ExplicitScope implements ParserScope {
052    /** Construct a new scope that includes the objects in the given
053     *  list, which must contain only variables.
054     *  @param list The list of variables for the scope.
055     */
056    public ExplicitScope(NamedList list) {
057        _list = list;
058    }
059
060    /** Look up and return the value with the specified name in the
061     *  scope. Return null if the name is not defined in this scope.
062     *  @param name The name of the variable to be looked up.
063     *  @return The token associated with the given name in the scope.
064     *  @exception IllegalActionException If a value in the scope
065     *  exists with the given name, but cannot be evaluated.
066     */
067    @Override
068    public ptolemy.data.Token get(String name) throws IllegalActionException {
069        Variable variable = (Variable) _list.get(name);
070
071        if (variable == null) {
072            return null;
073        }
074
075        return variable.getToken();
076    }
077
078    /** Look up and return the type of the value with the specified
079     *  name in the scope. Return null if the name is not defined in
080     *  this scope.
081     *  @param name The name of the variable to be looked up.
082     *  @return The token associated with the given name in the scope.
083     *  @exception IllegalActionException If a value in the scope
084     *  exists with the given name, but cannot be evaluated.
085     */
086    @Override
087    public ptolemy.data.type.Type getType(String name)
088            throws IllegalActionException {
089        Variable variable = (Variable) _list.get(name);
090
091        if (variable == null) {
092            return null;
093        } else {
094            return variable.getType();
095        }
096    }
097
098    /** Look up and return the type term for the specified name
099     *  in the scope. Return null if the name is not defined in this
100     *  scope, or is a constant type.
101     *  @param name The name of the variable to be looked up.
102     *  @return The InequalityTerm associated with the given name in
103     *  the scope.
104     *  @exception IllegalActionException If a value in the scope
105     *  exists with the given name, but cannot be evaluated.
106     */
107    @Override
108    public ptolemy.graph.InequalityTerm getTypeTerm(String name)
109            throws IllegalActionException {
110        Variable variable = (Variable) _list.get(name);
111
112        if (variable == null) {
113            return null;
114        } else {
115            return variable.getTypeTerm();
116        }
117    }
118
119    /** Return the list of variables within the scope.
120     *  @return The list of variables within the scope.
121     */
122    @Override
123    public Set identifierSet() {
124        Set set = new HashSet();
125
126        for (Iterator variables = _list.elementList().iterator(); variables
127                .hasNext();) {
128            Variable variable = (Variable) variables.next();
129            set.add(variable.getName());
130        }
131
132        return set;
133    }
134
135    /** Return the list of variables in this scope.
136     *  @return The list of variables in this scope.
137     */
138    public NamedList variableList() {
139        return _list;
140    }
141
142    private NamedList _list;
143}