⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 binerizationoperator.java

📁 一个数据挖掘软件ALPHAMINERR的整个过程的JAVA版源代码
💻 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.
 */

package eti.bi.alphaminer.patch.standard.operation.operator;


import java.util.Vector;


import weka.core.Range;

import com.prudsys.pdm.Core.CategoricalAttribute;
import com.prudsys.pdm.Core.Category;
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 com.prudsys.pdm.Transform.MiningTransformationFactory;
import com.prudsys.pdm.Transform.MiningTransformationStep;
import com.prudsys.pdm.Transform.OneToMultiple.Binning;
import com.prudsys.pdm.Transform.Special.CopyMiningStream;

import eti.bi.alphaminer.core.handler.ICaseHandler;
import eti.bi.alphaminer.core.transform.XelopesTransformAction;
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.BIModel;
import eti.bi.alphaminer.vo.BIObject;
import eti.bi.alphaminer.vo.IBIData;
import eti.bi.alphaminer.vo.IBIModel;
import eti.bi.alphaminer.vo.IOperatorNode;
import eti.bi.exception.AppException;
import eti.bi.exception.SysException;
import eti.bi.common.Locale.Resource;
/**
 * ReplacementOperator is a kind of Operator
 */
public class BinerizationOperator extends TransformOperator {

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;


	/**
	 * @param a_CaseID
	 * @param a_CaseWindow
	 * @param aOperatorInfo
	 */
	public BinerizationOperator(String a_CaseID, INodeInfo aNodeInfo, ICaseHandler aCaseHandler) {
		super(a_CaseID, aNodeInfo, aCaseHandler);
		// TODO Auto-generated constructor stub
	}

	boolean m_Binning = true;


	/**
	 * Set node id and update operator text of the Binerization 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 Binerization 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 + "]");		
	}
	
    /*
     * (non-Javadoc)
     * 
     * @see eti.bi.alphaminer.ui.operator.Operator#hasResult()
     */
    public boolean hasResult() {
        /*
         * Normally the operator has to check if the output data object and/or
         * the output model object are exist by calling
         * m_OutputBIObject.hasResult(int) by passing BIObject.DATA and
         * BIObject.MODEL respectively. You may check both existance or either
         * one by simple modifying the logic below.
         */
        if (m_OutputBIObject != null) {
            return (m_OutputBIObject.hasResult(BIObject.DATA)); //&&
            //   					m_OutputBIObject.hasResult(BIObject.MODEL));
        } else {
            return false;
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see eti.bi.alphaminer.ui.operator.Operator#execute(eti.bi.alphaminer.vo.OperatorNode,
     *      java.util.Vector)
     */
    public void execute(IOperatorNode a_OperatorNode, Vector a_Parents)
            throws MiningException, AppException, SysException {

         /* 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");
        
        
        /* Prepare output data model Initial the output BIData and/or BIModel before doing the execution */
        IBIData aOutputBIData = new BIData(getCaseID(), getNodeID());
        @SuppressWarnings("unused") IBIModel aOutputBIModel = new BIModel(getCaseID(), getNodeID(), IBIModel.TYPE_CLASSIFIER);
        aOutputBIData.copyTransformActionHistory(aInputBIData.getTransformActionHistory());

        /* Execute transform */
        if(m_Binning){
	        MiningTransformationFactory mtf = new MiningTransformationFactory();
	        Binning binning = prepareBinning(aInputBIData.getMetaData(), a_OperatorNode);
	        mtf.addOneToMultipleMapping(binning);
	        MiningTransformationStep mts = mtf.createMiningTransformationStep();
	        XelopesTransformAction aTransformAction = new XelopesTransformAction(
	                m_CaseID, m_NodeID, mts);
	        MiningStoredData aOutputMiningStoredData = aTransformAction
	                .transform(aInputBIData.getMiningStoredData());
	        aOutputBIData.setMiningStoredData(aOutputMiningStoredData);
	        aOutputBIData.addTransformActionHistory(aTransformAction);
        
        }else{
        
	        MiningStoredData msd2 = new MiningStoredData();
	        (new CopyMiningStream()).transform(aInputBIData.getMiningStoredData(), msd2);
	//		MiningStoredData msd2 = action4.transform(aInputBIData.getMiningStoredData());
			aOutputBIData.setMiningStoredData(msd2);
        }
        /* 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();

    }

    @SuppressWarnings("unchecked")
	public Binning prepareBinning(MiningDataSpecification a_MetaData,
            IOperatorNode a_Node) throws MiningException {
        Binning binning = new Binning();

        String sourceName = null;
        String nominalIndex = null;
        sourceName = (String) a_Node.getParameterValue("target");
                   
        nominalIndex = (String) a_Node.getParameterValue("selection");
        
        MiningAttribute mAtt = a_MetaData.getMiningAttribute(sourceName);

        nominalIndex = parseSelection(nominalIndex);

        binning.setCategories(null);
        Range range = new Range(nominalIndex);
        range.setUpper(((CategoricalAttribute) mAtt).getCategoriesNumber());
        int[] selection = range.getSelection();
        Vector<Category> v = new Vector<Category>();
        for (int i = 0; i < selection.length; i++) {
            System.out.println(selection[i]);
            Category cat = ((CategoricalAttribute) mAtt)
                    .getCategory(selection[i]);
            v.add(cat);
        }

        binning.setFeatureName(sourceName);
        binning.setRemoveSourceAttributes(true);
        binning.setCategories(v);

        return binning;
    }

    private String parseSelection(String a_Selection) {
        String parsed = "";
        for (int i = 0; i < a_Selection.length(); i++) {
            if (a_Selection.charAt(i) == '1') {
                parsed += Integer.toString(i + 1) + ",";
            }
        }

        if (parsed.length()>0 && parsed.charAt(parsed.length() - 1) == ',') {
            parsed = parsed.substring(0, parsed.length() - 1);
        }
        return parsed;
    }

    private void validateParameters(MiningDataSpecification a_MetaData,
            IOperatorNode a_Node) throws AppException {
        String sourceName = null;
        String selection = null;
        String message = "";

        sourceName = (String) a_Node.getParameterValue("target");
        if (sourceName == null){
            message += Resource.srcStr("AttributeMessage");
            throw new AppException(message);
        }

        selection = (String) a_Node.getParameterValue("selection");
        if (selection == null){
            message += Resource.srcStr("SelectCategoryMessage");
            throw new AppException(message);
        }
        
        if (sourceName != null && selection != null) {
            MiningAttribute mAtt = a_MetaData.getMiningAttribute(sourceName);

            if (mAtt == null || !(mAtt instanceof CategoricalAttribute)) {
                message += Resource.srcStr("categoricalattributeMessage");
                throw new AppException(message);
                
            } else {
                if (selection.length() != ((CategoricalAttribute) mAtt)
                        .getCategoriesNumber()) {
                    message += Resource.srcStr("SelectCategoryMessage");
                    throw new AppException(message);
                    
                } else {
                    for (int i = 0; i < selection.length(); i++) {
                        if (selection.charAt(i) != '1'
                                && selection.charAt(i) != '0') {
                            message += Resource.srcStr("SelectCategoryMessage");
                            throw new AppException(message);
                           
                        }
                    }
                    
                    String nominalIndex = parseSelection(selection);
                    if (nominalIndex.length()<=0){
                        m_Binning = false;
                    }else 
                        m_Binning = true;
                }
            }

        }

//        if (!message.equals("")) {
//            MessageDialog.showWarning(message, "Invalid Parameters");
//            throw new AppException(message);
//        }
    }    
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -