sf.util.graph.DirectedGraph Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of schemacrawler Show documentation
Show all versions of schemacrawler Show documentation
SchemaCrawler is an open-source Java API that makes working with database metadata as easy as working with plain old Java objects. SchemaCrawler is also a database schema discovery and comprehension, and schema documentation tool. You can search for database schema objects using regular expressions, and output the schema and data in a readable text format. The output is designed to be diff-ed against other database schemas.
/*
*
* SchemaCrawler
* http://www.schemacrawler.com
* Copyright (c) 2000-2016, Sualeh Fatehi.
*
* This library is free software; you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Foundation;
* either version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
*/
package sf.util.graph;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
public class DirectedGraph>
{
private final String name;
private final Map> verticesMap;
private final Set> edges;
public DirectedGraph(final String name)
{
this.name = name;
verticesMap = new HashMap<>();
edges = new HashSet<>();
}
/**
* Adds vertices, and a directed edge between them. Simple directed
* graphs do not allow self-loops.
* https://en.wikipedia.org/wiki/Loop_(graph_theory)
*
* @param from
* Vertex value at the start of the edge
* @param to
* Vertex value at the end of the edge
*/
public void addEdge(final T from, final T to)
{
if (!from.equals(to))
{
edges.add(new DirectedEdge(addVertex(from), addVertex(to)));
}
}
/**
* Adds a vertex.
*
* @param value
* Vertex value
* @return The newly added vertex
*/
public Vertex addVertex(final T value)
{
final Vertex vertex;
if (verticesMap.containsKey(value))
{
vertex = verticesMap.get(value);
}
else
{
vertex = new Vertex(value);
verticesMap.put(value, vertex);
}
return vertex;
}
public Set> edgeSet()
{
return new HashSet<>(edges);
}
public Set> getIncomingEdges(final Vertex vertexTo)
{
Objects.requireNonNull(vertexTo);
final Set> incomingEdges = new HashSet<>();
for (final DirectedEdge edge: edges)
{
if (edge.getTo().equals(vertexTo))
{
incomingEdges.add(edge);
}
}
return incomingEdges;
}
/**
* @return the name
*/
public String getName()
{
return name;
}
public Set> getOutgoingEdges(final Vertex vertexFrom)
{
Objects.requireNonNull(vertexFrom);
final Set> outgoingEdges = new HashSet<>();
for (final DirectedEdge edge: edges)
{
if (edge.getFrom().equals(vertexFrom))
{
outgoingEdges.add(edge);
}
}
return outgoingEdges;
}
@Override
public String toString()
{
final StringBuilder writer = new StringBuilder(4096);
writer.append("digraph {\n");
if (name != null && !name.isEmpty())
{
writer.append(String.format(" [label=\"%s\"]\n", name));
}
// writer.append(" graph [rankdir=\"LR\"];\n");
for (final Vertex vertex: verticesMap.values())
{
writer.append(" ").append(vertex);
if (vertex.hasAttribute("fillcolor"))
{
writer.append(String.format(" [fillcolor=%s, style=filled]",
vertex.getAttribute("fillcolor")));
}
writer.append(";\n");
}
for (final DirectedEdge edge: edges)
{
writer.append(" ").append(edge).append(";\n");
}
writer.append("}\n");
return writer.toString();
}
public Set> vertexSet()
{
return new HashSet<>(verticesMap.values());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy