gt:         if (bufferCount < window_size) {
context: /**
  * Global Sensor Networks (GSN) Source Code
  * Copyright (c) 2006-2014, Ecole Polytechnique Federale de Lausanne (EPFL)
  * 
  * This file is part of GSN.
  * 
  * GSN is free software: 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 3 of the License, or
  * (at your option) any later version.
  * 
  * GSN 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 GSN.  If not, see <http://www.gnu.org/licenses/>.
  * 
  * File: src/gsn/vsensor/DataCleanVirtualSensor.java
  *
  * @author Sofiane Sarni
  *
  */
  
  package gsn.vsensor;
  
  import gsn.beans.DataTypes;
  import gsn.beans.StreamElement;
  import gsn.utils.Helpers;
  import gsn.utils.models.ModelFitting;
  import org.apache.commons.httpclient.HttpClient;
  import org.apache.commons.httpclient.HttpException;
  import org.apache.commons.httpclient.methods.PostMethod;
  import org.apache.commons.httpclient.methods.RequestEntity;
  import org.apache.commons.httpclient.methods.StringRequestEntity;
  import org.apache.commons.httpclient.UsernamePasswordCredentials;
  import org.apache.commons.httpclient.auth.AuthScope;
  import org.apache.log4j.Logger;
  
  import java.io.IOException;
  import java.io.Serializable;
  import java.io.UnsupportedEncodingException;
  import java.util.TreeMap;
  
  import static gsn.utils.models.ModelFitting.getModelIdFromString;
  
  
  public class DataCleanVirtualSensor extends AbstractVirtualSensor {
  
      private static final transient Logger logger = Logger.getLogger(BridgeVirtualSensor.class);
  
      private static final String OPERATOR = "_operator";
      private static final String DEPLOYMENT = "_deployment";
      private static final String STATION = "_station";
      private static final String SENSOR = "_sensor";
      private static final String FROM = "_from";
      private static final String TO = "_to";
      private static final String DIRTINESS = "_dirtiness";
  
      private static final String xml_template = "<metadata>\n" +
              "\t<deployement>" + DEPLOYMENT + "</deployment>\n" +
              "\t<operator>" + OPERATOR + "</operator>\n" +
              "\t<station>" + STATION + "</station>\n" +
              "\t<sensor>" + SENSOR + "</sensor>\n" +
              "\t<from>" + FROM + "</from>\n" +
              "\t<to>" + TO + "</to>\n" +
              "\t<dirtiness>" + DIRTINESS + "</dirtiness>\n" +
              "</metadata>";
  
      private static final String PARAM_MODEL = "model";
      private static final String PARAM_ERROR_BOUND = "error_bound";
      private static final String PARAM_WINDOW_SIZE = "window_size";
  
      // optional parameters
      private static final String PARAM_METADATA_SERVER = "metadata_server_url"; // metadata server for posting metadata, e.g. http://www.example.com/dataclean.php
  
      private static final String PARAM_METADATA_USERNAME = "user"; // username for metadata server
      private static final String PARAM_METADATA_PASSWORD = "password"; // password for metadata server
  
      private static final String PARAM_METADATA_OPERATOR = "operator"; // operator for metadata server, typically e-mail
      private static final String PARAM_METADATA_DEPLOYEMENT = "deployment"; // name of deployemnt for metadata server
      private static final String PARAM_METADATA_STATION = "station"; // name of station for metadata server
      private static final String PARAM_METADATA_SENSOR = "sensor"; // name of station for metadata server
      private static final String PARAM_LOGGING_INTERVAL = "logging-interval";
  
      private static final int NORMAL_RESULT = 200; // normal result after http post
  
      private int model = -1;
      private int window_size = 0;
      private double error_bound = 0;
  
      private double[] stream;
      private long[] timestamps;
      private double[] processed;
      private double[] dirtiness;
      private double[] quality;
  
      private String metadata_server_url;
      private String username;
      private String password;
      private String operator;
      private String deployement;
      private String station;
      private String sensor;
  
      private String prepared_xml_request;
  
      private boolean publish_to_metadata_server = false;
      private boolean metadata_server_requieres_password = false;
  
  
      private int bufferCount = 0;
      private boolean logging_timestamps = false;
      private long logging_interval = 1;
      private long logging_counter = 0;
  
  
      public boolean initialize() {
  
          TreeMap<String, String> params = getVirtualSensorConfiguration().getMainClassInitialParams();
  
          String model_str = params.get(PARAM_MODEL);
  
          if (model_str == null) {
              logger.warn("Parameter \"" + PARAM_MODEL + "\" not provided in Virtual Sensor file");
              return false;
          } else {
              model = getModelIdFromString(model_str.trim());
              if (model == -1) {
                  logger.warn("Parameter \"" + PARAM_MODEL + "\" incorrect in Virtual Sensor file");
                  return false;
              }
          }
  
          String window_size_str = params.get(PARAM_WINDOW_SIZE);
  
          if (window_size_str == null) {
              logger.warn("Parameter \"" + PARAM_WINDOW_SIZE + "\" not provided in Virtual Sensor file");
              return false;
          } else try {
              window_size = Integer.parseInt(window_size_str.trim());
          } catch (NumberFormatException e) {
              logger.warn("Parameter \"" + PARAM_WINDOW_SIZE + "\" incorrect in Virtual Sensor file");
              return false;
          }
  
          if (window_size < 0) {
              logger.warn("Window size should always be positive.");
              return false;
          }
  
          String error_bound_str = params.get(PARAM_ERROR_BOUND);
  
          if (error_bound_str == null) {
              logger.warn("Parameter \"" + PARAM_ERROR_BOUND + "\" not provided in Virtual Sensor file");
              return false;
          } else try {
              error_bound = Double.parseDouble(error_bound_str.trim());
          } catch (NumberFormatException e) {
              logger.warn("Parameter \"" + PARAM_ERROR_BOUND + "\" incorrect in Virtual Sensor file");
              return false;
          }
  
          metadata_server_url = params.get(PARAM_METADATA_SERVER);
  
          if (metadata_server_url != null) {
              publish_to_metadata_server = true;
  
              username = params.get(PARAM_METADATA_USERNAME);
              password = params.get(PARAM_METADATA_PASSWORD);
  
              if (username != null && password != null)
                  metadata_server_requieres_password = true;
  
              operator = params.get(PARAM_METADATA_OPERATOR);
              deployement = params.get(PARAM_METADATA_DEPLOYEMENT);
              station = params.get(PARAM_METADATA_STATION);
              sensor = params.get(PARAM_METADATA_SENSOR);
  
              if ((operator == null) || (deployement == null) || (station == null) || (sensor == null)) {
                  logger.warn("A parameter required for publishing metadata is missing. Couldn't publish to metadata server.");
                  publish_to_metadata_server = false;
              } else {
                  prepared_xml_request = xml_template.replaceAll(OPERATOR, operator);
                  prepared_xml_request = prepared_xml_request.replaceAll(DEPLOYMENT, deployement);
                  prepared_xml_request = prepared_xml_request.replaceAll(STATION, station);
                  prepared_xml_request = prepared_xml_request.replaceAll(SENSOR, sensor);
              }
  
          }
  
          stream = new double[window_size];
          timestamps = new long[window_size];
          processed = new double[window_size];
          dirtiness = new double[window_size];
          quality = new double[window_size];
  
          String logging_interval_str = params.get(PARAM_LOGGING_INTERVAL);
          if (logging_interval_str != null) {
              logging_timestamps = true;
              try {
                  logging_interval = Integer.parseInt(logging_interval_str.trim());
              } catch (NumberFormatException e) {
                  logger.warn("Parameter \"" + PARAM_LOGGING_INTERVAL + "\" incorrect in Virtual Sensor file");
                  logging_timestamps = false;
              }
          }
  
          return true;
      }
  
      public void dataAvailable(String inputStreamName, StreamElement data) {
  
          if (logging_timestamps && logging_counter % logging_interval == 0) {
              logger.warn(getVirtualSensorConfiguration().getName() + " , " + logging_counter + " , " + System.currentTimeMillis());
          }
  
          logging_counter++;
  
  