📄 defaultnetassembler.java
字号:
//Skye Bender-deMoll draft translation code 2/13/01
//skyebend@bennington.edu
package PajekConverter;
import java.text.*;
import java.util.*;
/*
NetAssembler takes a dataEntry object and constructs a network of nodes, assigns catagories, colors
performs operations
*/
public class DefaultNetAssembler extends Object
{
//globals
private DefaultDataEntry data; //holds parsed data
private Vector nodeList = new Vector(); //holds nodes
private int numRows = 0;
private int currentRow = 0;
private boolean rowIsError = false;
private boolean rowIsForOutput = true;
private int nextPajekId =1;
private String rowErrors = "";
private Hashtable IDTable = new Hashtable(100); //associates string id w/ pajek (int) id
private Hashtable colorTable = new Hashtable(16); //associates classes with colors
private Hashtable arcColorTable = new Hashtable(16); //associates classes with colors
private Hashtable shapeTable = new Hashtable(4); //associates classes with shapes
private static String[] PAJEKCOLORS = {"Blue","Green","Red","Yellow","Orange",
"Purple","Brown","Black","Gray","White","RedViolet",
"CornflowerBlue","MidnightBlue","SpringGreen","Tan","Lavender"};
private static String[] PAJEKSHAPES = {"ellipse","box","diamond","cross"};
//instantiates DefaultNetAssembler with rows number of rows
public DefaultNetAssembler(DefaultDataEntry dataEntry, int nextId)
{
data = dataEntry;
numRows = dataEntry.getNumRows();
nextPajekId = nextId;
//setup shapes
//PAJEKCOLORS = new Vector((Collection)asList(pajekColors));
}
//------Methods---------
//set up shapes, send errors to satus display
public String setupShape()
{
String shapeErrors = "";
Vector shapeClasses = data.getShapeClasses();
int numClasses = shapeClasses.size();
//make sure there are not too many shape classes
if (numClasses>4)
{
shapeErrors = "ERROR: More than four shape classes in data, first 4 used";
//numClasses = 4;
}
else
{
//make vector of shapes
Vector pajekShapes= new Vector(java.util.Arrays.asList(PAJEKSHAPES));
//create associations between shapes and classes
for (int i=0; i<numClasses; i++)
{
//if the class is a shape, then use it, otherwise make assigments
if (pajekShapes.contains(shapeClasses.elementAt(i)))
{
shapeTable.put(shapeClasses.get(i),shapeClasses.get(i));
//remove the color from list of avalible
pajekShapes.remove(shapeClasses.elementAt(i));
}
else
{
//assign class to color form list
shapeTable.put(shapeClasses.get(i),pajekShapes.firstElement());
//remove the color form list of avalible
pajekShapes.removeElementAt(0);
}
}
//output "key"
shapeErrors = "\nNODE SHAPE KEY:"+shapeTable+"\n";
}
return shapeErrors;
}
//set up size ,send errors to status display
public String setupSize()
{
String sizeErrors = "";
return sizeErrors;
}
//set up color,send errors to status display
public String setupColor()
{
String colorErrors = "";
Vector colorClasses = data.getColorClasses();
int classSize = colorClasses.size();
//make sure there are not too many color classes
if (classSize>16)
{
colorErrors = "ERROR: More than 16 node color classes in data, default used\n";
//classSize = 16;
//debug
System.out.println(colorClasses);
}
else //setup colors
{
//make vector of colors
Vector pajekColors= new Vector(java.util.Arrays.asList(PAJEKCOLORS));
//create associations between colors and classes
for (int i=0; i<classSize; i++)
{
//if the class is a color, then use it, otherwise make assigments
if (pajekColors.contains(colorClasses.elementAt(i)))
{
colorTable.put(colorClasses.get(i),colorClasses.get(i));
//remove the color from list of avalible
pajekColors.remove(colorClasses.get(i));
}
else
{
//assign class to color form list
colorTable.put(colorClasses.get(i),pajekColors.firstElement());
//remove the color form list of avalible
pajekColors.removeElementAt(0);
}
}
//output "key"
colorErrors = "NODE COLOR KEY:"+colorTable+"\n";
}
return colorErrors;
}
//set up color,send errors to status display
public String setupArcColor()
{
String arcColorErrors = "";
Vector arcColorClasses = data.getArcColorClasses();
int classSize = arcColorClasses.size();
//make sure there are not too many arc color classes
if (classSize>16)
{
arcColorErrors = "ERROR: More than 16 arc color classes in data, default used";
//classSize = 16;
}
else
{
//make vector of colors
Vector pajekColors= new Vector(java.util.Arrays.asList(PAJEKCOLORS));
//create associations between colors and classes
for (int i=0; i<classSize; i++)
{
//if the class is a color, then use it, otherwise make assigments
if (pajekColors.contains(arcColorClasses.elementAt(i)))
{
arcColorTable.put(arcColorClasses.get(i),arcColorClasses.get(i));
//remove the color from list of avalible
pajekColors.remove(arcColorClasses.elementAt(i));
}
else
{
//assign class to color form list
arcColorTable.put(arcColorClasses.get(i),pajekColors.firstElement());
//remove the color form list of avalible
pajekColors.removeElementAt(0);
}
}
//output "key"
arcColorErrors = "ARC COLOR KEY:"+arcColorTable+"\n";
}
return arcColorErrors;
}
//set up x and y coords,send errors to status display
public String setupCoords()
{
String coordErrors = "";
return coordErrors;
}
//setup edges
//NODES
//create the nodes with lables,send errors to status display
public String makeNodes()
{
String makeNodeErrors = "";
//loop over all entries and create node for each unique id
for (int i = 0; i<numRows; i++)
{
//check that node is tagged for output
if (data.isForOutput(i))
{
//check that id does not already exist
if (!IDTable.containsKey((String)data.getId(i)))
{
//associate Id string w/ pajek id
IDTable.put((String)data.getId(i),new Integer(nextPajekId));
//make new node w/ PajekId and label and add to list
PajekNode workNode = new PajekNode(nextPajekId, data.getLabel(i));
//assign shape corresponding to the data class
workNode.setShape((String)shapeTable.get(data.getShape(i)));
//assign size
workNode.setSize(doubleToString(data.getSize(i)));
//assign color
workNode.setColor((String)colorTable.get(data.getColor(i)));
//assign xCoor
workNode.setXCoord(doubleToString(data.getX(i)));
//assign yCoor
workNode.setYCoord(doubleToString(data.getY(i)));
//assign size
//add node to list
nodeList.add(workNode);
//increment node id
nextPajekId++;
}
}
}
return makeNodeErrors;
}
//EDGES (ARCS)
//create network structure send errors to status dispaly
public String makeArcs()
{
String makeArcErrors = "";
//loop over all entries and create indicated edges
for (int i = 0; i<numRows; i++)
{
//check that line is tagged for output
if (data.isForOutput(i))
{
//check that link exists (not equal to null or dummy char
if (!(data.getLink(i).equals("_")))
{
//get index work node corresponding to rawId
int workIndex = ((Integer)IDTable.get(data.getId(i))).intValue() - 1;
// make sure link node exists
try
{
int linkIndex = ((Integer)IDTable.get(data.getLink(i))).intValue();
//add the arc to the node corresponding to index
((PajekNode)nodeList.elementAt(workIndex)).addLink(linkIndex);
//add the arc color to the node corresponding to index
((PajekNode)nodeList.elementAt(workIndex)).addArcColor((String)arcColorTable.get(data.getArcColor(i)));
//add the arc width to the node corresponding to index
((PajekNode)nodeList.elementAt(workIndex)).addArcWidth(data.getArcWidth(i));
//add the arc weight to the noe corresponding to index
((PajekNode)nodeList.elementAt(workIndex)).addArcWeight(data.getArcWeight(i));
}
catch (NullPointerException e)
{
makeArcErrors += "ERROR:line "+(i+1)+": No node ID corresponds to linkID \""+data.getLink(i)+"\"\n";
//debug
//System.out.println(IDTable);
}
}
}
}
return makeArcErrors;
}
//-----------Accesors-------------------
// some set commands may parse their input
// or check ranges
//getNodes
public Vector getNodes()
{
return nodeList;
}
//-----------Utility Methods ----------
//TO INT takes text from passed string and parses it to return an int
//returns -1 if unable to parse
public int toInt(String str)
{
int returnInt = -1;
str.trim();
if (!(str.equals("_")))
{
try
{
returnInt = new DecimalFormat().parse(str).intValue();
}
catch(ParseException e)
{
rowIsError = true;
rowErrors = rowErrors +"\tIntegerParsingError:"+ e.getMessage()+"\n";
}
}
return returnInt;
}
//TO DOUBLE takes text from passed string and parses it to return a double
// returns 0.000 if an exception is thrown
public double toDouble(String str)
{
double returnDouble = 0.000;
str.trim();
if (!(str.equals("_")))
{
try
{
returnDouble = new DecimalFormat().parse(str).doubleValue();
}
catch(ParseException e)
{
rowIsError = true;
rowErrors = rowErrors +"\tDoubleParsingError:"+ e.getMessage()+"\n";
}
}
return returnDouble;
}
//DOUBLE TO STRING takes a double and returns a formated string
public String doubleToString(double dbl){
NumberFormat returnFormat = NumberFormat.getNumberInstance();
returnFormat.setMinimumFractionDigits(3);
returnFormat.setMaximumFractionDigits(3);
returnFormat.setGroupingUsed(false); //so it won't spit out commas
String returnString = returnFormat.format(dbl);
return returnString;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -