package net.sf.jclec.problem.funopt.binarray; import javolution.xml.XmlFormat; import javolution.xml.XmlElement; import net.sf.jclec.JCLEC; import net.sf.jclec.IConfigure; import net.sf.jclec.util.range.IRange; import org.apache.commons.configuration.Configuration; import org.apache.commons.configuration.ConfigurationRuntimeException; import org.apache.commons.lang.builder.EqualsBuilder; /** * An search space element. * * @author Sebastián Ventura */ public class SearchSpaceElement implements JCLEC, IConfigure { ///////////////////////////////////////////////////////////////// // --------------------------------------- Serialization constant ///////////////////////////////////////////////////////////////// /** Generated by Eclipse */ private static final long serialVersionUID = -7396547527917746670L; ///////////////////////////////////////////////////////////////// // --------------------------------------------------- XML Format ///////////////////////////////////////////////////////////////// protected static final XmlFormat<SearchSpaceElement> SEARCH_SPACE_ELEMENT_XML = new XmlFormat<SearchSpaceElement>(SearchSpaceElement.class) { public void format(SearchSpaceElement searchSpaceElement, XmlElement xml) { xml.add(searchSpaceElement.range, "range"); xml.add(searchSpaceElement.precission, "precission", INTEGER_XML); } public SearchSpaceElement parse(XmlElement xml) { SearchSpaceElement searchSpaceElement = xml.<SearchSpaceElement>object(); searchSpaceElement.range = xml.<IRange> get("range"); searchSpaceElement.precission = xml.get("precission", INTEGER_XML); return searchSpaceElement; } public String defaultName() { return "search-space-element"; } }; ///////////////////////////////////////////////////////////////// // --------------------------------------------------- Properties ///////////////////////////////////////////////////////////////// /** Search range */ protected IRange range; /** Number of bits used to represent this dimension */ protected int precission; ///////////////////////////////////////////////////////////////// // ------------------------------------------------- Constructors ///////////////////////////////////////////////////////////////// /** * Empty (default) constructor. */ public SearchSpaceElement() { super(); } /** * Constructor used in utit tests. */ public SearchSpaceElement(IRange range, int precission) { super(); setRange(range); setPrecission(precission); } ///////////////////////////////////////////////////////////////// // ------------------------------- Setting and getting properties ///////////////////////////////////////////////////////////////// public final int getPrecission() { return precission; } public final void setPrecission(int precission) { this.precission = precission; } public final IRange getRange() { return range; } public final void setRange(IRange range) { this.range = range; } ///////////////////////////////////////////////////////////////// // ---------------------------- Implementing IConfigure interface ///////////////////////////////////////////////////////////////// @SuppressWarnings("unchecked") public void configure(Configuration settings) { // Get precission property int precission = settings.getInt("precission"); // Set precission setPrecission(precission); // Search range try { // Range classname String rangeClassname = settings.getString("range[@type]"); // Evaluator class Class<? extends IRange> rangeClass = (Class<? extends IRange>) Class.forName(rangeClassname); // Evaluator instance IRange range = rangeClass.newInstance(); // Configure species if (range instanceof IConfigure) { ((IConfigure) range).configure(settings.subset("range")); } // Set species setRange(range); } catch (ClassNotFoundException e) { throw new ConfigurationRuntimeException("Illegal range classname"); } catch (InstantiationException e) { throw new ConfigurationRuntimeException("Problems creating an instance of range", e); } catch (IllegalAccessException e) { throw new ConfigurationRuntimeException("Problems creating an instance of range", e); } } ///////////////////////////////////////////////////////////////// // ------------------------- Overwriting java.lang.Object methods ///////////////////////////////////////////////////////////////// public boolean equals(Object other) { if (other instanceof SearchSpaceElement) { SearchSpaceElement cother = (SearchSpaceElement) other; EqualsBuilder eb = new EqualsBuilder(); eb.append(precission, cother.precission); eb.append(range, cother.range); return eb.isEquals(); } else { return false; } } }