/*
 * BasicGraph.java
 *
 * Created on February 15, 2008, 3:12 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package medusa.graph;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 *
 * @author Sean
 */
public class BasicGraph implements Serializable{
    
    // we will mostly add or remove to edges?
    private List edges = new ArrayList();
    private List uniqueEdges = new ArrayList();
    // nodes, on the other hand, need fast searching
    private Map nodes = new HashMap();
    // comment lines
    private String comment;
    /**
     * Sets the value of comment
     *
     * @param argComment Value to assign to this.comment
     */
    public final void setComment(final String argComment) {
        this.comment = argComment;
    }
    
    
    /** Creates a new instance of BasicGraph */
    public BasicGraph() {
    }
    
    
    // check if the graph is planar
    public boolean isPlanar(){
        int planar=getNodeSize()*3-6;
        return (getEdgeSize()Edge to Graph
     * @param e Edge to add
     */
    public void addEdge(Edge e){
        if ((!getEdges().contains(e)) & (e!=null)){
            //System.out.println("debug: adding edge "+e.report());
            getEdges().add(e);
            addNode(e.getFromName());
            addNode(e.getToName());
            // add unique edge, if it doesnt already exist
            UniqueEdge ue = new UniqueEdge(e);
            //System.out.println("debug: adding unique edge "+ue.report()+" "+ue.getClass());
            if (!uniqueEdges.contains(ue))
                getUniqueEdges().add(ue);
        }
    }
    
    
    //    public double[][] getSimilarityMatrix(){
    //
    //
    //    }
    
    /**
     * Adds an edge by creating it from two Nodes.
     * @param n1
     * @param n2
     * @param conf
     * @param type
     * @see Node
     */
    public void addEdge(Node n1, Node n2, float conf, int type){
        Edge e = new Edge(n1.getLabel(), n2.getLabel(), conf, type);
        if ((!getEdges().contains(e)) & (e!=null)){
            //System.out.println("debug: adding edge "+e.report());
            getEdges().add(e);
            addNode(n1);
            addNode(n2);
            // add unique edge, if it doesnt already exist
            UniqueEdge ue = new UniqueEdge(e);
            //System.out.println("debug: adding unique edge "+ue.report()+" "+ue.getClass());
            if (!uniqueEdges.contains(ue))
                getUniqueEdges().add(ue);
        }
    }
    
    
    /*public UniqueEdge[] uniqueEdgeArray(){
      return (UniqueEdge[]) uniqueEdges.toArray(new UniqueEdge[0]);
      }*/
    
//    public float[][] getFloatSimilarityMatrix(){
//        float[][] mat=new float[getNodeSize()][getNodeSize()];
//        // reset matrix
//        for(int i=0;i i=edgesIterator();i.hasNext();){
//            
//        }
//    }
    
    public void addNode(Node n){
        
        // check if graph contains a node with the same name. If not, add it.
        if (!nodes.containsKey(n.getLabel())){
            // set connections to 1 and put in hashmap
            n.addConnection();
            
            getNodes().put(n.getLabel(),n);
            //System.out.println("added node "+n.getLabel());
        }  else {
            // if contains, add connection and set node to new settings
            Node n1 = getNode(n.getLabel());
            n1.addConnection();
            //n1.copyPos(n);
            getNodes().put(n.getLabel(),n);
        }
    }
    
    /**
     *
     * @param name
     * @return
     */
    public Node getNode(String name){
        return (Node) getNodes().get(name);
    }
    
    public Node[] getNodesForEdge(Edge e){
        Node[] nodeArray=new Node[2];
        nodeArray[0]=getNode(e.getToName());
        nodeArray[1]=getNode(e.getFromName());
        
        return nodeArray;
    }
    
    //    public void renameNodes(String oldName, String newName){
    //        for (Iterator i = edgesIterator(); i.hasNext();){
    //            Edge e = (Edge) i.next();
    //            e.renameNode(oldName,newName);
    //        }
    //        renameNode(oldName,newName);
    //    }
    
    public void addNode(String name){
        Node n = new Node(name);
        n.addConnection();
        if (!nodes.containsKey(name)){
            getNodes().put(name,n);
            //System.out.println("added node "+n.getLabel());
        }  else{
            n = getNode(name);
            //System.out.println("increased linkage in node "+n.getLabel());
            n.addConnection();
            getNodes().put(name,n);
            
        }
    }
    
    
    public String getComment(){
        return comment;
    }
    
    public void clear(){
        getEdges().clear();
        getNodes().clear();
        getUniqueEdges().clear();
        setComment("");
    }
    
    public void addComment(String line){
        if (line==null)
            return;
        if (line.length()>1)
            setComment(line);
    }
    
    /*public Graph(Graph g){
      edges=g.getEdgeAL();
      uniqueEdges=g.getUniqueEdges();
     */
    
    
    // add an internal edge, i.e, add edge only if
    // both nodes are already connected
    /*public void addInternalEdge(Edge e){
      if (edges.contains(e))
      return;
      Object n1 = nodes.get(new Node(e.n1));
      Object n2 = nodes.get(new Node(e.n2));
      if ( (n1!=null) & (n2!=null) )
      addEdge(e);
      }*/
    
    
    public void removeEdge(Edge e){
        if (!getEdges().contains(e))
            return;
        getEdges().remove(e);
        removeNode(e.getFromName());
        removeNode(e.getToName());
        UniqueEdge ue = new UniqueEdge(e);
        if (!getEdges().contains(ue))
            getUniqueEdges().remove(ue);
    }
    

    
    private void removeNode(String name){
        //System.out.println("removing node "+name);
        Node n = getNode(name);
        getNodes().remove(name);
        if (n.removeConnection()){
            //System.out.println("replacing node "+n.name);
            getNodes().put(name,n);
        }
    }
    
    private void removeNode(Node n){
        removeNode(n.getLabel());
    }
    
    /**
     * Gets an iterator of all Nodes in the graph
     * @return
     */
    public Iterator nodesIterator(){
        
        return getNodes().values().iterator();
    }
    
    /**
     * Returns an iterator of all Edges in the graph
     * @return
     */
    public Iterator edgesIterator(){
        return getEdges().iterator();
    }
    /**
     * Copies all node settings from one graph to another
     * @param temp the graph with the node settings
     */
    public void copyNodeSettings(BasicGraph temp){
        if (temp==null)
            return;
        for (Iterator i = temp.nodesIterator(); i.hasNext();){
            Node n = (Node)i.next();
            setNode(n);
        }
    }
    
    public String report(){
        StringBuffer sb=new StringBuffer();
        if (getComment()!=null){
            sb.append(getComment());
            sb.append("\n");
        }
        
        sb.append("*edges");
        for(Iterator i = edgesIterator();i.hasNext();){
            Edge e = (Edge)i.next();
            sb.append("\n");
            sb.append(e.report());
            
        }
        
        sb.append("\n*nodes\n");
        for(Iterator i = nodesIterator();i.hasNext();){
            Node n = (Node)i.next();
            sb.append(n.report());
            sb.append("\n");
        }
        
        return sb.toString();
    }
    
    public void setNode(Node n){
        if (getNodes().containsKey(n.getLabel())){
            Node n2 = getNode(n.getLabel());
            /*System.out.println("color changed from "+n2.color+" to "+
              n.color);*/
            n2.setColor(n.getColor());
            n2.setShape(n.getShape());
            //System.out.println("Setting "+n2.getLabel()+" to "+n.getX()
            //		       +" "+n.getY());
            n2.setX(n.getX());
            n2.setY(n.getY());
            n2.setAnnotation(n.getAnnotation());
        }
    }
    
    public Iterator uniqueEdgesIterator(){
        return getUniqueEdges().iterator();
    }
    
    public List getEdges() {
        return edges;
    }
    
    public int getNodeSize(){
        return getNodes().size();
    }
    public int getEdgeSize(){
        return getEdges().size();
    }
    public int getUniqueEdgeSize(){
        return getUniqueEdges().size();
    }

    public List getUniqueEdges() {
        return uniqueEdges;
    }

    public Map getNodes() {
        return nodes;
    }
    
}

 

Ads help maintain this website.