/*
 * 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 ecatalog.ECatalog;

import org.w3c.dom.Element;
import javax.xml.bind.JAXBException;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Unmarshaller;
import ecatalog.jaxb.simpleShapePenaltyFunctionConfig.SimpleShapePenaltyFunctionConfigType;


/*
 *  TODO: now it does not used the offsetFraction. Decide the correct way, and FIX THIS CLASS & Config file!
 */
public class SimpleShapePenaltyFunction implements NumberAttributePenaltyFunction {
    enum Type {EQUAL, LESS, MORE};
    Type type;
    //double offsetFraction;

    NumberAttribute attribute;
    double minValue, maxValue;


    public SimpleShapePenaltyFunction() {}
    public SimpleShapePenaltyFunction(NumberAttribute attribute, Type type) {
	configure(attribute, type);
    }

    public void configure(NumberAttribute attribute, Element configElement) throws Exception {
	Unmarshaller u = JAXBContext.newInstance("ecatalog.jaxb.simpleShapePenaltyFunctionConfig").createUnmarshaller();
        SimpleShapePenaltyFunctionConfigType config = (SimpleShapePenaltyFunctionConfigType) ((JAXBElement<?>)u.unmarshal(configElement)).getValue();
	Type type = Type.valueOf(config.getType());
	//double offsetFraction = config.getOffsetFraction();
	//configure(type, offsetFraction);
	configure(attribute, type);
    }

    //public void configure(Type type, double offsetFraction) {
    public void configure(NumberAttribute attribute, Type type) {
	this.attribute = attribute;

	minValue = attribute.getMinValueId();
	maxValue = attribute.getMaxValueId();

	this.type = type;
	//this.offsetFraction = offsetFraction;
    }



    /*
     * @result   a penalty.  if the value satisfies the constraint, it is a value between 0 (best) and 1 (ok).
                             otherwise, it is a value between 1 (best) and 2 (worst)
     */

    public double getPenaltyForValue(double constraintValue, double itemValue) {
	//if (constraintValue == itemValue)
	//    return 0.0;

	double penalty;
	if (type == Type.EQUAL) {
	    if (itemValue >= maxValue)
		penalty = 2.0;
	    else if (itemValue <= minValue)
		penalty = 2.0;
	    else if (itemValue <= constraintValue)
		penalty = 2.0 * (constraintValue - itemValue) / (constraintValue - minValue);
	    else
		penalty = 2.0 * (itemValue - constraintValue) / (maxValue - constraintValue);
    
	} else if (type == Type.LESS) {
	    if (itemValue >= maxValue)
		penalty = 2.0;
	    else if (itemValue <= minValue)
		penalty = 0.0;
	    else if (itemValue <= constraintValue)
		penalty = (itemValue - minValue) / (constraintValue - minValue);
	    else
		penalty = 1.0 + (itemValue - constraintValue) / (maxValue - constraintValue);

	} else if (type == Type.MORE) {
	    if (itemValue >= maxValue)
		penalty = 0.0;
	    else if (itemValue <= minValue)
		penalty = 2.0;
	    if (itemValue <= constraintValue)
		penalty = 1.0 + (constraintValue - itemValue) / (constraintValue - minValue);
	    else
		penalty = (maxValue - itemValue) / (maxValue - constraintValue);

	} else {	    
	    ECatalog.error(new Error("undefined penalty type"));
	    throw new Error(); //it is already done, but to avoid the java compiling error
	}

	return -penalty;
    }


    /*
     * @result   a penalty.  0 means just satisfied. -1 means really bad (penalty)
     */
    /*
    public double getPenaltyForValue(double constraintValue, double itemValue) {
	if (constraintValue == itemValue)
	    return 0.0;

	double minValue = constraintValue * (1.0 - offsetFraction);
	double maxValue = constraintValue * (1.0 + offsetFraction);

	switch (type) {
	case EQUAL: 
	    if (itemValue > constraintValue) {
		if (itemValue >= maxValue)
		    return -1.0;
		return ((constraintValue - itemValue) / (maxValue - constraintValue));
	    } else if (itemValue < constraintValue) {
		if (itemValue <= minValue)
		    return -1.0;
		return -1 + ((itemValue - minValue) / (constraintValue - minValue));
	    }
	    
	case LESS:
	    if (itemValue >= maxValue)
		return -1.0;
	    else if (itemValue <= constraintValue)
		return 0.0;
	    return 1 - 2 * ((itemValue - minValue) / (maxValue - minValue));

	case MORE:
	    if (itemValue >= constraintValue)
		return 0.0;
	    else if (itemValue <= minValue)
		return -1.0;
	    return -1 + 2 * ((itemValue - minValue) / (maxValue - minValue));
	    
	default: ECatalog.error(new Error("undefined penalty type"));
	         throw new Error(); //it is already done, but to avoid the java compiling error
	}
    }
    */


    /*
     * @result   a penalty.  0 means just satisfied. +1 means even better. -1 means really bad (penalty)
     */
    /*
    public double getPenaltyForValue(double constraintValue, double itemValue) {
	if (constraintValue == itemValue)
	    return 0.0;

	double minValue = constraintValue * (1.0 - offsetFraction);
	double maxValue = constraintValue * (1.0 + offsetFraction);
	switch (type) {
	case EQUAL: 
	    if (itemValue > constraintValue) {
		if (itemValue >= maxValue)
		    return -1.0;
		return ((constraintValue - itemValue) / (maxValue - constraintValue));
	    } else if (itemValue < constraintValue) {
		if (itemValue <= minValue)
		    return -1.0;
		return -1 + ((itemValue - minValue) / (constraintValue - minValue));
	    }
	    
	case LESS:
	    if (itemValue >= maxValue)
		return -1.0;
	    else if (itemValue <= minValue)
		return 1.0;
	    return 1 - 2 * ((itemValue - minValue) / (maxValue - minValue));

	case MORE:
	    if (itemValue >= maxValue)
		return 1.0;
	    else if (itemValue <= minValue)
		return -1.0;
	    return -1 + 2 * ((itemValue - minValue) / (maxValue - minValue));
	    
	default: throw new Error("undefined penalty type");
	}
    }
    */
}

