All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.anarres.graphviz.builder.jgrapht.JGraphTGraphVizBuilder Maven / Gradle / Ivy

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.anarres.graphviz.builder.jgrapht;

import com.google.common.base.Function;
import com.google.common.base.Functions;
import com.google.common.base.MoreObjects;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import org.anarres.graphviz.builder.GraphVizEdge;
import org.anarres.graphviz.builder.GraphVizGraph;
import org.anarres.graphviz.builder.GraphVizNode;
import org.anarres.graphviz.builder.GraphVizScope;
import org.jgrapht.Graph;

/**
 *
 * @author shevek
 */
public class JGraphTGraphVizBuilder implements GraphVizScope {

    private static final Function EDGE_LABEL_NULL = Functions.constant(null);

    private final Graph graph;
    private final GraphVizScope scope = this;
    private Function nodeLabelFunction = Functions.toStringFunction();
    private Function edgeLabelFunction = EDGE_LABEL_NULL;
    private Function edgeHeadLabelFunction = EDGE_LABEL_NULL;
    private Function edgeTailLabelFunction = EDGE_LABEL_NULL;

    public JGraphTGraphVizBuilder(@Nonnull Graph graph, @CheckForNull Function nodeLabelFunction) {
        this.graph = graph;
        this.nodeLabelFunction = (Function) MoreObjects.firstNonNull(nodeLabelFunction, Functions.toStringFunction());
    }

    public JGraphTGraphVizBuilder(@Nonnull Graph graph) {
        this(graph, null);
    }

    @Nonnull
    public JGraphTGraphVizBuilder withNodeLabelFunction(Function nodeLabelFunction) {
        this.nodeLabelFunction = nodeLabelFunction;
        return this;
    }

    @Nonnull
    public JGraphTGraphVizBuilder withEdgeLabelFunction(Function edgeLabelFunction) {
        this.edgeLabelFunction = edgeLabelFunction;
        return this;
    }

    @Nonnull
    public JGraphTGraphVizBuilder withEdgeHeadLabelFunction(Function edgeHeadLabelFunction) {
        this.edgeHeadLabelFunction = edgeHeadLabelFunction;
        return this;
    }

    @Nonnull
    public JGraphTGraphVizBuilder withEdgeTailLabelFunction(Function edgeTailLabelFunction) {
        this.edgeTailLabelFunction = edgeTailLabelFunction;
        return this;
    }

    public void build(@Nonnull GraphVizGraph out) {
        for (V node : graph.vertexSet()) {
            GraphVizNode n = out.node(scope, node);
            String label = nodeLabelFunction.apply(node);
            if (label != null)
                n.label(label);
        }
        for (E edge : graph.edgeSet()) {
            GraphVizEdge e = out.edge(scope, graph.getEdgeSource(edge), graph.getEdgeTarget(edge));
            String label = edgeLabelFunction.apply(edge);
            if (label != null)
                e.label(label);
            String headLabel = edgeHeadLabelFunction.apply(edge);
            if (headLabel != null)
                e.headLabel(headLabel);
            String tailLabel = edgeTailLabelFunction.apply(edge);
            if (tailLabel != null)
                e.tailLabel(tailLabel);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy