Package dev.roanh.gmark.util
Class Util
java.lang.Object
dev.roanh.gmark.util.Util
Class providing various small utilities as well
as thread bound random operations.
-
Field Summary
Modifier and TypeFieldDescriptionprivate static final ThreadLocal<Random>
Random instances for each thread. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T,
R> R applyOrNull
(T data, Function<T, R> function) Applies the given function to the given data, unless the data isnull
thennull
is returned instead.static <T> ArrayList<T>
asArrayList
(T... items) Creates a new array list populated with the given items.cartesianProduct
(List<List<T>> sets) Computes the Cartesian product of the given input sets.private static <T> void
computeAllSubsets
(List<T> items, int offset, int max, List<T> set, Consumer<List<T>> consumer) Computes all unordered subsets of the given list of items.static <T> void
computeAllSubsets
(List<T> items, Consumer<List<T>> consumer) Computes all unordered subsets of the given list of items.computeTreeDecompositionWidth2
(SimpleGraph<T, List<Tree<List<T>>>> graph) Computes the tree decomposition of the given input graph assuming that the input graph has the following properties: The input graph has a tree width of at most 2. The input graph is connected. The input graph contains no self loops. The input graph contains no parallel edges.static <V,
E> UniqueGraph<Object, Void> edgeLabelsToNodes
(UniqueGraph<V, E> in) Converts the given edge labelled input graph to a graph where all edge labels have been turned into labelled vertices.static <T extends IDable,
M>
List<SimpleGraph.SimpleEdge<T,M>> findMaximalMatching
(SimpleGraph<T, M> graph) Finds a maximal (not maximum) matching in the given graph.generateLabels
(int n) Generates a list of the given sizen
with sequentially numbered predicate objects.static Random
Gets a random instance bound to the current thread.static boolean
Checks if the given folder is empty or not.static <T> void
runIfNotNull
(T data, Consumer<T> fun) Runs the give consumer on the given data only if the given data is not equal tonull
.static <T> Supplier<Map<SelectivityClass,
T>> Returns aSupplier
that constructs a new enum map from theSelectivityClass
enum to the given data type.static <T> T
selectRandom
(Collection<T> data) Randomly selects an element from the given collection.static <T> T
selectRandom
(List<T> data) Selects an element at random from the given list.static void
setRandomSeed
(long seed) Sets the seed of the random instance for the current thread.static void
triplesToSQL
(Path triples, Path sql, boolean overwrite) Takes a file containing a graph in RDF triple format and outputs a SQL INSERT statement to populate a database with the graph.static int
uniformRandom
(int min, int max) Generates a random integer between the given minimum and maximum value (both inclusive).
-
Field Details
-
random
Random instances for each thread.
-
-
Constructor Details
-
Util
private Util()Prevent instantiation.
-
-
Method Details
-
getRandom
Gets a random instance bound to the current thread.- Returns:
- A thread local random instance.
-
setRandomSeed
public static void setRandomSeed(long seed) Sets the seed of the random instance for the current thread.- Parameters:
seed
- The new seed.- See Also:
-
selectRandom
Selects an element at random from the given list.- Type Parameters:
T
- The element data type.- Parameters:
data
- The list to pick an element from.- Returns:
- The selected element or
null
when the provided list was empty. - See Also:
-
selectRandom
Randomly selects an element from the given collection.- Type Parameters:
T
- The element data type.- Parameters:
data
- The collection to pick an element from.- Returns:
- The selected element or
null
when the provided collection was empty. - See Also:
-
uniformRandom
public static int uniformRandom(int min, int max) Generates a random integer between the given minimum and maximum value (both inclusive).- Parameters:
min
- The minimum value.max
- The maximum value.- Returns:
- The randomly generated value.
-
selectivityMapSupplier
Returns aSupplier
that constructs a new enum map from theSelectivityClass
enum to the given data type.- Type Parameters:
T
- The data type to map to.- Returns:
- A supplier that returns a map that maps from selectivity classes to the given data type.
- See Also:
-
applyOrNull
Applies the given function to the given data, unless the data isnull
thennull
is returned instead.- Type Parameters:
T
- The input data type.R
- The output data type.- Parameters:
data
- The data to give to the function.function
- The function to run on the data.- Returns:
- The result of applying the given function to the given
data or
null
when the given data isnull
.
-
isEmpty
Checks if the given folder is empty or not.- Parameters:
folder
- The folder to check.- Returns:
- True if the given folder does not contain any files or folders.
- Throws:
IOException
- When an IOException occurs.
-
triplesToSQL
public static void triplesToSQL(Path triples, Path sql, boolean overwrite) throws IOException, FileNotFoundException, FileAlreadyExistsException Takes a file containing a graph in RDF triple format and outputs a SQL INSERT statement to populate a database with the graph.- Parameters:
triples
- The input file with the RDF triple graph.sql
- The file to write the generated SQL statement to.overwrite
- True if the given output file should be overwritten if it already exists.- Throws:
IOException
- When an IOException occurs.FileNotFoundException
- When the given graph file was not found.FileAlreadyExistsException
- When the given output file already exists and overwriting is not requested.
-
edgeLabelsToNodes
Converts the given edge labelled input graph to a graph where all edge labels have been turned into labelled vertices. Essentially, for each edge(a) --b--> (c)
the edge will be turned into two edges with a new node with the former edge label in the middle, giving(a) --> (b) --> (c)
. Thus this transform doubles the number of edges in the graph and adds as many new nodes as there used to be edges in the old graph. The returned graph hasObject
as the vertex data type. There are two options for the actual class of these vertex data objects. Either they are a vertex data object from the old graph and thus of generic type V. Or they are aDataProxy
instance wrapping an old edge label of generic type E.- Type Parameters:
V
- The vertex data type.E
- The edge label data type.- Parameters:
in
- The input graph to transform.- Returns:
- The transformed graph without edge labels.
- See Also:
-
generateLabels
Generates a list of the given sizen
with sequentially numbered predicate objects. This means each predicate will have a textual name that matches their numerical ID.- Parameters:
n
- The number of labels to generate.- Returns:
- The generated set of labels.
-
runIfNotNull
Runs the give consumer on the given data only if the given data is not equal tonull
.- Type Parameters:
T
- The data type.- Parameters:
data
- The data to run the consumer on.fun
- The consumer to pass the given data to.
-
asArrayList
Creates a new array list populated with the given items.- Type Parameters:
T
- The type of the data.- Parameters:
items
- The items to put in the list.- Returns:
- The newly created and populated array list.
- See Also:
-
computeTreeDecompositionWidth2
public static <T extends IDable> Tree<List<T>> computeTreeDecompositionWidth2(SimpleGraph<T, List<Tree<List<T>>>> graph) throws IllegalArgumentExceptionComputes the tree decomposition of the given input graph assuming that the input graph has the following properties:- The input graph has a tree width of at most 2.
- The input graph is connected.
- The input graph contains no self loops.
- The input graph contains no parallel edges.
- Type Parameters:
T
- The data type of the graph.- Parameters:
graph
- The graph to compute the tree decomposition of. This graph instance will be modified so a copy should be passed if this is a problem.- Returns:
- The computed tree decomposition, not necessarily a nice tree decomposition.
- Throws:
IllegalArgumentException
- When the input graph is not connected or has a treewidth that is larger than 2.
-
findMaximalMatching
public static <T extends IDable,M> List<SimpleGraph.SimpleEdge<T,M>> findMaximalMatching(SimpleGraph<T, M> graph) Finds a maximal (not maximum) matching in the given graph.- Type Parameters:
T
- The graph data type.M
- The metadata data type.- Parameters:
graph
- The graph to find a maximal matching for.- Returns:
- The edges of the found maximal matching.
-
cartesianProduct
Computes the Cartesian product of the given input sets. For example, for the following input sets:{ {A, B}, {C, D}, {E} }
The output is as follows:{ {A, C, E}, {A, D, E}, {B, C, E}, {B, D, E} }
- Type Parameters:
T
- The type of the data in the lists.- Parameters:
sets
- The sets to compute the Cartesian product of.- Returns:
- The computed Cartesian product of the input sets.
- Throws:
ArithmeticException
- When the size of the output list with sets would be more thanInteger.MAX_VALUE
.
-
computeAllSubsets
Computes all unordered subsets of the given list of items. Computed sets are passed to the given consumer and should be treated as ephemeral. They only exist for the during of the consumer call and should be copied if persistence is required. Note that unordered subsets are computed, this means that the output will not contain identical sets with a different order of items. In addition, while technically a valid subset, the empty set is never returned from this subroutine. Note that the input list itself will be one of the returned subsets. As an implementation note, it is allowed to add elements to the passed list of items in the consumer, though these items will not be considered for any subsets.- Type Parameters:
T
- The list data type.- Parameters:
items
- The items to compute subsets of.consumer
- The consumer computed subsets are passed to.
-
computeAllSubsets
private static <T> void computeAllSubsets(List<T> items, int offset, int max, List<T> set, Consumer<List<T>> consumer) Computes all unordered subsets of the given list of items. Computed sets are passed to the given consumer and should be treated as ephemeral. They only exist for the during of the consumer call and should be copied if persistence is required. Note that unordered subsets are computed, this means that the output will not contain identical sets with a different order of items. In addition, while technically a valid subset, the empty set is never returned from this subroutine. Note that the input list itself will be one of the returned subsets. As an implementation note, it is allowed to add elements to the passed list of items in the consumer, though these items will not be considered for any subsets.- Type Parameters:
T
- The list data type.- Parameters:
items
- The items to compute subsets of.offset
- The current item to consider of inclusion in the output.max
- The maximum index in the array to consider for any subset.set
- The current output working set.consumer
- The consumer computed subsets are passed to.
-