pelib  2.0.0
src/demo.cpp
Go to the documentation of this file.
00001 /*
00002  Copyright 2015 Nicolas Melot
00003 
00004  This file is part of Pelib.
00005 
00006  Pelib is free software: you can redistribute it and/or modify
00007  it under the terms of the GNU General Public License as published by
00008  the Free Software Foundation, either version 3 of the License, or
00009  (at your option) any later version.
00010 
00011  Pelib is distributed in the hope that it will be useful,
00012  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00014  GNU General Public License for more details.
00015 
00016  You should have received a copy of the GNU General Public License
00017  along with Pelib. If not, see <http://www.gnu.org/licenses/>.
00018 */
00019 
00020 
00021 #include <iostream>
00022 #include <cstdlib>
00023 #include <fstream>
00024 #include <string>
00025 #include <boost/regex.hpp>
00026 #include <iomanip>
00027 
00028 #include <pelib/AmplInput.hpp>
00029 #include <pelib/AmplOutput.hpp>
00030 
00031 #include <pelib/AmplInputData.hpp>
00032 #include <pelib/AmplInputScalar.hpp>
00033 #include <pelib/AmplInputVector.hpp>
00034 #include <pelib/AmplInputMatrix.hpp>
00035 
00036 #include <pelib/AmplOutputScalar.hpp>
00037 #include <pelib/AmplOutputVector.hpp>
00038 #include <pelib/AmplOutputMatrix.hpp>
00039 
00040 using namespace pelib;
00041 
00042 Algebra
00043 parse(AlgebraParser &parser, std::istream &input)
00044 {
00045         Algebra rec;
00046         
00047         try {
00048                 rec = parser.parse(input);
00049         } catch(ParseException &e)
00050         {
00051                 std::cerr << e.what() << std::endl;
00052         }
00053 
00054         return rec;
00055 }
00056 
00057 int
00058 main(int argc, char **argv)
00059 {
00060         AmplInput input(AmplInput::floatHandlers());
00061         AmplOutput output(AmplOutput::floatHandlers());
00062         Algebra rec;
00063 
00064         // We use an AMPL output, output format
00065         AlgebraOutput &out = input;
00066 
00067         // Set floating point var output format to fixed at 7 digits
00068         std::cout << std::setprecision(6)                                                                                                        
00069         << std::setiosflags(std::ios::fixed)                                                                                                     
00070         << std::setiosflags(std::ios::showpoint);
00071 
00072         // Open input file
00073         std::ifstream myfile;
00074         myfile.open (argv[1], std::ios::in);
00075 
00076         // Load input file
00077         rec = parse(input, myfile);
00078 
00079         // Close input file
00080         myfile.close();
00081 
00082         out.dump(std::cout, rec);
00083 
00084         // Output one raw value in parameters scalar n, in vector Wi and float matrix e
00085         std::cout << rec.find<Scalar<float> >("n")->getValue() << std::endl;
00086         std::cout << rec.find<Vector<int, float> >("Wi")->getSize() << std::endl;
00087         std::cout << rec.find<Matrix<int, int, float> >("e")->getRowSize() << std::endl;
00088 
00089         // Extract, rename and output a few parameters
00090         Scalar<float> nn(rec.find<Scalar<float> >("n"));
00091         nn.setName("nn");
00092         out.dump(std::cout, &nn);
00093 
00094         Vector<int, float> yy(rec.find<Vector<int, float> >("Wi"));
00095         yy.setName("yy");
00096         out.dump(std::cout, &yy);
00097         
00098         Matrix<int, int, float> ee(rec.find<Matrix<int, int, float> >("e"));
00099         ee.setName("ee");
00100         out.dump(std::cout, &ee);
00101 
00102         return EXIT_SUCCESS;
00103 }
00104