/****************************************************************************
 * This file was derived from the CNCL library by the Pablo research group.
 *
 * The complete CNCL library is available at:
 *       http://www.comnets.rwth-aachen.de/doc/cncl.html
 *
 * The CNCL copyright notice is included here:
 *
 * |_|_|_  |_|_    |_    |_|_|_  |_               C O M M U N I C A T I O N
 *|_       |_  |_  |_  |_        |_                         N E T W O R K S
 *|_       |_  |_  |_  |_        |_                               C L A S S
 * |_|_|_  |_    |_|_    |_|_|_  |_|_|_|_                     L I B R A R Y
 *
 *
 *
 * Copyright (C) 1992-1998   Communication Networks
 *                           Aachen University of Technology
 *                           D-52056 Aachen
 *                           Germany
 *                           Email: cncl-adm@comnets.rwth-aachen.de
 *
 *
 * This library is free  software; you can  redistribute it and/or modify
 * it  under   the terms  of the GNU   Library  General Public License as
 * published by the  Free Software Foundation;  either  version 2 of  the
 * License, or  (at  your option)  any  later  version.  This  library 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 Library General Public
 * Library General Public License along with  this library; if not, write
 * to the Free Software Foundation,  675  Mass Ave, Cambridge, MA  02139,
 * USA.
 ****************************************************************************/

//   -*- C++ -*-
/*****************************************************************************
 *
 * Class: FlFSet --- Fuzzy set abstract base class
 *
 *****************************************************************************/

#include <fstream>
#include "FlFSet.h"
//#include <FlPipe.h>

using std::endl;
using std::cin;
using std::istream;
using std::ofstream;
using std::ifstream;

/*
 * Constructors
 */
FlFSet::FlFSet()
    : FlNamed(""), ymin(0), ymax(0)
{}


FlFSet::FlFSet(double min, double max)
    : FlNamed(""), ymin(min), ymax(max)
{}


FlFSet::FlFSet(FlStringR xname, double min, double max)
    : FlNamed(xname), ymin(min), ymax(max)
{}



/*
 * Initialize
 */
void FlFSet::init(FlStringR name, double min, double max)
{
    set_name(name);
    ymin = min;
    ymax = max;
}



/*
 * Compute center of gravity for defuzzyfication
 */
double FlFSet::center_of_gravity(double /*min*/, double /*max*/) const
{
    error(NIL, "FlFSet::center_of_gravity(): not yet implemented");
    return 0;
}

/*
 *
 */
void FlFSet::printDataPoints(ostream &strm, 
			       double xmin, double xmax, int steps) const
{
  double val,inc;

  inc = (xmax - xmin) / steps;

  for (int st = 0; st <= steps; st++) 
    {
      val = xmin + (st * inc);
      strm << val << "\t" << get_membership(val) << endl;
    }

}

/*
 *
 */
void FlFSet::generatePlotFiles(double xmin, double xmax, 
				 int steps, FlStringR tag) const
{
  ofstream datFile(name() + tag + ".dat");
  ofstream gPlotFile(name() + tag + ".gp");
  
  printDataPoints(datFile, xmin, xmax, steps);

  gPlotFile << "#set term fig" << endl;
  gPlotFile << "#set out \"" + name() + tag + ".fig\"" << endl;
  gPlotFile << "set title" << endl;
  gPlotFile << "set size 0.7, 0.7" << endl;
  gPlotFile << "set nokey" << endl;
  gPlotFile << "set yrange [0:1.4]" << endl;
  gPlotFile << "set ytics 0,.2,1" << endl;
  gPlotFile << "set ylabel \"Membership Value\" 3" << endl;
  gPlotFile << "set xlabel \"" + name() + "\"" << endl;
  gPlotFile << "" << endl;
  gPlotFile << "plot \"" + name() + tag + ".dat\" with lines" << endl;

  
  datFile.close();
  gPlotFile.close();
}

/*
 *
 */
void FlFSet::plot(double xmin, double xmax, 
		    int steps, FlStringR tag) const  
{
//   FlPipe p;

//   generatePlotFiles(xmin, xmax, steps, tag);

//   ifstream gPlotFile(name() + tag + ".gp" );

//   p.open("gnuplot");
//   ostream &gPlot = p.out();
//   istream &in  = p.in();

//   gPlot << gPlotFile.rdbuf() << endl;

//   cout << "Press RETURN to continue.";
//   cin.get();
 
//   gPlot << "exit" << endl;

//   gPlotFile.close();
//   p.close();
}

