Search in sources :

Example 11 with EdgeDraft

use of org.gephi.io.importer.api.EdgeDraft in project gephi-plugins-bootcamp by gephi.

the class MatrixFileImporter method importData.

private void importData(LineNumberReader reader) throws Exception {
    // read type code initial line
    String line = reader.readLine();
    String typecode = line;
    report.log("Typecode is " + typecode);
    // read comment lines if any
    boolean comment = true;
    while (comment) {
        line = reader.readLine();
        comment = line.startsWith("%");
    }
    String[] str = line.split("( )+");
    int nRows = (Integer.valueOf(str[0].trim()));
    int nColumns = (Integer.valueOf(str[1].trim()));
    int nNonZeros = (Integer.valueOf(str[2].trim()));
    report.log("Number of rows: " + nRows);
    report.log("Number of cols: " + nColumns);
    report.log("Number of non zeros: " + nNonZeros);
    while ((line = reader.readLine()) != null) {
        //Read coordinates and value
        str = line.split("( )+");
        int node1Index = (Integer.valueOf(str[0].trim()));
        int node2Index = (Integer.valueOf(str[1].trim()));
        float weight = 1f;
        if (str.length > 2) {
            weight = (Double.valueOf(str[2].trim())).floatValue();
        }
        //Get or create node
        NodeDraft node1;
        if (container.nodeExists(String.valueOf(node1Index))) {
            node1 = container.getNode(String.valueOf(node1Index));
        } else {
            node1 = container.factory().newNodeDraft(String.valueOf(node1Index));
            //Don't forget to add the node
            container.addNode(node1);
        }
        NodeDraft node2;
        if (container.nodeExists(String.valueOf(node2Index))) {
            node2 = container.getNode(String.valueOf(node2Index));
        } else {
            node2 = container.factory().newNodeDraft(String.valueOf(node2Index));
            //Don't forget to add the node
            container.addNode(node2);
        }
        //Create edge
        EdgeDraft edgeDraft = container.factory().newEdgeDraft();
        edgeDraft.setSource(node1);
        edgeDraft.setTarget(node2);
        edgeDraft.setWeight(weight);
        container.addEdge(edgeDraft);
    }
}
Also used : EdgeDraft(org.gephi.io.importer.api.EdgeDraft) NodeDraft(org.gephi.io.importer.api.NodeDraft)

Example 12 with EdgeDraft

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

the class AbstractProcessor method flushToElementAttributes.

protected void flushToElementAttributes(ElementDraft elementDraft, Element element) {
    for (ColumnDraft col : elementDraft.getColumns()) {
        if (elementDraft instanceof EdgeDraft && col.getId().equals("weight")) {
            continue;
        }
        Object val = elementDraft.getValue(col.getId());
        if (val != null) {
            TimeMap existingMap;
            if (col.isDynamic() && (existingMap = (TimeMap) element.getAttribute(col.getId())) != null && !existingMap.isEmpty()) {
                TimeMap valMap = (TimeMap) val;
                Object[] keys = existingMap.toKeysArray();
                Object[] vals = existingMap.toValuesArray();
                for (int i = 0; i < keys.length; i++) {
                    valMap.put(keys[i], vals[i]);
                }
                element.setAttribute(col.getId(), valMap);
            } else {
                element.setAttribute(col.getId(), val);
            }
        }
    }
}
Also used : ColumnDraft(org.gephi.io.importer.api.ColumnDraft) EdgeDraft(org.gephi.io.importer.api.EdgeDraft) TimeMap(org.gephi.graph.api.types.TimeMap)

Example 13 with EdgeDraft

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

the class MultiGraph method generate.

