/**
 * Copyright (C) 2003 - 2009
 * Computational Intelligence Research Group (CIRG@UP)
 * Department of Computer Science
 * University of Pretoria
 * South Africa
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package net.sourceforge.cilib.problem;

import java.util.logging.Level;
import java.util.logging.Logger;
import net.sourceforge.cilib.cooperative.CooperativeEntity;
import net.sourceforge.cilib.type.DomainRegistry;
import net.sourceforge.cilib.type.StringBasedDomainRegistry;
import net.sourceforge.cilib.type.parser.DomainParser;
import net.sourceforge.cilib.type.parser.ParseException;
import net.sourceforge.cilib.type.types.Type;
import net.sourceforge.cilib.type.types.Types;
import net.sourceforge.cilib.type.types.container.Vector;

/**
 * TODO test this class.
 * @author Theuns Cloete
 */
public class CooperativeOptimisationProblemAdapter extends OptimisationProblemAdapter {
    private static final long serialVersionUID = 6643809900184758346L;

    private OptimisationProblem problem = null;
    private CooperativeEntity context = null;
    private DomainRegistry domainRegistry = null;
    private int dimension = 0;
    private int offset = 0;

    /**
     * Creates an OptimisationProblemAdapter that has the specified dimension starting at the given
     * offset (index) position of the full given problem.
     * @param p problem the full problem that is being split up
     * @param c Reference to the {@link net.sourceforge.cilib.cooperative.CooperativeEntity}
     * @param d dimension the dimension that that this sub-problem should be
     * @param o offset the offset (index) position in the full-problem where this sub-problem should
     *        start
     */
    public CooperativeOptimisationProblemAdapter(OptimisationProblem p, CooperativeEntity c, int d, int o) {
        problem = p;
        context = c.getClone();
        dimension = d;
        offset = o;
        domainRegistry = new StringBasedDomainRegistry();
        String expandedDomain = "";
        for (int i = offset; i < offset + dimension; i++) {
            String tmp = Types.getRepresentation(((Vector) context.getCandidateSolution()).get(i));
            expandedDomain += tmp;//((Vector) context.getCandidateSolution()).get(i).getRepresentation();
            if (i < offset + dimension - 1)
                expandedDomain += ",";
        }
        try {
//        DomainParser dp = new DomainParser();
//        if (dp.parse(expandedDomain)) {
//            domainRegistry.setDomainString(expandedDomain);
//        }
//        else
//            throw new InitialisationException("The expanded domain string \"" + expandedDomain + "\" could not be parsed.");
            DomainParser.parse(expandedDomain);
        } catch (ParseException ex) {
            Logger.getLogger(CooperativeOptimisationProblemAdapter.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public CooperativeOptimisationProblemAdapter(CooperativeOptimisationProblemAdapter copy) {
        super(copy);
        problem = copy.problem.getClone();
        context = copy.context.getClone();
        domainRegistry = copy.domainRegistry.getClone();
        dimension = copy.dimension;
        offset = copy.offset;
    }

    public CooperativeOptimisationProblemAdapter getClone() {
        return new CooperativeOptimisationProblemAdapter(this);
    }

    public int getDimension() {
        return dimension;
    }

    public int getOffset() {
        return offset;
    }

    public void updateContext(CooperativeEntity c) {
        context = c.getClone();
    }

    @Override
    protected Fitness calculateFitness(Type solution) {
        Vector participant = (Vector) solution;
        for (int i = 0; i < dimension; ++i) {
            ((Vector) context.getCandidateSolution()).setReal(offset + i, participant.getReal(i));
        }
        return problem.getFitness(context.getCandidateSolution());
    }

    public DomainRegistry getDomain() {
        return domainRegistry;
    }

    public DomainRegistry getBehaviouralDomain() {
        // QUESTION What exactly does the problem.getBehaviouralDomain() method return and what is
        // really needed?
        return domainRegistry;
    }
}

@@ -921,73 +921,8 @@
 m;%0A%0A
-import java.util.logging.Level;%0Aimport java.util.logging.Logger;%0A
 impo
@@ -1092,120 +1092,8 @@
 ry;%0A
-import net.sourceforge.cilib.type.parser.DomainParser;%0Aimport net.sourceforge.cilib.type.parser.ParseException;%0A
 impo
@@ -2472,28 +2472,45 @@
 ring
- expandedDomain = %22%22
+Builder builder = new StringBuilder()
 ;%0A  
@@ -2683,29 +2683,27 @@
     
-expandedDomain += 
+builder.append(
 tmp
+)
 ;//(
@@ -2835,29 +2835,27 @@
     
-expandedDomain += 
+builder.append(
 %22,%22
+)
 ;%0A  
@@ -2866,536 +2866,68 @@
   %7D%0A
-        try %7B%0A//        DomainParser dp = new DomainParser();%0A//        if (dp.parse(expandedDomain)) %7B%0A//            domainRegistry.setDomainString(expandedDomain);%0A//        %7D%0A//        else%0A//            throw new InitialisationException(%22The expanded domain string %5C%22%22 + expandedDomain + %22%5C%22 could not be parsed.%22);%0A            DomainParser.parse(expandedDomain);%0A        %7D catch (ParseException ex) %7B%0A            Logger.getLogger(CooperativeOptimisationProblemAdapter.class.getName()).log(Level.SEVERE, null, ex);%0A        %7D
+%0A        domainRegistry.setDomainString(builder.toString());
 %0A   
@@ -3254,32 +3254,46 @@
 .offset;%0A    %7D%0A%0A
+    @Override%0A
     public Coope
@@ -3964,32 +3964,46 @@
 tion());%0A    %7D%0A%0A
+    @Override%0A
     public Domai

Set expanded domain string correctly.