Package org.mycore.tools
Class MCRTopologicalSort<T>
java.lang.Object
org.mycore.tools.MCRTopologicalSort<T>
This class implements an algorithm for topological ordering.
It can be used to retrieve the order in which MyCoRe object can be imported to be
sure that parent objects are imported first.
It also checks for circular dependencies and will throw an exception if it occurs.
The doTopoSort() method can only be called once, since it processes the internal data.
Afterwards prepareData() must be called again or a new object has to be used.
For performance reasons each node label will be mapped to an integer (position in node list)
The algorithm is described in
http://en.wikipedia.org/wiki/Topological_sorting
- Version:
- $Revision: 28688 $ $Date: 2013-12-18 15:27:20 +0100 (Mi, 18 Dez 2013) $
- Author:
- Robert Stephan
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
add an edge to the graphvoid
add a node to the graphint[]
based upon first pseudo code in http://en.wikipedia.org/w/index.php?returns a node id for a given nodegetNodeName
(Integer id) return the name of the given nodegetNodes()
return the Map of Integer to NodeObjectsstatic void
prepareData
(MCRTopologicalSort<String> ts, String[] files, Path dir) parses MCRObject xml files for parent links and creates the graph uses StAX cursor API (higher performance)static void
prepareMCRObjects
(MCRTopologicalSort<String> ts, String[] mcrids) reads MCRObjectIDs as Strings, retrieves parent links from MCRLinkTableManager and creates the graph uses StAX cursor API (higher performance)boolean
removeEdge
(Integer from, Integer to) removes an edge from grapnvoid
reset()
resets the topological sort data structuretoString()
-
Constructor Details
-
MCRTopologicalSort
public MCRTopologicalSort()
-
-
Method Details
-
prepareData
parses MCRObject xml files for parent links and creates the graph uses StAX cursor API (higher performance)- Parameters:
ts
- - the topological sort data structurefiles
- - a list of file namesdir
- - the directory where the files can be found
-
prepareMCRObjects
reads MCRObjectIDs as Strings, retrieves parent links from MCRLinkTableManager and creates the graph uses StAX cursor API (higher performance) -
reset
public void reset()resets the topological sort data structure -
getNodes
return the Map of Integer to NodeObjects -
addNode
add a node to the graph- Parameters:
name
- - the node name
-
getNodeID
returns a node id for a given node- Parameters:
name
- - the node name- Returns:
- the node id
-
getNodeName
return the name of the given node- Parameters:
id
- - the node id- Returns:
- the node name
-
addEdge
add an edge to the graph- Parameters:
from
- - the source nodeto
- - the target node
-
removeEdge
removes an edge from grapn- Parameters:
from
- - the source node idto
- - the target node id- Returns:
- true, if there are no more incoming edges on the [to]-node ([to] = leaf node)
-
doTopoSort
public int[] doTopoSort()based upon first pseudo code in http://en.wikipedia.org/w/index.php?title=Topological_sorting&oldid=611829125 The algorithm will destroy the input data -> the method can only be called once- Returns:
- an array of ids which define the order in which the elements have to be retrieved from the given input list or null if an error occured
-
toString
-