001    package org.hackystat.telemetry.analyzer.language.ast;
002    
003    import org.hackystat.telemetry.analyzer.language.TelemetryLanguageException;
004    
005    
006    /**
007     * Definition for telemetry "streams" object. A streams object consists of one or more telemetry
008     * streams.
009     * 
010     * @author (Cedric) Qin Zhang
011     * @version $Id$
012     */
013    public class TelemetryStreamsDefinition extends TelemetryDefinition {
014    
015      private String description;
016      private Expression expression;
017      private Variable[] variables;
018      
019      /**
020       * Constucts this instance.
021       * 
022       * @param name The name of this definition.
023       * @param expression The expression that defines this telemetry "streams" object.
024       * @param variables The variables used in the expression. Variables are essentially holding
025       *        places so that real value can be swapped in when the expression is evaluated.
026       *        Null is valid if there is no variable used in this definition.
027       * @param textPosition The text position of the definition string in the input.
028       * 
029       * @throws TelemetryLanguageException If the variable array contains duplicated variable
030       *         declaration or does not declare all variables needed by the expression.
031       */
032      public TelemetryStreamsDefinition(String name, Expression expression,
033          Variable[] variables, TextPosition textPosition) throws TelemetryLanguageException {
034        
035        super(name, textPosition);
036        this.expression = expression;
037        this.variables = variables == null ? new Variable[0] : variables;
038    
039        // check whether template list contains duplicated declaration
040        //HashSet set = new HashSet(); // don't use tree set, see TemplatedParameter
041        // impl.
042    //    for (Iterator iter = this.templatedParamters.iterator(); iter.hasNext();) {
043    //      TemplatedParameter param = (TemplatedParameter) iter.next();
044    //      if (set.contains(param)) {
045    //        throw new Exception("Duplicated parameter declaration.");
046    //      }
047    //      else {
048    //        set.add(param);
049    //      }
050    //    }
051    
052        // check whether template list contains all templates required by expression
053        //TODO: reimplement
054    //    int size = expression.size();
055    //    for (int i = 0; i < size; i++) {
056    //      ExpressionElement element = expression.getElement(i);
057    //      if (element instanceof VariableOperand) {
058    //        VariableOperand variable = (VariableOperand) element;
059    //        for (Iterator iter = variable.getParameters().iterator(); iter.hasNext();) {
060    //          Parameter param = (Parameter) iter.next();
061    //          if (param instanceof TemplatedParameter) {
062    //            if (!set.contains(param)) {
063    //              throw new Exception("Undeclared parameter '"
064    //                  + ((TemplatedParameter) param).getTemplateName() + "' used.");
065    //            }
066    //          }
067    //        }
068    //      }
069    //    }
070      }
071      
072      /**
073       * Gets the expression that defines this telemetry "streams" object.
074       * 
075       * @return The expression.
076       */
077      public Expression getExpression() {
078        return this.expression;
079      }
080    
081      /**
082       * Gets an array of variables used in the definition.
083       * 
084       * @return An array of <code>Varaible</code> objects. If there is no variable used,
085       *         then an empty array is returned.
086       */
087      public Variable[] getVariables() {
088        return this.variables;
089      }
090    
091      /**
092       * Gets the description of this telemetry "streams" object.
093       * 
094       * @return The description.
095       */
096      public String getDescription() {
097        return this.description;
098      }
099    
100      /**
101       * Sets the description.
102       * 
103       * @param description The description.
104       */
105      public void setDescription(String description) {
106        this.description = description;
107      }
108    }