Class GraphBuilderImpl<V,E>
- java.lang.Object
-
- science.aist.gtf.graph.builder.impl.GraphBuilderImpl<V,E>
-
- Type Parameters:
V
- the type of the decorated vertex valueE
- the type of the decorated edge value
- All Implemented Interfaces:
GraphBuilder<V,E>
public class GraphBuilderImpl<V,E> extends Object implements GraphBuilder<V,E>
Implementation of a graph builder (it creates directed weighted edges)
Vertices are identified using an ID, which is extracted using a key mapper. the default key mapper tries to extract the key from a method that is annotation with
GraphId
, make sure, that this method has a consistent state. If noGraphId
can be found the decorated vertex value is used as a key. If there are multiple methods that are annotated withGraphId
the first one is used. Please be aware this could lead to unwanted behaviour, and should therefore be avoided.If an edge between vertex a and b is created, it first checks if there is already an edge, if this is the case the weight of the edge is incremented by one. The provided data element will be ignored, but callback functions are called.
- Since:
- 1.0
- Author:
- Andreas Pointner
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addGraphCreationCallback(Consumer<Graph<V,E>> callback)
This method allows adding callback methods, that are applied once the toGraph method is called.Graph<V,E>
addSubGraph(Graph<V,E> subGraph)
Adds the subgraph to this graph, and returns a new resulting graph, that represents a view on the elements of the all vertices and edges.GraphBuilder<V,E>
addVertex(V value)
Same asGraphBuilder.getOrAddVertex(Object)
but it returns the builder itself to provide a fluent way.VertexBuilderWith<V,E>
addVertexWith(V value)
Same asGraphBuilder.addVertex(Object)
but returns aVertexBuilderWith
that allows to use a callback method for the returned vertex, to be able to add additional parameters (e.g.static <V,E>
GraphBuilder<V,E>create()
Creates a new graph builder using the default key mapper as well as the default factory (GraphFactoryFactory.getDefaultFactory()
static <V,E>
GraphBuilder<V,E>create(Function<V,Object> keyMapper)
Creates a new graph builder using the default factory (GraphFactoryFactory.getDefaultFactory()
static <V,E>
GraphBuilder<V,E>create(Function<V,Object> keyMapper, BinaryOperator<V> merger)
Creates a new graph builder.static <V,E>
GraphBuilder<V,E>create(Function<V,Object> keyMapper, GraphFactory graphFactory)
Creates a new graph builderstatic <V,E>
GraphBuilder<V,E>create(Function<V,Object> keyMapper, GraphFactory graphFactory, BinaryOperator<V> merger)
Creates a new graph builder.static <V,E>
GraphBuilder<V,E>create(GraphFactory graphFactory)
Creates a new graph builder using the default key mapperEdgeBuilder<V,E>
from(V value)
Start to create a new Edge to the vertex that decorates value.EdgeBuilder<V,E>
fromByKey(Object key)
Start to create a new Edge to the vertex that is identified by the given key.static <V> Function<V,Object>
getDefaultKeyMapper()
Creates a new instance of the default key mapper, which either uses a method that is annotated withGraphId
or uses the element itself as a fallback.GraphStateAccessor<V,E>
getGraphState()
Returns a readonly version of the current graph state.Vertex<V,E>
getOrAddVertex(V value)
Method to add a new vertex to the graph builder.Graph<V,E>
toGraph()
Creates the graph based on the vertices and edges defines in that graph builder.<T> void
updateKey(V element, T key, BiConsumer<V,T> mapping)
Allows to update the key for a given node value in the list.
-
-
-
Method Detail
-
getDefaultKeyMapper
public static <V> Function<V,Object> getDefaultKeyMapper()
Creates a new instance of the default key mapper, which either uses a method that is annotated withGraphId
or uses the element itself as a fallback.- Type Parameters:
V
- the type of the decorated vertex- Returns:
- an function representing the key mapper.
-
create
public static <V,E> GraphBuilder<V,E> create()
Creates a new graph builder using the default key mapper as well as the default factory (GraphFactoryFactory.getDefaultFactory()
- Type Parameters:
V
- the type of the decorated vertex valueE
- the type of the decorated edge value- Returns:
- a new instance of the GraphBuilder
-
create
public static <V,E> GraphBuilder<V,E> create(GraphFactory graphFactory)
Creates a new graph builder using the default key mapper- Type Parameters:
V
- the type of the decorated vertex valueE
- the type of the decorated edge value- Parameters:
graphFactory
- specify the graph factory that should be used- Returns:
- a new instance of the GraphBuilder
-
create
public static <V,E> GraphBuilder<V,E> create(Function<V,Object> keyMapper)
Creates a new graph builder using the default factory (GraphFactoryFactory.getDefaultFactory()
- Type Parameters:
V
- the type of the decorated vertex valueE
- the type of the decorated edge value- Parameters:
keyMapper
- specify the key mapper that should be used- Returns:
- a new instance of the GraphBuilder
-
create
public static <V,E> GraphBuilder<V,E> create(Function<V,Object> keyMapper, GraphFactory graphFactory)
Creates a new graph builder- Type Parameters:
V
- the type of the decorated vertex valueE
- the type of the decorated edge value- Parameters:
graphFactory
- specify the graph factory that should be usedkeyMapper
- specify the key mapper that should be used- Returns:
- a new instance of the GraphBuilder
-
create
public static <V,E> GraphBuilder<V,E> create(Function<V,Object> keyMapper, GraphFactory graphFactory, BinaryOperator<V> merger)
Creates a new graph builder.- Type Parameters:
V
- the type of the decorated vertex valueE
- the type of the decorated edge value- Parameters:
graphFactory
- specify the graph factory that should be usedkeyMapper
- specify the key mapper that should be usedmerger
- specific the merger that should be used- Returns:
- a new instance of the GraphBuilder
-
create
public static <V,E> GraphBuilder<V,E> create(Function<V,Object> keyMapper, BinaryOperator<V> merger)
Creates a new graph builder.- Type Parameters:
V
- the type of the decorated vertex valueE
- the type of the decorated edge value- Parameters:
keyMapper
- specify the key mapper that should be usedmerger
- specific the merger that should be used- Returns:
- a new instance of the GraphBuilder
-
getGraphState
public GraphStateAccessor<V,E> getGraphState()
Description copied from interface:GraphBuilder
Returns a readonly version of the current graph state.- Specified by:
getGraphState
in interfaceGraphBuilder<V,E>
- Returns:
- the current graph state of the builder
- See Also:
GraphState.readonly()
-
getOrAddVertex
public Vertex<V,E> getOrAddVertex(V value)
Description copied from interface:GraphBuilder
Method to add a new vertex to the graph builder. This method takes a value, which is used as the decorated value for the created vertex. If there is already another vertex, that confirms to the value then an existing instance of a vertex may be reused.- Specified by:
getOrAddVertex
in interfaceGraphBuilder<V,E>
- Parameters:
value
- the value of the decorated element- Returns:
- the new created or cached vertex
-
addVertex
public GraphBuilder<V,E> addVertex(V value)
Description copied from interface:GraphBuilder
Same asGraphBuilder.getOrAddVertex(Object)
but it returns the builder itself to provide a fluent way.- Specified by:
addVertex
in interfaceGraphBuilder<V,E>
- Parameters:
value
- the value of the decorated element- Returns:
- the graph builder instance
- See Also:
GraphBuilder.getOrAddVertex(Object)
-
addVertexWith
public VertexBuilderWith<V,E> addVertexWith(V value)
Description copied from interface:GraphBuilder
Same asGraphBuilder.addVertex(Object)
but returns aVertexBuilderWith
that allows to use a callback method for the returned vertex, to be able to add additional parameters (e.g.MetaTag
s)- Specified by:
addVertexWith
in interfaceGraphBuilder<V,E>
- Parameters:
value
- the value of the decorated element- Returns:
- a
VertexBuilderWith
-
from
public EdgeBuilder<V,E> from(V value)
Description copied from interface:GraphBuilder
Start to create a new Edge to the vertex that decorates value. If there is no vertex that decorated the value yet, a new vertex will be created, as it would be usingGraphBuilder.addVertex(Object)
method- Specified by:
from
in interfaceGraphBuilder<V,E>
- Parameters:
value
- the value of the decorated element- Returns:
- a
EdgeBuilder
-
fromByKey
public EdgeBuilder<V,E> fromByKey(Object key)
Description copied from interface:GraphBuilder
Start to create a new Edge to the vertex that is identified by the given key. Make sure to add the vertex first, otherwise this method may fail.- Specified by:
fromByKey
in interfaceGraphBuilder<V,E>
- Parameters:
key
- the key which identifies the vertex- Returns:
- a
EdgeBuilder
-
toGraph
public Graph<V,E> toGraph()
Description copied from interface:GraphBuilder
Creates the graph based on the vertices and edges defines in that graph builder. This makes the GraphBuilder as "finished" which means, that all further operations on the graph builder will result in anIllegalStateException
.- Specified by:
toGraph
in interfaceGraphBuilder<V,E>
- Returns:
- a
Graph
-
updateKey
public <T> void updateKey(V element, T key, BiConsumer<V,T> mapping)
Description copied from interface:GraphBuilder
Allows to update the key for a given node value in the list. The key must only be updated via this method to maintain a consistent state. The element that is passed to this function must be part of the list and must have the old key. The mapping function will be called with the element and the new key, and must update the elements key value.- Specified by:
updateKey
in interfaceGraphBuilder<V,E>
- Type Parameters:
T
- the type of the key- Parameters:
element
- the element where the key should be updatedkey
- the new keymapping
- the mapping function to set the new key
-
addGraphCreationCallback
public void addGraphCreationCallback(Consumer<Graph<V,E>> callback)
Description copied from interface:GraphBuilder
This method allows adding callback methods, that are applied once the toGraph method is called. One the callback method is added there is no more way to remove it and it will be executed on the graph creation. This method can be used for e.g. adding MetaTags:graphBuilder.addGraphCreationCallback(g -> g.addMetaTag(new MetaTagImpl<>("key", "value")));
- Specified by:
addGraphCreationCallback
in interfaceGraphBuilder<V,E>
- Parameters:
callback
- the callback method to be added
-
addSubGraph
public Graph<V,E> addSubGraph(Graph<V,E> subGraph)
Description copied from interface:GraphBuilder
Adds the subgraph to this graph, and returns a new resulting graph, that represents a view on the elements of the all vertices and edges.
Note: subGraph is no longer valid afterwards, as the resulting vertices and edges are removed from the graph and added to the new graph.
- Specified by:
addSubGraph
in interfaceGraphBuilder<V,E>
- Parameters:
subGraph
- the subgraph that should be added- Returns:
- the resulting snapshot on the subgraph
-
-