Commit dab82f38 authored by stram's avatar stram
Browse files

Making the graph more generic

git-svn-id: https://dynaq.opendfki.de/repos/application/trunk@1053 8b50620d-ddf8-0310-9f5d-8b2f31fd9ac4
parent 4cc311b8
......@@ -67,6 +67,11 @@ import prefuse.util.ColorLib;
import prefuse.util.UpdateListener;
import prefuse.visual.VisualItem;
/*
* Displays a visual graph for DynaQ documents. Currently displays the collaboration graph.
* If a different graph is needed, the class UnderlyingGraph should be extended
* and the new class should be instanced in getGraph.
*/
public class DynaQDocumentGraph extends JPanel implements ActionListener, ComponentListener{
private static final long serialVersionUID = -3576920818494441486L;
......@@ -154,13 +159,19 @@ public class DynaQDocumentGraph extends JPanel implements ActionListener, Compon
}
}
/*
* Returns the prefuse graph for the given documents.
* Currently uses the collaboration graph. If a difference attribute from the
* documents is needed, the abstract class Underlying Graph should be extended
* and referenced here.
*/
public Graph getGraph(List<DynaQDocument> documents){
graph = null;
if(documents==null){
List<DynaQDocument> list = getResults();
graph = new UnderlyingGraph(list).getGraph();
graph = new UnderlyingGraphCollaboration(list).getGraph();
} else {
graph = new UnderlyingGraph(documents).getGraph();
graph = new UnderlyingGraphCollaboration(documents).getGraph();
}
return graph;
}
......
package org.dynaq.documents.graph;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
//import java.util.List;
import java.util.Map;
import org.dynaq.core.DynaQDocument;
import prefuse.data.Edge;
import prefuse.data.Graph;
import prefuse.data.Node;
import prefuse.data.Tuple;
public class UnderlyingGraph {
private List<DynaQDocument> docList;
private Graph graph;
private Map<String,Node> creatorMap;
public abstract class UnderlyingGraph {
Graph graph;
Map<String,Node> attributeMap;
private static final String CREATOR = "dc:creator";
static final String DATE = "date";
static final String DATE = "date"; //indicates the date the attribute first appeared
static final String ATTRIBUTE = "name"; //the name of the attribute that is used for the nodes
static final String NUM_DATE_EDGES = "num_date_edges"; //an int value of an edge for a node according to the DateSlider
static final String NUM_DATE_NODES = "num_date_nodes"; //an int value of a node for a node according to the DateSlider
static final String NAME = "name";
static final String DEGREE = "degree";
static final String NUM_DATE_NODES = "num_date_nodes"; //an int value of a node for a node according to the DateSlider
static final String DEGREE = "degree"; //refers to the degree of a node, including multiple edges
static final String EDGE_COUNT = "edge_count";
public UnderlyingGraph(List<DynaQDocument> documentList){
docList = documentList;
createCollaborationGraph();
}
private void createCollaborationGraph(){
/*
* initializes the graphs with the needed columns
*/
void createGraph(){
graph = new Graph();
creatorMap = new HashMap<String,Node>();
attributeMap = new HashMap<String,Node>();
//defining columns
graph.addColumn(NAME, String.class);
graph.addColumn(ATTRIBUTE, String.class);
graph.addColumn(DEGREE, int.class);
graph.addColumn(DATE, Date.class);
graph.addColumn(NUM_DATE_EDGES, int.class);
graph.addColumn(NUM_DATE_NODES, int.class);
graph.addColumn(EDGE_COUNT, int.class);
for(DynaQDocument doc: docList){
List<String> creators = doc.getAttributeValues(CREATOR);
List<String> dates = doc.getAttributeValues(DATE);
if(creators!=null && creators.size()>0){
creators = parseAuthors(creators.get(0));
cleanList(creators);
Date date = null;
if(dates!=null && dates.size()>0){
String sdate = dates.get(0);
date = getDateFromString(sdate);
}
//updating the authors information, including adding new nodes
for(String authorName:creators){
String pAuthorName = authorName.toLowerCase().trim();
if(creatorMap.containsKey(pAuthorName)){ //updating author date in case already exists
Node authorNode = creatorMap.get(pAuthorName);
Date authorDate = authorNode.getDate(DATE);
if(date!=null && date.before(authorDate)){
authorNode.setDate(DATE, date);
}
} else { //creating author node
Node authorNode = graph.addNode();
authorNode.setString(NAME, authorName);
authorNode.setInt(DEGREE, 0);
if(date!=null){
authorNode.setDate(DATE, date);
}
creatorMap.put(pAuthorName, authorNode);
}
}
//creating edges between authors if they co-wrote this paper
if(creators.size()>1){
for(int i=0; i<(creators.size()-1); i++){
String author1name = creators.get(i);
Node author1 = creatorMap.get(author1name.toLowerCase().trim());
for(int j=i+1; j<creators.size(); j++){
String author2name = creators.get(j);
Node author2 = creatorMap.get(author2name.toLowerCase().trim());
author1.setInt(DEGREE, author1.getInt(DEGREE)+1);
author2.setInt(DEGREE, author2.getInt(DEGREE)+1);
addEdgeToGraph(graph,author1,author2,date);
}
}
}
}
}
}
@SuppressWarnings({ "rawtypes", "unused" })
private Graph rebuildGraph(Graph graph){
Graph ng = new Graph();
//defining columns
ng.addColumn(NAME, String.class);
ng.addColumn(DEGREE, int.class);
ng.addColumn(DATE, Date.class);
ng.addColumn(EDGE_COUNT, int.class);
//adding nodes
Iterator nodes = graph.getNodes().tuples();
while(nodes.hasNext()){
Tuple node = (Tuple) nodes.next();
String author = node.getString(NAME);
if(author!=null && author!="null" && author.length()>0){
int degree = node.getInt(DEGREE);
Date date = node.getDate(DATE);
Node newNode = addNodeToGraph(ng, author, degree, date);
creatorMap.put(author, newNode);
}
}
//adding edges
Iterator edges = graph.edges();
while(edges.hasNext()){
Edge edge = (Edge)(Tuple) edges.next();
Node source = edge.getSourceNode();
Node target = edge.getTargetNode();
String sourceAuthor = source.getString(NAME);
String targetAuthor = target.getString(NAME);
Node nSource = creatorMap.get(sourceAuthor);
Node nTarget = creatorMap.get(targetAuthor);
Edge newEdge = ng.addEdge(nSource, nTarget);
newEdge.setInt(EDGE_COUNT, 0);
}
return ng;
}
/*
* adding the nodes and edges to the graph
*/
abstract void populateGraph();
private Edge addEdgeToGraph(Graph graph, Node source, Node target, Date date){
Edge addEdgeToGraph(Graph graph, Node source, Node target, Date date){
Edge edge = graph.addEdge(source, target);
if(date!=null){
edge.setDate(DATE, date);
......@@ -145,71 +50,14 @@ public class UnderlyingGraph {
return edge;
}
private Node addNodeToGraph(Graph graph, String author, int degree, Date date){
Node addNodeToGraph(Graph graph, String attribute, int degree, Date date){
Node node = graph.addNode();
node.setString(NAME, author);
node.setString(ATTRIBUTE, attribute);
node.setDate(DATE, date);
node.setInt(DEGREE, degree);
return node;
}
/*
* receives a String date in the form of yyyymmdd*
* returns a Date instance
*/
private Date getDateFromString(String sdate){
if(sdate!=null && sdate.length()>=8){
String syear = sdate.substring(0, 4);
String smonth = sdate.substring(4, 6);
String sday = sdate.substring(6, 8);
try{
int year = Integer.valueOf(syear);
int month = Integer.valueOf(smonth);
int day = Integer.valueOf(sday);
Calendar calendar = new GregorianCalendar();
calendar.set(year, month, day);
return calendar.getTime();
} catch(Exception e){
return null;
}
} else {
return null;
}
}
/*
* receives a list of authors in one string
* returns a list of all the authors
*/
private List<String> parseAuthors(String allAuthors){
String[] authorArray = allAuthors.split("\\s[aA][nN][dD]\\s|,");
List<String> list = new LinkedList<String>();
for(String author: authorArray){
if(author.length()>1){
if(author.startsWith(" ")){
list.add(author.replaceFirst("\\s", ""));
} else {
list.add(author);
}
}
}
return list;
}
/*
* removes all the null instances of a list of strings
*/
private void cleanList(List<String> list){
boolean wasRemoved = list.remove(null);
while(wasRemoved){
wasRemoved = list.remove(null);
}
}
/*
* updates the NUM_DATE column of the graph for both nodes and edges
*/
......
package org.dynaq.documents.graph;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
//import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
//import java.util.Map;
import org.dynaq.core.DynaQDocument;
import prefuse.data.Edge;
import prefuse.data.Graph;
import prefuse.data.Node;
import prefuse.data.Tuple;
public class UnderlyingGraphCollaboration extends UnderlyingGraph {
private List<DynaQDocument> docList;
/*
private Graph graph;
private Map<String,Node> attributeMap;
*/
static final String CREATOR = "dc:creator";
// static final String DATE = "date";
// static final String ATTRIBUTE = "name";
/*
static final String NUM_DATE_EDGES = "num_date_edges"; //an int value of an edge for a node according to the DateSlider
static final String NUM_DATE_NODES = "num_date_nodes"; //an int value of a node for a node according to the DateSlider
static final String NAME = "name";
static final String DEGREE = "degree";
static final String EDGE_COUNT = "edge_count";
*/
public UnderlyingGraphCollaboration(List<DynaQDocument> documentList){
docList = documentList;
createGraph();
populateGraph();
}
void populateGraph(){
for(DynaQDocument doc: docList){
List<String> creators = doc.getAttributeValues(CREATOR);
List<String> dates = doc.getAttributeValues(DATE); //if the date attribute of the document has a different name, change it here
if(creators!=null && creators.size()>0){
creators = parseAuthors(creators.get(0));
cleanList(creators);
Date date = null;
if(dates!=null && dates.size()>0){
String sdate = dates.get(0);
date = getDateFromString(sdate);
}
//updating the authors information, including adding new nodes
for(String authorName:creators){
String pAuthorName = authorName.toLowerCase().trim();
if(attributeMap.containsKey(pAuthorName)){ //updating author date in case already exists
Node authorNode = attributeMap.get(pAuthorName);
Date authorDate = authorNode.getDate(DATE);
if(date!=null && date.before(authorDate)){
authorNode.setDate(DATE, date);
}
} else { //creating author node
Node authorNode = graph.addNode();
authorNode.setString(ATTRIBUTE, authorName);
authorNode.setInt(DEGREE, 0);
if(date!=null){
authorNode.setDate(DATE, date);
}
attributeMap.put(pAuthorName, authorNode);
}
}
//creating edges between authors if they co-wrote this paper
if(creators.size()>1){
for(int i=0; i<(creators.size()-1); i++){
String author1name = creators.get(i);
Node author1 = attributeMap.get(author1name.toLowerCase().trim());
for(int j=i+1; j<creators.size(); j++){
String author2name = creators.get(j);
Node author2 = attributeMap.get(author2name.toLowerCase().trim());
author1.setInt(DEGREE, author1.getInt(DEGREE)+1);
author2.setInt(DEGREE, author2.getInt(DEGREE)+1);
addEdgeToGraph(graph,author1,author2,date);
}
}
}
}
}
}
/*
private void createGraph(){
graph = new Graph();
attributeMap = new HashMap<String,Node>();
//defining columns
graph.addColumn(NAME, String.class);
graph.addColumn(DEGREE, int.class);
graph.addColumn(DATE, Date.class);
graph.addColumn(NUM_DATE_EDGES, int.class);
graph.addColumn(NUM_DATE_NODES, int.class);
graph.addColumn(EDGE_COUNT, int.class);
for(DynaQDocument doc: docList){
List<String> creators = doc.getAttributeValues(ATTRIBUTE);
List<String> dates = doc.getAttributeValues(DATE);
if(creators!=null && creators.size()>0){
creators = parseAuthors(creators.get(0));
cleanList(creators);
Date date = null;
if(dates!=null && dates.size()>0){
String sdate = dates.get(0);
date = getDateFromString(sdate);
}
//updating the authors information, including adding new nodes
for(String authorName:creators){
String pAuthorName = authorName.toLowerCase().trim();
if(attributeMap.containsKey(pAuthorName)){ //updating author date in case already exists
Node authorNode = attributeMap.get(pAuthorName);
Date authorDate = authorNode.getDate(DATE);
if(date!=null && date.before(authorDate)){
authorNode.setDate(DATE, date);
}
} else { //creating author node
Node authorNode = graph.addNode();
authorNode.setString(NAME, authorName);
authorNode.setInt(DEGREE, 0);
if(date!=null){
authorNode.setDate(DATE, date);
}
attributeMap.put(pAuthorName, authorNode);
}
}
//creating edges between authors if they co-wrote this paper
if(creators.size()>1){
for(int i=0; i<(creators.size()-1); i++){
String author1name = creators.get(i);
Node author1 = attributeMap.get(author1name.toLowerCase().trim());
for(int j=i+1; j<creators.size(); j++){
String author2name = creators.get(j);
Node author2 = attributeMap.get(author2name.toLowerCase().trim());
author1.setInt(DEGREE, author1.getInt(DEGREE)+1);
author2.setInt(DEGREE, author2.getInt(DEGREE)+1);
addEdgeToGraph(graph,author1,author2,date);
}
}
}
}
}
}
*/
@SuppressWarnings({ "rawtypes", "unused" })
private Graph rebuildGraph(Graph graph){
Graph ng = new Graph();
//defining columns
ng.addColumn(ATTRIBUTE, String.class);
ng.addColumn(DEGREE, int.class);
ng.addColumn(DATE, Date.class);
ng.addColumn(EDGE_COUNT, int.class);
//adding nodes
Iterator nodes = graph.getNodes().tuples();
while(nodes.hasNext()){
Tuple node = (Tuple) nodes.next();
String author = node.getString(ATTRIBUTE);
if(author!=null && author!="null" && author.length()>0){
int degree = node.getInt(DEGREE);
Date date = node.getDate(DATE);
Node newNode = addNodeToGraph(ng, author, degree, date);
attributeMap.put(author, newNode);
}
}
//adding edges
Iterator edges = graph.edges();
while(edges.hasNext()){
Edge edge = (Edge)(Tuple) edges.next();
Node source = edge.getSourceNode();
Node target = edge.getTargetNode();
String sourceAuthor = source.getString(ATTRIBUTE);
String targetAuthor = target.getString(ATTRIBUTE);
Node nSource = attributeMap.get(sourceAuthor);
Node nTarget = attributeMap.get(targetAuthor);
Edge newEdge = ng.addEdge(nSource, nTarget);
newEdge.setInt(EDGE_COUNT, 0);
}
return ng;
}
/*
private Edge addEdgeToGraph(Graph graph, Node source, Node target, Date date){
Edge edge = graph.addEdge(source, target);
if(date!=null){
edge.setDate(DATE, date);
}
return edge;
}
private Node addNodeToGraph(Graph graph, String author, int degree, Date date){
Node node = graph.addNode();
node.setString(NAME, author);
node.setDate(DATE, date);
node.setInt(DEGREE, degree);
return node;
}
*/
/*
* receives a String date in the form of yyyymmdd*
* returns a Date instance
*/
private Date getDateFromString(String sdate){
if(sdate!=null && sdate.length()>=8){
String syear = sdate.substring(0, 4);
String smonth = sdate.substring(4, 6);
String sday = sdate.substring(6, 8);
try{
int year = Integer.valueOf(syear);
int month = Integer.valueOf(smonth);
int day = Integer.valueOf(sday);
Calendar calendar = new GregorianCalendar();
calendar.set(year, month, day);
return calendar.getTime();
} catch(Exception e){
return null;
}
} else {
return null;
}
}
/*
* receives a list of authors in one string
* returns a list of all the authors
*/
private List<String> parseAuthors(String allAuthors){
String[] authorArray = allAuthors.split("\\s[aA][nN][dD]\\s|,");
List<String> list = new LinkedList<String>();
for(String author: authorArray){
if(author.length()>1){
if(author.startsWith(" ")){
list.add(author.replaceFirst("\\s", ""));
} else {
list.add(author);
}
}
}
return list;
}
/*
* removes all the null instances of a list of strings
*/
private void cleanList(List<String> list){
boolean wasRemoved = list.remove(null);
while(wasRemoved){
wasRemoved = list.remove(null);
}
}
/*
* updates the NUM_DATE column of the graph for both nodes and edges
*/
/*
@SuppressWarnings("rawtypes")
public static Graph updateGraphDate(Graph graph, DateSlider slider){
Iterator nodes = graph.nodes();
Iterator edges = graph.edges();
while(nodes.hasNext()){
Node node = (Node) nodes.next();
Date date = node.getDate(DATE);
int numDate = slider.getInt(date);
node.setInt(NUM_DATE_NODES, numDate);
}
while(edges.hasNext()){
Edge edge = (Edge) edges.next();
Date date = edge.getDate(DATE);
int numDate = slider.getInt(date);
edge.setInt(NUM_DATE_EDGES, numDate);
}
return graph;
}
*/
/*
public Graph getGraph(){
return graph;
}
*/
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment