001    package org.hackystat.telemetry.analyzer.language.ast;
002    
003    
004    import java.util.ArrayList;
005    import java.util.List;
006    
007    import org.hackystat.telemetry.analyzer.language.TelemetryLanguageException;
008    
009    /**
010     * Telemetry report definition, which contains one or more telemetry charts.
011     * 
012     * @author (Cedric) Qin ZHANG
013     * @version $Id$
014     */
015    public class TelemetryReportDefinition extends TelemetryDefinition {
016    
017      private String title;
018      private String docString;
019      private Variable[] variables; //List<Variable>
020      private List<ChartReference> chartReferences; 
021    
022      /**
023       * Constructs this instance.
024       * 
025       * @param name The name of the chart.
026       * @param docString The doc string of the report. 
027       *        The report title is extracted fromthe doc string.
028       * @param variables The variables used in the expression. Variables are essentially holding
029       *        places so that real value can be swapped in when the expression is evaluated.
030       *        Null is valid if there is no variable used in this definition.
031       * @param chartReferences A list of <code>ChartReference</code> objects referring to
032       *        the telemetry charts that should be contained in this report.
033       * @param textPosition The text position of the definition string in the input.
034       * 
035       * @throws TelemetryLanguageException If the variable list contains duplicated variable
036       *         declaration or does not declare all variables needed by the referred telemetry
037       *         charts.
038       */
039      public TelemetryReportDefinition(String name, String docString, Variable[] variables, 
040          List<ChartReference> chartReferences, TextPosition textPosition) 
041      throws TelemetryLanguageException {
042        
043        super(name, textPosition);
044    
045        this.docString = docString == null ? "" : docString;
046        int commaIndex = this.docString.indexOf('.');
047        this.title = commaIndex >= 0 ? this.docString.substring(0, commaIndex) : this.docString;
048        
049        this.variables = variables == null ? new Variable[0] : variables;
050        this.chartReferences = (chartReferences == null)
051        ? new ArrayList<ChartReference>() : chartReferences;
052    
053        // check whether all variable has unique name or not.
054        //TreeSet varNames = new TreeSet();
055    //    for (Iterator i = this.variables.iterator(); i.hasNext();) {
056    //      String varName = ((Variable) i.next()).getName();
057    //      if (varNames.contains(varName)) {
058    //        throw new Exception("Duplicated variable name declaration.");
059    //      }
060    //      else {
061    //        varNames.add(varName);
062    //      }
063    //    }
064    
065        // check whether all templates used in chartDefs are declared
066    //    for (Iterator i = this.streamsRefs.iterator(); i.hasNext();) {
067    //      NameReference ref = (NameReference) i.next();
068    //      for (Iterator j = ref.getParameters().iterator(); j.hasNext();) {
069    //        Object param = j.next();
070    //        if (param instanceof TemplatedParameter) {
071    //          if (!set.contains(param)) {
072    //            throw new Exception("Undeclared parameter '"
073    //                + ((TemplatedParameter) param).getTemplateName() + "' used.");
074    //          }
075    //        }
076    //      }
077    //    }
078      }
079    
080      /**
081       * Gets the title of the chart.
082       * 
083       * @return The chart title.
084       */
085      public String getTitle() {
086        return this.title;
087      }
088      
089      /**
090       * Gets the doc string for the chart.
091       * 
092       * @return The doc string.
093       */
094      public String getDocString() {
095        return this.docString;
096      }
097      
098      /**
099       * Gets an array of variables used in the definition.
100       * 
101       * @return An array of <code>Variable</code> objects. If there is no variable used,
102       *         then an empty array is returned.
103       */
104      public Variable[] getVariables() {
105        return this.variables;
106      }
107    
108      /**
109       * Gets all telemetry charts references in this report.
110       * 
111       * @return A list of <code>ChartReference</code> objects.
112       */
113      public List<ChartReference> getChartReferences() {
114        return this.chartReferences;
115      }
116    }