/** * Gets the id of an input edge. * @param edge an edge of the graph * @return id of the edge */ private Integer getEdgeId(Edge edge) { Integer id = null; for (Map.Entry<Edge, Integer> e : edgeToId.entrySet()) { boolean edgeEquals = true; for (int i = 0; i < edgeSchema.getColumnCount(); i++) { String colName = edgeSchema.getColumnName(i); if (!edge.get(colName).equals(e.getKey().get(colName))) { edgeEquals = false; break; } } if (edgeEquals) { id = e.getValue(); break; } } return id; }
@SuppressWarnings("unchecked") @Override public Iterator edges() { Collection<obvious.data.Edge> obvEdges = network.getEdges(); ArrayList<Edge> prefEdges = new ArrayList<Edge>(); for (obvious.data.Edge edge : obvEdges) { prefEdges.add(new WrapToPrefEdge(network, (TupleImpl) edge, edge.getRow())); } return prefEdges.iterator(); }
/** * Adds an edge to the graph. * @param edge edge to add * @param source source node * @param target target node * @param edgeType unused parameter * @return true if added */ public boolean addEdge(Edge edge, Node source, Node target, obvious.data.Graph.EdgeType edgeType) { try { int edgeId = graph.addEdge(getNodeId(source), getNodeId(target)); for (int i = 0; i < edge.getSchema().getColumnCount(); i++) { if (edgeSchema.hasColumn(edge.getSchema().getColumnName(i))) { TypedFormat format = formatFactory.getFormat( edge.getSchema().getColumnType(i).getSimpleName()); StringBuffer v = format.format(edge.get(i), new StringBuffer(), new FieldPosition(0)); graph.getEdgeTable().setValueAt(v.toString(), edgeId, graph.getEdgeTable().indexOf(edge.getSchema().getColumnName(i))); } } edgeToId.put(edge, edgeId); fireNetworkEvent(edge.getRow(), edge.getRow(), 0, NetworkListener.INSERT_EDGE); return true; } catch (Exception e) { throw new ObviousRuntimeException(e); } }
/** * Converts an Obvious tree to an infovis tree. * @param tree tree to convert * @return the converted infovis tree */ private infovis.Tree convertToIvtkTree(Tree<Node, Edge> tree) { if (tree.getEdges().size() != 0 && tree.getNodes().size() != 0) { Schema nodeSchema = tree.getNodes().iterator().next().getSchema() .getDataSchema(); Schema edgeSchema = tree.getEdges().iterator().next().getSchema() .getDataSchema(); Tree<Node, Edge> ivtkTree = new IvtkObviousTree(nodeSchema, edgeSchema); ObviousLib.fillTree(tree, ivtkTree); return (infovis.Tree) ivtkTree.getUnderlyingImpl(infovis.Tree.class); } else { throw new ObviousRuntimeException("Empty tree!"); } }
/** * Constructor. * @param inNetwork obvious network to wrap */ public WrapToPrefGraph(Network inNetwork) { super(new WrapToPrefTable(inNetwork.getNodeTable()), new WrapToPrefTable(inNetwork.getEdgeTable()), true); this.network = inNetwork; if (network.getNodes().size() != 0) { this.nodeSchema = network.getNodes().iterator().next().getSchema(); } if (network.getEdges().size() != 0) { this.edgeSchema = network.getEdges().iterator().next().getSchema(); } m_nodeTuples = new TupleManager(getNodeTable(), this, Table.class); m_edgeTuples = new TupleManager(getEdgeTable(), this, Table.class); }
/** * Gets the id of an input edge. * @param edge an edge of the graph * @return id of the edge */ private Integer getEdgeId(Edge edge) { Integer id = null; for (Map.Entry<Edge, Integer> e : edgeToId.entrySet()) { boolean edgeEquals = true; for (int i = 0; i < edgeSchema.getColumnCount(); i++) { String colName = edgeSchema.getColumnName(i); if (graph.getEdgeTable().getColumn(colName) != null) { if (!edge.get(colName).equals(e.getKey().get(colName))) { edgeEquals = false; break; } } } if (edgeEquals) { id = e.getValue(); break; } } return id; }
/** * Removes an edge. * @param edge edge to remove * @return true if removed */ public boolean removeEdge(Edge edge) { try { boolean removed = this.graph.removeEdge(edge.getRow()); if (removed) { fireNetworkEvent(edge.getRow(), edge.getRow(), 0, NetworkListener.DELETE_EDGE); } return removed; } catch (Exception e) { throw new ObviousRuntimeException(e); } }
@SuppressWarnings("unchecked") @Override public Iterator edges(Node node) { Collection<obvious.data.Edge> obvEdges = network.getIncidentEdges( getObviousNode(node)); ArrayList<Edge> prefEdges = new ArrayList<Edge>(); for (obvious.data.Edge edge : obvEdges) { prefEdges.add(new WrapToPrefEdge(network, (TupleImpl) edge, edge.getRow())); } return prefEdges.iterator(); }
TypedFormat format = formatFactory.getFormat( edgeSchema.getColumnType(i).getSimpleName()); StringBuffer value = format.format(edge.get(i), new StringBuffer(), new FieldPosition(0)); serializer.writeCharacters(value.toString());
@SuppressWarnings("unchecked") @Override public Iterator inEdges(Node node) { Collection<obvious.data.Edge> obvEdges = network.getInEdges( getObviousNode(node)); ArrayList<Edge> prefEdges = new ArrayList<Edge>(); for (obvious.data.Edge edge : obvEdges) { prefEdges.add(new WrapToPrefEdge(network, (TupleImpl) edge, edge.getRow())); } return prefEdges.iterator(); }
Boolean graphCol = colName.equals(sourceKey) || colName.equals( targetKey); if (colName == null || edge.get(colName) == null) { break; if (graphCol && edge.get(colName).equals(sourceKey) && nodeKey != null) { prefEdge.set(colName, source.get(nodeKey)); } else if (graphCol && edge.get(colName).equals(targetKey) && nodeKey != null) { prefEdge.set(colName, target.get(nodeKey)); } else if (graphCol && edge.get(colName).equals(sourceKey) && nodeKey == null) { prefEdge.set(colName, source.getRow()); } else if (graphCol && edge.get(colName).equals(targetKey) && nodeKey == null) { prefEdge.set(colName, target.getRow()); } else { prefEdge.set(colName, edge.get(colName));
@SuppressWarnings("unchecked") @Override public Iterator outEdges(Node node) { Collection<obvious.data.Edge> obvEdges = network.getOutEdges( getObviousNode(node)); ArrayList<Edge> prefEdges = new ArrayList<Edge>(); for (obvious.data.Edge edge : obvEdges) { prefEdges.add(new WrapToPrefEdge(network, (TupleImpl) edge, edge.getRow())); } return prefEdges.iterator(); }
/** * Loads the table with the data of external CSV files. * @return an obvious network * @throws ObviousxException when exception occurs */ public Network loadGraph() throws ObviousxException { CSVTableImport nodeImport = new CSVTableImport(nodeFile, new TableImpl(nodeSchema), separator); CSVTableImport edgeImport = new CSVTableImport(edgeFile, new TableImpl(edgeSchema), separator); Table nodeTable = nodeImport.loadTable(); Table edgeTable = edgeImport.loadTable(); for (int i = 0; i < nodeTable.getRowCount(); i++) { Node node = new NodeImpl(nodeTable, i); network.addNode(node); if (nodeId == null) { idToNode.put(node.getRow(), node); } else { idToNode.put(node.get(nodeId), node); } } for (int i = 0; i < edgeTable.getRowCount(); i++) { Edge edge = new EdgeImpl(edgeTable, i); network.addEdge(edge, idToNode.get(edge.get(sourceCol)), idToNode.get(edge.get(targetCol)), network.getEdgeType(edge)); } return this.network; }
/** * Removes an edge. * @param edge edge to remove * @return true if removed */ public boolean removeEdge(Edge edge) { try { return edgeTable.removeRow(edge.getRow()); } catch (Exception e) { throw new ObviousRuntimeException(e); } }
/** * Removes an edge. * @param edge edge to remove * @return true if removed */ public boolean removeEdge(Edge edge) { try { if (!edgeToId.containsKey(edge)) { return false; } graph.removeEdge(getEdgeId(edge)); edgeToId.remove(edge); fireNetworkEvent(edge.getRow(), edge.getRow(), 0, NetworkListener.DELETE_EDGE); return true; } catch (Exception e) { throw new ObviousRuntimeException(e); } }
/** * Get the opposite node for a couple (node,edge). * @param node spotted node * @param edge spotted edge * @return opposite node */ public Node getOpposite(Node node, Edge edge) { prefuse.data.Node prefNode = this.graph.getNodeFromKey( graph.getKey(node.getRow())); prefuse.data.Edge prefEdge = this.graph.getEdge(edge.getRow()); return new PrefuseObviousNode(this.graph.getAdjacentNode(prefEdge, prefNode)); }
/** * Gets incident edges for a spotted node. * @param edge edge spotted * @return collection of incident nodes */ public Collection<Node> getIncidentNodes(Edge edge) { Collection<Node> node = new ArrayList<Node>(); prefuse.data.Edge prefEdge = this.graph.getEdge(edge.getRow()); node.add(new PrefuseObviousNode(prefEdge.getSourceNode())); node.add(new PrefuseObviousNode(prefEdge.getTargetNode())); return node; }
@Override public int getEdge(int source, int target) { obvious.data.Node sourceNode = getObviousNode(source); obvious.data.Node targetNode = getObviousNode(target); return network.getConnectingEdge(sourceNode, targetNode).getRow(); }