001//
002//  CBGraph.java
003//  InfoVisual
004//
005//  Created by Josep Lluis Arcos on 11/04/07.
006//  Copyright 2007 IIIA, CSIC. All rights reserved.
007//
008
009package es.ucm.fdi.gaia.jcolibri.extensions.visualization.infovisual;
010
011/*
012 * From Java. (http://java.sun.com/).
013 */
014import java.util.Iterator;
015
016/*
017 * From prefuse ( http://prefuse.org/ ).
018 */
019import prefuse.Visualization;
020import prefuse.data.Edge;
021import prefuse.data.Graph;
022import prefuse.data.Node;
023import prefuse.data.Schema;
024import prefuse.visual.AggregateItem;
025import prefuse.visual.AggregateTable;
026import prefuse.visual.VisualGraph;
027import prefuse.visual.VisualItem;
028
029public class CBGraph extends Graph {
030
031  //---------------------------------------------------------------
032  //  PROPERTIES of the Class.
033  //---------------------------------------------------------------
034
035  /** Name node field included in generated Graphs */
036  public static final String LABEL = "name";
037  
038  /** Class node field included in generated Graphs */
039  public static final String NODE_CLASS = "class";
040
041  /** Label edge field for storing case distances */
042  public static final String DISTANCE = "distance";
043
044  //---------------------------------------------------------------
045  //  CONSTRUCTORS of the Class.
046  //---------------------------------------------------------------
047
048  /**
049   * Empty constructor.
050   */
051
052  public CBGraph()
053  {
054   getNodeTable().addColumns(LABEL_SCHEMA);
055   getEdgeTable().addColumns(EDGE_SCHEMA);
056  }
057
058  /**
059   * Hand coded Graph.
060   */
061  public void generateGraph(){
062
063        for ( int i=0; i<3; ++i ) {
064                Node n1 = addNode();
065                n1.setString(LABEL,"Case-"+Integer.toString(i*3));
066                n1.setString(NODE_CLASS,Integer.toString(i));
067                Node n2 = addNode();
068                n2.setString(LABEL,"Case-"+Integer.toString(i*3+1));
069                n2.setString(NODE_CLASS,Integer.toString(i));
070                Node n3 = addNode();
071                n3.setString(LABEL,"Case-"+Integer.toString(i*3+2));
072                n3.setString(NODE_CLASS,Integer.toString(i));
073                Edge e1 = addEdge(n1, n2);
074                Edge e2 = addEdge(n1, n3);
075                Edge e3 = addEdge(n2, n3);
076                e1.setString(DISTANCE,"0.2");
077                e2.setString(DISTANCE,"0.2");
078                e3.setString(DISTANCE,"0.3");
079        }
080        addEdge(0, 3);
081        addEdge(3, 6);
082        addEdge(6, 0);
083        
084        getEdge(9).setString(DISTANCE,"0.5");
085        getEdge(10).setString(DISTANCE,"0.7");
086        getEdge(11).setString(DISTANCE,"0.5");
087  }
088
089  /**
090   * Hand coded groups.
091   */
092  public void generateGroups(Visualization vis,VisualGraph vg){
093
094        AggregateTable at = vis.addAggregates("aggregates");
095        at.addColumn(VisualItem.POLYGON, float[].class);
096        at.addColumn("id", int.class);
097        
098        // add nodes to aggregates
099        // create an aggregate for each 3-clique of nodes
100        Iterator<?> nodes = vg.nodes();
101        for ( int i=0; i<3; ++i ) {
102                AggregateItem aitem = (AggregateItem)at.addItem();
103                aitem.setInt("id", i);
104                for ( int j=0; j<3; ++j ) {
105                        aitem.addItem((VisualItem)nodes.next());
106                }
107                if (i==0) aitem.addItem((VisualItem)vg.getNode(3));
108        }
109  }
110
111  /**
112   * Adding a case as a Node.
113   * Returns the case identifier in the graph
114   */
115  public int addCase(String name, String solution){
116        int index = this.addNodeRow();
117        Node node = getNode(index);
118        node.setString(LABEL, name);
119        node.setString(NODE_CLASS, solution);
120        
121        return index;
122  }
123
124  /**
125   * Adding a case as a Node.
126   * Returns the case identifier in the graph
127   */
128  public int addDistance(int case1, int case2, float distance){
129        int index = addEdge(case1,case2);
130        Edge edge = getEdge(index);
131        edge.setString(DISTANCE,Float.toString(distance));
132        return index;
133  }
134
135    // ------------------------------------------------------------------------
136
137    /** Node table schema used for generated Graphs */
138    public static final Schema LABEL_SCHEMA = new Schema();
139    static {
140        LABEL_SCHEMA.addColumn(LABEL, String.class, "");
141        LABEL_SCHEMA.addColumn(NODE_CLASS, String.class, "");
142    }
143
144    /** edge table schema used for generated Graphs */
145    public static final Schema EDGE_SCHEMA = new Schema();
146    static {
147        EDGE_SCHEMA.addColumn(DISTANCE, String.class, "");
148    }
149
150
151}