editablebayesnet.java

来自「Weka」· Java 代码 · 共 1,981 行 · 第 1/5 页

JAVA
1,981
字号
		}		return ((UndoAction) m_undoStack.lastElement()).getRedoMsg();	} // lastActionMsg	/** undo the last edit action performed on the network.	 * returns message representing the action performed.	 */	public String undo() {		if (!canUndo()) {			return "";		}		UndoAction undoAction = (UndoAction) m_undoStack.elementAt(m_nCurrentEditAction);		m_bNeedsUndoAction = false;		undoAction.undo();		m_bNeedsUndoAction = true;		m_nCurrentEditAction--;		// undo stack debugging		/*		if (m_nCurrentEditAction>0) {			String sXML = (String) m_sXMLStack.elementAt(m_nCurrentEditAction);			String sXMLCurrent = toXMLBIF03();			if (!sXML.equals(sXMLCurrent)) {				String sDiff = "";				String sDiff2 = "";				for (int i = 0; i < sXML.length() && sDiff.length() < 80; i++) {					if (sXML.charAt(i) != sXMLCurrent.charAt(i)) {						sDiff += sXML.charAt(i);						sDiff2 += sXMLCurrent.charAt(i);					}				}				JOptionPane.showMessageDialog(null,"Undo error\n" + sDiff + " \n" + sDiff2);			}		}		*/		return undoAction.getUndoMsg();	} // undo	/** redo the last edit action performed on the network.	 * returns message representing the action performed.	 */	public String redo() {		if (!canRedo()) {			return "";		}		m_nCurrentEditAction++;		UndoAction undoAction = (UndoAction) m_undoStack.elementAt(m_nCurrentEditAction);		m_bNeedsUndoAction = false;		undoAction.redo();		m_bNeedsUndoAction = true;		// undo stack debugging		/*		if (m_nCurrentEditAction < m_sXMLStack.size()) {			String sXML = (String) m_sXMLStack.elementAt(m_nCurrentEditAction);			String sXMLCurrent = toXMLBIF03();			if (!sXML.equals(sXMLCurrent)) {				String sDiff = "";				String sDiff2 = "";				for (int i = 0; i < sXML.length() && sDiff.length() < 80; i++) {					if (sXML.charAt(i) != sXMLCurrent.charAt(i)) {						sDiff += sXML.charAt(i);						sDiff2 += sXMLCurrent.charAt(i);					}				}				JOptionPane.showMessageDialog(null,"redo error\n" + sDiff + " \n" + sDiff2);			}		}		*/		return undoAction.getRedoMsg();	} // redo	/** add undo action to the undo stack.	 * @param action operation that needs to be added to the undo stack	 */	void addUndoAction(UndoAction action) {		int iAction = m_undoStack.size() - 1;		while (iAction > m_nCurrentEditAction) {			m_undoStack.removeElementAt(iAction--);		}		if (m_nSavedPointer > m_nCurrentEditAction) {			m_nSavedPointer = -2;		}		m_undoStack.addElement(action);		//m_sXMLStack.addElement(toXMLBIF03());		m_nCurrentEditAction++;	} // addUndoAction	/** remove all actions from the undo stack */	public void clearUndoStack() {		m_undoStack = new FastVector();		//m_sXMLStack = new FastVector();		m_nCurrentEditAction = -1;		m_nSavedPointer = -1;	} // clearUndoStack	/** base class for actions representing operations on the Bayesian network	 * that can be undone/redone	 */	class UndoAction implements Serializable {		/** for serialization */		static final long serialVersionUID = 1;		public void undo() {		}		public void redo() {		}		public String getUndoMsg() {			return getMsg();		}		public String getRedoMsg() {			return getMsg();		}		String getMsg() {			String sStr = toString();			int iStart = sStr.indexOf('$');			int iEnd = sStr.indexOf('@');			StringBuffer sBuffer = new StringBuffer();			for(int i= iStart + 1; i < iEnd; i++) {				char c = sStr.charAt(i);				if (Character.isUpperCase(c)) {					sBuffer.append(' ');				}				sBuffer.append(sStr.charAt(i));			}			return sBuffer.toString();		} // getMsg	} // class UndoAction	class AddNodeAction extends UndoAction {		/** for serialization */		static final long serialVersionUID = 1;		String m_sName;		int m_nPosX;		int m_nPosY;		int m_nCardinality;		AddNodeAction(String sName, int nCardinality, int nPosX, int nPosY) {			m_sName = sName;			m_nCardinality = nCardinality;			m_nPosX = nPosX;			m_nPosY = nPosY;		} // c'tor		public void undo() {			try {				deleteNode(getNrOfNodes() - 1);			} catch (Exception e) {				e.printStackTrace();			}		} // undo		public void redo() {			try {				addNode(m_sName, m_nCardinality, m_nPosX, m_nPosY);			} catch (Exception e) {				e.printStackTrace();			}		} // redo	} // class AddNodeAction	class DeleteNodeAction extends UndoAction {		/** for serialization */		static final long serialVersionUID = 1;		int m_nTargetNode;		Attribute m_att;		Estimator[] m_CPT;		ParentSet m_ParentSet;		FastVector m_deleteArcActions;		int m_nPosX;		int m_nPosY;		DeleteNodeAction(int nTargetNode) {			m_nTargetNode = nTargetNode;			m_att = m_Instances.attribute(nTargetNode);			try {				SerializedObject so = new SerializedObject(m_Distributions[nTargetNode]);				m_CPT = (Estimator[]) so.getObject();				;				so = new SerializedObject(m_ParentSets[nTargetNode]);				m_ParentSet = (ParentSet) so.getObject();			} catch (Exception e) {				e.printStackTrace();			}			m_deleteArcActions = new FastVector();			for (int iNode = 0; iNode < getNrOfNodes(); iNode++) {				if (m_ParentSets[iNode].contains(nTargetNode)) {					m_deleteArcActions.addElement(new DeleteArcAction(nTargetNode, iNode));				}			}			m_nPosX = getPositionX(m_nTargetNode);			m_nPosY = getPositionY(m_nTargetNode);		} // c'tor		public void undo() {			try {				m_Instances.insertAttributeAt(m_att, m_nTargetNode);				int nAtts = m_Instances.numAttributes();				// update parentsets				ParentSet[] parentSets = new ParentSet[nAtts];				int nX = 0;				for (int iParentSet = 0; iParentSet < nAtts; iParentSet++) {					if (iParentSet == m_nTargetNode) {						SerializedObject so = new SerializedObject(m_ParentSet);						parentSets[iParentSet] = (ParentSet) so.getObject();						nX = 1;					} else {						parentSets[iParentSet] = m_ParentSets[iParentSet - nX];						for (int iParent = 0; iParent < parentSets[iParentSet].getNrOfParents(); iParent++) {							int nParent = parentSets[iParentSet].getParent(iParent);							if (nParent >= m_nTargetNode) {								parentSets[iParentSet].SetParent(iParent, nParent + 1);							}						}					}				}				m_ParentSets = parentSets;				// update distributions				Estimator[][] distributions = new Estimator[nAtts][];				nX = 0;				for (int iNode = 0; iNode < nAtts; iNode++) {					if (iNode == m_nTargetNode) {						SerializedObject so = new SerializedObject(m_CPT);						distributions[iNode] = (Estimator[]) so.getObject();						nX = 1;					} else {						distributions[iNode] = m_Distributions[iNode - nX];					}				}				m_Distributions = distributions;				for (int deletedArc = 0; deletedArc < m_deleteArcActions.size(); deletedArc++) {					DeleteArcAction action = (DeleteArcAction) m_deleteArcActions.elementAt(deletedArc);					action.undo();				}				m_nPositionX.insertElementAt(m_nPosX, m_nTargetNode);				m_nPositionY.insertElementAt(m_nPosY, m_nTargetNode);				m_nEvidence.insertElementAt(-1, m_nTargetNode);				m_fMarginP.insertElementAt(new double[getCardinality(m_nTargetNode)], m_nTargetNode);			} catch (Exception e) {				e.printStackTrace();			}		} // undo		public void redo() {			try {				deleteNode(m_nTargetNode);			} catch (Exception e) {				e.printStackTrace();			}		} // redo	} // class DeleteNodeAction	class DeleteSelectionAction extends UndoAction {		/** for serialization */		static final long serialVersionUID = 1;		FastVector m_nodes;		Attribute[] m_att;		Estimator[][] m_CPT;		ParentSet[] m_ParentSet;		FastVector m_deleteArcActions;		int[] m_nPosX;		int[] m_nPosY;		public DeleteSelectionAction(FastVector nodes) {			m_nodes = new FastVector();			int nNodes = nodes.size();			m_att = new Attribute[nNodes];			m_CPT = new Estimator[nNodes][];			m_ParentSet = new ParentSet[nNodes];			m_nPosX = new int[nNodes];			m_nPosY = new int[nNodes];			m_deleteArcActions = new FastVector();			for (int iNode = 0; iNode < nodes.size(); iNode++) {				int nTargetNode = (Integer) nodes.elementAt(iNode);				m_nodes.addElement(nTargetNode);				m_att[iNode] = m_Instances.attribute(nTargetNode);				try {					SerializedObject so = new SerializedObject(m_Distributions[nTargetNode]);					m_CPT[iNode] = (Estimator[]) so.getObject();					;					so = new SerializedObject(m_ParentSets[nTargetNode]);					m_ParentSet[iNode] = (ParentSet) so.getObject();				} catch (Exception e) {					e.printStackTrace();				}				m_nPosX[iNode] = getPositionX(nTargetNode);				m_nPosY[iNode] = getPositionY(nTargetNode);				for (int iNode2 = 0; iNode2 < getNrOfNodes(); iNode2++) {					if (!nodes.contains(iNode2) && m_ParentSets[iNode2].contains(nTargetNode)) {						m_deleteArcActions.addElement(new DeleteArcAction(nTargetNode, iNode2));					}				}			}		} // c'tor		public void undo() {			try {				for (int iNode = 0; iNode < m_nodes.size(); iNode++) {					int nTargetNode = (Integer) m_nodes.elementAt(iNode);					m_Instances.insertAttributeAt(m_att[iNode], nTargetNode);				}				int nAtts = m_Instances.numAttributes();				// update parentsets				ParentSet[] parentSets = new ParentSet[nAtts];				int[] offset = new int[nAtts];				for (int iNode = 0; iNode < nAtts; iNode++) {					offset[iNode] = iNode;				}				for (int iNode = m_nodes.size() - 1; iNode >= 0; iNode--) {					int nTargetNode = (Integer) m_nodes.elementAt(iNode);					for (int i = nTargetNode; i < nAtts - 1; i++) {						offset[i] = offset[i + 1];					}				}				int iTargetNode = 0;				for (int iParentSet = 0; iParentSet < nAtts; iParentSet++) {					if (iTargetNode < m_nodes.size()							&& (Integer) m_nodes.elementAt(iTargetNode) == (Integer) iParentSet) {						SerializedObject so = new SerializedObject(m_ParentSet[iTargetNode]);						parentSets[iParentSet] = (ParentSet) so.getObject();						iTargetNode++;					} else {						parentSets[iParentSet] = m_ParentSets[iParentSet - iTargetNode];						for (int iParent = 0; iParent < parentSets[iParentSet].getNrOfParents(); iParent++) {							int nParent = parentSets[iParentSet].getParent(iParent);							parentSets[iParentSet].SetParent(iParent, offset[nParent]);						}					}				}				m_ParentSets = parentSets;				// update distributions				Estimator[][] distributions = new Estimator[nAtts][];				iTargetNode = 0;				for (int iNode = 0; iNode < nAtts; iNode++) {					if (iTargetNode < m_nodes.size() && (Integer) m_nodes.elementAt(iTargetNode) == (Integer) iNode) {						SerializedObject so = new SerializedObject(m_CPT[iTargetNode]);						distributions[iNode] = (Estimator[]) so.getObject();						iTargetNode++;					} else {						distributions[iNode] = m_Distributions[iNode - iTargetNode];					}				}				m_Distributions = distributions;				for (int iNode = 0; iNode < m_nodes.size(); iNode++) {					int nTargetNode = (Integer) m_nodes.elementAt(iNode);					m_nPositionX.insertElementAt(m_nPosX[iNode], nTargetNode);					m_nPositionY.insertElementAt(m_nPosY[iNode], nTargetNode);					m_nEvidence.insertElementAt(-1, nTargetNode);					m_fMarginP.insertElementAt(new double[getCardinality(nTargetNode)], nTargetNode);				}				for (int deletedArc = 0; deletedArc < m_deleteArcActions.size(); deletedArc++) {					DeleteArcAction action = (DeleteArcAction) m_deleteArcActions.elementAt(deletedArc);					action.undo();				}			} catch (Exception e) {				e.printStackTrace();			}		} // undo		public void redo() {			try {				for (int iNode = m_nodes.size() - 1; iNode >= 0; iNode--) {					int nNode = (Integer) m_nodes.elementAt(iNode);					deleteNode(nNode);				}			} catch (Exception e) {				e.printStackTrace();			}		} // redo	} // class DeleteSelectionAction	class AddArcA

⌨️ 快捷键说明

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