package scripts;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.codelutin.math.matrix.*;

import fr.ifremer.isisfish.entities.Population;
import fr.ifremer.isisfish.entities.PopulationGroup;
import fr.ifremer.isisfish.entities.Species;
import fr.ifremer.isisfish.simulator.SimulationContext;

/**
 * ChangeMatrixBackend.java
 *
 * Created: 6 septembre 2006
 *
 * @author anonymous <anonymous@labs.libre-entreprise.org>
 * @version $Revision: 1.1 $
 *
 * Last update: $Date: 2007/03/01 17:17:33 $
 * by : $Author: bpoussin $
 */
public class RuleUtil {

    /** to use log facility, just put in your code: log.info("..."); */
    static private Log log = LogFactory.getLog(RuleUtil.class);

    /**
     * @param context
     * @return
     */
    public static double getTotalCatchTons(SimulationContext context, Species species) {
        double result = 0;
        for (Population pop : species.getPopulation()) {
            MatrixND mat = context.getPopulationMonitor().getHoldCatch(pop);
            if(mat!=null) {
                mat = mat.copy();
                mat = mat.sumOverDim(0); // sum over Strategies
                mat = mat.sumOverDim(1); // sum over metiers
                mat = mat.sumOverDim(3); // sum over zones
                List<PopulationGroup> groups = mat.getSemantics(2);

                for (int c=0; c<groups.size(); c++) {
                    PopulationGroup group = groups.get(c);
                    double weight = group.getMeanWeight();
                    result += mat.getValue(0, 0, c, 0) * weight / 1000.0;
                }
            }
        }
        return result;
    }

    
    
    
}
