/** * Returns a graph structured as an m-by-n grid. * @param m the number of rows of the grid * @param n the number of columns of the grid * @return an m-by-n grid structured graph */ public static Graph getGrid(int m, int n) { Graph g = new Graph(); g.getNodeTable().addColumns(LABEL_SCHEMA); Node[] nodes = new Node[m*n]; for ( int i = 0; i < m*n; ++i ) { nodes[i] = g.addNode(); nodes[i].setString(LABEL, String.valueOf(i)); if ( i >= n ) g.addEdge(nodes[i-n], nodes[i]); if ( i % n != 0 ) g.addEdge(nodes[i-1], nodes[i]); } return g; }
/* package */ GraphBuilder() { graph = new Graph( true ); Table nodeTable = graph.getNodeTable(); nodeTable.addColumn( GraphDisplay.NAME_LABEL, String.class ); nodeTable.addColumn( GraphDisplay.USER_OBJECT, Object.class ); Table edgeTable = graph.getEdgeTable(); edgeTable.addColumn( GraphDisplay.USES_EDGES, boolean.class, false ); childList = new ArrayList(); }
private Graph getGraph(final Schema schema) { graph = new Graph(true); graph.addColumns(schema); return graph; }
/** * Get the source node id (node table row number) for the given edge * id (edge table row number). * @param edge an edge id (edge table row number) * @return the source node id (node table row number) */ public int getSourceNode(int edge) { return getNodeIndex(getEdgeTable().getLong(edge, m_skey)); }
/** * Builds a completely unconnected (edge-free) graph with the given * number of nodes * @param n the number of nodes * @return a graph with n nodes and no edges */ public static Graph getNodes(int n) { Graph g = new Graph(); g.getNodeTable().addColumns(LABEL_SCHEMA); for ( int i=0; i < n; i++ ) { Node node = g.addNode(); node.setString(LABEL, String.valueOf(i)); } return g; }
public static Graph getHoneycomb(int levels) { Graph g = new Graph(); g.getNodeTable().addColumns(LABEL_SCHEMA); ArrayList layer1 = halfcomb(g, levels); ArrayList layer2 = halfcomb(g, levels); for ( int i=0; i<(levels<<1); ++i ) { Node n1 = (Node)layer1.get(i); Node n2 = (Node)layer2.get(i); g.addEdge(n1, n2); } return g; }
Schema ns = graph.getNodeTable().getSchema(); Schema es = graph.getEdgeTable().getSchema(); checkGraphMLSchema(ns); checkGraphMLSchema(es); graph.getEdgeSourceField(), graph.getEdgeTargetField() }); xml.println(); graph.isDirected() ? Tokens.DIRECTED : Tokens.UNDIRECTED); Iterator nodes = graph.nodes(); while ( nodes.hasNext() ) { Node n = (Node)nodes.next(); Iterator edges = graph.edges(); while ( edges.hasNext() ) { Edge e = (Edge)edges.next(); for ( int i=0; i<es.getColumnCount(); ++i ) { String field = es.getColumnName(i); if ( field.equals(graph.getEdgeSourceField()) || field.equals(graph.getEdgeTargetField()) ) continue;
private Node addChild( Node parent, String name, Object object ) { Node childNode = graph.addNode(); childNode.set( GraphDisplay.NAME_LABEL, name ); childNode.set( GraphDisplay.USER_OBJECT, object ); // check for application node if( parent != null ) { graph.addEdge( parent, childNode ); } return childNode; }
nt = addTable(nGroup, graph.getNodeTable(), filter, nodeSchema); et = addTable(eGroup, graph.getEdgeTable(), filter, edgeSchema); graph.isDirected(), graph.getNodeKeyField(), graph.getEdgeSourceField(), graph.getEdgeTargetField()); vg.setVisualization(this); vg.setGroup(group);
/** * Add a child edge between the given nodes. * @param parent the parent node id (node table row number) * @param child the child node id (node table row number) * @return the added child edge id */ public int addChildEdge(int parent, int child) { return super.addEdge(parent, child); }
public void tableChanged(Table t, int start, int end, int col, int type) { if ( !containsSet(t) ) throw new IllegalStateException( "Graph shouldn't be listening to an unrelated table"); if ( type != EventConstants.UPDATE ) { if ( t == getNodeTable() ) { // update the linkage structure table if ( col == EventConstants.ALL_COLUMNS ) { boolean added = type==EventConstants.INSERT; for ( int r=start; r<=end; ++r ) updateNodeData(r, added); } } else { // update the linkage structure table if ( col == EventConstants.ALL_COLUMNS ) { boolean added = type==EventConstants.INSERT; for ( int r=start; r<=end; ++r ) updateDegrees(start, added?1:-1); } } // clear the spanning tree reference m_spanning = null; } fireGraphEvent(t, start, end, col, type); }
/** * Dispose of this graph. Unregisters this graph as a listener to its * included tables. */ public void dispose() { getNodeTable().removeTableListener(m_listener); getEdgeTable().removeTableListener(m_listener); }
private Node addNode(final Class type) { if (type != null && !nodes.containsKey(type)) { Node node = graph.addNode(); node.set("type", type); nodes.put(type, node); } return (Node) nodes.get(type); }
/** * Internal method for updating the linkage of this graph. * @param e the edge id for the updated link * @param incr the increment value, 1 for an added link, * -1 for a removed link */ protected void updateDegrees(int e, int incr) { if ( !getEdgeTable().isValidRow(e) ) return; int s = getSourceNode(e); int t = getTargetNode(e); if ( s < 0 || t < 0 ) return; updateDegrees(e, s, t, incr); if ( incr < 0 ) { m_edgeTuples.invalidate(e); } }
prefEdge = graph.addEdge(graph.getNode(source.getRow()), graph.getNode(target.getRow())); } else { prefEdge = graph.addEdge(graph.getNodeFromKey(source.getInt(nodeKey)), graph.getNodeFromKey(target.getInt(nodeKey)));
/** * Updates this graph to use a different edge structure for the same nodes. * All other settings will remain the same (e.g., directionality, keys) * @param edges the new edge table. */ public void setEdgeTable(Table edges) { Table oldEdges = getEdgeTable(); oldEdges.removeTableListener(m_listener); m_edgeTuples.invalidateAll(); m_links.clear(); init(getNodeTable(), edges, m_directed, m_nkey, m_skey, m_tkey); }
/** * Get a filtered iterator over the edges and nodes of this graph. * @see prefuse.data.tuple.TupleSet#tuples(prefuse.data.expression.Predicate) */ public Iterator tuples(Predicate filter) { if ( filter == null ) { return tuples(); } else { return new CompositeIterator( m_edgeTuples.iterator(getEdgeTable().rows(filter)), m_nodeTuples.iterator(getNodeTable().rows(filter))); } }
/** * Initialize the link table, which holds adjacency lists for this graph. */ protected void initLinkTable() { // set up cache of node data m_links = createLinkTable(); IntIterator edges = getEdgeTable().rows(); while ( edges.hasNext() ) { updateDegrees(edges.nextInt(), 1); } }