Package org.stringtemplate.v4.test

Examples of org.stringtemplate.v4.test.BaseTest


          new DebugInterpreter(outputFileST.groupThatCreatedThisInstance,
              outputFileST.impl.nativeGroup.errMgr,
              debug);
      InstanceScope scope = new InstanceScope(null, outputFileST);
      StringWriter sw = new StringWriter();
      AutoIndentWriter out = new AutoIndentWriter(sw);
      interp.exec(out, scope);

      for (String e : interp.evals) {
        if (e.contains(pattern)) {
          evals.add(e);
View Full Code Here


  public void write(ST code, String fileName) {
    try {
//      long start = System.currentTimeMillis();
      Writer w = tool.getOutputFileWriter(g, fileName);
      STWriter wr = new AutoIndentWriter(w);
      wr.setLineWidth(lineWidth);
      code.write(wr);
      w.close();
//      long stop = System.currentTimeMillis();
    }
    catch (IOException ioe) {
View Full Code Here

      boolean debug = false;
      DebugInterpreter interp =
          new DebugInterpreter(outputFileST.groupThatCreatedThisInstance,
              outputFileST.impl.nativeGroup.errMgr,
              debug);
      InstanceScope scope = new InstanceScope(null, outputFileST);
      StringWriter sw = new StringWriter();
      AutoIndentWriter out = new AutoIndentWriter(sw);
      interp.exec(out, scope);

      for (String e : interp.evals) {
View Full Code Here

public class STModelAdaptor implements ModelAdaptor {
  public Object getProperty(Interpreter interp, ST self, Object o, Object property, String propertyName)
    throws STNoSuchPropertyException
  {
    ST st = (ST)o;
    return st.getAttribute(propertyName);
  }
View Full Code Here

      //System.out.println(event+"=="+((Wrapper)o).event+" is "+(this.event == ((Wrapper)o).event));
      return this.event == ((Wrapper)o).event;
    }

    public String toString() {
      ST st = event.scope.st;
      if ( st.isAnonSubtemplate() ) return "{...}";
      if ( st.debugState!=null && st.debugState.newSTEvent!=null ) {
        return st.toString()+" @ "+st.debugState.newSTEvent.getFileName()+":"+
             st.debugState.newSTEvent.getLine();
      }
      else {
        return st.toString();
      }
    }
View Full Code Here

        ((FSDataOutputStream) outStream).close();
        outStream = null;
        return 0;
      }

      ST createTab_stmt = new ST("CREATE <" + EXTERNAL + "> TABLE " +
          tableName + "(\n" +
          "<" + LIST_COLUMNS + ">)\n" +
          "<" + TBL_COMMENT + ">\n" +
          "<" + LIST_PARTITIONS + ">\n" +
          "<" + SORT_BUCKET + ">\n" +
          "<" + ROW_FORMAT + ">\n" +
          "LOCATION\n" +
          "<" + TBL_LOCATION + ">\n" +
          "TBLPROPERTIES (\n" +
          "<" + TBL_PROPERTIES + ">)\n");

      // For cases where the table is external
      String tbl_external = "";
      if (tbl.getTableType() == TableType.EXTERNAL_TABLE) {
        duplicateProps.add("EXTERNAL");
        tbl_external = "EXTERNAL";
      }

      // Columns
      String tbl_columns = "";
      List<FieldSchema> cols = tbl.getCols();
      List<String> columns = new ArrayList<String>();
      for (FieldSchema col : cols) {
        String columnDesc = "  " + col.getName() + " " + col.getType();
        if (col.getComment() != null) {
          columnDesc = columnDesc + " COMMENT '" + escapeHiveCommand(col.getComment()) + "'";
        }
        columns.add(columnDesc);
      }
      tbl_columns = StringUtils.join(columns, ", \n");

      // Table comment
      String tbl_comment = "";
      String tabComment = tbl.getProperty("comment");
      if (tabComment != null) {
        duplicateProps.add("comment");
        tbl_comment = "COMMENT '" + escapeHiveCommand(tabComment) + "'";
      }

      // Partitions
      String tbl_partitions = "";
      List<FieldSchema> partKeys = tbl.getPartitionKeys();
      if (partKeys.size() > 0) {
        tbl_partitions += "PARTITIONED BY ( \n";
        List<String> partCols = new ArrayList<String>();
        for (FieldSchema partKey : partKeys) {
          String partColDesc = "  " + partKey.getName() + " " + partKey.getType();
          if (partKey.getComment() != null) {
            partColDesc = partColDesc + " COMMENT '" +
                escapeHiveCommand(partKey.getComment()) + "'";
          }
          partCols.add(partColDesc);
        }
        tbl_partitions += StringUtils.join(partCols, ", \n");
        tbl_partitions += ")";
      }

      // Clusters (Buckets)
      String tbl_sort_bucket = "";
      List<String> buckCols = tbl.getBucketCols();
      if (buckCols.size() > 0) {
        duplicateProps.add("SORTBUCKETCOLSPREFIX");
        tbl_sort_bucket += "CLUSTERED BY ( \n  ";
        tbl_sort_bucket += StringUtils.join(buckCols, ", \n  ");
        tbl_sort_bucket += ") \n";
        List<Order> sortCols = tbl.getSortCols();
        if (sortCols.size() > 0) {
          tbl_sort_bucket += "SORTED BY ( \n";
          // Order
          List<String> sortKeys = new ArrayList<String>();
          for (Order sortCol : sortCols) {
            String sortKeyDesc = "  " + sortCol.getCol() + " ";
            if (sortCol.getOrder() == BaseSemanticAnalyzer.HIVE_COLUMN_ORDER_ASC) {
              sortKeyDesc = sortKeyDesc + "ASC";
            }
            else if (sortCol.getOrder() == BaseSemanticAnalyzer.HIVE_COLUMN_ORDER_DESC) {
              sortKeyDesc = sortKeyDesc + "DESC";
            }
            sortKeys.add(sortKeyDesc);
          }
          tbl_sort_bucket += StringUtils.join(sortKeys, ", \n");
          tbl_sort_bucket += ") \n";
        }
        tbl_sort_bucket += "INTO " + tbl.getNumBuckets() + " BUCKETS";
      }

      // Row format (SerDe)
      String tbl_row_format = "";
      StorageDescriptor sd = tbl.getTTable().getSd();
      SerDeInfo serdeInfo = sd.getSerdeInfo();
      tbl_row_format += "ROW FORMAT";
      if (tbl.getStorageHandler() == null) {
        if (serdeInfo.getParametersSize() > 1) {
          // There is a "serialization.format" property by default,
          // even with a delimited row format.
          // But our result will only cover the following four delimiters.
          tbl_row_format += " DELIMITED \n";
          Map<String, String> delims = serdeInfo.getParameters();
          // Warn:
          // If the four delimiters all exist in a CREATE TABLE query,
          // this following order needs to be strictly followed,
          // or the query will fail with a ParseException.
          if (delims.containsKey(serdeConstants.FIELD_DELIM)) {
            tbl_row_format += "  FIELDS TERMINATED BY '" +
                escapeHiveCommand(StringEscapeUtils.escapeJava(delims.get(
                serdeConstants.FIELD_DELIM))) + "' \n";
          }
          if (delims.containsKey(serdeConstants.COLLECTION_DELIM)) {
            tbl_row_format += "  COLLECTION ITEMS TERMINATED BY '" +
                escapeHiveCommand(StringEscapeUtils.escapeJava(delims.get(
                serdeConstants.COLLECTION_DELIM))) + "' \n";
          }
          if (delims.containsKey(serdeConstants.MAPKEY_DELIM)) {
            tbl_row_format += "  MAP KEYS TERMINATED BY '" +
                escapeHiveCommand(StringEscapeUtils.escapeJava(delims.get(
                serdeConstants.MAPKEY_DELIM))) + "' \n";
          }
          if (delims.containsKey(serdeConstants.LINE_DELIM)) {
            tbl_row_format += "  LINES TERMINATED BY '" +
                escapeHiveCommand(StringEscapeUtils.escapeJava(delims.get(
                serdeConstants.LINE_DELIM))) + "' \n";
          }
        }
        else {
          tbl_row_format += " SERDE \n  '" +
              escapeHiveCommand(serdeInfo.getSerializationLib()) + "' \n";
        }
        tbl_row_format += "STORED AS INPUTFORMAT \n  '" +
            escapeHiveCommand(sd.getInputFormat()) + "' \n";
        tbl_row_format += "OUTPUTFORMAT \n  '" +
            escapeHiveCommand(sd.getOutputFormat()) + "'";
      }
      else {
        duplicateProps.add(org.apache.hadoop.hive.metastore.api.hive_metastoreConstants.META_TABLE_STORAGE);
        tbl_row_format += " SERDE \n  '" +
            escapeHiveCommand(serdeInfo.getSerializationLib()) + "' \n";
        tbl_row_format += "STORED BY \n  '" + escapeHiveCommand(tbl.getParameters().get(
            org.apache.hadoop.hive.metastore.api.hive_metastoreConstants.META_TABLE_STORAGE)) + "' \n";
        // SerDe Properties
        if (serdeInfo.getParametersSize() > 0) {
          tbl_row_format += "WITH SERDEPROPERTIES ( \n";
          List<String> serdeCols = new ArrayList<String>();
          for (Map.Entry<String, String> entry : serdeInfo.getParameters().entrySet()) {
            serdeCols.add("  '" + entry.getKey() + "'='"
                + escapeHiveCommand(StringEscapeUtils.escapeJava(entry.getValue())) + "'");
          }
          tbl_row_format += StringUtils.join(serdeCols, ", \n");
          tbl_row_format += ")";
        }
      }
      String tbl_location = "  '" + escapeHiveCommand(sd.getLocation()) + "'";

      // Table properties
      String tbl_properties = "";
      Map<String, String> properties = tbl.getParameters();
      if (properties.size() > 0) {
        List<String> realProps = new ArrayList<String>();
        for (String key : properties.keySet()) {
          if (properties.get(key) != null && !duplicateProps.contains(key)) {
            realProps.add("  '" + key + "'='" +
                escapeHiveCommand(StringEscapeUtils.escapeJava(properties.get(key))) + "'");
          }
        }
        tbl_properties += StringUtils.join(realProps, ", \n");
      }

      createTab_stmt.add(EXTERNAL, tbl_external);
      createTab_stmt.add(LIST_COLUMNS, tbl_columns);
      createTab_stmt.add(TBL_COMMENT, tbl_comment);
      createTab_stmt.add(LIST_PARTITIONS, tbl_partitions);
      createTab_stmt.add(SORT_BUCKET, tbl_sort_bucket);
      createTab_stmt.add(ROW_FORMAT, tbl_row_format);
      createTab_stmt.add(TBL_LOCATION, tbl_location);
      createTab_stmt.add(TBL_PROPERTIES, tbl_properties);

      outStream.writeBytes(createTab_stmt.render());
      ((FSDataOutputStream) outStream).close();
      outStream = null;
    } catch (FileNotFoundException e) {
      LOG.info("show create table: " + stringifyException(e));
      return 1;
View Full Code Here

    @Override
    public ST genExpr(CodeGenerator generator,
                    STGroup templates,
                    DFA dfa)
    {
      ST eST;
      if ( templates!=null ) {
        if ( synpred ) {
          eST = templates.getInstanceOf("evalSynPredicate");
        }
        else {
          eST = templates.getInstanceOf("evalPredicate");
          generator.grammar.decisionsWhoseDFAsUsesSemPreds.add(dfa);
        }
        String predEnclosingRuleName = predicateAST.enclosingRuleName;
        /*
        String decisionEnclosingRuleName =
          dfa.getNFADecisionStartState().getEnclosingRule();
        // if these rulenames are diff, then pred was hoisted out of rule
        // Currently I don't warn you about this as it could be annoying.
        // I do the translation anyway.
        */
        //eST.add("pred", this.toString());
        if ( generator!=null ) {
          eST.add("pred",
                   generator.translateAction(predEnclosingRuleName,predicateAST));
        }
      }
      else {
        eST = new ST("<pred>");
        eST.add("pred", this.toString());
        return eST;
      }
      if ( generator!=null ) {
        String description =
          generator.target.getTargetStringLiteralFromString(this.toString());
        eST.add("description", description);
      }
      return eST;
    }
View Full Code Here

    }

    // now that we have computed list of specialStates, gen code for 'em
    for (int i = 0; i < specialStates.size(); i++) {
      DFAState ss = specialStates.get(i);
      ST stateST =
        generator.generateSpecialState(ss);
      specialStateSTs.add(stateST);
    }

    // check that the tables are not messed up by encode/decode
View Full Code Here

    public String getDOT(State startState) {
    if ( startState==null ) {
      return null;
    }
    // The output DOT graph for visualization
    ST dot;
    markedStates = new HashSet<Object>();
        if ( startState instanceof DFAState ) {
            dot = stlib.getInstanceOf("dfa");
      dot.add("startState",
          Utils.integer(startState.stateNumber));
      dot.add("useBox",
          Tool.internalOption_ShowNFAConfigsInDFA);
      walkCreatingDFADOT(dot, (DFAState)startState);
        }
        else {
            dot = stlib.getInstanceOf("nfa");
      dot.add("startState",
          Utils.integer(startState.stateNumber));
      walkRuleNFACreatingDOT(dot, startState);
        }
    dot.add("rankdir", rankdir);
        return dot.render();
    }
View Full Code Here

        }

    markedStates.add(Utils.integer(s.stateNumber)); // mark this node as completed.

        // first add this node
        ST st;
        if ( s.isAcceptState() ) {
            st = stlib.getInstanceOf("stopstate");
        }
        else {
            st = stlib.getInstanceOf("state");
        }
        st.add("name", getStateLabel(s));
        dot.add("states", st);

        // make a DOT edge for each transition
    for (int i = 0; i < s.getNumberOfTransitions(); i++) {
      Transition edge = s.transition(i);
      /*
      System.out.println("dfa "+s.dfa.decisionNumber+
        " edge from s"+s.stateNumber+" ["+i+"] of "+s.getNumberOfTransitions());
      */
      if ( STRIP_NONREDUCED_STATES ) {
        if ( edge.target instanceof DFAState &&
          ((DFAState)edge.target).getAcceptStateReachable()!=DFA.REACHABLE_YES )
        {
          continue; // don't generate nodes for terminal states
        }
      }
      st = stlib.getInstanceOf("edge");
      st.add("label", getEdgeLabel(edge));
      st.add("src", getStateLabel(s));
            st.add("target", getStateLabel(edge.target));
      st.add("arrowhead", arrowhead);
            dot.add("edges", st);
            walkCreatingDFADOT(dot, (DFAState)edge.target); // keep walkin'
        }
    }
View Full Code Here

TOP

Related Classes of org.stringtemplate.v4.test.BaseTest

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.