editablebayesnet.java

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

JAVA
1,981
字号
	 * @param iNode index of node of interest	 */	public int getPositionX(int iNode) {		return (Integer) (m_nPositionX.elementAt(iNode));	}	/** get y position of a node	 * @param iNode index of node of interest	 */	public int getPositionY(int iNode) {		return (Integer) (m_nPositionY.elementAt(iNode));	}	/** align set of nodes with the left most node in the list	 * @param nodes list of indexes of nodes to align	 */	public void alignLeft(FastVector nodes) {		// update undo stack		if (m_bNeedsUndoAction) {			addUndoAction(new alignLeftAction(nodes));		}		int nMinX = -1;		for (int iNode = 0; iNode < nodes.size(); iNode++) {			int nX = getPositionX((Integer) nodes.elementAt(iNode));			if (nX < nMinX || iNode == 0) {				nMinX = nX;			}		}		for (int iNode = 0; iNode < nodes.size(); iNode++) {			int nNode = (Integer) nodes.elementAt(iNode);			m_nPositionX.setElementAt(nMinX, nNode);		}	} // alignLeft	/** align set of nodes with the right most node in the list	 * @param nodes list of indexes of nodes to align	 */	public void alignRight(FastVector nodes) {		// update undo stack		if (m_bNeedsUndoAction) {			addUndoAction(new alignRightAction(nodes));		}		int nMaxX = -1;		for (int iNode = 0; iNode < nodes.size(); iNode++) {			int nX = getPositionX((Integer) nodes.elementAt(iNode));			if (nX > nMaxX || iNode == 0) {				nMaxX = nX;			}		}		for (int iNode = 0; iNode < nodes.size(); iNode++) {			int nNode = (Integer) nodes.elementAt(iNode);			m_nPositionX.setElementAt(nMaxX, nNode);		}	} // alignRight	/** align set of nodes with the top most node in the list	 * @param nodes list of indexes of nodes to align	 */	public void alignTop(FastVector nodes) {		// update undo stack		if (m_bNeedsUndoAction) {			addUndoAction(new alignTopAction(nodes));		}		int nMinY = -1;		for (int iNode = 0; iNode < nodes.size(); iNode++) {			int nY = getPositionY((Integer) nodes.elementAt(iNode));			if (nY < nMinY || iNode == 0) {				nMinY = nY;			}		}		for (int iNode = 0; iNode < nodes.size(); iNode++) {			int nNode = (Integer) nodes.elementAt(iNode);			m_nPositionY.setElementAt(nMinY, nNode);		}	} // alignTop	/** align set of nodes with the bottom most node in the list	 * @param nodes list of indexes of nodes to align	 */	public void alignBottom(FastVector nodes) {		// update undo stack		if (m_bNeedsUndoAction) {			addUndoAction(new alignBottomAction(nodes));		}		int nMaxY = -1;		for (int iNode = 0; iNode < nodes.size(); iNode++) {			int nY = getPositionY((Integer) nodes.elementAt(iNode));			if (nY > nMaxY || iNode == 0) {				nMaxY = nY;			}		}		for (int iNode = 0; iNode < nodes.size(); iNode++) {			int nNode = (Integer) nodes.elementAt(iNode);			m_nPositionY.setElementAt(nMaxY, nNode);		}	} // alignBottom	/** center set of nodes half way between left and right most node in the list	 * @param nodes list of indexes of nodes to center	 */	public void centerHorizontal(FastVector nodes) {		// update undo stack		if (m_bNeedsUndoAction) {			addUndoAction(new centerHorizontalAction(nodes));		}		int nMinY = -1;		int nMaxY = -1;		for (int iNode = 0; iNode < nodes.size(); iNode++) {			int nY = getPositionY((Integer) nodes.elementAt(iNode));			if (nY < nMinY || iNode == 0) {				nMinY = nY;			}			if (nY > nMaxY || iNode == 0) {				nMaxY = nY;			}		}		for (int iNode = 0; iNode < nodes.size(); iNode++) {			int nNode = (Integer) nodes.elementAt(iNode);			m_nPositionY.setElementAt((nMinY + nMaxY) / 2, nNode);		}	} // centerHorizontal	/** center set of nodes half way between top and bottom most node in the list	 * @param nodes list of indexes of nodes to center	 */	public void centerVertical(FastVector nodes) {		// update undo stack		if (m_bNeedsUndoAction) {			addUndoAction(new centerVerticalAction(nodes));		}		int nMinX = -1;		int nMaxX = -1;		for (int iNode = 0; iNode < nodes.size(); iNode++) {			int nX = getPositionX((Integer) nodes.elementAt(iNode));			if (nX < nMinX || iNode == 0) {				nMinX = nX;			}			if (nX > nMaxX || iNode == 0) {				nMaxX = nX;			}		}		for (int iNode = 0; iNode < nodes.size(); iNode++) {			int nNode = (Integer) nodes.elementAt(iNode);			m_nPositionX.setElementAt((nMinX + nMaxX) / 2, nNode);		}	} // centerVertical	/** space out set of nodes evenly between left and right most node in the list	 * @param nodes list of indexes of nodes to space out	 */	public void spaceHorizontal(FastVector nodes) {		// update undo stack		if (m_bNeedsUndoAction) {			addUndoAction(new spaceHorizontalAction(nodes));		}		int nMinX = -1;		int nMaxX = -1;		for (int iNode = 0; iNode < nodes.size(); iNode++) {			int nX = getPositionX((Integer) nodes.elementAt(iNode));			if (nX < nMinX || iNode == 0) {				nMinX = nX;			}			if (nX > nMaxX || iNode == 0) {				nMaxX = nX;			}		}		for (int iNode = 0; iNode < nodes.size(); iNode++) {			int nNode = (Integer) nodes.elementAt(iNode);			m_nPositionX.setElementAt((int) (nMinX + iNode * (nMaxX - nMinX) / (nodes.size() - 1.0)), nNode);		}	} // spaceHorizontal	/** space out set of nodes evenly between top and bottom most node in the list	 * @param nodes list of indexes of nodes to space out	 */	public void spaceVertical(FastVector nodes) {		// update undo stack		if (m_bNeedsUndoAction) {			addUndoAction(new spaceVerticalAction(nodes));		}		int nMinY = -1;		int nMaxY = -1;		for (int iNode = 0; iNode < nodes.size(); iNode++) {			int nY = getPositionY((Integer) nodes.elementAt(iNode));			if (nY < nMinY || iNode == 0) {				nMinY = nY;			}			if (nY > nMaxY || iNode == 0) {				nMaxY = nY;			}		}		for (int iNode = 0; iNode < nodes.size(); iNode++) {			int nNode = (Integer) nodes.elementAt(iNode);			m_nPositionY.setElementAt((int) (nMinY + iNode * (nMaxY - nMinY) / (nodes.size() - 1.0)), nNode);		}	} // spaceVertical	/** replace attribute with specified name and values	 * @param nTargetNode index of node the replace specification for	 * @param sName new name of the node	 * @param values array of values of the node	 */	void replaceAtt(int nTargetNode, String sName, FastVector values) {		Attribute newAtt = new Attribute(sName, values);		if (m_Instances.classIndex() == nTargetNode) {			m_Instances.setClassIndex(-1);			m_Instances.insertAttributeAt(newAtt, nTargetNode);			m_Instances.deleteAttributeAt(nTargetNode + 1);			m_Instances.setClassIndex(nTargetNode);		} else {			m_Instances.insertAttributeAt(newAtt, nTargetNode);			m_Instances.deleteAttributeAt(nTargetNode + 1);		}	} // replaceAtt	/** return marginal distibution for a node	 * @param iNode index of node of interest	 */	public double[] getMargin(int iNode) {		return (double[]) m_fMarginP.elementAt(iNode);	};	/** set marginal distibution for a node	 * @param iNode index of node to set marginal distribution for	 * @param fMarginP marginal distribution	 */	public void setMargin(int iNode, double[] fMarginP) {		m_fMarginP.setElementAt(fMarginP, iNode);	}	/** get evidence state of a node. -1 represents no evidence set, otherwise	 * the index of a value of the node	 * @param iNode index of node of interest	 */	public int getEvidence(int iNode) {		return (Integer) m_nEvidence.elementAt(iNode);	}	/** set evidence state of a node. -1 represents no evidence set, otherwise	 * the index of a value of the node 	 * @param iNode index of node of interest	 * @param iValue evidence value to set	 */	public void setEvidence(int iNode, int iValue) {		m_nEvidence.setElementAt(iValue, iNode);	}	/** return list of children of a node	 * @param iNode index of node of interest	 */	public FastVector getChildren(int nTargetNode) {		FastVector children = new FastVector();		for (int iNode = 0; iNode < getNrOfNodes(); iNode++) {			if (m_ParentSets[iNode].contains(nTargetNode)) {				children.addElement(iNode);			}		}		return children;	} // getChildren	/** returns network in XMLBIF format	*/	public String toXMLBIF03() {		if (m_Instances == null) {			return ("<!--No model built yet-->");		}		StringBuffer text = new StringBuffer();		text.append(getBIFHeader());		text.append("\n");		text.append("\n");		text.append("<BIF VERSION=\"0.3\">\n");		text.append("<NETWORK>\n");		text.append("<NAME>" + XMLNormalize(m_Instances.relationName()) + "</NAME>\n");		for (int iAttribute = 0; iAttribute < m_Instances.numAttributes(); iAttribute++) {			text.append("<VARIABLE TYPE=\"nature\">\n");			text.append("<NAME>" + XMLNormalize(m_Instances.attribute(iAttribute).name()) + "</NAME>\n");			for (int iValue = 0; iValue < m_Instances.attribute(iAttribute).numValues(); iValue++) {				text.append("<OUTCOME>" + XMLNormalize(m_Instances.attribute(iAttribute).value(iValue))						+ "</OUTCOME>\n");			}			text.append("<PROPERTY>position = (" + getPositionX(iAttribute) + "," + getPositionY(iAttribute)					+ ")</PROPERTY>\n");			text.append("</VARIABLE>\n");		}		for (int iAttribute = 0; iAttribute < m_Instances.numAttributes(); iAttribute++) {			text.append("<DEFINITION>\n");			text.append("<FOR>" + XMLNormalize(m_Instances.attribute(iAttribute).name()) + "</FOR>\n");			for (int iParent = 0; iParent < m_ParentSets[iAttribute].getNrOfParents(); iParent++) {				text.append("<GIVEN>"						+ XMLNormalize(m_Instances.attribute(m_ParentSets[iAttribute].getParent(iParent)).name())						+ "</GIVEN>\n");			}			text.append("<TABLE>\n");			for (int iParent = 0; iParent < m_ParentSets[iAttribute].getCardinalityOfParents(); iParent++) {				for (int iValue = 0; iValue < m_Instances.attribute(iAttribute).numValues(); iValue++) {					text.append(m_Distributions[iAttribute][iParent].getProbability(iValue));					text.append(' ');				}				text.append('\n');			}			text.append("</TABLE>\n");			text.append("</DEFINITION>\n");		}		text.append("</NETWORK>\n");		text.append("</BIF>\n");		return text.toString();	} // toXMLBIF03	/** return fragment of network in XMLBIF format	 * @param nodes array of indexes of nodes that should be in the fragment	 */	public String toXMLBIF03(FastVector nodes) {		StringBuffer text = new StringBuffer();		text.append(getBIFHeader());		text.append("\n");		text.append("\n");		text.append("<BIF VERSION=\"0.3\">\n");		text.append("<NETWORK>\n");		text.append("<NAME>" + XMLNormalize(m_Instances.relationName()) + "</NAME>\n");		for (int iNode = 0; iNode < nodes.size(); iNode++) {			int nNode = (Integer) nodes.elementAt(iNode);			text.append("<VARIABLE TYPE=\"nature\">\n");			text.append("<NAME>" + XMLNormalize(m_Instances.attribute(nNode).name()) + "</NAME>\n");			for (int iValue = 0; iValue < m_Instances.attribute(nNode).numValues(); iValue++) {				text.append("<OUTCOME>" + XMLNormalize(m_Instances.attribute(nNode).value(iValue)) + "</OUTCOME>\n");			}			text.append("<PROPERTY>position = (" + getPositionX(nNode) + "," + getPositionY(nNode) + ")</PROPERTY>\n");			text.append("</VARIABLE>\n");		}		for (int iNode = 0; iNode < nodes.size(); iNode++) {			int nNode = (Integer) nodes.elementAt(iNode);			text.append("<DEFINITION>\n");			text.append("<FOR>" + XMLNormalize(m_Instances.attribute(nNode).name()) + "</FOR>\n");			for (int iParent = 0; iParent < m_ParentSets[nNode].getNrOfParents(); iParent++) {				text.append("<GIVEN>"						+ XMLNormalize(m_Instances.attribute(m_ParentSets[nNode].getParent(iParent)).name())						+ "</GIVEN>\n");			}			text.append("<TABLE>\n");			for (int iParent = 0; iParent < m_ParentSets[nNode].getCardinalityOfParents(); iParent++) {				for (int iValue = 0; iValue < m_Instances.attribute(nNode).numValues(); iValue++) {					text.append(m_Distributions[nNode][iParent].getProbability(iValue));					text.append(' ');				}				text.append('\n');			}			text.append("</TABLE>\n");			text.append("</DEFINITION>\n");		}		text.append("</NETWORK>\n");		text.append("</BIF>\n");		return text.toString();	} // toXMLBIF03	/** undo stack for undoin edit actions, or redo edit actions */	FastVector m_undoStack = new FastVector();	/** current action in undo stack */	int m_nCurrentEditAction = -1;	/** action that the network is saved */	int m_nSavedPointer = -1;	/***************************************************************************	 * flag to indicate whether an edit action needs to introduce an undo	 * action. This is only false when an undo or redo action is performed.	 **************************************************************************/	boolean m_bNeedsUndoAction = true;	/** return whether there is something on the undo stack that can be performed */	public boolean canUndo() {		return m_nCurrentEditAction >= 0;	}	/** return whether there is something on the undo stack that can be performed */	public boolean canRedo() {		return m_nCurrentEditAction < m_undoStack.size() - 1;	}	/** return true when current state differs from the state the network was last saved */	public boolean isChanged() {		return m_nCurrentEditAction != m_nSavedPointer;	}	/** indicate the network state was saved */	public void isSaved() {		m_nSavedPointer = m_nCurrentEditAction;	}	/** get message representing the last action performed on the network */	public String lastActionMsg() {		if (m_undoStack.size() == 0) {			return "";

⌨️ 快捷键说明

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