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

📄 operatorprocessthread.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.
 */
/**
 * Created on 2006/8/12
 *
 * @Author: Xiaojun Chen
 * Description: an editable textpanel with Undo、Redo,Copy、Paste、Cut
 * $Revision$ 1.0
 *
 */
/**
 * OperatorProcessThread is a thread for operator processing, which will be registered in OperatorProgress, and managed by OperatorProgress
 * */
package eti.bi.alphaminer.core.handler;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Vector;
import com.prudsys.pdm.Core.MiningException;
import eti.bi.alphaminer.operation.operator.AssessmentOperator;
import eti.bi.alphaminer.operation.operator.DeploymentOperator;
import eti.bi.alphaminer.operation.operator.ModelOperator;
import eti.bi.alphaminer.operation.operator.Operator;
import eti.bi.alphaminer.operation.operator.OperatorProgressModel;
import eti.bi.alphaminer.operation.operator.TransformOperator;
import eti.bi.alphaminer.tools.SystemTools.CaseHelpToolBarHandler;
import eti.bi.alphaminer.ui.CaseDiagramPanel;
import eti.bi.alphaminer.ui.CaseWindow;
import eti.bi.alphaminer.ui.IMessageDialog;
import eti.bi.alphaminer.ui.ISystemMessageHandler;
import eti.bi.alphaminer.ui.dialog.MessageDialog;
import eti.bi.alphaminer.vo.BICase;
import eti.bi.alphaminer.vo.Node;
import eti.bi.alphaminer.vo.NodeFactory;
import eti.bi.alphaminer.vo.OperatorNode;
import eti.bi.common.Locale.Resource;
import eti.bi.common.System.SysLog;
import eti.bi.exception.AppException;
import eti.bi.exception.SysException;
import eti.bi.util.MemoryUtil;

public class OperatorProcessThread extends Thread {
	private BICase aCase;
	private Vector<String> NodeIDs;
	private CaseWindow aCaseWindow;
	private HashSet<String> aSet;
	private CaseHandler caseHandler;
	private IMessageDialog m_MessageDialog = new MessageDialog();
	private boolean running;
	private boolean issuccessful;
	private OperatorNode last_OperatorNode;
	private OperatorNode current_OperatorNode;

	public OperatorProcessThread(BICase a_Case, Vector<String> a_NodeIDs, CaseWindow a_CaseWindow,
			HashSet<String> a_Set, CaseHandler aCaseHandler) {
		aCase = a_Case;
		NodeIDs = a_NodeIDs;
		aCaseWindow = a_CaseWindow;
		aSet = a_Set;
		caseHandler = aCaseHandler;
	}

	@SuppressWarnings("deprecation")
	public void run() {
		try {
			running = true;
			issuccessful = false;
			aCaseWindow.getCaseDiagramPanel().enableDrag(false);
			// aCaseWindow.repaint();
			if (NodeIDs != null) {
				Iterator<String> iterator = NodeIDs.iterator();
				while (iterator.hasNext()) {
					String aNodeID = iterator.next();
					executeNode(aNodeID);
					aCaseWindow.sendNotify("succeed node|" + aNodeID);
					if (aSet != null) {
						// Mark the node id to indentify the node has been
						// executed
						aSet.add(aNodeID);
					}
				}
			}
			aCaseWindow.getCaseDiagramPanel().enableDrag(true);
			issuccessful = true;
		} catch (SysException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			SysLog.error(null, e);
		} catch (AppException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			SysLog.error(null, e);
		} catch (MiningException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			SysLog.error(null, e);
		} finally {
			running = false;
			CaseHelpToolBarHandler caseHelpToolBarHandler = aCaseWindow.getCaseHelpToolBarHandler();
			if(caseHelpToolBarHandler!=null) {
				caseHelpToolBarHandler.stopMonitor(true,true);
			}
			stop();
		}
	}

	private void executeNode(String a_NodeID) throws SysException, AppException, MiningException {
		Vector parentNodes = aCase.getParentOfNodes(a_NodeID);
		// Execute the parent nodes one by one from begining first before
		// execute the selected node
		for (int i = 0; i < parentNodes.size(); i++) {
			// Get the operator from the case diagram panel
			Operator operator = ((CaseDiagramPanel) aCaseWindow.getDiagramDrawingPanel())
					.getOperator((String) parentNodes.elementAt(i));
			// If the operator has been executed with result stored, skip it.
			if (operator.hasResult()) {
				// System.out.println("has result");
				continue;
			}
			// Check if the node has already been executed in this looping,
			// avoid duplicated execution
			if (aSet.contains((String) parentNodes.elementAt(i))) {
				continue;
			}
			// For assessment operator, do not execute mode that is de-selected.
			// TWang. Apr 14, 2005.
			Operator tmp_operator = ((CaseDiagramPanel) aCaseWindow.getDiagramDrawingPanel()).getOperator(a_NodeID);
			if (tmp_operator instanceof AssessmentOperator) {
				OperatorNode node = (OperatorNode) aCase.getNode(a_NodeID);
				Enumeration keys = node.getParameters().keys();
				boolean find = true;
				while (keys.hasMoreElements()) {
					String tmpKey = (String) keys.nextElement();
					if (tmpKey.indexOf(operator.getNodeID()) != -1) {
						if ((node.getParameterValue(tmpKey)).equals("false"))
							find = false;
						break;
					}
				}
				if (!find) {
					continue;
				}
			}
			// End. TWang.
			// execute the parent node
			executeNode((String) parentNodes.elementAt(i));
		}
		// Get the selected node after all the parent nodes are executed
		Node selectedNode = aCase.getNode(NodeFactory.OPERATOR, a_NodeID);
		aCaseWindow.sendNotify("run node|" + a_NodeID);
		// long startTime = System.currentTimeMillis();
		// Result nodeResult =
		// CommunicationHandler.getInstance().executeNode(selectedNode);
		boolean succeess = false;
		try {
			// Executed the selected node
			current_OperatorNode = (OperatorNode) selectedNode;
			processOperator((OperatorNode) selectedNode);
			last_OperatorNode = (OperatorNode) selectedNode;
			succeess = true;
		} catch (AppException e) {
			aCaseWindow.sendNotify("error node|" + a_NodeID);
			e.printStackTrace();
			SysLog.error(null, e);
			CaseHelpToolBarHandler caseHelpToolBarHandler = aCaseWindow.getCaseHelpToolBarHandler();
			caseHelpToolBarHandler.stopMonitor(false,false);
			m_MessageDialog.showWarning(e.getMessage(), Resource.srcStr("NodeMessage"));
			throw e;
		} catch (SysException e) {
			aCaseWindow.sendNotify("error node|" + a_NodeID);
			e.printStackTrace();
			SysLog.error(null, e);
			CaseHelpToolBarHandler caseHelpToolBarHandler = aCaseWindow.getCaseHelpToolBarHandler();
			caseHelpToolBarHandler.stopMonitor(false,false);
			m_MessageDialog.showError(e.getMessage(), Resource.srcStr("NodeMessage"));
			throw e;
		} catch (MiningException e) {
			aCaseWindow.sendNotify("error node|" + a_NodeID);
			e.printStackTrace();
			SysLog.error(null, e);
			CaseHelpToolBarHandler caseHelpToolBarHandler = aCaseWindow.getCaseHelpToolBarHandler();
			caseHelpToolBarHandler.stopMonitor(false,false);
			m_MessageDialog.showError(e.getMessage(), Resource.srcStr("NodeMessage"));
			throw e;
		} catch (IllegalArgumentException e) {
			aCaseWindow.sendNotify("error node|" + a_NodeID);
			e.printStackTrace();
			SysLog.error(null, e);
			CaseHelpToolBarHandler caseHelpToolBarHandler = aCaseWindow.getCaseHelpToolBarHandler();
			caseHelpToolBarHandler.stopMonitor(false,false);
			m_MessageDialog.showError(e.getMessage(), Resource.srcStr("NodeMessage"));
			throw e;
		} catch (Exception e) {
			aCaseWindow.sendNotify("error node|" + a_NodeID);
			e.printStackTrace();
			SysLog.error(null, e);
			CaseHelpToolBarHandler caseHelpToolBarHandler = aCaseWindow.getCaseHelpToolBarHandler();
			caseHelpToolBarHandler.stopMonitor(false,false);
			m_MessageDialog.showError(Resource.srcStr("SystemErrorMessage"), Resource.srcStr("NodeMessage"));
			throw new SysException(e);
		} finally {
			if (!succeess) {
				issuccessful = false;
				CaseHelpToolBarHandler caseHelpToolBarHandler = aCaseWindow.getCaseHelpToolBarHandler();
				if (caseHelpToolBarHandler != null) {
					caseHelpToolBarHandler.stopMonitor(false,false);
				}
			}
		}
		// while (System.currentTimeMillis() - startTime <= 100);
		aCaseWindow.sendNotify("finish node|" + a_NodeID);
	}

