Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   *    This program is free software; you can redistribute it and/or modify
   *    it under the terms of the GNU General Public License as published by
   *    the Free Software Foundation; either version 2 of the License, or
   *    (at your option) any later version.
   *
   *    This program is distributed in the hope that it will be useful,
   *    but WITHOUT ANY WARRANTY; without even the implied warranty of
   *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  *    GNU General Public License for more details.
  *
  *    You should have received a copy of the GNU General Public License
  *    along with this program; if not, write to the Free Software
  *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
 
 /*
  *    ArffOutputMeta.java
  *    Copyright 2007 Pentaho Corporation.  All rights reserved. 
  *
  */
 
 package org.pentaho.di.arff;
 
 import java.util.List;
 import java.util.Map;
 
 import  org.pentaho.di.core.CheckResult;
 import  org.pentaho.di.core.CheckResultInterface;
 import  org.pentaho.di.core.Const;
 import  org.pentaho.di.core.Counter;
 import  org.pentaho.di.core.annotations.Step;
 import  org.pentaho.di.core.database.DatabaseMeta;
 import  org.pentaho.di.core.exception.KettleException;
 import  org.pentaho.di.core.exception.KettleStepException;
 import  org.pentaho.di.core.exception.KettleXMLException;
 import  org.pentaho.di.core.row.RowMetaInterface;
 import  org.pentaho.di.core.row.ValueMetaInterface;
 import  org.pentaho.di.core.variables.VariableSpace;
 import  org.pentaho.di.core.xml.XMLHandler;
 import  org.pentaho.di.repository.ObjectId;
 import  org.pentaho.di.repository.Repository;
 import  org.pentaho.di.trans.Trans;
 import  org.pentaho.di.trans.TransMeta;
 import  org.pentaho.di.trans.step.BaseStepMeta;
 import  org.pentaho.di.trans.step.StepDataInterface;
 import  org.pentaho.di.trans.step.StepInterface;
 import  org.pentaho.di.trans.step.StepMeta;
 import  org.pentaho.di.trans.step.StepMetaInterface;
 import  org.pentaho.dm.commons.ArffMeta;
Contains the meta data for the ArffOutput step.

Author(s):
Mark Hall (mhall{[at]}pentaho.org)
Version:
1.0
 
 @Step(id = "ArffOutput", image = "AO.png", name = "Arff Output", description = "Writes data in ARFF format to a file", categoryDescription = "Data Mining")
 public class ArffOutputMeta extends BaseStepMeta implements StepMetaInterface {
 
   protected static Class<?> PKG = ArffOutputMeta.class;
 
   // Meta data for the output fields
   protected ArffMeta[] m_outputFields;
 
   // Filename for the ARFF file
   protected String m_fileName;
 
   // Character encoding to use (if not default encoding)
   protected String m_encoding;
 
   // Newline character sequence
   protected String m_newLine;
 
   // File format (DOS/UNIX)
   protected String m_fileFormat;
 
   // Relation name line for the ARFF file
   protected String m_relationName;
 
   // Output instances in sparse format
   protected boolean m_outputSparseInstances;

  
The anme of the field to use to set the weights. Null indicates no weight setting (i.e. equal weights)
 
   protected String m_weightField;

  
Creates a new ArffOutputMeta instance.
 
   public ArffOutputMeta() {
     super(); // allocate BaseStepMeta
     setDefault();
   }

  
Allocate an array to hold meta data for the output fields

Parameters:
num number of meta data objects to allocate
  public void allocate(int num) {
     = new ArffMeta[num];
  }

  
Set the name of the field to use to set instance weights from.

Parameters:
wfn the name of the field to use to set instance weights or null for equal weights.
  public void setWeightFieldName(String wfn) {
     = wfn;
  }

  
Gets the name of the field to use to set instance weights.

Returns:
the name of the field to use or null if all instances are to receive equal weights
  public String getWeightFieldName() {
    return ;
  }

  
Set whether to output instances in sparse format

Parameters:
o true if instances are to be output in sparse format
  public void setOutputSparseIntsances(boolean o) {
  }

  
Get whether to output instances in sparse format

Returns:
true if instances are to be output in sparse format
  public boolean getOutputSparseInstance() {
  }

  
Set the relation name to use

Parameters:
r the relation name
  public void setRelationName(String r) {
     = r;
  }

  
Get the relation name for the current ARFF file

Returns:
the relation name
  public String getRelationName() {
    return ;
  }

  
Set the filename to use

Parameters:
fn the filename to use
  public void setFileName(String fn) {
     = fn;
  }

  
Get the filename of the current ARFF file

Returns:
the filename
  public String getFileName() {
    return ;
  }

  
Set the character encoding to use

Parameters:
e the character encoding to use
  public void setEncoding(String e) {
     = e;
  }

  
Get the character encoding in use

Returns:
a String value
  public String getEncoding() {
    return ;
  }

  
Set the file format to use (DOS/UNIX)

Parameters:
ff the file format to use
  public void setFileFormat(String ff) {
     = ff;
  }

  
Get the file format in use

Returns:
return the file format as a String (DOS or UNIX)
  public String getFileFormat() {
    return ;
  }

  
Set the array of meta data for the output fields

Parameters:
am an array of ArffMeta
  public void setOutputFields(ArffMeta[] am) {
     = am;
  }

  
Get the meta data for the output fields

Returns:
an array of ArffMeta
  public ArffMeta[] getOutputFields() {
    return ;
  }

  
Sets up the ArffMeta array based on the incomming Kettle row format.

Parameters:
rmi a RowMetaInterface value
  public void setupArffMeta(RowMetaInterface rmi) {
    if (rmi != null) {
      allocate(rmi.size());
      // initialize the output fields to all incoming fields with
      // corresponding arff types
      for (int i = 0; i < .i++) {
        ValueMetaInterface inField = rmi.getValueMeta(i);
        int fieldType = inField.getType();
        switch (fieldType) {
        case ValueMetaInterface.TYPE_NUMBER:
        case ValueMetaInterface.TYPE_INTEGER:
        case ValueMetaInterface.TYPE_BOOLEAN:
          [i] = new ArffMeta(inField.getName(), fieldType,
              ArffMeta.NUMERIC);
          break;
        case ValueMetaInterface.TYPE_STRING:
          [i] = new ArffMeta(inField.getName(), fieldType,
              ArffMeta.NOMINAL);
          // check for indexed values
          if (inField.getStorageType() == ValueMetaInterface.STORAGE_TYPE_INDEXED) {
            Object[] legalVals = inField.getIndex();
            StringBuffer temp = new StringBuffer();
            boolean first = true;
            for (Object l : legalVals) {
              if (first) {
                temp.append(l.toString().trim());
                first = false;
              } else {
                temp.append(",").append(l.toString().trim());
              }
            }
            [i].setNominalVals(temp.toString());
          }
          // -1);
          break;
        case ValueMetaInterface.TYPE_DATE:
          [i] = new ArffMeta(inField.getName(), fieldType,
              ArffMeta.DATE);
          [i].setDateFormat(inField.getDateFormat().toPattern());
          break;
        }
      }
    }
  }

  
Return the XML describing this (configured) step

Returns:
a String containing the XML
  public String getXML() {
    StringBuffer retval = new StringBuffer(100);
    if ( != null) {
      retval.append(XMLHandler.addTagValue("arff_file_name"));
      retval.append(XMLHandler.addTagValue("relation_name"));
      retval.append(XMLHandler.addTagValue("file_format"));
      retval.append(XMLHandler.addTagValue("encoding"));
      retval.append(XMLHandler.addTagValue("output_sparse_instances",
          ));
      if (!Const.isEmpty()) {
        retval.append(XMLHandler.addTagValue("weight_field"));
      }
      retval.append("    <arff>" + Const.CR);
      if ( != null) {
        for (int i = 0; i < .i++) {
          if ([i] != null) {
            retval.append("        ").append([i].getXML())
                .append(Const.CR);
          }
        }
      }
      retval.append("    </arff>" + Const.CR);
    }
    return retval.toString();
  }

  
Loads the meta data for this (configured) step from XML.

Parameters:
stepnode the step to load
Throws:
KettleXMLException if an error occurs
  public void loadXML(Node stepnodeList<DatabaseMeta> databases,
      Map<String, Counter> countersthrows KettleXMLException {
     = XMLHandler.getTagValue(stepnode"arff_file_name");
     = XMLHandler.getTagValue(stepnode"relation_name");
     = XMLHandler.getTagValue(stepnode"file_format");
     = XMLHandler.getTagValue(stepnode"encoding");
     = XMLHandler.getTagValue(stepnode"weight_field");
    String osi = XMLHandler.getTagValue(stepnode"output_sparse_instances");
    if (!Const.isEmpty(osi)) {
    }
     = getNewLine();
    Node fields = XMLHandler.getSubNode(stepnode"arff");
    int nrfields = XMLHandler.countNodes(fields, ArffMeta.XML_TAG);
    allocate(nrfields);
    for (int i = 0; i < nrfieldsi++) {
      Node fnode = XMLHandler.getSubNodeByNr(fields, ArffMeta.XML_TAG, i);
      [i] = new ArffMeta(fnode);
    }
  }

  
Read this step's configuration from a repository

Parameters:
rep the repository to access
id_step the id for this step
Throws:
KettleException if an error occurs
  public void readRep(Repository rep, ObjectId id_step,
      List<DatabaseMeta> databasesMap<String, Counter> counters)
      throws KettleException {
     = rep.getStepAttributeString(id_step, 0, "arff_file_name");
     = rep.getStepAttributeString(id_step, 0, "relation_name");
     = rep.getStepAttributeString(id_step, 0, "file_format");
     = rep.getStepAttributeString(id_step, 0, "encoding");
     = rep.getStepAttributeString(id_step, 0, "weight_field");
     = rep.getStepAttributeBoolean(id_step, 0,
        "output_sparse_instances");
     = getNewLine();
    int nrFields = rep.countNrStepAttributes(id_step"arff_field");
    allocate(nrFields);
    for (int i = 0; i < nrFieldsi++) {
      [i] = new ArffMeta(repid_stepi);
    }
  }

  
Save this step's meta data to a repository

Parameters:
rep the repository to save to
id_transformation transformation id
id_step step id
Throws:
KettleException if an error occurs
  public void saveRep(Repository rep, ObjectId id_transformation,
      ObjectId id_stepthrows KettleException {
    if ( != null) {
      rep.saveStepAttribute(id_transformationid_step, 0, "arff_file_name",
          );
      rep.saveStepAttribute(id_transformationid_step, 0, "relation_name",
          );
      rep.saveStepAttribute(id_transformationid_step, 0, "file_format",
          );
      rep.saveStepAttribute(id_transformationid_step, 0, "encoding",
          );
      rep.saveStepAttribute(id_transformationid_step, 0,
          "output_sparse_instances");
      if (!Const.isEmpty()) {
        rep.saveStepAttribute(id_transformationid_step, 0, "weight_field",
            );
      }
    }
    if ( != null) {
      for (int i = 0; i < .i++) {
        if ([i] != null) {
          [i].saveRep(repid_transformationid_stepi);
        }
      }
    }
  }

  
Generates row meta data to represent the fields output by this step

Parameters:
row the meta data for the output produced
origin the name of the step to be used as the origin
info The input rows metadata that enters the step through the specified channels in the same order as in method getInfoSteps(). The step metadata can then choose what to do with it: ignore it or not.
nextStep if this is a non-null value, it's the next step in the transformation. The one who's asking, the step where the data is targetted towards.
space not sure what this is :-)
Throws:
KettleStepException if an error occurs
  public void getFields(RowMetaInterface rowString origin,
      RowMetaInterface[] info, StepMeta nextStep, VariableSpace space)
      throws KettleStepException {
    // Nothing to do!
  }

  
Check the settings of this step and put findings in a remarks list.

Parameters:
remarks the list to put the remarks in. see org.pentaho.di.core.CheckResult
transmeta the transform meta data
stepMeta the step meta data
prev the fields coming from a previous step
input the input step names
output the output step names
info the fields that are used as information by the step
  public void check(List<CheckResultInterface> remarks, TransMeta transmeta,
      StepMeta stepMeta, RowMetaInterface prevString[] input,
      String[] output, RowMetaInterface info) {
    CheckResult cr;
    if ((prev == null) || (prev.size() == 0)) {
      cr = new CheckResult(CheckResult.TYPE_RESULT_WARNING,
          "Not receiving any fields from previous steps!"stepMeta);
      remarks.add(cr);
    } else {
      cr = new CheckResult(CheckResult.TYPE_RESULT_OK,
          "Step is connected to previous one, receiving " + prev.size()
              + " fields"stepMeta);
      remarks.add(cr);
    }
    // See if we have input streams leading to this step!
    if (input.length > 0) {
      cr = new CheckResult(CheckResult.TYPE_RESULT_OK,
          "Step is receiving info from other steps."stepMeta);
      remarks.add(cr);
    } else {
      cr = new CheckResult(CheckResult.TYPE_RESULT_ERROR,
          "No input received from other steps!"stepMeta);
      remarks.add(cr);
    }
  }

  
Check for equality

Parameters:
obj an Object to compare with
Returns:
true if equal to the supplied object
  public boolean equals(Object obj) {
    if (obj != null && (obj.getClass().equals(this.getClass()))) {
      ArffOutputMeta m = (ArffOutputMetaobj;
      return (getXML() == m.getXML());
    }
    return false;
  }

  
Clone this step's meta data

Returns:
the cloned meta data
  public Object clone() {
    ArffOutputMeta retval = (ArffOutputMetasuper.clone();
    if ( != null) {
      retval.allocate(.);
      for (int i = 0; i < .i++) {
        retval.getOutputFields()[i] = (ArffMeta) [i].clone();
      }
    } else {
      retval.allocate(0);
    }
    return retval;
  }

  
Set default values
  public void setDefault() {
     = "DOS";
     = "file";
     = "NewRelation";
     = null;
     = getNewLine();
  }
  protected String getNewLine() {
    String nl = System.getProperty("line.separator");
    if ( != null) {
      if (.equalsIgnoreCase("DOS")) {
        nl = "\r\n";
      } else if (.equalsIgnoreCase("UNIX")) {
        nl = "\n";
      }
    }
    return nl;
  }
  /*
   * (non-Javadoc)
   * 
   * @see org.pentaho.di.trans.step.BaseStepMeta#getDialogClassName()
   */
  public String getDialogClassName() {
    return "org.pentaho.di.arff.ArffOutputDialog";
  }

  
Get the executing step, needed by Trans to launch a step.

Parameters:
stepMeta the step info
stepDataInterface the step data interface linked to this step. Here the step can store temporary data, database connections, etc.
cnr the copy number to get.
tr the transformation info.
trans the launching transformation
Returns:
a StepInterface value
  public StepInterface getStep(StepMeta stepMeta,
      StepDataInterface stepDataInterfaceint cnr, TransMeta tr, Trans trans) {
    return new ArffOutput(stepMetastepDataInterfacecnrtrtrans);
  }

  
Get a new instance of the appropriate data class. This data class implements the StepDataInterface. It basically contains the persisting data that needs to live on, even if a worker thread is terminated.

Returns:
a StepDataInterface value
  public StepDataInterface getStepData() {
    return new ArffOutputData();
  }
New to GrepCode? Check out our FAQ X