📄 addexpressionoperator.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.
*/
/*
*
* $Author$
* $Date$
* $Revision$
*
*
*/
package eti.bi.alphaminer.patch.standard.operation.operator;
import java.io.File;
import java.util.Vector;
import weka.filters.unsupervised.attribute.Add;
import weka.filters.unsupervised.attribute.AddExpression;
import com.prudsys.pdm.Core.MiningAttribute;
import com.prudsys.pdm.Core.MiningDataSpecification;
import com.prudsys.pdm.Core.MiningException;
import com.prudsys.pdm.Input.MiningStoredData;
import eti.bi.alphaminer.core.handler.ICaseHandler;
import eti.bi.alphaminer.core.transform.DataTransformAction;
import eti.bi.alphaminer.core.transform.StandardTransformAction;
import eti.bi.alphaminer.core.transform.WekaTransformAction;
import eti.bi.alphaminer.core.transform.filter.AgeParser;
import eti.bi.alphaminer.operation.operator.INodeInfo;
import eti.bi.alphaminer.operation.operator.Operator;
import eti.bi.alphaminer.operation.operator.TransformOperator;
import eti.bi.alphaminer.vo.BIData;
import eti.bi.alphaminer.vo.BIObject;
import eti.bi.alphaminer.vo.IBIData;
import eti.bi.alphaminer.vo.IOperatorNode;
import eti.bi.exception.AppException;
import eti.bi.exception.SysException;
/**
* TransformVariablesOperator is a kind of Operator
*/
public class AddExpressionOperator extends TransformOperator {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* @param a_CaseID
* @param a_CaseWindow
* @param aOperatorInfo
*/
public AddExpressionOperator(String a_CaseID, INodeInfo aNodeInfo, ICaseHandler aCaseHandler) {
super(a_CaseID, aNodeInfo, aCaseHandler);
// TODO Auto-generated constructor stub
}
/**
* Set node id and update operator text of the Add Expression at the same time.
* @param a_NodeID ID of the node
*/
public void setNodeID(String a_NodeID) {
setLabel(getDescription() + " [" + a_NodeID + "]");
super.setNodeID(a_NodeID);
}
/**
* Set node id and update operator text of the Add Expression at the same time.
* @param a_NodeID ID of the node
*/
public void setDescription(String a_Description) {
m_Description = a_Description;
setLabel(m_Description + " [" + m_NodeID + "]");
}
public boolean hasResult()
{
if (m_OutputBIObject != null)
{
return (m_OutputBIObject.hasResult(BIObject.DATA));
}else
{
return false;
}
}
public void execute(IOperatorNode a_OperatorNode, Vector a_Parents)
throws MiningException, SysException, AppException
{
/* Get input bi object from parent node */
Operator parentOp = (Operator)a_Parents.elementAt(0);
setInputBIObject(parentOp.getOutputBIObject());
IBIData aInputBIData = getInputBIObject().getBIData();
/* Get parameter from user input */
validateParameters(aInputBIData.getMetaData(),a_OperatorNode);
// String aTargetAttrName = a_OperatorNode.getParameterValue("target");
String expression = (String) a_OperatorNode.getParameterValue("expression");
/* Prepare output mining data */
//aOutputMiningStoredData.reset();
BIData aOutputBIData = new BIData(getCaseID(), getNodeID());
aOutputBIData.copyTransformActionHistory(aInputBIData.getTransformActionHistory());
/* Execute transform */
if(expression.toLowerCase().trim().startsWith("age")){
AgeParser ageParser = prepareAgeParser(aInputBIData.getMetaData(),a_OperatorNode);
StandardTransformAction action1 = new StandardTransformAction(m_CaseID, m_NodeID, ageParser);
MiningStoredData msd1 = null;
try{
msd1 = action1.transform(aInputBIData.getMiningStoredData());
}catch (Exception e) {
e.printStackTrace();
// if(e.getMessage().equals("Data transform Error."))
// throw new MiningException("Invalid expression format");
// else
throw new MiningException(e.getMessage());
}
aOutputBIData.addTransformActionHistory(action1);
aOutputBIData.setMiningStoredData(msd1);
// System.out.println(msd1);
}else if(expression.trim().equals("")){
File tempFile = aOutputBIData.getTempFile();
Add addAttr = prepareAdd(aInputBIData.getMetaData(),a_OperatorNode);
DataTransformAction action2 = new WekaTransformAction(m_CaseID, m_NodeID, tempFile.getAbsolutePath(), addAttr);
MiningStoredData msd2 = null;
try {
msd2 = action2.transform(aInputBIData.getMiningStoredData());
} catch (Exception e) {
e.printStackTrace();
if(e.getMessage().equals("Data transform Error."))
throw new MiningException("Invalid expression format");
else throw new MiningException(e.getMessage());
}
aOutputBIData.addTransformActionHistory(action2);
aOutputBIData.setMiningStoredData(msd2);
}else{
File tempFile = aOutputBIData.getTempFile();
AddExpression addExpr = prepareAddExpression(aInputBIData.getMetaData(),a_OperatorNode);
DataTransformAction action3 = new WekaTransformAction(m_CaseID, m_NodeID, tempFile.getAbsolutePath(), addExpr);
MiningStoredData msd3 = null;
try {
msd3 = action3.transform(aInputBIData.getMiningStoredData());
} catch (Exception e) {
e.printStackTrace();
if(e.getMessage().equals("Data transform Error."))
throw new MiningException("Invalid expression format");
else throw new MiningException(e.getMessage());
}
aOutputBIData.addTransformActionHistory(action3);
aOutputBIData.setMiningStoredData(msd3);
}
/* Set Output Mining Data */
// MiningAttribute aTargetAttribute = (MiningAttribute) aOutputBIData.getMetaData().getMiningAttribute(aTargetAttrName);
// aOutputBIData.setTargetAttribute(aTargetAttribute);
aOutputBIData.copyTargetAttribute(aInputBIData.getTargetAttribute());
m_OutputBIObject.setBIData(aOutputBIData);
/* set run time parameter value to the node object (It needs to be stored in the BIML) */
a_OperatorNode.setParameterValue("Temporary data", aOutputBIData.getTempBIDataPath());
/* write temp data */
aOutputBIData.writeTempBIData();
}
private AddExpression prepareAddExpression(MiningDataSpecification a_MetaData, IOperatorNode a_Node)
throws MiningException
{
AddExpression addExpr = new AddExpression();
String expression = null;
String newAttr = null;
newAttr = (String) a_Node.getParameterValue("target");
expression = (String) a_Node.getParameterValue("expression");
addExpr.setName(newAttr);
addExpr.setExpression(expression);
return addExpr;
}
private AgeParser prepareAgeParser(MiningDataSpecification a_MetaData, IOperatorNode a_Node)
throws MiningException
{
AgeParser ageParser = new AgeParser();
String expression = null;
String temp;
int sourceAttrIndex = -1;
String sourceName = null;
String dateFormat = null;
String newAttr = null;
newAttr = (String) a_Node.getParameterValue("target");
expression = (String) a_Node.getParameterValue("expression");
temp = expression.trim().substring(3);
int bIndex = temp.indexOf("a");
int eIndex = temp.indexOf(",");
if(bIndex ==-1 || eIndex == -1)
throw new MiningException("Invalid expression format");
sourceAttrIndex = Integer.parseInt(temp.substring(bIndex+1, eIndex));
MiningAttribute[] attrs = a_MetaData.getAttributesArray();
if(sourceAttrIndex<=0 || sourceAttrIndex>attrs.length)
throw new MiningException("Invalid attribute: a"+sourceAttrIndex);
sourceName = attrs[sourceAttrIndex-1].getName();
temp = temp.substring(eIndex+1);
bIndex = temp.indexOf("\"");
eIndex = temp.lastIndexOf("\"");
if(bIndex ==-1 || eIndex == -1)
throw new MiningException("Invalid expression format");
temp = temp.substring(bIndex+1, eIndex);
dateFormat = temp;
ageParser.setSourceName(sourceName);
ageParser.setNewAttributeName(newAttr);
ageParser.setDateFormat(dateFormat);
return ageParser;
}
private Add prepareAdd(MiningDataSpecification a_MetaData, IOperatorNode a_Node)
throws MiningException
{
Add addAttr = new Add();
String newAttr = null;
newAttr = (String) a_Node.getParameterValue("target");
addAttr.setAttributeName(newAttr);
addAttr.setAttributeIndex("last");
return addAttr;
}
public void clearResult(){
super.clearResult();
try {
IOperatorNode On = m_CaseHandler.getOperatorNode(m_CaseID,m_NodeID);
On.setParameterValue("Varindex", "0");
} catch (Exception be)
{
System.err.println(be.getMessage());
}
}
private void validateParameters(MiningDataSpecification a_MetaData, IOperatorNode a_Node) throws AppException {
String expression = null;
String targetName = null;
String message = "";
@SuppressWarnings("unused") MiningAttribute mAtt = null;
targetName = (String) a_Node.getParameterValue("target");
if(targetName == null){
message += "New attribute should not be empty\n";
throw new AppException(message);
}
MiningAttribute [] attributes = a_MetaData.getAttributesArray();
for(int i=0; i<attributes.length; i++){
if(attributes[i].getName().equals(targetName)){
message += "New attribute cannot be any existing attribute\n";
throw new AppException(message);
}
}
expression = (String) a_Node.getParameterValue("expression");
if(expression == null){
message += "Expression should not be empty\n";
throw new AppException(message);
}
// if(!message.equals("")){
// MessageDialog.showWarning(message, "Invalid input value");
// throw new AppException(message);
// }
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -