All Downloads are FREE. Search and download functionalities are using the official Maven repository.

es.ucm.fdi.gaia.jcolibri.extensions.visualization.infovisual.CBGraph Maven / Gradle / Ivy

Go to download

jCOLIBRI is a java framework for the development of Case-Based Reasoning systems.

There is a newer version: 3.2
Show newest version
//
//  CBGraph.java
//  InfoVisual
//
//  Created by Josep Lluis Arcos on 11/04/07.
//  Copyright 2007 IIIA, CSIC. All rights reserved.
//

package es.ucm.fdi.gaia.jcolibri.extensions.visualization.infovisual;

/*
 * From Java. (http://java.sun.com/).
 */
import java.util.Iterator;

/*
 * From prefuse ( http://prefuse.org/ ).
 */
import prefuse.Visualization;
import prefuse.data.Edge;
import prefuse.data.Graph;
import prefuse.data.Node;
import prefuse.data.Schema;
import prefuse.visual.AggregateItem;
import prefuse.visual.AggregateTable;
import prefuse.visual.VisualGraph;
import prefuse.visual.VisualItem;

public class CBGraph extends Graph {

  //---------------------------------------------------------------
  //  PROPERTIES of the Class.
  //---------------------------------------------------------------

  /** Name node field included in generated Graphs */
  public static final String LABEL = "name";
  
  /** Class node field included in generated Graphs */
  public static final String NODE_CLASS = "class";

  /** Label edge field for storing case distances */
  public static final String DISTANCE = "distance";

  //---------------------------------------------------------------
  //  CONSTRUCTORS of the Class.
  //---------------------------------------------------------------

  /**
   * Empty constructor.
   */

  public CBGraph()
  {
   getNodeTable().addColumns(LABEL_SCHEMA);
   getEdgeTable().addColumns(EDGE_SCHEMA);
  }

  /**
   * Hand coded Graph.
   */
  public void generateGraph(){

	for ( int i=0; i<3; ++i ) {
		Node n1 = addNode();
		n1.setString(LABEL,"Case-"+Integer.toString(i*3));
		n1.setString(NODE_CLASS,Integer.toString(i));
		Node n2 = addNode();
		n2.setString(LABEL,"Case-"+Integer.toString(i*3+1));
		n2.setString(NODE_CLASS,Integer.toString(i));
		Node n3 = addNode();
		n3.setString(LABEL,"Case-"+Integer.toString(i*3+2));
		n3.setString(NODE_CLASS,Integer.toString(i));
		Edge e1 = addEdge(n1, n2);
		Edge e2 = addEdge(n1, n3);
		Edge e3 = addEdge(n2, n3);
		e1.setString(DISTANCE,"0.2");
		e2.setString(DISTANCE,"0.2");
		e3.setString(DISTANCE,"0.3");
	}
	addEdge(0, 3);
	addEdge(3, 6);
	addEdge(6, 0);
	
	getEdge(9).setString(DISTANCE,"0.5");
	getEdge(10).setString(DISTANCE,"0.7");
	getEdge(11).setString(DISTANCE,"0.5");
  }

  /**
   * Hand coded groups.
   */
  public void generateGroups(Visualization vis,VisualGraph vg){

	AggregateTable at = vis.addAggregates("aggregates");
	at.addColumn(VisualItem.POLYGON, float[].class);
	at.addColumn("id", int.class);
        
	// add nodes to aggregates
	// create an aggregate for each 3-clique of nodes
	Iterator nodes = vg.nodes();
	for ( int i=0; i<3; ++i ) {
		AggregateItem aitem = (AggregateItem)at.addItem();
		aitem.setInt("id", i);
		for ( int j=0; j<3; ++j ) {
			aitem.addItem((VisualItem)nodes.next());
		}
		if (i==0) aitem.addItem((VisualItem)vg.getNode(3));
	}
  }

  /**
   * Adding a case as a Node.
   * Returns the case identifier in the graph
   */
  public int addCase(String name, String solution){
	int index = this.addNodeRow();
	Node node = getNode(index);
	node.setString(LABEL, name);
	node.setString(NODE_CLASS, solution);
	
	return index;
  }

  /**
   * Adding a case as a Node.
   * Returns the case identifier in the graph
   */
  public int addDistance(int case1, int case2, float distance){
	int index = addEdge(case1,case2);
	Edge edge = getEdge(index);
	edge.setString(DISTANCE,Float.toString(distance));
	return index;
  }

    // ------------------------------------------------------------------------

    /** Node table schema used for generated Graphs */
    public static final Schema LABEL_SCHEMA = new Schema();
    static {
        LABEL_SCHEMA.addColumn(LABEL, String.class, "");
        LABEL_SCHEMA.addColumn(NODE_CLASS, String.class, "");
    }

    /** edge table schema used for generated Graphs */
    public static final Schema EDGE_SCHEMA = new Schema();
    static {
        EDGE_SCHEMA.addColumn(DISTANCE, String.class, "");
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy