📄 mininghierarchynode.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.
*/
/**
* Title: XELOPES Data Mining Library
* Description: The XELOPES library is an open platform-independent and data-source-independent library for Embedded Data Mining.
* Copyright: Copyright (c) 2002 Prudential Systems Software GmbH
* Company: ZSoft (www.zsoft.ru), Prudsys (www.prudsys.com)
* @author Valentine Stepanenko (valentine.stepanenko@zsoft.ru)
* @author Michael Thess
* @version 1.0
*/
package com.prudsys.pdm.Core;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Vector;
/**
* Node of a taxonomy which is described by a Directed Acyclic Graph (DAG). <p>
*
* This means that any node may contain multiple children and
* multiple parents. Cycles are not allowed. <p>
*
* The node may contain any object (Category for example), just its
* key or both. <p>
*
* From PDM CWM extension.
*
* @see MiningTreeNode
* @see MiningGraphNode
*/
public class MiningHierarchyNode extends com.prudsys.pdm.Cwm.Core.Class
{
// -----------------------------------------------------------------------
// Variables declarations
// -----------------------------------------------------------------------
/** Is node a root node? */
public boolean root;
/** Is node a leaf node? */
public boolean leaf;
/** Level of the node (if defined). */
public int level;
/** Children of the node. */
public MiningHierarchyNode[] children;
/** Are children allowed? */
public boolean allowsChildren;
/** Parents of this node. */
public MiningHierarchyNode[] parents;
/** Are parents allowed? */
public boolean allowsParents;
/** Node value object. */
private Object nodeValue;
/** Key to node value object. */
private int nodeValueKey = -1;
// -----------------------------------------------------------------------
// Methods of node manipulation
// -----------------------------------------------------------------------
/**
* Get number of childs.
*
* @return number of childs
*/
public int getChildCount()
{
return ( children != null ) ? children.length : 0;
}
/**
* Get child node at specified index.
*
* @param childIndex index of child
* @return child at specified index, null if not found
*/
public MiningHierarchyNode getChildAt( int childIndex )
{
return ( children != null ) ? children[childIndex] : null;
}
/**
* Get index of child node.
*
* @param childNode child node
* @return index child index, -1 if not found
*/
public int getChildIndex( MiningHierarchyNode childNode )
{
int index = -1;
if( children == null )
{
index = -1;
}
else
{
for( int i = 0; i < children.length; i++ )
{
if( children[i].equals( childNode ) )
{
index = i;
}
}
}
return index;
}
/**
* Are children allowed?
*
* @return true, if allowed, elso false
*/
public boolean getAllowsChildren()
{
return allowsChildren;
}
/**
* Return list of all children
*
* @return all children
*/
public Enumeration children()
{
if( children != null )
{
Vector v = new Vector( Arrays.asList( children ) );
return v.elements();
}
else
{
return null;
}
}
/**
* Add child node to children list.
*
* @param child child node to add
*/
public void addChildNode(MiningHierarchyNode child)
{
int nchild = getChildCount();
MiningHierarchyNode[] child2 = new MiningHierarchyNode[nchild];
for (int i = 0; i < nchild; i++)
child2[i] = (MiningHierarchyNode) children[i];
children = new MiningHierarchyNode[nchild+1];
for (int i = 0; i < nchild; i++)
children[i] = child2[i];
children[nchild] = child;
}
/**
* Remove child from node.
*
* @param child child to be removed
*/
public void removeChild(MiningHierarchyNode child)
{
if(children == null) return;
int index = getChildIndex(child);
if(index != -1)
{
removeChild(index);
}
}
/**
* Remove child of given index.
*
* @param childIndex index of child to be removed
*/
public void removeChild(int childIndex)
{
MiningHierarchyNode[] childs = new MiningHierarchyNode[children.length-1];
System.arraycopy(children,0,childs,0,childIndex);
System.arraycopy(children,childIndex+1,childs,childIndex,children.length-childIndex-1);
children = childs;
if(children.length == 0) leaf = true;
}
/**
* Remove all children.
*/
public void removeAllChildren()
{
children = null;
leaf = true;
}
/**
* Get number of parents.
*
* @return number of parents
*/
public int getParentsCount()
{
return ( parents != null ) ? parents.length : 0;
}
/**
* Get parent node at specified index.
*
* @param parentIndex index of parent
* @return parent at specified index, null if not found
*/
public MiningHierarchyNode getParentAt( int parentIndex )
{
return ( parents != null ) ? parents[parentIndex] : null;
}
/**
* Get index of parent node.
*
* @param parentNode parent node
* @return parent index, -1 if not found
*/
public int getParentIndex( MiningHierarchyNode parentNode )
{
int index = -1;
if( parents == null )
{
index = -1;
}
else
{
for( int i = 0; i < parents.length; i++ )
{
if( parents[i].equals( parentNode ) )
{
index = i;
}
}
}
return index;
}
/**
* Are parents allowed?
*
* @return true, if allowed, elso false
*/
public boolean getAllowsParents()
{
return allowsParents;
}
/**
* Return list of all parents
*
* @return all parents
*/
public Enumeration parents()
{
if( parents != null )
{
Vector v = new Vector( Arrays.asList( parents ) );
return v.elements();
}
else
{
return null;
}
}
/**
* Add parent node to parent list.
*
* @param parent parent node to add
*/
public void addParentNode(MiningHierarchyNode parent)
{
int npar = getParentsCount();
MiningHierarchyNode[] par2 = new MiningHierarchyNode[npar];
for (int i = 0; i < npar; i++)
par2[i] = (MiningHierarchyNode) parents[i];
parents = new MiningHierarchyNode[npar+1];
for (int i = 0; i < npar; i++)
parents[i] = par2[i];
parents[npar] = parent;
}
/**
* Remove parent from node.
*
* @param parent parent to be removed
*/
public void removeParent(MiningHierarchyNode parent)
{
if(parents == null) return;
int index = getParentIndex(parent);
if(index != -1)
{
removeParent(index);
}
}
/**
* Remove parent of given index.
*
* @param parentIndex index of parent to be removed
*/
public void removeParent(int parentIndex)
{
MiningHierarchyNode[] pars = new MiningHierarchyNode[parents.length-1];
System.arraycopy(parents,0,pars,0,parentIndex);
System.arraycopy(parents,parentIndex+1,pars,parentIndex,parents.length-parentIndex-1);
parents = pars;
if(parents.length == 0) root = true;
}
/**
* Remove all parents.
*/
public void removeAllParents()
{
parents = null;
root = true;
}
/**
* Is node a leaf?
*
* @return true, if leaf, elso false
*/
public boolean isLeaf()
{
return leaf;
}
/**
* Set node leaf status.
*
* @param leaf true if node is leaf, else false
*/
public void setLeaf( boolean leaf )
{
this.leaf = leaf;
}
/**
* Is node a root?
*
* @return true, if root, elso false
*/
public boolean isRoot()
{
return root;
}
/**
* Set node root status.
*
* @param root true if node is root, else false
*/
public void setRoot( boolean root )
{
this.root = root;
}
/**
* Return hierarchy level (root level = 0).
*
* @return level of node
*/
public int getLevel()
{
return level;
}
/**
* Set level (root level = 0).
*
* @param lev level of node
*/
public void setLevel( int lev )
{
this.level = lev;
}
/**
* Get value of node.
*
* @return node value
*/
public Object getNodeValue()
{
return nodeValue;
}
/**
* Sets value of node.
*
* @param nodeValue new value of node
*/
public void setNodeValue(Object nodeValue)
{
this.nodeValue = nodeValue;
}
/**
* Returns key of node value.
*
* @return key of node value
*/
public int getNodeValueKey()
{
return nodeValueKey;
}
/**
* Sets key of node value.
*
* @param nodeValueKey new key of node value
*/
public void setNodeValueKey(int nodeValueKey)
{
this.nodeValueKey = nodeValueKey;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -