Search in sources :

Example 21 with NodeDraft

use of org.gephi.io.importer.api.NodeDraft in project gephi by gephi.

the class DefaultProcessor method process.

protected void process(ContainerUnloader container, Workspace workspace) {
    //Architecture
    GraphController graphController = Lookup.getDefault().lookup(GraphController.class);
    graphModel = graphController.getGraphModel(workspace);
    //Get graph
    Graph graph = graphModel.getGraph();
    GraphFactory factory = graphModel.factory();
    //Time Format & Time zone
    graphModel.setTimeFormat(container.getTimeFormat());
    graphModel.setTimeZone(container.getTimeZone());
    //Progress
    Progress.start(progressTicket, container.getNodeCount() + container.getEdgeCount());
    //Attributes - Creates columns for properties
    flushColumns(container);
    //Counters
    int addedNodes = 0, addedEdges = 0;
    //Create all nodes
    ElementIdType elementIdType = container.getElementIdType();
    for (NodeDraft draftNode : container.getNodes()) {
        String idString = draftNode.getId();
        Object id = toElementId(elementIdType, idString);
        Node node = graph.getNode(id);
        boolean newNode = false;
        if (node == null) {
            node = factory.newNode(id);
            addedNodes++;
            newNode = true;
        }
        flushToNode(draftNode, node);
        if (newNode) {
            graph.addNode(node);
        }
        Progress.progress(progressTicket);
    }
    //Create all edges and push to data structure
    for (EdgeDraft draftEdge : container.getEdges()) {
        String idString = draftEdge.getId();
        Object id = toElementId(elementIdType, idString);
        String sourceId = draftEdge.getSource().getId();
        String targetId = draftEdge.getTarget().getId();
        Node source = graph.getNode(toElementId(elementIdType, sourceId));
        Node target = graph.getNode(toElementId(elementIdType, targetId));
        Object type = draftEdge.getType();
        int edgeType = graphModel.addEdgeType(type);
        Edge edge = graph.getEdge(source, target, edgeType);
        boolean newEdge = false;
        if (edge == null) {
            switch(container.getEdgeDefault()) {
                case DIRECTED:
                    edge = factory.newEdge(id, source, target, edgeType, draftEdge.getWeight(), true);
                    break;
                case UNDIRECTED:
                    edge = factory.newEdge(id, source, target, edgeType, draftEdge.getWeight(), false);
                    break;
                case MIXED:
                    boolean directed = draftEdge.getDirection() == null || !draftEdge.getDirection().equals(EdgeDirection.UNDIRECTED);
                    edge = factory.newEdge(id, source, target, edgeType, draftEdge.getWeight(), directed);
                    break;
            }
            addedEdges++;
            newEdge = true;
        }
        flushToEdge(draftEdge, edge);
        if (newEdge) {
            graph.addEdge(edge);
        }
        Progress.progress(progressTicket);
    }
    //Report
    int touchedNodes = container.getNodeCount();
    int touchedEdges = container.getEdgeCount();
    if (touchedNodes != addedNodes || touchedEdges != addedEdges) {
        Logger.getLogger(getClass().getSimpleName()).log(Level.INFO, "# Nodes loaded: {0} ({1} added)", new Object[] { touchedNodes, addedNodes });
        Logger.getLogger(getClass().getSimpleName()).log(Level.INFO, "# Edges loaded: {0} ({1} added)", new Object[] { touchedEdges, addedEdges });
    } else {
        Logger.getLogger(getClass().getSimpleName()).log(Level.INFO, "# Nodes loaded: {0}", new Object[] { touchedNodes });
        Logger.getLogger(getClass().getSimpleName()).log(Level.INFO, "# Edges loaded: {0}", new Object[] { touchedEdges });
    }
    Progress.finish(progressTicket);
}
Also used : ElementIdType(org.gephi.io.importer.api.ElementIdType) GraphFactory(org.gephi.graph.api.GraphFactory) EdgeDraft(org.gephi.io.importer.api.EdgeDraft) Graph(org.gephi.graph.api.Graph) NodeDraft(org.gephi.io.importer.api.NodeDraft) Node(org.gephi.graph.api.Node) Edge(org.gephi.graph.api.Edge) GraphController(org.gephi.graph.api.GraphController)

Example 22 with NodeDraft

use of org.gephi.io.importer.api.NodeDraft in project gephi by gephi.

the class ImporterPajek method importData.

private void importData(LineNumberReader reader) throws Exception {
    //Progress
    Progress.start(progressTicket);
    try {
        // ignore everything until we see '*Vertices'
        String curLine = skip(reader, "*vertices");
        if (// no vertices in the graph; return empty graph
        curLine == null) {
            report.logIssue(new Issue(NbBundle.getMessage(ImporterPajek.class, "importerNET_error_dataformat1"), Issue.Level.CRITICAL));
        }
        // create appropriate number of vertices
        StringTokenizer stringTokenizer = new StringTokenizer(curLine);
        // skip past "*vertices";
        stringTokenizer.nextToken();
        int num_vertices = Integer.parseInt(stringTokenizer.nextToken());
        //Progress
        Progress.switchToDeterminate(progressTicket, num_vertices);
        verticesArray = new NodeDraft[num_vertices];
        for (int i = 0; i < num_vertices; i++) {
            String label = "" + (i + 1);
            NodeDraft node = container.factory().newNodeDraft(label);
            node.setLabel(label);
            verticesArray[i] = node;
        }
        curLine = null;
        while (reader.ready()) {
            if (cancel) {
                reader.close();
                return;
            }
            curLine = reader.readLine();
            if (curLine == null || curLine.startsWith("*")) {
                break;
            }
            if (curLine.isEmpty()) {
                // skip blank lines
                report.logIssue(new Issue(NbBundle.getMessage(ImporterPajek.class, "importerNET_error_dataformat2", reader.getLineNumber()), Issue.Level.WARNING));
                continue;
            }
            try {
                readVertex(curLine, num_vertices);
            } catch (IllegalArgumentException iae) {
                reader.close();
                throw iae;
            }
            Progress.progress(progressTicket);
        }
        //Append nodes
        for (NodeDraft node : verticesArray) {
            container.addNode(node);
        }
        //Get arcs
        curLine = readArcsOrEdges(curLine, reader);
        //Get edges
        readArcsOrEdges(curLine, reader);
        reader.close();
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }
    Progress.finish(progressTicket);
}
Also used : StringTokenizer(java.util.StringTokenizer) Issue(org.gephi.io.importer.api.Issue) NodeDraft(org.gephi.io.importer.api.NodeDraft) IOException(java.io.IOException)

Example 23 with NodeDraft

use of org.gephi.io.importer.api.NodeDraft in project gephi by gephi.

the class ImporterTGF method addEdge.

private void addEdge(String source, String target, String label) {
    NodeDraft sourceNode;
    if (!container.nodeExists(source)) {
        sourceNode = container.factory().newNodeDraft(source);
        container.addNode(sourceNode);
    } else {
        sourceNode = container.getNode(source);
    }
    NodeDraft targetNode;
    if (!container.nodeExists(target)) {
        targetNode = container.factory().newNodeDraft(target);
        container.addNode(targetNode);
    } else {
        targetNode = container.getNode(target);
    }
    if (!container.edgeExists(sourceNode.getId(), targetNode.getId())) {
        EdgeDraft edge = container.factory().newEdgeDraft();
        edge.setSource(sourceNode);
        edge.setTarget(targetNode);
        edge.setLabel(label);
        container.addEdge(edge);
    }
}
Also used : EdgeDraft(org.gephi.io.importer.api.EdgeDraft) NodeDraft(org.gephi.io.importer.api.NodeDraft)

Example 24 with NodeDraft

use of org.gephi.io.importer.api.NodeDraft in project gephi by gephi.

the class ImporterTGF method addNode.

private void addNode(String id, String label) {
    NodeDraft node;
    if (!container.nodeExists(id)) {
        node = container.factory().newNodeDraft(id);
        node.setLabel(label);
        container.addNode(node);
    }
}
Also used : NodeDraft(org.gephi.io.importer.api.NodeDraft)

Example 25 with NodeDraft

use of org.gephi.io.importer.api.NodeDraft in project gephi by gephi.

the class ImporterTLP method parseEdge.

private void parseEdge(String[] tokens, int cptLine) {
    if (tokens.length != 4) {
        report.logIssue(new Issue(NbBundle.getMessage(ImporterGDF.class, "importerTPL_error_dataformat1", cptLine), Issue.Level.WARNING));
    }
    String id = tokens[1];
    EdgeDraft edge = container.factory().newEdgeDraft(id);
    NodeDraft source = container.getNode(tokens[2]);
    NodeDraft target = container.getNode(tokens[3]);
    edge.setSource(source);
    edge.setTarget(target);
    container.addEdge(edge);
}
Also used : EdgeDraft(org.gephi.io.importer.api.EdgeDraft) Issue(org.gephi.io.importer.api.Issue) NodeDraft(org.gephi.io.importer.api.NodeDraft)

Aggregations

NodeDraft (org.gephi.io.importer.api.NodeDraft)31 EdgeDraft (org.gephi.io.importer.api.EdgeDraft)15 Issue (org.gephi.io.importer.api.Issue)14 IOException (java.io.IOException)5 StringTokenizer (java.util.StringTokenizer)5 Random (java.util.Random)4 SQLException (java.sql.SQLException)2 ColumnDraft (org.gephi.io.importer.api.ColumnDraft)2 ResultSet (java.sql.ResultSet)1 ResultSetMetaData (java.sql.ResultSetMetaData)1 Statement (java.sql.Statement)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 Edge (org.gephi.graph.api.Edge)1 Graph (org.gephi.graph.api.Graph)1 GraphController (org.gephi.graph.api.GraphController)1 GraphFactory (org.gephi.graph.api.GraphFactory)1 Node (org.gephi.graph.api.Node)1 ContainerUnloader (org.gephi.io.importer.api.ContainerUnloader)1 ElementDraft (org.gephi.io.importer.api.ElementDraft)1