All Downloads are FREE. Search and download functionalities are using the official Maven repository.

javax.constraints.impl.search.SolutionIterator Maven / Gradle / Ivy

Go to download

This is a JSR331 interface for the open source Java constraint programming library "Sugar" v. 2.1.3

The newest version!
package javax.constraints.impl.search;

import javax.constraints.Solution;

/**
 * This is a simple implementation of the SolutionIterator interface 
 * that allows a user to search and to iterate through multiple solutions.
 * The expected use:
 * 
*
 * SolutionIterator iter = solver.solutionIterator();
 * while(iter.hasNext()) {
 *   Solution solution = iter.next();
 *     ...
 * }
 *
 * 
*/ public class SolutionIterator implements javax.constraints.SolutionIterator { Solver solver; Solution solution; int solutionNumber; boolean noSolutions; public SolutionIterator(javax.constraints.Solver solver) { this.solver = (Solver)solver; solution = null; noSolutions = false; solutionNumber = 0; } @Override public boolean hasNext() { if (noSolutions) return false; solution = null; if (solutionNumber == 0) { solution = solver.findSolution(); } else { solution = solver.findNextSolution(); } if (solution != null) { solutionNumber++; return true; } else return false; } @Override public Solution next() { if (solution == null) throw new RuntimeException("Cannot use SolutionIterator.next() before checking the hasNext() returned true"); return solution; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy