/*
 * ECatalog is a database front-end, with two main features:
 * 1. Use of preferences
 *  A preference-based approach, where the user is allowed to define the importance of each criterion.
 *  Then the items are ranked accordingly to his criteria.
 * 2. Trade-off analysis
 *  A cooperative database approach, where the system "argues" with the user about his criteria.
 *  When there are no matching items, the system explains the minimal conflicting set and
 *  give some possible strong and weak relaxations about his criteria.
 * This package also containts the software and the set-up details used for our User Study,
 * comparing the use or not of the two previous features mentioned above.
 *
 * Copyright (C) 2006 David Portabella Clotet, Artificial Intelligence Laboratory, EPFL
 * 
 * This file is part of ecatalog-1.0.zip
 * 
 * ECatalog is free software and a free user study set-up;
 * 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.
 * 
 * ECatalog 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 ECatalog; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 * @version 1.0
 * @author David Portabella
 * To contact the author:
 * email: david@portabella.name and david.portabella@epfl.ch
 * 
 * More information about ECatalog:
 *  http://sourceforge.net/projects/ecatalog/
 *  http://icwww.epfl.ch/~portabel/ecatalogs/
 */

package ecatalog.db;

import java.util.Vector;
import java.util.Arrays;
import java.util.Comparator;
import java.sql.*;

import dpc.utils.TextUtil;
import dpc.utils.ArrayConverter;

public class DBRank {

    /* Compute the ranks, taking into account the constraints (except the MUST constraints) */
    static public double[] computeRanks(Database db, ResultSet rs) throws SQLException {
	Vector<Double> ranks = new Vector<Double>();
	while (rs.next()) {
	    ranks.add(computeItemRank(db, new ResultSetItem(db, rs)));
	    //System.out.println("valueIndex: " + i + "\trank: " + ranks[i] + "\tid=" + rs.getString("id"));
	}

	return ArrayConverter.toDoubles(ranks);
    }


    static public double computeItemRank(Database db, Item item) throws SQLException {
	double rank = 0;
	for (Constraint c : db.constraints) {
	    if (!c.areDetailsDefined() || c.getWeight().getWeightValue() == -1)
		continue;
	    double penalty = c.getPenaltyForItem(item);
	    double weightedPenalty = c.getWeight().getWeightValue() * penalty;
	    rank += weightedPenalty;
	}
	return rank;
    }



    /* Sort the ranks
     * @param ranks[]              the ranks
     * @return int[] sortedIndex   sortedNIndex[0]==i means that ranks[i] has the bigger rank
     */ 
    static public int[] sortRanks(double ranks[]) {
	Integer[] sortedIndex = new Integer[ranks.length];
	for (int i = 0; i < ranks.length; i++)
	    sortedIndex[i] = i;

	class MyComparator implements Comparator {
	    double[] ranks;
	    MyComparator(double[] ranks) { this.ranks = ranks; }
	    public int compare(Object o1, Object o2) {
		double diff = ranks[(Integer)o2] - ranks[(Integer)o1];
		if (diff == 0.0)
		    return 0;
		return (diff < 0) ? -1 : 1;
	    }
	    public boolean equals(Object obj) { return this.equals(obj); }
	};

	Arrays.sort(sortedIndex, new MyComparator(ranks));
	return ArrayConverter.toIntegers(sortedIndex);
    }
}