@Override
public void generate(ContainerLoader container) {
    NodeDraft[] nodeArray = new NodeDraft[numberOfNodes];
    for (int i = 0; i < numberOfNodes; i++) {
        NodeDraft nodeDraft = container.factory().newNodeDraft("n" + i);
        container.addNode(nodeDraft);
        nodeArray[i] = nodeDraft;
    }
    String[] edgeTypes = new String[numberOfEdgeTypes];
    for (int i = 0; i < edgeTypes.length; i++) {
        edgeTypes[i] = "Type " + i;
    }
    Random random = new Random();
    if (wiringProbability > 0) {
        for (int i = 0; i < numberOfNodes - 1; i++) {
            NodeDraft node1 = nodeArray[i];
            for (int j = i + 1; j < numberOfNodes; j++) {
                NodeDraft node2 = nodeArray[j];
                if (random.nextDouble() < wiringProbability) {
                    if (random.nextDouble() < 0.3) {
                        //Double
                        EdgeDraft edgeDraft1 = container.factory().newEdgeDraft();
                        edgeDraft1.setSource(node1);
                        edgeDraft1.setTarget(node2);
                        edgeDraft1.setType(edgeTypes[0]);
                        edgeDraft1.setLabel((String) edgeDraft1.getType());
                        container.addEdge(edgeDraft1);
                        EdgeDraft edgeDraft2 = container.factory().newEdgeDraft();
                        edgeDraft2.setSource(node1);
                        edgeDraft2.setTarget(node2);
                        edgeDraft2.setType(edgeTypes[1]);
                        edgeDraft2.setLabel((String) edgeDraft2.getType());
                        container.addEdge(edgeDraft2);
                    } else {
                        //Single
                        EdgeDraft edgeDraft = container.factory().newEdgeDraft();
                        edgeDraft.setSource(node1);
                        edgeDraft.setTarget(node2);
                        edgeDraft.setType(edgeTypes[random.nextInt(edgeTypes.length)]);
                        edgeDraft.setLabel((String) edgeDraft.getType());
                        container.addEdge(edgeDraft);
                    }
                }
            }
        }
    }
}
Also used : EdgeDraft(org.gephi.io.importer.api.EdgeDraft) Random(java.util.Random) NodeDraft(org.gephi.io.importer.api.NodeDraft)

Example 14 with EdgeDraft

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

the class RandomGraph method generate.

@Override
public void generate(ContainerLoader container) {
    int max = numberOfNodes;
    if (wiringProbability > 0) {
        max += numberOfNodes - 1;
    }
    Progress.start(progress, max);
    int progressUnit = 0;
    Random random = new Random();
    NodeDraft[] nodeArray = new NodeDraft[numberOfNodes];
    for (int i = 0; i < numberOfNodes && !cancel; i++) {
        NodeDraft nodeDraft = container.factory().newNodeDraft();
        container.addNode(nodeDraft);
        nodeArray[i] = nodeDraft;
        Progress.progress(progress, ++progressUnit);
    }
    if (wiringProbability > 0) {
        for (int i = 0; i < numberOfNodes - 1 && !cancel; i++) {
            NodeDraft node1 = nodeArray[i];
            for (int j = i + 1; j < numberOfNodes && !cancel; j++) {
                NodeDraft node2 = nodeArray[j];
                if (random.nextDouble() < wiringProbability) {
                    EdgeDraft edgeDraft = container.factory().newEdgeDraft();
                    edgeDraft.setSource(node1);
                    edgeDraft.setTarget(node2);
                    container.addEdge(edgeDraft);
                }
            }
            Progress.progress(progress, ++progressUnit);
        }
    }
    Progress.finish(progress);
    progress = null;
}
Also used : EdgeDraft(org.gephi.io.importer.api.EdgeDraft) Random(java.util.Random) NodeDraft(org.gephi.io.importer.api.NodeDraft)

Example 15 with EdgeDraft

use of org.gephi.io.importer.api.EdgeDraft 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)

Aggregations

EdgeDraft (org.gephi.io.importer.api.EdgeDraft)18 NodeDraft (org.gephi.io.importer.api.NodeDraft)15 Issue (org.gephi.io.importer.api.Issue)9 IOException (java.io.IOException)3 Random (java.util.Random)3 StringTokenizer (java.util.StringTokenizer)3 ColumnDraft (org.gephi.io.importer.api.ColumnDraft)3 ResultSet (java.sql.ResultSet)1 ResultSetMetaData (java.sql.ResultSetMetaData)1 SQLException (java.sql.SQLException)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 TimeMap (org.gephi.graph.api.types.TimeMap)1 ElementDraft (org.gephi.io.importer.api.ElementDraft)1