📄 cobwebclusteringoperator.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;
/**
* @author XiaoguangXu HITSZ-ICE
*
*/
import java.util.Vector;
import com.prudsys.pdm.Core.MiningAlgorithm;
import com.prudsys.pdm.Core.MiningAlgorithmSpecification;
import com.prudsys.pdm.Core.MiningException;
import com.prudsys.pdm.Core.MiningModel;
import com.prudsys.pdm.Models.Statistics.StatisticsAlgorithm;
import com.prudsys.pdm.Models.Statistics.StatisticsSettings;
import com.prudsys.pdm.Utils.GeneralUtils;
import eti.bi.alphaminer.core.handler.ICaseHandler;
import eti.bi.alphaminer.operation.operator.INodeInfo;
import eti.bi.alphaminer.operation.operator.Operator;
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.common.Locale.Resource;
import eti.bi.exception.SysException;
/**
* implementation of an cobweb clustering algorithm.
*
*/
public class CobwebClusteringOperator extends KMeansOperator {
/**
*
*/
private static final long serialVersionUID = 1456485389648465466L;
/** Parameter name for MiningSettingSpecification and MiningAlgorithm */
public static final String ALGORITHM_NAME = "Cobweb (Weka)";
private static String MAP_WEKA_CLASS_PARAMETERS = "wekaClassParameters";
// Parameter name for Cobweb Operator in BIML
public static String ACUITY = "acurity";
public static String CUT_OFF = "c";
public static String SAVE_INSTANCE_DATA = "saveInstanceData";
/* Default parameter value for Cobweb Operator */
public static String DEFAULT_ACUITY = String.valueOf(1.0);
public static String DEFAULT_CUT_OFF = String.valueOf(0.002);
public static String DEFAULT_SAVE_INSTANCE_DATA = "";
/* Parameter name for MiningSettingSpecification and MiningAlgorithm */
public CobwebClusteringOperator(String a_CaseID, INodeInfo aNodeInfo, ICaseHandler aCaseHandler) {
super(a_CaseID, aNodeInfo, aCaseHandler);
m_DefaultModelName = "Cobweb model";
}
public void setNodeID(String a_NodeID) {
setLabel(getDescription() + " [" + a_NodeID + "]");
setDefaultModelName("Cobweb_" + a_NodeID);
super.setNodeID(a_NodeID);
}
public void setDescription(String a_Description) {
m_Description = a_Description;
setLabel(m_Description + " [" + m_NodeID + "]");
setDefaultModelName("Cobweb_" + m_NodeID);
}
/**
* Test if the Clustering Operator contains any results.
*
* @return true if Clustering Operator has result; false otherwise.
*/
public boolean hasResult() {
if (m_OutputBIObject != null) {
return (m_OutputBIObject.hasResult(BIObject.DATA) && m_OutputBIObject.hasResult(BIObject.MODEL));
} else {
return false;
}
}
@SuppressWarnings("unchecked")
public void execute(IOperatorNode a_OperatorNode, Vector a_Parents) throws SysException, MiningException {
/* Get parameter from user input */
/*
* String saveInstanceData = a_OperatorNode .getParameterValue(SAME_INSTANCE_MODELS); if (saveInstanceData ==
* null) { saveInstanceData = DEFAULT_BUILD_LOG_MODELS; }
*/
String cobwebParameters;
String acuity = (String) a_OperatorNode.getParameterValue(ACUITY);
if (acuity == null) {
acuity = DEFAULT_ACUITY;
}
String cutoff = (String) a_OperatorNode.getParameterValue(CUT_OFF);
if (cutoff == null) {
cutoff = DEFAULT_CUT_OFF;
}
String saveInstanceData = (String) a_OperatorNode.getParameterValue(SAVE_INSTANCE_DATA);
if (saveInstanceData == null) {
saveInstanceData = DEFAULT_SAVE_INSTANCE_DATA;
}
cobwebParameters = " -A " + acuity + " -C " + cutoff + saveInstanceData;
/* Get input bi object from parent node */
Operator parentOp = (Operator) a_Parents.elementAt(0);
setInputBIObject(parentOp.getOutputBIObject());
IBIData aInputBIData = getInputBIObject().getBIData();
aInputBIData.getMiningStoredData().reset();
if (!aInputBIData.hasResult()) {
throw new SysException("No data inputed.");
}
/* Prepare output data model */
BIData aOutputBIData = new BIData(getCaseID(), getNodeID());
aOutputBIData.setTargetAttribute(aInputBIData.getTargetAttribute());
aOutputBIData.setTransformActionHistory(aInputBIData.getTransformActionHistory());
aOutputBIData.setTargetAttribute(aInputBIData.getTargetAttribute());
aOutputBIData.setMiningStoredData(aInputBIData.getMiningStoredData());
BIModel aOutputBIModel = new BIModel(getCaseID(), getNodeID(), IBIModel.TYPE_CLUSTERING);
/* Execure Clustering Model Building */
/* Create MiningSettings object and assign metadata */
StatisticsSettings miningSettings = new StatisticsSettings();
miningSettings.setDataSpecification(aInputBIData.getMetaData());
//miningSettings.setDistance( dist );
miningSettings.verifySettings();
aOutputBIModel.setMiningSettings(miningSettings);
/* Get default mining algorithm specification from 'algorithms.xml': */
MiningAlgorithmSpecification miningAlgorithmSpecification =
MiningAlgorithmSpecification.getMiningAlgorithmSpecification( ALGORITHM_NAME ,getNodeInfo());
if (miningAlgorithmSpecification == null) {
throw new SysException("Can't find clustering method.");
}
/* Get class name from algorithms specification */
String className = miningAlgorithmSpecification.getClassname();
if (className == null) {
throw new SysException("classname attribute expected.");
}
/* Set and display mining parameters */
miningAlgorithmSpecification.setMAPValue(MAP_WEKA_CLASS_PARAMETERS, cobwebParameters);//
aOutputBIModel.setMiningAlgorithmSpecification(miningAlgorithmSpecification);
displayMiningAlgSpecParameters(miningAlgorithmSpecification);
/* Create algorithm object with default values */
//MiningAlgorithm algorithm = (CDBasedClusteringAlgorithm)
//GeneralUtils.createMiningAlgorithmInstance(className);
MiningAlgorithm algorithm = (StatisticsAlgorithm)
GeneralUtils.createMiningAlgorithmInstance(className,this.getClass().getClassLoader());
/* Put it all together */
algorithm.setMiningInputStream(aInputBIData.getMiningStoredData());
algorithm.setMiningSettings(miningSettings);
algorithm.setMiningAlgorithmSpecification(miningAlgorithmSpecification);
algorithm.verify();
/* Build the mining model */
MiningModel model = algorithm.buildModel();
m_SystemMessageHandler.appendMessage(Resource.srcStr("calculationtime") + " [s]: "
+ algorithm.getTimeSpentToBuildModel() + Resource.srcStr("ms"));
m_SystemMessageHandler.nextLine();
m_SystemMessageHandler.nextLine();
/* set output mining data and model to the output mining object */
aOutputBIModel.setMiningModel(model);
aOutputBIModel.setModelName(m_DefaultModelName);
m_OutputBIObject.setBIData(aOutputBIData);
m_OutputBIObject.setBIModel(aOutputBIModel);
/* set run time parameter value to the node object (It needs to be stored in the BIML) */
//a_OperatorNode.setParameterValue("Temporary model", aOutputBIModel.getTempBIModelPath());
//aOutputBIModel.writeTempBIModel();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -