📄 wekatransformaction.java
字号:
/*
* 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.
*/
/*
* Created on 2005/1/6
*
* $Author$
* $Date$
* $Revision$
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package eti.bi.alphaminer.core.transform;
import java.io.FileWriter;
import java.io.IOException;
import weka.core.Instances;
import weka.filters.Filter;
import com.prudsys.pdm.Adapters.Weka.WekaCoreAdapter;
import com.prudsys.pdm.Core.MiningException;
import com.prudsys.pdm.Input.MiningInputStream;
import com.prudsys.pdm.Input.MiningStoredData;
import com.prudsys.pdm.Input.Records.Arff.MiningArffStream;
import eti.bi.exception.SysException;
/**
* @author kelvinj
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class WekaTransformAction extends DataTransformAction {
private Filter m_Filter;
private String m_TempFilePath;
public WekaTransformAction(String a_CaseID, String a_NodeID, String a_TempFilePath, Filter a_Filter)
{
super(a_CaseID, a_NodeID);
// m_Filter = new AddExpression();
// ((AddExpression)m_Filter).setExpression("a5/a6");
// ((AddExpression)m_Filter).setFieldName("NewField");
m_TempFilePath = a_TempFilePath;
m_Filter = a_Filter;
}
// public void setFilter(Filter a_Filter)
// {
//// m_Filter.setExpression(expression);
// m_Filter = a_Filter;
// }
public MiningStoredData transform(MiningStoredData inputMiningStoredData) throws SysException
{
Instances wekaInstance;
try {
inputMiningStoredData.reset();
wekaInstance = (Instances) WekaCoreAdapter.PDMMiningInputStream2WekaInstances(inputMiningStoredData);
m_Filter.setInputFormat(wekaInstance);
Instances m_TransformedInstance = Filter.useFilter(wekaInstance, m_Filter);
MiningStoredData outoutMiningStoredData = getMiningStoredDataFromInstance(m_TransformedInstance);
return outoutMiningStoredData;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
//<< tyleung 6/4/2005
// if (e.getClass() == java.lang.reflect.InvocationTargetException.class){
if(e.getClass().toString().equals("class java.lang.reflect.InvocationTargetException")) {
throw new SysException("Data transformation unsuccessful.\n" +
"Please verify the input data set.");
}else{
throw new SysException("Data transform Error.");
}
// tyleung 6/4/2005>>
}
}
/* Convert the Weka Instances object to Xelopes MiningStoredData object by writing to and reading from an arff file */
public MiningStoredData getMiningStoredDataFromInstance(Instances transformedInstance)
throws MiningException
{
try
{
FileWriter fwriter = new FileWriter(m_TempFilePath, false);
fwriter.write(transformedInstance.toString());
fwriter.close();
MiningInputStream inputData = new MiningArffStream(m_TempFilePath);
MiningStoredData miningStoredData = new MiningStoredData(inputData);
inputData.close();
return miningStoredData;
} catch (IOException ie)
{
throw new MiningException(ie.getMessage());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -