// created: 28.12.00
// modified: 30.12.00

import java.util.*;

/***********************/
/* class ValueFunction */
/***********************/

abstract class ValueFunction
 {
 
 abstract float evaluate(Object var);

 }

/****************************/
/* class CountValueFunction */
/****************************/

abstract class CountValueFunction extends ValueFunction
 {

 int max = 0;
 float scale = 0.0f;

 CountValueFunction(){}

 void setMax(int m)
  {
  if (m >= 0)
   {
   max = m;
   scale = 1.0f/(new Integer(max)).floatValue();
   /****/
   System.out.println("setting max as "+String.valueOf(max));
   System.out.println("setting scale as "+String.valueOf(scale));
   /****/
   }
  }

 int getMax()
  {
  return max;
  }

 }

/****************************/
/* class ExistValueFunction */
/****************************/

abstract class ExistValueFunction extends ValueFunction
 {

 ExistValueFunction(){}

 }

/*****************************/
/* class ChoiceValueFunction */
/*****************************/
 
class ChoiceValueFunction extends ValueFunction
 {
 String theChoice;

 ChoiceValueFunction()  // a string of "" indicates 1.0f is always returned
  {
  theChoice = "";
  }

 ChoiceValueFunction(String choice)
  {
  theChoice = choice;
  }
 
 float evaluate(Object var)
  {
//  if (theChoice.equals(""))
//    {
//    /****/
//    System.out.println("--ChoiceValueFunction evaluates as "+String.valueOf(1.0f));
//    /****/
//    return 0.0f;
//    }
  String choice = (String) var;
  /****/
  System.out.println("--choice = "+theChoice);
  System.out.println("--var = "+choice);
  /****/
  if (choice.equals(theChoice))
    {
    /****/
    System.out.println("--ChoiceValueFunction evaluates as "+String.valueOf(1.0f));
    /****/
    return 1.0f;
    }
  else 
    {
    /****/
    System.out.println("--ChoiceValueFunction evaluates as "+String.valueOf(0.0f));
    /****/
    return 0.0f;
    }
  }

 String getChoice()
  {
  return theChoice;
  }

 void setChoice(String choice)
  {
  theChoice = choice;
  }
 }

/*******************************/
/* class PositiveValueFunction */
/*******************************/

class PositiveValueFunction extends CountValueFunction
 {

 PositiveValueFunction()
  {
  super();
  }

 PositiveValueFunction(int m)
  {
  super.setMax(m);
  }

 float evaluate(Object var)
  {
  float result = ((Integer) var).floatValue()*scale;
  /****/
  System.out.println("--PositiveValueFunction evaluates as "+String.valueOf(result));
  System.out.println("---max = "+String.valueOf(max));
  System.out.println("---scale = "+String.valueOf(scale));
  /****/
  return result;
  }

 }

/*******************************/
/* class NegativeValueFunction */
/*******************************/

class NegativeValueFunction extends CountValueFunction
 {

 NegativeValueFunction()
  {
  super();
  }

 NegativeValueFunction(int m)
  {
  super.setMax(m);
  }

 float evaluate(Object var)
  {
  float result = ((new Integer(max)).floatValue() - ((Integer) var).floatValue())*scale;
  /****/
  System.out.println("--NegativeValueFunction evaluates as "+String.valueOf(result));
  System.out.println("---max = "+String.valueOf(max));
  System.out.println("---scale = "+String.valueOf(scale));
  /****/
  return result; 
  }

 }

/***************************/
/* class SomeValueFunction */
/***************************/

class SomeValueFunction extends ExistValueFunction
 {

 SomeValueFunction(){}

 float evaluate(Object var)
  {
  if (((Boolean) var).booleanValue())
    {
    /****/
    System.out.println("--SomeValueFunction evaluates as "+String.valueOf(1.0f));
    /****/
    return 1.0f;
    }
  else 
    {
    /****/
    System.out.println("--SomeValueFunction evaluates as "+String.valueOf(0.0f));
    /****/
    return 0.0f;
    }
  }

 }

/***************************/
/* class NoneValueFunction */
/***************************/

class NoneValueFunction extends ExistValueFunction
 {

 NoneValueFunction(){}

 float evaluate(Object var)
  {
  if (((Boolean) var).booleanValue())
    {
    /****/
    System.out.println("--NoneValueFunction evaluates as "+String.valueOf(0.0f));
    /****/
    return 0.0f;
    }
  else
    {
    /****/
    System.out.println("--NoneValueFunction evaluates as "+String.valueOf(1.0f));
    /****/
    return 1.0f;
    }
  }

 }

/**********************************/
/* class CountChoiceValueFunction */
/**********************************/

class CountChoiceValueFunction extends ChoiceValueFunction
 {
 CountValueFunction function;

 CountChoiceValueFunction()
  {
  super();
  function = new PositiveValueFunction();
  }

 CountChoiceValueFunction(CountValueFunction func)
  {
  super();
  function = func;
  }

 CountChoiceValueFunction(String choice, CountValueFunction func)
  {
  super(choice); 
  function = func;
  }

 float evaluate(Object var)
  { 
  float isChoice = super.evaluate(((Vector) var).elementAt(0));  // this returns a 0.0f or 1.0f   
  float result = isChoice*function.evaluate(((Vector) var).elementAt(1));
  /****/
  System.out.println("--ChoiceCountValueFunction evaluates as "+String.valueOf(result));
  /****/
  return result;
  }

 CountValueFunction getFunction()
  {
  return function;
  } 

 int getMax()
  {
  return function.getMax();
  }

 void setFunction(CountValueFunction func)
  {
  function = func;
  }
  
 void setMax(int m)
  {
  function.setMax(m);
  }
 
 }

