/**
 *
 *  THIS AGENT IS NOT COMPLETE.
 *  It is skeleton for a wrapper that computes result from request.
 *  You must complete the go(..) method self dependently.
 *  Generated 2002.9.17  by Sight 2.0, Ulm university.
 */
package impl.manipulators;

import Sight.Agents.*;
import Sight.Agents.Prototypes.*;
import Sight.Agents.Signals.*;
import Sight.dds.*;
import java.io.*;
import Sight.Agents.util.*;

public class numberFilter extends Wrapper implements Serializable, Fast {
  /* Data structure to pass query to this robot. */
  public class Request extends Sight.Request implements java.io.Serializable {
        /** Minimal allowed value, leave empty if not restricted */
        public CharSequence Min;
        /** Maximal allowed value, leave empty if not restricted */
        public CharSequence Max;
        /** Value to check for belonging to the interval above */
        public CharSequence test_value;

       /** Submit request, compute key by SHA digesting algorithm. */
       public Sight.Agents.Request submit() { return Sight.Agents.Request.submit(Public, this, getKey()); };
       /** Submit request with the known key. */
       public Sight.Agents.Request submit(String key)
        { return Sight.Agents.Request.submit(Public, this, key); };
       };

  /** Get default request for this robot. */
  public static Request getDefaultRequest() {
     return Public.getRequest(); }
  private Request getRequest() { return new Request(); }; // must be non static

    /** Type definitions for Request. */
   public dStructure getRequestDds() {
    return new Sight.dds.Records(true,
     new dField[] {
        new dField("Min","CharSequence","Minimal allowed value, leave empty if not restricted",""),
        new dField("Max","CharSequence","Maximal allowed value, leave empty if not restricted",""),
        new dField("test_value","CharSequence","Value to check for belonging to the interval above",""),
      }
    );
   }

    /** Type definitions for Result. */
   public dStructure getResultDds() {
    return new Sight.dds.Records(false,
     new dField[] {
        new dField("Min","CharSequence","Minimal allowed value, leave empty if not restricted",""),
        new dField("Max","CharSequence","Maximal allowed value, leave empty if not restricted",""),
        new dField("test_value","CharSequence","Value to check for belonging to the interval above","")
        }
    );
   }


 /* Record type, used in results for this robot. */
 /* The result of this robot is an array of records. */
  public static class Record extends Sight.Record implements Serializable {
        /** Minimal allowed value, leave empty if not restricted */
        public CharSequence Min;
        /** Maximal allowed value, leave empty if not restricted */
        public CharSequence Max;
        /** Value to check for belonging to the interval above */
        public CharSequence test_value;
      };
      /** The result of this robot. */
      public static class Result extends Sight.Result implements Serializable {
         public Record[] a; // array of results.
         /** Create report for one record. */
         public CharSequence getReportForRecord(Object ro, int level)
          { Record r = (Record) ro;
           switch (level) {
              case 0:  return " "+r.test_value+", belongs to ["+r.Min+".."+r.Max+"]";
              case 1:  return " "+r.test_value+", belongs to ["+r.Min+".."+r.Max+"]";
              default:
               {
                 StringBuffer b = new StringBuffer(r.test_value+" Belongs to ["+r.Min+".."+r.Max+"] ");
                 return b;
               }
            }
          };
      }

   /** Implementation of the central go method.  */
   public Object go(Object o_request) throws Exception
    {
     /** @todo: complete the go(...) method for this wrapper. */
     Request request = (Request) o_request;
     Result  result  = new Result();
     // we suppose only one record in the response of this robot:
     Record record = new Record();
     // .... (now fill in your record somehow):

     double min = Double.MIN_VALUE;
     double max = Double.MAX_VALUE;
     double x = 0;

     try {

     }
     catch (Exception ex) {

     }

     try {
       min = Double.parseDouble(request.Min.toString().trim());
     }
     catch (Exception ex) {

     }

     try {
       max = Double.parseDouble(request.Max.toString().trim());
     }
     catch (Exception ex) {
     }

     try {
       x = Double.parseDouble(request.test_value.toString().trim());
     }
     catch (Exception ex) {
      throw new FatalAgentException(request.test_value+" not a number.");
     }

     String pPattern = "["+request.Min+".."+request.Max+"]";

     if ( !(x>=min && x<=max) )
      { p(request.test_value+" is outside "+pPattern);
        return EMPTY;
      } else
         p(request.test_value+" BELONGS TO "+pPattern);

     result.a = new Record[1];
     result.a[0] = record;

     record.test_value = request.test_value;
     record.Min = request.Min;
     record.Max = request.Max;
     // set the agent signature:
     result.setAgentSignature("<b><i>Number filter "+pPattern+"</i></b> ");
     return result;
    };

  final Result EMPTY;
  public numberFilter() {
   setDescription("Check the numeric value for belonging to certain interval.");
   setMasterURL("");
   // this robot newer uses caching:
   setWriteCache(false);
   setReadCache(false);
   EMPTY = new Result();
   EMPTY.a = new Record[0];

   };
  /** Public instance of agent for user. */
  public static numberFilter Public = new numberFilter();
  public static Agent getAvailableAgent() { return Public; };
  /** main(..) method provided to check robot, launching it
   * as standalone program. */
  public static void main(String[] args) {
  Sight.Agents.util.Pind.showConsoles();
      try {
        Request request = getDefaultRequest();
        // Now modify and assign request fields in accordance with task:
        Sight.Agents.Request submission = request.submit();
        // processing now started in separate thread:
        Result response = (Result) submission.getResult();
        // print report, verbosity level 2:
        System.out.println(response.getReport(2));
      } catch (Exception exc)
         { if (exc!=null) System.out.println(exc.getMessage());
           exc.printStackTrace();
         };
   }
;
  }