Package com.gengoai.hermes
Class RelationGraph
- java.lang.Object
-
- com.gengoai.graph.DefaultGraphImpl<Annotation>
-
- com.gengoai.hermes.RelationGraph
-
- All Implemented Interfaces:
Graph<Annotation>
,Serializable
,Iterable<Annotation>
public class RelationGraph extends DefaultGraphImpl<Annotation>
A graph where vertices are annotations and edges represent relations. This allows for the implicit graph relation ofHString
to be visualized and manipulated in a traditional graph framework.- Author:
- David B. Bracewell
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
RelationGraph()
Instantiates a new Relation graph.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description RelationEdge
addEdge(Annotation fromVertex, Annotation toVertex)
RelationEdge
addEdge(Annotation fromVertex, Annotation toVertex, double weight)
Set<RelationEdge>
edges()
RelationGraph
filterByEdge(@NonNull Predicate<RelationEdge> edgePredicate)
Filters the graph by evaluating the edges using the given predicate.RelationGraph
filterByVertex(@NonNull Predicate<? super Annotation> vertexPredicate)
Filters the graph by evaluating the edges using the given predicate.static RelationGraph
from(@NonNull Collection<RelationEdge> edges)
Creates a RelationGraph from a collection of edgesRelationEdge
getEdge(Annotation v1, Annotation v2)
Set<RelationEdge>
getEdges(Annotation vertex)
Set<RelationEdge>
getInEdges(Annotation vertex)
Set<RelationEdge>
getOutEdges(Annotation vertex)
Set<Annotation>
getSubTreeNodes(@NonNull Annotation node, String... childRelations)
Gets the sub-tree for the given node following the given child relations.HString
getSubTreeText(Annotation node, boolean includeGiven)
Gets the text covering the sub-tree with this node (if includeGiven is true)RelationEdge
removeEdge(Annotation fromVertex, Annotation toVertex)
void
removeEdgeIf(@NonNull Predicate<RelationEdge> predicate)
Removes an edge given from the graph if it evaluates to true using the given predicate.void
render(@NonNull Resource output)
Renders the graph usingGraphViz
in PNG format to specified output location.void
render(@NonNull Resource output, GraphViz.Format format)
Renders the graph in the specified format to the specified output location.List<RelationEdge>
shortestConnection(@NonNull Annotation source, @NonNull Annotation target)
Determines the shortest connection (undirected) between the source and target annotationList<RelationEdge>
shortestPath(@NonNull Annotation source, @NonNull Annotation target)
Determines the shortest path (directed) between the source and target annotation-
Methods inherited from class com.gengoai.graph.DefaultGraphImpl
addEdge, addVertex, addVertices, containsEdge, containsVertex, degree, equals, getEdgeFactory, getPredecessors, getSuccessors, hashCode, inDegree, isDirected, isEmpty, iterator, numberOfEdges, numberOfVertices, outDegree, removeEdge, removeVertex, toString, vertices
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface com.gengoai.graph.Graph
addEdges, containsEdge, getNeighbors, getPredecessorsWeights, getSuccessorWeights, getWeight, getWeights, merge, merge, parallelstream, stream
-
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
-
-
-
Method Detail
-
from
public static RelationGraph from(@NonNull @NonNull Collection<RelationEdge> edges)
Creates a RelationGraph from a collection of edges- Parameters:
edges
- the edges- Returns:
- the relation graph
-
addEdge
public RelationEdge addEdge(Annotation fromVertex, Annotation toVertex)
- Specified by:
addEdge
in interfaceGraph<Annotation>
- Overrides:
addEdge
in classDefaultGraphImpl<Annotation>
-
addEdge
public RelationEdge addEdge(Annotation fromVertex, Annotation toVertex, double weight)
- Specified by:
addEdge
in interfaceGraph<Annotation>
- Overrides:
addEdge
in classDefaultGraphImpl<Annotation>
-
edges
public Set<RelationEdge> edges()
- Specified by:
edges
in interfaceGraph<Annotation>
- Overrides:
edges
in classDefaultGraphImpl<Annotation>
-
filterByEdge
public RelationGraph filterByEdge(@NonNull @NonNull Predicate<RelationEdge> edgePredicate)
Filters the graph by evaluating the edges using the given predicate. The edges passing the predicate and their incident vertices are used to construct a new filtered RelationGraph.- Parameters:
edgePredicate
- the edge predicate- Returns:
- the relation graph
-
filterByVertex
public RelationGraph filterByVertex(@NonNull @NonNull Predicate<? super Annotation> vertexPredicate)
Filters the graph by evaluating the edges using the given predicate. The vertices passing the predicate and their incident edges are used to construct a new filtered RelationGraph.- Parameters:
vertexPredicate
- the vertex predicate- Returns:
- the relation graph
-
getEdge
public RelationEdge getEdge(Annotation v1, Annotation v2)
- Specified by:
getEdge
in interfaceGraph<Annotation>
- Overrides:
getEdge
in classDefaultGraphImpl<Annotation>
-
getEdges
public Set<RelationEdge> getEdges(Annotation vertex)
-
getInEdges
public Set<RelationEdge> getInEdges(Annotation vertex)
- Specified by:
getInEdges
in interfaceGraph<Annotation>
- Overrides:
getInEdges
in classDefaultGraphImpl<Annotation>
-
getOutEdges
public Set<RelationEdge> getOutEdges(Annotation vertex)
- Specified by:
getOutEdges
in interfaceGraph<Annotation>
- Overrides:
getOutEdges
in classDefaultGraphImpl<Annotation>
-
getSubTreeNodes
public Set<Annotation> getSubTreeNodes(@NonNull @NonNull Annotation node, String... childRelations)
Gets the sub-tree for the given node following the given child relations.- Parameters:
node
- the nodechildRelations
- the child relations- Returns:
- the sub tree nodes
-
getSubTreeText
public HString getSubTreeText(Annotation node, boolean includeGiven)
Gets the text covering the sub-tree with this node (if includeGiven is true)- Parameters:
node
- the nodeincludeGiven
- the include given- Returns:
- the sub tree text
-
removeEdge
public RelationEdge removeEdge(Annotation fromVertex, Annotation toVertex)
- Specified by:
removeEdge
in interfaceGraph<Annotation>
- Overrides:
removeEdge
in classDefaultGraphImpl<Annotation>
-
removeEdgeIf
public void removeEdgeIf(@NonNull @NonNull Predicate<RelationEdge> predicate)
Removes an edge given from the graph if it evaluates to true using the given predicate.- Parameters:
predicate
- the predicate
-
render
public void render(@NonNull @NonNull Resource output) throws IOException
Renders the graph usingGraphViz
in PNG format to specified output location.- Parameters:
output
- the PNG file to render the graph to- Throws:
IOException
- Something went wrong rendering the graph
-
render
public void render(@NonNull @NonNull Resource output, @NonNull GraphViz.Format format) throws IOException
Renders the graph in the specified format to the specified output location.- Parameters:
output
- the location to save the rendered graph.format
- the graphic format to save the rendered graph in- Throws:
IOException
- Something went wrong rendering the graph
-
shortestConnection
public List<RelationEdge> shortestConnection(@NonNull @NonNull Annotation source, @NonNull @NonNull Annotation target)
Determines the shortest connection (undirected) between the source and target annotation- Parameters:
source
- the source annotationtarget
- the target annotation- Returns:
- the list of shortest edges between the two annotations or an empty list if there is no path
-
shortestPath
public List<RelationEdge> shortestPath(@NonNull @NonNull Annotation source, @NonNull @NonNull Annotation target)
Determines the shortest path (directed) between the source and target annotation- Parameters:
source
- the source annotationtarget
- the target annotation- Returns:
- the list of shortest edges between the two annotations or an empty list if there is no path
-
-