Search in sources :

Example 1 with GraphFactory

use of org.gephi.graph.api.GraphFactory in project gephi-plugins-bootcamp by gephi.

the class AddNodesTool method getListeners.

@Override
public ToolEventListener[] getListeners() {
    return new ToolEventListener[] { new MouseClickEventListener() {

        @Override
        public void mouseClick(int[] positionViewport, float[] position3d) {
            // Get current graph
            GraphController gc = Lookup.getDefault().lookup(GraphController.class);
            Graph graph = gc.getGraphModel().getGraph();
            GraphFactory factory = gc.getGraphModel().factory();
            // Add node
            Node node = factory.newNode();
            node.setX(position3d[0]);
            node.setY(position3d[1]);
            node.setSize(10f);
            graph.addNode(node);
        }
    }, new NodeClickEventListener() {

        @Override
        public void clickNodes(Node[] nodes) {
            // Get mouse position
            float[] position3d = VizController.getInstance().getGraphIO().getMousePosition3d();
            // Get current graph
            GraphController gc = Lookup.getDefault().lookup(GraphController.class);
            Graph graph = gc.getGraphModel().getGraph();
            GraphFactory factory = gc.getGraphModel().factory();
            // Add node
            Node node = factory.newNode();
            node.setX(position3d[0]);
            node.setY(position3d[1]);
            node.setSize(10f);
            graph.addNode(node);
            // Add edges with the clicked nodes
            for (Node n : nodes) {
                Edge edge = factory.newEdge(node, n);
                graph.addEdge(edge);
            }
        }
    } };
}
Also used : MouseClickEventListener(org.gephi.tools.spi.MouseClickEventListener) GraphFactory(org.gephi.graph.api.GraphFactory) ToolEventListener(org.gephi.tools.spi.ToolEventListener) Graph(org.gephi.graph.api.Graph) Node(org.gephi.graph.api.Node) NodeClickEventListener(org.gephi.tools.spi.NodeClickEventListener) Edge(org.gephi.graph.api.Edge) GraphController(org.gephi.graph.api.GraphController)

Example 2 with GraphFactory

use of org.gephi.graph.api.GraphFactory in project gephi by gephi.

the class StandardTest method generateGraph.

// UTILITY
private Graph generateGraph() {
    GraphFactory factory = graphModel.factory();
    Node[] nodes = new Node[6];
    for (int i = 0; i < nodes.length; i++) {
        nodes[i] = factory.newNode();
    }
    Graph graph = graphModel.getGraph();
    graph.addAllNodes(Arrays.asList(nodes));
    Column column1 = graphModel.getNodeTable().addColumn("part1", String.class);
    Column column2 = graphModel.getNodeTable().addColumn("rank2", Integer.class);
    for (int i = 0; i < nodes.length; i++) {
        nodes[i].setAttribute(column2, i);
        nodes[i].setAttribute(column1, i % 2 == 0 ? "A" : "B");
    }
    return graph;
}
Also used : GraphFactory(org.gephi.graph.api.GraphFactory) Graph(org.gephi.graph.api.Graph) Column(org.gephi.graph.api.Column) Node(org.gephi.graph.api.Node)

Example 3 with GraphFactory

use of org.gephi.graph.api.GraphFactory 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(container, draftNode, node);
        if (newNode) {
            graph.addNode(node);
        }
        Progress.progress(progressTicket);
    }
    final EdgeMergeStrategy edgesMergeStrategy = containers[0].getEdgesMergeStrategy();
    // 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);
        boolean createDirected = true;
        switch(container.getEdgeDefault()) {
            case DIRECTED:
                createDirected = true;
                break;
            case UNDIRECTED:
                createDirected = false;
                break;
            case MIXED:
                createDirected = draftEdge.getDirection() != EdgeDirection.UNDIRECTED;
                draftEdge.setDirection(createDirected ? EdgeDirection.DIRECTED : EdgeDirection.UNDIRECTED);
                break;
        }
        Edge edge = graph.getEdge(source, target, edgeType);
        if (edge != null && edgesMergeStrategy == EdgeMergeStrategy.NO_MERGE) {
            // Undirected and directed edges are incompatible, check for them or we could get an exception:
            final Edge incompatibleEdge = findIncompatibleEdge(graph, source, target, createDirected, edgeType);
            if (incompatibleEdge == null) {
                // Force create, no merge
                edge = null;
            } else {
                String message = NbBundle.getMessage(DefaultProcessor.class, "DefaultProcessor.error.incompatibleEdges", String.format("[%s -> %s; %s, type %s]", sourceId, targetId, createDirected ? "Directed" : "Undirected", type), String.format("[%s -> %s; %s; type: %s; id: %s]", incompatibleEdge.getSource().getId(), incompatibleEdge.getTarget().getId(), incompatibleEdge.isDirected() ? "Directed" : "Undirected", incompatibleEdge.getTypeLabel(), incompatibleEdge.getId()));
                report.logIssue(new Issue(message, Issue.Level.WARNING));
                Progress.progress(progressTicket);
                continue;
            }
        }
        boolean newEdge = edge == null;
        if (newEdge) {
            if (!graph.hasEdge(id)) {
                edge = factory.newEdge(id, source, target, edgeType, draftEdge.getWeight(), createDirected);
            } else {
                // The id is already in use by a different edge, generate a new id:
                edge = factory.newEdge(source, target, edgeType, draftEdge.getWeight(), createDirected);
            }
            addedEdges++;
        }
        flushToEdge(container, draftEdge, edge, newEdge);
        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) Issue(org.gephi.io.importer.api.Issue) NodeDraft(org.gephi.io.importer.api.NodeDraft) Node(org.gephi.graph.api.Node) EdgeDraft(org.gephi.io.importer.api.EdgeDraft) Graph(org.gephi.graph.api.Graph) EdgeMergeStrategy(org.gephi.io.importer.api.EdgeMergeStrategy) Edge(org.gephi.graph.api.Edge) GraphController(org.gephi.graph.api.GraphController)

Aggregations

Graph (org.gephi.graph.api.Graph)3 GraphFactory (org.gephi.graph.api.GraphFactory)3 Node (org.gephi.graph.api.Node)3 Edge (org.gephi.graph.api.Edge)2 GraphController (org.gephi.graph.api.GraphController)2 Column (org.gephi.graph.api.Column)1 EdgeDraft (org.gephi.io.importer.api.EdgeDraft)1 EdgeMergeStrategy (org.gephi.io.importer.api.EdgeMergeStrategy)1 ElementIdType (org.gephi.io.importer.api.ElementIdType)1 Issue (org.gephi.io.importer.api.Issue)1 NodeDraft (org.gephi.io.importer.api.NodeDraft)1 MouseClickEventListener (org.gephi.tools.spi.MouseClickEventListener)1 NodeClickEventListener (org.gephi.tools.spi.NodeClickEventListener)1 ToolEventListener (org.gephi.tools.spi.ToolEventListener)1