/*
* GraphAdapter.java
*
* Created on 05 June 2007, 12:18
*
*Author: Mark Holliman
*/
package paperscope;
import prefuse.data.tuple.AbstractTupleSet.*;
import prefuse.data.Graph;
import prefuse.data.Table;
import prefuse.data.Schema;
import java.lang.reflect.*;
import java.lang.String;
import java.util.Vector;
public class GraphAdapter {
Graph thisGraph;
Table nodes;
Table edges;
Paper[] paperList;
Vector focus_indices;
Schema nodeSchema;
Schema edgeSchema;
//=== Default Constructor
public GraphAdapter(Paper[] papers)
{
createSchema();
this.paperList = papers;
focus_indices = new Vector();
nodes = generateNodesTable();
edges = generateEdgesTable();
thisGraph = new Graph(nodes, edges, true, "DEFAULT_NODE_KEY", "source", "target");
}
//=== Constructor for Secondary Queries, creates graph with all the added nodes and edges
public GraphAdapter(Paper[] newPapers, int newFocus, Paper[] dupeNodes)
{
createSchema();
//==== Set the paperList for this GraphAdapter to the list of new papers being added
this.paperList = newPapers;
//==== Generate the table of the new nodes, updates the paperList with node keys too
nodes = generateSecondNodesTable();
//==== Generate the table of new edges
edges = generateSecondEdgesTable(newFocus, dupeNodes);
//==== create the graph
thisGraph = new Graph(nodes, edges, true, "DEFAULT_NODE_KEY", "source", "target");
}
//###################################################################################################
//=== Generate a table with all the papers as nodes, called to build the first table
public Table generateNodesTable()
{
//==== Get the number of papers
int nodeCount = Array.getLength(paperList);
//==== Instantiate the node table schema
Table node_table = this.nodeSchema.instantiate();
//==== Add each paper to a row in the node table
for (int i=0; i<nodeCount; i++)
{
//=== Set all the rows of the table with the paper's information
node_table.addRow();
node_table.setInt(i, "DEFAULT_NODE_KEY", i);
node_table.setString(i, "bibcode", paperList[i].getBibcode());
node_table.setString(i, "title", paperList[i].getTitle());
node_table.setString(i, "authorList", paperList[i].getAuthorList());
node_table.setString(i, "date", paperList[i].getDate());
node_table.setString(i, "journal", paperList[i].getJournal());
node_table.setString(i, "citCount", paperList[i].getCitCount());
node_table.setString(i, "abstractURL", paperList[i].getAbstractURL());
node_table.setString(i, "refType", paperList[i].getRefType());
node_table.setString(i, "nodeLabel", paperList[i].getNodeLabel());
node_table.setString(i, "hoverLabel", paperList[i].getHoverLabel());
node_table.setString(i, "visible", "true");
node_table.setString(i, "comment", "");
node_table.setString(i, "commentStatus", "f");
node_table.setDouble(i, "xCoordinate", 0);
node_table.setDouble(i, "yCoordinate", 0);
//=== Since this is a first search, the first paper will always be a focus node
if(i==0)
{
focus_indices.addElement(i);
node_table.setString(i, "refHidden", "false");
node_table.setString(i, "citHidden", "false");
node_table.setString(i, "refExpanded", "true");
node_table.setString(i, "citExpanded", "true");
}
}
return node_table;
} //end generateNodeTable
//############################################################################################
//=== Generate a table of edges
public Table generateEdgesTable()
{
int focus_count = focus_indices.size();
int rowCount = 0;
Table edge_table = this.edgeSchema.instantiate();
for (int i=0; i<focus_count; i++)
{
int focus_index = (Integer) focus_indices.elementAt(i);
String[] ref_links = paperList[focus_index].getPaperLinks("REFERENCES");
String[] cit_links = paperList[focus_index].getPaperLinks("CITATIONS");
int num_ref_links = Array.getLength(ref_links);
int num_cit_links = Array.getLength(cit_links);
//==== Loop though all the papers, checking to find the reference and citation nodes
for (int y=0; y<Array.getLength(paperList); y++)
{
//==== Loop through all the reference links, find their DEFAULT_NODE_KEY, add a row for the edge
for (int z=0; z<num_ref_links; z++)
{
if (paperList[y].getBibcode().equals(ref_links[z]))
{
edge_table.addRow();
edge_table.setInt(rowCount, "source", focus_index);
edge_table.setInt(rowCount, "target", y);
//edge_table.setString(rowCount, "edgeType", "ref");
rowCount++;
}
}
//==== Loop through all the citation links, find their DEFAULT_NODE_KEY, add a row for the edge
for (int x=0; x<num_cit_links; x++)
{
if (paperList[y].getBibcode().equals(cit_links[x]))
{
edge_table.addRow();
edge_table.setInt(rowCount, "source", y);
edge_table.setInt(rowCount, "target", focus_index);
//edge_table.setString(rowCount, "edgeType", "cit");
rowCount++;
}
}
} //end for loop, loop through all papers
}
return edge_table;
} //end generateEdgeTable
//###################################################################################################
//==== Generate a table with all the papers as nodes, used for adding new nodes at the end of the table
public Table generateSecondNodesTable()
{
//=== Counter for loop
int nodeCount = Array.getLength(paperList);
//=== Instatiate the table for the nodes
Table node_table = nodeSchema.instantiate();
for (int i=0; i<nodeCount; i++)
{
//==== Generate the table row
node_table.addRow();
node_table.setInt(i, "DEFAULT_NODE_KEY", paperList[i].getDefaultNodeKey());
node_table.setString(i, "bibcode", paperList[i].getBibcode());
node_table.setString(i, "title", paperList[i].getTitle());
node_table.setString(i, "authorList", paperList[i].getAuthorList());
node_table.setString(i, "date", paperList[i].getDate());
node_table.setString(i, "journal", paperList[i].getJournal());
node_table.setString(i, "citCount", paperList[i].getCitCount());
node_table.setString(i, "abstractURL", paperList[i].getAbstractURL());
node_table.setString(i, "refType", paperList[i].getRefType());
node_table.setString(i, "nodeLabel", paperList[i].getNodeLabel());
node_table.setString(i, "hoverLabel", paperList[i].getHoverLabel());
node_table.setString(i, "visible", "true");
node_table.setString(i, "comment", "");
node_table.setString(i, "commentStatus", "f");
node_table.setDouble(i, "xCoordinate", 0);
node_table.setDouble(i, "yCoordinate", 0);
}
return node_table;
} //end generateNewNodeTable
//############################################################################################
//=== Generate a table of edges
public Table generateSecondEdgesTable(int newFocus, Paper[] dupeNodes)
{
int rowCount = 0;
Table edge_table = edgeSchema.instantiate();
//==== Loop through the duplicate nodes, creating edges to the new index
for (int i=0; i<Array.getLength(dupeNodes); i++)
{
//=== Add a row for the 'ref' type links
if(dupeNodes[i].getRefType().equals("REFERENCES"))
{
edge_table.addRow();
edge_table.setInt(rowCount, "source", newFocus);
edge_table.setInt(rowCount, "target", dupeNodes[i].getDefaultNodeKey());
//edge_table.setString(rowCount, "edgeType", "ref");
rowCount++;
}
//=== Add a row for the 'cit' type links
else if (dupeNodes[i].getRefType().equals("CITATIONS"))
{
edge_table.addRow();
edge_table.setInt(rowCount, "source", dupeNodes[i].getDefaultNodeKey());
edge_table.setInt(rowCount, "target", newFocus);
//edge_table.setString(rowCount, "edgeType", "cit");
rowCount++;
}
}
//==== Loop through all the new papers, adding an edge from each to the new index
for (int j=0; j<Array.getLength(paperList); j++)
{
//=== Add a row for the 'ref' type links
if(paperList[j].getRefType().equals("REFERENCES"))
{
edge_table.addRow();
edge_table.setInt(rowCount, "source", newFocus);
edge_table.setInt(rowCount, "target", this.paperList[j].getDefaultNodeKey());
//edge_table.setString(rowCount, "edgeType", "ref");
rowCount++;
}
//=== Add a row for the 'cit' type links
else if(paperList[j].getRefType().equals("CITATIONS"))
{
edge_table.addRow();
edge_table.setInt(rowCount, "source", this.paperList[j].getDefaultNodeKey());
edge_table.setInt(rowCount, "target", newFocus);
//edge_table.setString(rowCount, "edgeType", "cit");
rowCount++;
}
}
return edge_table;
} //end generateNewEdgesTable
//###################################################################################################
public void createSchema()
{
//==== Create the node table schema
this.nodeSchema = new Schema();
nodeSchema.addColumn("DEFAULT_NODE_KEY", int.class);
nodeSchema.addColumn("bibcode", String.class);
nodeSchema.addColumn("title", String.class);
nodeSchema.addColumn("authorList", String.class);
nodeSchema.addColumn("date", String.class);
nodeSchema.addColumn("journal", String.class);
nodeSchema.addColumn("citCount", String.class);
nodeSchema.addColumn("abstractURL", String.class);
nodeSchema.addColumn("refType", String.class);
nodeSchema.addColumn("nodeLabel", String.class);
nodeSchema.addColumn("hoverLabel", String.class);
nodeSchema.addColumn("visible", String.class);
nodeSchema.addColumn("comment", String.class);
nodeSchema.addColumn("commentStatus", String.class);
nodeSchema.addColumn("xCoordinate", double.class);
nodeSchema.addColumn("yCoordinate", double.class);
nodeSchema.addColumn("refHidden", String.class);
nodeSchema.addColumn("citHidden", String.class);
nodeSchema.addColumn("refExpanded", String.class);
nodeSchema.addColumn("citExpanded", String.class);
//==== Create the edge table schema
this.edgeSchema = new Schema();
edgeSchema.addColumn("source", int.class);
edgeSchema.addColumn("target", int.class);
//edgeSchema.addColumn("edgeType", String.class);
}
//###################################################################################################
public Table getNodeTable()
{
return nodes;
}
//###################################################################################################
public Table getEdgeTable()
{
return edges;
}
//###################################################################################################
public Graph getGraph()
{
return thisGraph;
}
//###################################################################################################
public Paper[] getPaperArray()
{
return this.paperList;
}
}