	@SuppressWarnings("unchecked")
	private void processOperator(OperatorNode a_OperatorNode) throws SysException, AppException, MiningException {
		ISystemMessageHandler systemMessageHandler = caseHandler.getSystemMessageHandler(aCase.getCaseID());
		systemMessageHandler.appendLineMessage("");
		systemMessageHandler.appendLineMessage(Resource.srcStr("ProcessingNode") + a_OperatorNode.getNodeID());
		Operator operator = ((CaseDiagramPanel) aCaseWindow.getDiagramDrawingPanel()).getOperator(a_OperatorNode
				.getNodeID());
		Vector parentNodes = aCase.getParentOfNodes(a_OperatorNode.getNodeID());
		Vector<Operator> parentOperators = new Vector<Operator>();
		if (a_OperatorNode.getMinNumParent() > parentNodes.size()) {
			if (operator instanceof TransformOperator || operator instanceof ModelOperator) {
				throw new SysException(operator.getLabel() + " needs data input for execution.");
			} else if (operator instanceof DeploymentOperator || operator instanceof AssessmentOperator) {
				throw new SysException(operator.getLabel() + " needs data and model input for execution.");
			} else {
				throw new SysException(operator.getLabel() + " needs more input for execution.");
			}
		} else {
			// Find all parent Operators from parent Nodes
			for (int i = 0; i < parentNodes.size(); i++) {
				parentOperators.add((Operator) ((CaseDiagramPanel) aCaseWindow.getDiagramDrawingPanel())
						.getOperator((String) parentNodes.elementAt(i)));
			}
		}
		// Clear temp results every time before the operator is executed
		long start = 0, finish = 0, free = 0;
		long costtime = 0;
		operator.clearResult();
		try {
			start = MemoryUtil.usedMemory();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			SysLog.error(null, e);
		}
		costtime = System.currentTimeMillis();
		// set monitor
		CaseHelpToolBarHandler caseHelpToolBarHandler = aCaseWindow.getCaseHelpToolBarHandler();
		OperatorProgressModel operatorModel = operator.getProcessModel();
		if (caseHelpToolBarHandler != null) {
			if (operatorModel != null) {
				caseHelpToolBarHandler.setOperatorProgressModel(this, operatorModel);
			}
			caseHelpToolBarHandler.startMonitor();
		}
		// excute
		try {
			operator.execute(a_OperatorNode, parentOperators);
		} catch (SysException e) {
			operator.clearResult();
			throw e;
		} catch (AppException e) {
			operator.clearResult();
			throw e;
		} catch (MiningException e) {
			operator.clearResult();
			throw e;
		}
		finally{
			if (caseHelpToolBarHandler != null) {
				caseHelpToolBarHandler.stopMonitor(true,false);
			}
		}
		
		costtime = System.currentTimeMillis() - costtime;
		try {
			finish = MemoryUtil.usedMemory() - start;
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			SysLog.error(null, e1);
		}
		systemMessageHandler.appendLineMessage("\t" + Resource.srcStr("Process_CostTime") + costtime + "ms.");
		systemMessageHandler.appendLineMessage("\t" + Resource.srcStr("Process_CostMemory") + finish / (1024 * 1024)
				+ "M.");
		systemMessageHandler.appendLineMessage("\t" + Resource.srcStr("Process_freeMemory") + free / (1024 * 1024)
				+ "M.");
		systemMessageHandler.appendLineMessage(Resource.srcStr("FinishProcessNode") + a_OperatorNode.getNodeID());
	}

	public boolean isrunning() {
		return running;
	}

	public boolean issuccessful() {
		return issuccessful;
	}

	@SuppressWarnings("deprecation")
	public boolean stopRuning() throws Exception {
		if (current_OperatorNode == null) {
			this.stop();
			running = false;
			return true;
		}
		Operator operator = ((CaseDiagramPanel) aCaseWindow.getDiagramDrawingPanel()).getOperator(current_OperatorNode
				.getNodeID());
		operator.clearResult();
		if (last_OperatorNode != null) {
			aCaseWindow.sendNotify("stop node|" + last_OperatorNode.getNodeID() + ";"
					+ current_OperatorNode.getNodeID());
		} else {
			aCaseWindow.sendNotify("stop node|" + current_OperatorNode.getNodeID());
		}
		this.stop();
		running = false;
		return true;
	}
}

⌨️ 快捷键说明

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