|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcore.Search<T>
T
- the specific type of all elements of the search domain.public abstract class Search<T>
This Class is the abstraction of every search algorithm in a generic domain.
It is typically used to pass searches around and manipulate
them where maximum generality is desired (like the Collection interface).
In general a user can run each search in two equivalent ways:
Search<Integer> search = new MySearch(5);
search.run();
search.getResult(); // should return 42
or:
Search<Integer> search = new MySearch(5);
if (search.initialize()){
search.run();
search.getResult(); // should return 42
}else{
System.out.println("The search could not be initialized!");
}
Note: The search will only be executed if it can be initialized().
So an uninitialized search will first try to initialize itself before execution.
result
is a place holder for the solution found by your search.
After initializing a search this field will be set to null
.
The developer must explicitly assign the solution to this field,
in order to allow the user to access the result after execution of your search algorithm.neededSteps
is a counter for the number of iterations your
search algorithm needed.
After initializing a search this field will be reset to zero.
You must explicitly increment this field during your search, to allow users
instant access to the progress of your search.
The semantic of neededSteps
is not fixed and may differ in some subclasses.
return ( problem.initial() != null )
hash.clear()
neededSteps
.
This field will be automatically set to zero whenever a search is initialized.
You should increment this field during execution of the Search#search() method
so users can see the progress of the your search.neededSteps++;
false
whenever the execution should be terminated.if (! running() ) return;
result
.
If your search finds a solution then you should store this solution of Type T
into this field (by assigning the solution to it) before you terminate your search.
The result
will hold the solution until the Search#initialize() method
is called again.if ( problem.isGoal(state) ){ result=state; return; }
public class MySearch extends Search{
private Integer initial=null;
public MySearch(Integer i) {
initial=i;
}
protected void search(){
Integer current=initial;
while(running()){ // (4.)
neededSteps++; // (3.)
current++;
if (current % 42 == 0){ // (5.)
result = i;
break;
}
}
}
protected void canPrepare(){
return initial!=null; // (1.)
}
protected void prepare() {
if (initial<0) initial=-initial; // (2.)
}
}
DirectedSearch
,
UndirectedSearch
Field Summary | |
---|---|
protected int |
neededSteps
holding the number of steps needed for the search |
protected T |
result
holding the result of the search or null if nothing has been found yet |
Constructor Summary | |
---|---|
Search()
Creates a new instance of an abstract search. |
Method Summary | |
---|---|
protected abstract boolean |
canPrepare()
This method is the hook for the preparation of the search and determines if the search can be initialized. |
protected void |
finalize()
This method frees the search. |
T |
getResult()
This method returns the generic solution in the domain of the search. |
boolean |
initialize()
This method initializes the search if possible and if it is not already initialized. |
boolean |
initialized()
This method tells you whether a search is initialized (and can be performed) or not. |
int |
neededSteps()
This method returns the number of steps the search needed to find a solution or zero if the search was not yet successful. |
protected abstract void |
prepare()
This method is the hook for the preparation of the search and will be called from inside the initialize method. |
void |
run()
This method runs the search if it can be initialized. |
boolean |
running()
This method returns true only if the search has been started and is not finished or stopped. |
protected abstract void |
search()
This method is the hook for any concrete implementation of a search algorithm and will be called from inside the run method. |
void |
stop()
This method stops a running search. |
Methods inherited from class java.lang.Object |
---|
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
protected int neededSteps
protected T result
Constructor Detail |
---|
public Search()
Method Detail |
---|
public final boolean initialize()
public final boolean initialized()
public final void run()
run
in interface java.lang.Runnable
public final void stop()
public final boolean running()
public final T getResult()
public final int neededSteps()
protected abstract void search()
protected abstract boolean canPrepare()
protected abstract void prepare()
protected void finalize() throws java.lang.Throwable
finalize
in class java.lang.Object
java.lang.Throwable
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |