N
- public class MultiThreadedSearcher<N>
extends java.lang.Object
Collection initialSearchNodes;
Collection resultCollection;
NodeExpander expander = new NodeExpander();
MultiThreadedSearcher searcher = new MultiThreadedSearcher(expander, BREADTHFIRST);
searcher.addInitialNodes(initialSearchNodes);
searcher.startSearch(executor, progress, resultCollection);
Note that if you use this code from a plugin, you can also use:
searcher.startSearch(context.getExecutor(), context.getProgress(), resultCollection);
The progress object given to the startSearch method is only used for
cancellation checks, i.e. progress is never incremented.Modifier and Type | Field and Description |
---|---|
static int |
BREADTHFIRST
Constant representing a BREADTH-FIRST search.
|
static int |
DEPTHFIRST
Constant representing a DEPTH-FIRST search.
|
Constructor and Description |
---|
MultiThreadedSearcher(int numberOfThreads,
NodeExpander<N> expander,
ExpandCollection<N> expandCollection)
Instantiates a searcher.
|
MultiThreadedSearcher(int numberOfThreads,
NodeExpander<N> expander,
int searchType)
Instantiates a searcher.
|
MultiThreadedSearcher(NodeExpander<N> expander,
ExpandCollection<N> expandCollection)
Instantiates a searcher.
|
MultiThreadedSearcher(NodeExpander<N> expander,
int searchType)
Instantiates a searcher.
|
Modifier and Type | Method and Description |
---|---|
void |
addInitialNodes(java.util.Collection<N> initialNodes)
Sets the initial nodes of the search tree.
|
void |
addInitialNodes(N... initialNodes)
Sets the initial nodes of the search tree.
|
void |
startSearch(java.util.concurrent.Executor executor,
Progress progress,
java.util.Collection<N> resultCollection)
A call to this method initiates the search.
|
public static final int DEPTHFIRST
public static final int BREADTHFIRST
public MultiThreadedSearcher(int numberOfThreads, NodeExpander<N> expander, int searchType)
numberOfThreads
- specifies the number of threads to use. If unsure how to set
this value, use the other constructor without this parameterexpander
- The expander that will be used to expand each search node and
process the leafs of the search treesearchType
- the type of search, either DEPTHFIRST or BREADTHFIRSTpublic MultiThreadedSearcher(NodeExpander<N> expander, int searchType)
Runtime.getRuntime().availableProcessors()
expander
- The expander that will be used to expand each search node and
process the leafs of the search treesearchType
- the type of search, either DEPTHFIRST or BREADTHFIRSTpublic MultiThreadedSearcher(int numberOfThreads, NodeExpander<N> expander, ExpandCollection<N> expandCollection)
numberOfThreads
- specifies the number of threads to use. If unsure how to set
this value, use the other constructor without this parameterexpander
- The expander that will be used to expand each search node and
process the leafs of the search treeexpandCollection
- the collection to store nodes that need to be expandedpublic MultiThreadedSearcher(NodeExpander<N> expander, ExpandCollection<N> expandCollection)
Runtime.getRuntime().availableProcessors()
expander
- The expander that will be used to expand each search node and
process the leafs of the search treeexpandCollection
- the collection to store nodes that need to be expandedpublic void addInitialNodes(java.util.Collection<N> initialNodes)
initialNodes
- the collection of initial nodes.public void addInitialNodes(N... initialNodes)
initialNodes
- zero or more initial nodes.public void startSearch(java.util.concurrent.Executor executor, Progress progress, java.util.Collection<N> resultCollection) throws java.lang.InterruptedException, java.util.concurrent.ExecutionException
executor
- The executor in which the searcher can schedule it's threads.
If called from a plugin, use context.getExector() to pass to
this method.progress
- The progress which is polled for cancellation. Note that no
other changes are made to the progress. If changes are
necessary, this has to be handled by the NodeExpander. If
called from a plugin, use context.getProgress() to pass to
this method.resultCollection
- The collection in which the final result is stored by the
processLeaf method of the node expander. Note that the
searcher does not change this collection in any way, nor does
it handle any necessary synchronization.java.lang.InterruptedException
- If one of the threads was interupted;java.util.concurrent.ExecutionException
- If one of the threads threw an exception;