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

org.ggp.base.util.DotGraph Maven / Gradle / Ivy

There is a newer version: 0.0.15
Show newest version
package org.ggp.base.util;

import java.util.Map;
import java.util.Map.Entry;

import javax.annotation.concurrent.Immutable;
import javax.annotation.concurrent.NotThreadSafe;

import com.google.common.base.Preconditions;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSetMultimap;
import com.google.common.collect.Maps;
import com.google.common.collect.SetMultimap;

@Immutable
public class DotGraph {
    private final ImmutableSet nodes;
    private final ImmutableSetMultimap edges;

    private DotGraph(ImmutableSet nodes,
            ImmutableSetMultimap edges) {
        this.nodes = nodes;
        this.edges = edges;
    }

    private static class Node {
        private final String label;

        private Node(String label) {
            this.label = label;
        }

        public static Node create(String label) {
            return new Node(label);
        }

        public String getId() {
            return "\"@" + Integer.toHexString(hashCode()) + "\"";
        }

        public String getShape() {
            return "circle";
        }

        public String getColor() {
            return "white";
        }

        public String getLabel() {
            return label;
        }
    }

    @NotThreadSafe
    public static class DotGraphBuilder {
        private final Map nodes = Maps.newHashMap();
        private final SetMultimap edges = HashMultimap.create();

        private DotGraphBuilder() {
        }

        public void addNode(T object, String nodeLabel) {
            Preconditions.checkArgument(!nodes.containsKey(object));
            nodes.put(object, Node.create(nodeLabel));
        }

        public void addEdges(SetMultimap dependencyGraph) {
            for (Entry entry : dependencyGraph.entries()) {
                addEdge(entry.getKey(), entry.getValue());
            }
        }

        public void addEdge(T source, T destination) {
            edges.put(source, destination);
        }

        public DotGraph build() {
            ImmutableSet graphNodes = ImmutableSet.copyOf(nodes.values());
            ImmutableSetMultimap.Builder graphEdges = ImmutableSetMultimap.builder();
            for (Entry entry : edges.entries()) {
                graphEdges.put(nodes.get(entry.getKey()), nodes.get(entry.getValue()));
            }
            return new DotGraph(graphNodes,
                    graphEdges.build());
        }

    }

    public static  DotGraphBuilder builder() {
        return new DotGraphBuilder();
    }

    public String print() {
        //TODO: Implement
        StringBuilder sb = new StringBuilder();
        sb.append("digraph customGraph\n")
        .append("{\n");
        for (Node node : nodes) {
            sb.append("\t")
            .append(node.getId())
            .append("[shape=").append(node.getShape())
            .append(", style=filled, fillcolor=").append(node.getColor())
            .append(", label=\"").append(node.getLabel()).append("\"];\n");
        }
        for (Entry edge : edges.entries()) {
            sb.append("\t")
            .append(edge.getKey().getId())
            .append("->")
            .append(edge.getValue().getId())
            .append(";\n");
        }
        sb.append("}\n");
        return sb.toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy