📄 categoryhierarchy.java
字号:
allRoots.addElement(chn.getNodeValue());
};
return allRoots;
}
/**
* Return all leaves nodes of the DAG.
*
* @return vector Categories of all leaf nodes
*/
public Vector getAllLeafs() {
Vector allLeafs = new Vector();
for (int i = 0; i < nodes.size(); i++) {
MiningHierarchyNode chn = (MiningHierarchyNode) nodes.elementAt(i);
if (chn.isLeaf())
allLeafs.addElement(chn.getNodeValue());
};
return allLeafs;
}
/**
* Return all parents of the given node which are roots.
*
* @param node specified node
* @return all root nodes which are parents of node
*/
public Vector getAllRootParents(Category node) {
Vector rpars = new Vector();
Vector pars = getAllParents(node);
for (int i = 0; i < pars.size(); i++) {
Category parent = (Category) pars.elementAt(i);
if ( getNodeType(parent) == 0 )
rpars.addElement(parent);
}
return rpars;
}
/**
* Return all children of the given node which are leafs.
*
* @param node specified node
* @return all leaf nodes which are children of node
*/
public Vector getAllLeafChildren(Category node) {
Vector lchilds = new Vector();
Vector children = getAllChildren(node);
for (int i = 0; i < children.size(); i++) {
Category child = (Category) children.elementAt(i);
if ( getNodeType(child) == 2 )
lchilds.addElement(child);
}
return lchilds;
}
/**
* Get type of node (0 - root, 1 - intermediate, 2 - leaf).
*
* @param node node to check
* @return type, -1 for unknown node
*/
public int getNodeType(Category node) {
MiningHierarchyNode mhn = findNodeOfCategory(node);
if (mhn == null)
return -1;
if ( mhn.isRoot() )
return 0;
else if ( mhn.isLeaf() )
return 2;
else
return 1;
}
/**
* Get hierarchy level of node (0 - root).
* ATTENTION: Only useful for level-based DAGs. Use method
* isLevelBased to get this info.
*
* @param node node to check
* @return type, -1 for unknown node
*/
public int getNodeLevel(Category node) {
if (!levelBased) return -1;
MiningHierarchyNode mhn = findNodeOfCategory(node);
if (mhn == null)
return -1;
return mhn.getLevel();
}
/**
* Get all nodes of a given hierarchy level (0 - root).
* ATTENTION: Only useful for level-based DAGs. Use method
* isLevelBased to get this info.
*
* @param level hierarchy level
* @return vector of all nodes of given level (Categories of this level)
*/
public Vector getNodesOfLevel(int level) {
Vector allNodes = new Vector();
if (!levelBased) return allNodes;
for (int i = 0; i < nodes.size(); i++) {
MiningHierarchyNode chn = (MiningHierarchyNode) nodes.elementAt(i);
if (chn.getLevel() == level)
allNodes.addElement( chn.getNodeValue() );
};
return allNodes;
}
/**
* Show list of all relationsships of the form
* parent - child.
*/
public void printRelationships() {
int ic = 0;
for (int i = 0; i < nodes.size(); i++) {
MiningHierarchyNode chn = (MiningHierarchyNode) nodes.elementAt(i);
for (int j = 0; j < chn.getParentsCount(); j++) {
MiningHierarchyNode par = (MiningHierarchyNode) chn.getParentAt(j);
System.out.println( ic++ + ": " + ((Category)par.getNodeValue()).getDisplayValue() + " <- " +
((Category)chn.getNodeValue()).getDisplayValue());
};
};
}
/**
* Just for completeness.
* Is the graph cyclic? The DAG may not contain cycles;
* so it never should return true.
*
* @return true, if graph contains cycles, else false
*/
protected boolean isCyclic() {
for (int i = 0; i < nodes.size(); i++) {
MiningHierarchyNode chn = (MiningHierarchyNode) nodes.elementAt(i);
Category node = (Category)chn.getNodeValue();
Vector parents = getAllParents(node);
for (int j = 0; j < parents.size(); j++) {
Category par = (Category) parents.elementAt(j);
if (par.equals(node))
return true;
};
};
return false;
}
/**
* Find node of a given category.
*
* @param cat category
* @return node of category, null if not found
*/
protected MiningHierarchyNode findNodeOfCategory(Category cat) {
Integer Nr = (Integer) nodesHash.get(cat);
if (Nr == null)
return null;
int nr = Nr.intValue();
MiningHierarchyNode mhn = (MiningHierarchyNode) nodes.elementAt(nr);
return mhn;
}
// -----------------------------------------------------------------------
// Methods of PMML handling
// -----------------------------------------------------------------------
/**
* Create PMML object Taxonomy for use in PMML documents.
*
* Warning: Categories are written via their string representation
* into PMML. The parsePmmlObject method reconstructs the categories as
* String objects. Hence the PMML mechanism works only properly
* for categories that are based on String objects (this is usually the
* case).
*
* @return PMMLs Taxonomy object
* @throws MiningException if can't create pmml representation for this object
* @see com.prudsys.pdm.Adapters.PmmlVersion20.Taxonomy
*/
public Object createPmmlObject() throws MiningException
{
Taxonomy tax = new Taxonomy();
// Set name:
tax.setName( getName() );
// Use one childparent:
ChildParent cp = new ChildParent();
cp.setChildField("member");
cp.setParentField("pargroup");
cp.setIsRecursive("yes");
// Fill table with relationship values
InlineTable it = new InlineTable();
for (int i = 0; i < nodes.size(); i++) {
MiningHierarchyNode chn = (MiningHierarchyNode) nodes.elementAt(i);
for (int j = 0; j < chn.getParentsCount(); j++) {
MiningHierarchyNode par = (MiningHierarchyNode) chn.getParentAt(j);
Row row = new Row();
Member memb = new Member();
String membName = ((Category)chn.getNodeValue()).toString();
if (membName == null || membName.length() == 0) membName = " ";
memb.setName( membName );
row.setMember( memb );
Pargroup pgroup = new Pargroup();
pgroup.setName( ((Category)par.getNodeValue()).toString() );
row.setPargroup( pgroup );
it.addRow(row);
};
};
cp.setInlineTable(it);
tax.addChildParent(cp);
return tax;
}
/**
* Reads from PMML object Taxonomy.
*
* Warning: The createPmmlObject methods writes the categories via their
* string representation into PMML. The parsePmmlObject method reconstructs
* the categories as String objects. Hence the PMML mechanism works only
* properly for categories that are based on String objects (this is usually
* the case).
*
* @param pmml PMMLs Taxonomy object
* @exception MiningException cannot parse PMML object
* @see com.prudsys.pdm.Adapters.PmmlVersion20.Taxonomy
*/
public void parsePmmlObject( Object pmml ) throws MiningException
{
Taxonomy tax = (Taxonomy) pmml;
setName( tax.getName() );
ChildParent[] cp = tax.getChildParent();
InlineTable it = cp[0].getInlineTable();
Row[] rows = it.getRow();
if (rows == null) return;
// Read inline table and build graph:
nodes.removeAllElements();
for (int i = 0; i < rows.length; i++) {
Member memb = rows[i].getMember();
String childval = memb.getName();
Pargroup pgroup = rows[i].getPargroup();
String parval = pgroup.getName();
Category parent = new Category(parval);
Category child = new Category(childval);
addRelationship(parent, child);
};
}
// -----------------------------------------------------------------------
// Test
// -----------------------------------------------------------------------
/**
* Main routine for testing.
*
* @param argv test arguments
*/
public static void main(String[] argv) {
try {
CategoryHierarchy cah = new CategoryHierarchy();
// Define categories:
Category c1 = new Category("1");
Category c2 = new Category("2");
Category c3 = new Category("3");
Category c4 = new Category("4");
Category c5 = new Category("5");
Category c6 = new Category("6");
Category c7 = new Category("7");
Category c8 = new Category("8");
Category c9 = new Category("9");
Category c10 = new Category("10");
Category c11 = new Category("11");
Category c12 = new Category("12");
Category c13 = new Category("13");
Category c14 = new Category("14");
Category c15 = new Category("15");
Category c16 = new Category("16");
Category c17 = new Category("17");
Category c18 = new Category("18");
Category c19 = new Category("19");
Category c20 = new Category("20");
Category c21 = new Category("21");
Category c22 = new Category("22");
// First subtree:
cah.addRelationship(c1, c2);
cah.addRelationship(c1, c3);
cah.addRelationship(c3, c7);
cah.addRelationship(c2, c4);
cah.addRelationship(c2, c5);
cah.addRelationship(c2, c6);
cah.addRelationship(c4, c10);
cah.addRelationship(c4, c11);
cah.addRelationship(c4, c12);
cah.addRelationship(c4, c13);
cah.addRelationship(c7, c8);
cah.addRelationship(c7, c9);
cah.addRelationship(c12, c14);
cah.addRelationship(c12, c15);
// Second subtree:
cah.addRelationship(c20, c21);
cah.addRelationship(c20, c22);
cah.addRelationship(c21, c12);
cah.addRelationship(c21, c9);
// This would lead to a cyclic graph and hence to an exception:
// cah.addRelationship(c15, c1);
// Print taxonomy:
cah.printRelationships();
System.out.println("numberOfRelationships = " + cah.getNumberOfRelationships());
// Calculate levels:
cah.calcLevels(true); // false would throw exception since not level-based!!
// Get all root nodes:
System.out.println("Roots:");
Vector iv = cah.getAllRoots();
for (int i = 0; i < iv.size(); i++)
System.out.print( ((Category) iv.elementAt(i)).getDisplayValue() + " ");
System.out.println();
// Get all leaf nodes:
System.out.println("Leafs:");
iv = cah.getAllLeafs();
for (int i = 0; i < iv.size(); i++)
System.out.print( ((Category) iv.elementAt(i)).getDisplayValue() + " ");
System.out.println();
// Get all nodes of given level (usually should not be used for this type of graphs):
int level = 2;
System.out.println("Nodes of level " + level + ":");
iv = cah.getNodesOfLevel(level);
for (int i = 0; i < iv.size(); i++)
System.out.print( ((Category) iv.elementAt(i)).getDisplayValue() + " ");
System.out.println();
// Does graph contains cycles:
System.out.println("isCyclic: " + cah.isCyclic());
// Get type of node:
Category node = c20;
System.out.println("node = " + node.getDisplayValue() + " type = " +
cah.getNodeType(node));
// Get layer of node (usually should not be used for this type of graphs):
node = c9;
System.out.println("node = " + node.getDisplayValue() + " level = " +
cah.getNodeLevel(node));
// Get parents of child:
Category child = c9;
System.out.println("child = " + child.getDisplayValue() + ":");
iv = cah.getParents(child);
System.out.print("parents -> ");
for (int i = 0; i < iv.size(); i++)
System.out.print( ((Category) iv.elementAt(i)).getDisplayValue() + " ");
System.out.println();
iv = cah.getAllParents(child);
System.out.print("all parents -> ");
for (int i = 0; i < iv.size(); i++)
System.out.print( ((Category) iv.elementAt(i)).getDisplayValue() + " ");
System.out.println();
iv = cah.getAllRootParents(child);
System.out.print("root parents -> ");
for (int i = 0; i < iv.size(); i++)
System.out.print( ((Category) iv.elementAt(i)).getDisplayValue() + " ");
System.out.println();
// Get children of parent:
Category parent = c21;
System.out.println("parent = " + parent.getDisplayValue() + ":");
iv = cah.getChildren(parent);
System.out.print("children -> ");
for (int i = 0; i < iv.size(); i++)
System.out.print( ((Category) iv.elementAt(i)).getDisplayValue() + " ");
System.out.println();
iv = cah.getAllChildren(parent);
System.out.print("all children -> ");
for (int i = 0; i < iv.size(); i++)
System.out.print( ((Category) iv.elementAt(i)).getDisplayValue() + " ");
System.out.println();
iv = cah.getAllLeafChildren(parent);
System.out.print("leaf children -> ");
for (int i = 0; i < iv.size(); i++)
System.out.print( ((Category) iv.elementAt(i)).getDisplayValue() + " ");
System.out.println();
/*
com.prudsys.pdm.Input.Records.Csv.MiningCsvStream inputStream
= new com.prudsys.pdm.Input.Records.Csv.MiningCsvStream("D:\\Div\\glaxo\\new\\bondatenKundenkartenAll.txt");
inputStream.open();
MiningDataSpecification metaData = inputStream.getMetaData();
System.out.println("csvMetaData: " + metaData);
int ii = 0;
cah = new CategoryHierarchy();
CategoricalAttribute cldAtt = (CategoricalAttribute) metaData.getMiningAttribute("field3");
CategoricalAttribute parAtt = (CategoricalAttribute) metaData.getMiningAttribute("field4");
while ( inputStream.next() ) {
com.prudsys.pdm.Input.MiningVector mv = inputStream.read();
double kcld = mv.getValue(2);
double kpar = mv.getValue(3);
Category cldCat = cldAtt.getCategory(kcld);
Category parCat = parAtt.getCategory(kpar);
cah.addRelationship(parCat, cldCat);
ii++;
if (ii == (ii/10000*10000) ) System.out.println("ii = " + ii);
// if (ii == 100000) break;
}
cah.printRelationships();
*/
}
catch (Exception ex) {
ex.printStackTrace();
};
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -