How to manipulate attributes

sheymann edited this page Jan 4, 2015 · 1 revision
Clone this wiki locally

This Toolkit Example is part of the toolkit-demos project, that can be downloaded from the website.

Shows how to manipulate attributes. Attributes are the data associated to nodes and edges. The role of AttributeAPI is to store the different columns information.

The demo shows how to add columns and values to nodes and how to iterate.

//Init a project - and therefore a workspace
ProjectController pc = Lookup.getDefault().lookup(ProjectController.class);
Workspace workspace = pc.getCurrentWorkspace();

//Get controllers and models
ImportController importController = Lookup.getDefault().lookup(ImportController.class);

//Import file
Container container;
try {
    File file = new File(getClass().getResource("/org/gephi/toolkit/demos/resources/polblogs.gml").toURI());
    container = importController.importFile(file);
    container.getLoader().setEdgeDefault(EdgeDefault.DIRECTED);   //Force DIRECTED
    container.setAllowAutoNode(false);  //Don't create missing nodes
} catch (Exception ex) {

//Append imported data to GraphAPI
importController.process(container, new DefaultProcessor(), workspace);

//List node columns
AttributeController ac = Lookup.getDefault().lookup(AttributeController.class);
AttributeModel model = ac.getModel();
for (AttributeColumn col : model.getNodeTable().getColumns()) {

//Add boolean column
AttributeColumn testCol = model.getNodeTable().addColumn("test", AttributeType.BOOLEAN);

//Write values to nodes
GraphModel graphModel = Lookup.getDefault().lookup(GraphController.class).getModel();
for (Node n : graphModel.getGraph().getNodes()) {
    n.getNodeData().getAttributes().setValue(testCol.getIndex(), Boolean.TRUE);

//Iterate values - fastest
AttributeColumn sourceCol = model.getNodeTable().getColumn("source");
for (Node n : graphModel.getGraph().getNodes()) {

//Iterate values - normal
for (Node n : graphModel.getGraph().getNodes()) {