User Manual, Developers Guide and API Documentation

OutputFormatter.cpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002  * This file is part of openWNS (open Wireless Network Simulator)
00003  * _____________________________________________________________________________
00004  *
00005  * Copyright (C) 2004-2007
00006  * Chair of Communication Networks (ComNets)
00007  * Kopernikusstr. 16, D-52074 Aachen, Germany
00008  * phone: ++49-241-80-27910,
00009  * fax: ++49-241-80-22242
00010  * email: info@openwns.org
00011  * www: http://www.openwns.org
00012  * _____________________________________________________________________________
00013  *
00014  * openWNS is free software; you can redistribute it and/or modify it under the
00015  * terms of the GNU Lesser General Public License version 2 as published by the
00016  * Free Software Foundation;
00017  *
00018  * openWNS is distributed in the hope that it will be useful, but WITHOUT ANY
00019  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
00020  * A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
00021  * details.
00022  *
00023  * You should have received a copy of the GNU Lesser General Public License
00024  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00025  *
00026  ******************************************************************************/
00027 
00028 #include <WNS/probe/bus/detail/OutputFormatter.hpp>
00029 
00030 using namespace wns::probe::bus::detail;
00031 
00032 STATIC_FACTORY_REGISTER_WITH_CREATOR( HumanReadable, OutputFormatter, 
00033         "HumanReadable", StatEvalTableCreator );
00034 STATIC_FACTORY_REGISTER_WITH_CREATOR( PythonReadable, OutputFormatter, 
00035         "PythonReadable", StatEvalTableCreator );
00036 STATIC_FACTORY_REGISTER_WITH_CREATOR( MatlabReadable, OutputFormatter, 
00037         "MatlabReadable", StatEvalTableCreator );
00038 STATIC_FACTORY_REGISTER_WITH_CREATOR( MatlabReadableSparse, OutputFormatter, 
00039         "MatlabReadableSparse", StatEvalTableCreator );
00040 STATIC_FACTORY_REGISTER_WITH_CREATOR( LogEvalReadable, OutputFormatter, 
00041         "LogEvalReadable", StatEvalTableCreator );
00042 STATIC_FACTORY_REGISTER_WITH_CREATOR( Plain, OutputFormatter, 
00043         "Plain", StatEvalTableCreator );
00044 
00045 void
00046 OutputFormatter::print(std::ostream& strm,
00047                        std::string valueType) const
00048 {
00049     int dim = sorters().size();
00050     std::list<int> empty;
00051     this->doPrint(strm, empty, dim, valueType);
00052 }
00053 
00054 
00055 void
00056 HumanReadable::doPrint(std::ostream& strm,
00057                        std::list<int> fixedIndices,
00058                        int dim,
00059                        std::string valueType) const
00060 {
00061     size_t level = fixedIndices.size();
00062     assure(dim + level == sorters().size(), "dim/level mismatch");
00063 
00064     if (dim == 0)
00065     {
00066         // End of recursion, print the actual value
00067         strm << data.getByIndex(fixedIndices).get(valueType);
00068     }
00069     else if (dim == 1)
00070     {
00071         // Print the data for this line
00072         // Add last index
00073         fixedIndices.push_back(0);
00074         for (int ii = 0; ii< sorters().at(level).getResolution(); ++ii)
00075         {
00076             fixedIndices.back() = ii;
00077             strm << "\t";
00078             doPrint(strm, fixedIndices, dim-1, valueType);
00079             strm << "\t|";
00080         }
00081         return;
00082     }
00083     else if (dim == 2)
00084     {
00085         // Print the table headings. Note that the intervals are defined by the
00086         // next level
00087         strm << "\t|";
00088         for (int ii = 0; ii< sorters().at(level+1).getResolution(); ++ii)
00089         {
00090             strm << "\t" << sorters().at(level+1).getInterval(ii) << "\t|";
00091         }
00092         strm << "\n";
00093         // Add next index
00094         fixedIndices.push_back(0);
00095         for (int ii = 0; ii< sorters().at(level).getResolution(); ++ii)
00096         {
00097             fixedIndices.back() = ii;
00098             // Precede each line with the interval it represents
00099             strm << "\n" << sorters().at(level).getInterval(ii) << "\t|";
00100             doPrint(strm, fixedIndices, dim-1, valueType);
00101         }
00102         return;
00103     }
00104     else
00105     {
00106         // Print the dimension headings
00107         // Add next index
00108         fixedIndices.push_back(0);
00109         for (int ii = 0; ii< sorters().at(level).getResolution(); ++ii)
00110         {
00111             strm << "\n\nDimension: " << dim
00112                  << ", index: " << ii
00113                  << ", " << sorters().at(level).getInterval(ii) << "\n";
00114             fixedIndices.back() = ii;
00115             doPrint(strm, fixedIndices, dim-1, valueType);
00116         }
00117         return;
00118     }
00119 }
00120 
00121 
00122 void
00123 PythonReadable::print(std::ostream& strm,
00124                       std::string valueType) const
00125 {
00126     strm << "returnValue = ";
00127     OutputFormatter::print(strm, valueType);
00128     // "deletes" the last 2 chars to get rid of trailing commas
00129     strm.seekp(-2, std::ios_base::cur);
00130     strm << "\n";
00131 }
00132 
00133 
00134 void
00135 PythonReadable::doPrint(std::ostream& strm,
00136                         std::list<int> fixedIndices,
00137                         int dim,
00138                         std::string valueType) const
00139 {
00140     size_t level = fixedIndices.size();
00141     assure(dim + level == sorters().size(), "dim/level mismatch");
00142 
00143     if (dim == 0)
00144     {
00145         // End of recursion, print the actual value
00146         strm << data.getByIndex(fixedIndices).get(valueType);
00147     }
00148     else if (dim == 1)
00149     {
00150         // Print the data for this line
00151         // Add last index
00152         fixedIndices.push_back(0);
00153         strm << "[ ";
00154         for (int ii = 0; ii< sorters().at(level).getResolution(); ++ii)
00155         {
00156             fixedIndices.back() = ii;
00157             doPrint(strm, fixedIndices, dim-1, valueType);
00158             strm << ", ";
00159         }
00160         // "deletes" the last 2 chars to get rid of trailing commas
00161         strm.seekp(-2, std::ios_base::cur);
00162         strm << "],\n";
00163         return;
00164     }
00165     else if (dim == 2)
00166     {
00167         // Print the table headings. Note that the intervals are defined by the
00168         // next level
00169         strm << getPrefix();
00170         for (int ii = 0; ii< sorters().at(level+1).getResolution(); ++ii)
00171         {
00172             strm << sorters().at(level+1).getInterval(ii) << "  ";
00173         }
00174         strm << "\n";
00175         strm << "[\n";
00176         // Add next index
00177         fixedIndices.push_back(0);
00178         for (int ii = 0; ii< sorters().at(level).getResolution(); ++ii)
00179         {
00180             fixedIndices.back() = ii;
00181             // Precede each line with the interval it represents
00182             strm << getPrefix() << sorters().at(level).getInterval(ii) << ":\n";
00183             doPrint(strm, fixedIndices, dim-1, valueType);
00184         }
00185         // "deletes" the last 2 chars to get rid of trailing commas
00186         strm.seekp(-2, std::ios_base::cur);
00187         strm << "\n],\n";
00188         return;
00189     }
00190     else
00191     {
00192         // Print the dimension headings
00193         // Add next index
00194         fixedIndices.push_back(0);
00195         strm << "[\n";
00196         for (int ii = 0; ii< sorters().at(level).getResolution(); ++ii)
00197         {
00198             strm << "\n\n" << getPrefix() << "Dimension: " << dim
00199                  << ", index: " << ii
00200                  << ", " << sorters().at(level).getInterval(ii) << "\n";
00201             fixedIndices.back() = ii;
00202             doPrint(strm, fixedIndices, dim-1, valueType);
00203         }
00204         // "deletes" the last 2 chars to get rid of trailing commas
00205         strm.seekp(-2, std::ios_base::cur);
00206         strm << "\n],\n";
00207         return;
00208     }
00209 }
00210 
00211 
00212 void
00213 MatlabReadable::print(std::ostream& strm,
00214                       std::string valueType) const
00215 {
00216     strm << getPrefix() << "\n";
00217     size_t ii = 0;
00218     for (; ii<sorters().size(); ++ii)
00219     {
00220         strm << getPrefix() << ii+1 << ". Column: " << sorters().at(ii).getIdName() << "\n";
00221     }
00222     strm << getPrefix() << ii+1 << ". Column: value\n\n";
00223     OutputFormatter::print(strm, valueType);
00224 }
00225 
00226 
00227 void
00228 MatlabReadable::doPrint(std::ostream& strm,
00229                         std::list<int> fixedIndices,
00230                         int dim,
00231                         std::string valueType) const
00232 {
00233     size_t level = fixedIndices.size();
00234     assure(dim + level == sorters().size(), "dim/level mismatch");
00235 
00236     if (dim == 0)
00237     {
00238         // Print this entry with all its indices
00239         int counter = 0;
00240         std::list<int>::const_iterator index = fixedIndices.begin();
00241         std::list<int>::const_iterator end   = fixedIndices.end();
00242         for (; index != end; ++index)
00243         {
00244             strm << sorters().at(counter).getMin(*index) << "\t";
00245             ++counter;
00246         }
00247         strm << data.getByIndex(fixedIndices).get(valueType) << "\n";
00248         return;
00249     }
00250     else
00251     {
00252         // Add last index
00253         fixedIndices.push_back(0);
00254         for (int ii = 0; ii< sorters().at(level).getResolution(); ++ii)
00255         {
00256             fixedIndices.back() = ii;
00257             doPrint(strm, fixedIndices, dim-1, valueType);
00258         }
00259         return;
00260     }
00261 }
00262 
00263 void
00264 MatlabReadableSparse::doPrint(std::ostream& strm,
00265                               std::list<int> fixedIndices,
00266                               int dim,
00267                               std::string valueType) const
00268 {
00269     size_t level = fixedIndices.size();
00270     assure(dim + level == sorters().size(), "dim/level mismatch");
00271 
00272     if (dim == 0)
00273     {
00274         if(data.getByIndex(fixedIndices).get(valueType) != 0)
00275         {
00276             // Print this entry with all its indices
00277             int counter = 0;
00278             std::list<int>::const_iterator index = fixedIndices.begin();
00279             std::list<int>::const_iterator end   = fixedIndices.end();
00280             for (; index != end; ++index)
00281             {
00282                 strm << sorters().at(counter).getMin(*index) << "\t";
00283                 ++counter;
00284             }
00285             strm << data.getByIndex(fixedIndices).get(valueType) << "\n";
00286         }
00287         return;
00288     }
00289     else
00290     {
00291         // Add last index
00292         fixedIndices.push_back(0);
00293         for (int ii = 0; ii< sorters().at(level).getResolution(); ++ii)
00294         {
00295             fixedIndices.back() = ii;
00296             doPrint(strm, fixedIndices, dim-1, valueType);
00297         }
00298         return;
00299     }
00300 }
00301 
00302 void
00303 LogEvalReadable::doPrint(std::ostream& strm,
00304                               std::list<int> fixedIndices,
00305                               int dim,
00306                               std::string valueType) const
00307 {
00308     size_t level = fixedIndices.size();
00309     assure(dim + level == sorters().size(), "dim/level mismatch");
00310 
00311     if (dim == 0)
00312     {
00313         // Print this entry with all its indices
00314         int counter = 0;
00315         std::list<int>::const_iterator index = fixedIndices.begin();
00316         std::list<int>::const_iterator end   = fixedIndices.end();
00317         for (; index != end; ++index)
00318         {
00319             strm << sorters().at(counter).getMin(*index) << "\t";
00320             ++counter;
00321         }
00322         strm << data.getByIndex(fixedIndices).get(valueType) << "\n";
00323         return;
00324     }
00325     else
00326     {
00327         // Add last index
00328         fixedIndices.push_back(0);
00329         for (int ii = 0; ii< sorters().at(level).getResolution(); ++ii)
00330         {
00331             fixedIndices.back() = ii;
00332             doPrint(strm, fixedIndices, dim-1, valueType);
00333         }
00334         return;
00335     }
00336 }

Generated on Fri May 25 03:31:49 2012 for openWNS by  doxygen 1.5.5