public Solution getReferenceSolution(Problem p)
throws UnsupportedOperationException {
Solution result;
if (!haveReferenceSolution)
throw new UnsupportedOperationException("Domain.getReferenceSolution: A getReferenceSolution() method has not been specified for this domain. If its use is required, please specify one using setEquivalenceClasses() or by overriding Domain.getReferenceSolution().");
else {
if (haveBooleanSolutionCutoff)
result = findNearestEquivalenceClass(p).applyTo(p, booleanSolutionCutoff);
else
result = findNearestEquivalenceClass(p).applyTo(p);
}
result.setIsReferenceSolution(true);
return result;
}
-
Please explain more precisely what you expect to have in returned array.Guillaume– Guillaume2011-04-11 06:54:55 +00:00Commented Apr 11, 2011 at 6:54
-
I am having 2 types of classes. One class use this class to return non-array result. For another class, i want to use this class to return an array result. eg: Solution[] . How can I do it?karikari– karikari2011-04-11 07:00:39 +00:00Commented Apr 11, 2011 at 7:00
Add a comment
|
3 Answers
If you only need one solution normally, but one place needs multiple solutions, I suggest you have two methods; something like this:
public Solution getReferenceSolution(Problem p)
{
// Code as before
}
public List<Solution> getAllSolutions(Problem p)
{
// Whatever you need to do here
}
Note how it's now obvious from the method name whether you're looking for one solution or multiple ones; I wouldn't use overloading in this situation, as you're trying to do different things.
Comments
Do you mean like this?
public Solution[] getReferenceSolution(Problem p) {
Solution result;
// set result.
return new Solution[] { result };
}
3 Comments
karikari
this function is referred by other classes too. but those classes dont need array. How can I somehow override this function just for one class that want to return array?
karikari
how to declare it as an override?
Peter Lawrey
The return types need to be the same for all implementations (or a sub class). Either you use an array to return on all of them or none of them. Another option is to have a Solution which contains an array of Solution. This would allow you to return an "array" inside a Solution.
Maybe better to return a collection, e.g. an ArrayList:
public List<Solution> getReferenceSolution(Problem p)
throws UnsupportedOperationException {
List<Solution> solutions= new ArrayList<Solution>();
Solution result = ... // your code here
solutions.add(result);
return solutions;
}
Or maybe you want to pass the List as argument to the getReferenceSolution method and fill it inside the method?
public void getReferenceSolution(Problem p, List<Solution> solutions)
throws UnsupportedOperationException {
// your code to fill the list using solutions.add(Solution)
}