OpenMS  2.6.0
FuzzyStringComparator.h
Go to the documentation of this file.
1 // --------------------------------------------------------------------------
2 // OpenMS -- Open-Source Mass Spectrometry
3 // --------------------------------------------------------------------------
4 // Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
5 // ETH Zurich, and Freie Universitaet Berlin 2002-2020.
6 //
7 // This software is released under a three-clause BSD license:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of any author or any participating institution
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
16 // For a full list of authors, refer to the file AUTHORS.
17 // --------------------------------------------------------------------------
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
22 // INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // --------------------------------------------------------------------------
31 // $Maintainer: Timo Sachsenberg $
32 // $Authors: Clemens Groepl, Stephan Aiche $
33 // --------------------------------------------------------------------------
34 
35 #pragma once
36 
37 #include <OpenMS/CONCEPT/Types.h>
40 
41 
42 #include <map>
43 #include <sstream>
44 
45 namespace OpenMS
46 {
47  namespace Internal
48  {
49  namespace ClassTest
50  {
51  void OPENMS_DLLAPI testStringSimilar(const char * file,
52  int line,
53  const std::string & string_1,
54  const char * string_1_stringified,
55  const std::string & string_2,
56  const char * string_2_stringified);
57 
58  bool OPENMS_DLLAPI isFileSimilar(const std::string &,
59  const std::string &);
60  }
61  }
62 
66  class OPENMS_DLLAPI FuzzyStringComparator
67  {
68 
69  friend void OPENMS_DLLAPI
71  const char * file,
72  int line,
73  const std::string & string_1,
74  const char * string_1_stringified,
75  const std::string & string_2,
76  const char * string_2_stringified);
77 
78  friend bool OPENMS_DLLAPI
79  Internal::ClassTest::isFileSimilar(const std::string &,
80  const std::string &);
81 
84  {
85  };
86 
87 public:
88 
90 
91 
94 
96  virtual
98 
101 
103  FuzzyStringComparator & operator=(const FuzzyStringComparator & rhs);
104 
106 
108  const double & getAcceptableRelative() const;
109 
111  void setAcceptableRelative(const double rhs);
112 
114  const double & getAcceptableAbsolute() const;
115 
117  void setAcceptableAbsolute(const double rhs);
118 
120  const StringList & getWhitelist() const;
121 
123  StringList & getWhitelist();
124 
126  void setWhitelist(const StringList & rhs);
127 
129  void setMatchedWhitelist(const std::vector< std::pair<std::string, std::string> >& rhs);
130 
132  const std::vector< std::pair<std::string, std::string> >& getMatchedWhitelist() const;
133 
142  const int & getVerboseLevel() const;
143 
152  void setVerboseLevel(const int rhs);
153 
157  const int & getTabWidth() const;
158 
162  void setTabWidth(const int rhs);
163 
167  const int & getFirstColumn() const;
168 
172  void setFirstColumn(const int rhs);
173 
180  std::ostream & getLogDestination() const;
181 
192  void setLogDestination(std::ostream & rhs);
193 
201  bool compareStrings(std::string const & lhs, std::string const & rhs);
202 
211  bool compareStreams(std::istream & input_1, std::istream & input_2);
212 
227  bool compareFiles(const std::string & filename_1,
228  const std::string & filename_2);
229 
230 protected:
231 
240  bool compareLines_(std::string const & line_str_1, std::string const & line_str_2);
241 
243  void reportSuccess_() const;
244 
247  void reportFailure_(char const * const message) const;
248 
250  void writeWhitelistCases_(const std::string & prefix) const;
251 
254  void readNextLine_(std::istream & input_stream, std::string & line_string, int & line_number) const;
255 
257  bool openInputFileStream_(const std::string & filename, std::ifstream & input_stream) const;
258 
260  std::ostream * log_dest_;
261 
263  std::string input_1_name_;
265  std::string input_2_name_;
266 
268  struct InputLine
269  {
270  std::stringstream line_;
271  std::ios::pos_type line_position_;
272 
273  InputLine();
274 
276  void setToString(const std::string & s);
277 
279  void updatePosition();
280 
282  void seekGToSavedPosition();
283 
291  bool ok() const;
292  };
293 
296 
299 
302 
303  std::string line_str_1_max_;
304  std::string line_str_2_max_;
305 
308 
310  double ratio_max_;
311 
314 
316  double absdiff_max_;
317 
320  {
321  double number;
322  unsigned char letter;
323  bool is_number;
324  bool is_space;
325 
326  StreamElement_();
327 
329  void reset();
330 
334  void fillFromInputLine(InputLine& input_line, const std::string& str_line);
335  };
336 
341 
343  struct PrefixInfo_
344  {
348 
349  PrefixInfo_(const InputLine & input_line, const int tab_width_, const int first_column_);
350  };
351 
353 
357 
363 
366 
370  std::map<String, UInt> whitelist_cases_;
371 
373  std::vector< std::pair<std::string, std::string> > matched_whitelist_;
374  }; // class FuzzyStringComparator
375 
376 } //namespace OpenMS
377 
OpenMS::FuzzyStringComparator::StreamElement_
Stores information about characters, numbers, and white spaces loaded from the InputStream.
Definition: FuzzyStringComparator.h:319
OpenMS::TOPPBase
Base class for TOPP applications.
Definition: TOPPBase.h:144
OpenMS::FuzzyStringComparator::use_prefix_
bool use_prefix_
use a prefix when reporting
Definition: FuzzyStringComparator.h:365
OpenMS::Exception::IllegalArgument
A method or algorithm argument contains illegal values.
Definition: Exception.h:648
OpenMS::FuzzyStringComparator::StreamElement_::number
double number
Definition: FuzzyStringComparator.h:321
OpenMS::Internal::ClassTest::isFileSimilar
bool isFileSimilar(const std::string &filename_1, const std::string &filename_2)
Compare files using absdiff_max_allowed and ratio_max_allowed.
Types.h
OpenMS::FuzzyStringComparator::whitelist_
StringList whitelist_
Whitelist.
Definition: FuzzyStringComparator.h:368
OpenMS::FuzzyStringComparator::tab_width_
int tab_width_
Definition: FuzzyStringComparator.h:355
OpenMS::FuzzyStringComparator::first_column_
int first_column_
Definition: FuzzyStringComparator.h:356
OpenMS::FuzzyStringComparator::verbose_level_
int verbose_level_
Definition: FuzzyStringComparator.h:354
OpenMS::FuzzyStringComparator::input_line_2_
InputLine input_line_2_
Definition: FuzzyStringComparator.h:295
OpenMS::FuzzyStringComparator::line_num_2_
int line_num_2_
Definition: FuzzyStringComparator.h:298
OpenMS::String
A more convenient string class.
Definition: String.h:59
OpenMS::FuzzyStringComparator::ratio_max_
double ratio_max_
Maximum ratio of numbers observed so far, see ratio_max_allowed_.
Definition: FuzzyStringComparator.h:310
OpenMS::FuzzyStringComparator::line_num_1_
int line_num_1_
Definition: FuzzyStringComparator.h:297
OpenMS::FuzzyStringComparator::StreamElement_::is_number
bool is_number
Definition: FuzzyStringComparator.h:323
OpenMS::Size
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127
OpenMS::FuzzyStringComparator::whitelist_cases_
std::map< String, UInt > whitelist_cases_
Occurrences of whitelist entries.
Definition: FuzzyStringComparator.h:370
OpenMS::FuzzyStringComparator::input_1_name_
std::string input_1_name_
Name of first input e.g., filename.
Definition: FuzzyStringComparator.h:263
OpenMS::Internal::ClassTest::testStringSimilar
void testStringSimilar(const char *file, int line, const std::string &string_1, const char *string_1_stringified, const std::string &string_2, const char *string_2_stringified)
Compare strings using absdiff_max_allowed and ratio_max_allowed.
OpenMS::FuzzyStringComparator::is_status_success_
bool is_status_success_
Has comparison been successful so far? Note: this flag is changed in reportFailure_();.
Definition: FuzzyStringComparator.h:362
ListUtils.h
OpenMS::FuzzyStringComparator
Fuzzy comparison of strings, tolerates numeric differences.
Definition: FuzzyStringComparator.h:66
OpenMS::FuzzyStringComparator::line_num_2_max_
int line_num_2_max_
Definition: FuzzyStringComparator.h:301
OpenMS::FuzzyStringComparator::input_2_name_
std::string input_2_name_
Name of second input e.g., filename.
Definition: FuzzyStringComparator.h:265
OpenMS::FuzzyStringComparator::PrefixInfo_
Wrapper for the prefix information computed for the failure report.
Definition: FuzzyStringComparator.h:343
OpenMS::FuzzyStringComparator::AbortComparison
Internal exception class.
Definition: FuzzyStringComparator.h:83
OpenMS
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:46
OpenMS::FuzzyStringComparator::line_num_1_max_
int line_num_1_max_
Definition: FuzzyStringComparator.h:300
OpenMS::FuzzyStringComparator::InputLine::line_position_
std::ios::pos_type line_position_
Definition: FuzzyStringComparator.h:271
OpenMS::FuzzyStringComparator::line_str_1_max_
std::string line_str_1_max_
Definition: FuzzyStringComparator.h:303
OpenMS::Internal::ClassTest::setWhitelist
void setWhitelist(const char *const, const int line, const std::string &whitelist)
set the whitelist_
OpenMS::FuzzyStringComparator::absdiff_max_allowed_
double absdiff_max_allowed_
Maximum absolute difference of numbers allowed, see absdiff_max_.
Definition: FuzzyStringComparator.h:313
OpenMS::FuzzyStringComparator::PrefixInfo_::prefix
OpenMS::String prefix
Definition: FuzzyStringComparator.h:345
OpenMS::FuzzyStringComparator::element_2_
StreamElement_ element_2_
Stores information about characters, numbers, and white spaces loaded from the second input stream.
Definition: FuzzyStringComparator.h:340
OpenMS::FuzzyStringComparator::InputLine::line_
std::stringstream line_
Definition: FuzzyStringComparator.h:270
OpenMS::FuzzyStringComparator::StreamElement_::letter
unsigned char letter
Definition: FuzzyStringComparator.h:322
OpenMS::FuzzyStringComparator::ratio_max_allowed_
double ratio_max_allowed_
Maximum ratio of numbers allowed, see ratio_max_.
Definition: FuzzyStringComparator.h:307
OpenMS::FuzzyStringComparator::absdiff_max_
double absdiff_max_
Maximum difference of numbers observed so far, see absdiff_max_allowed_.
Definition: FuzzyStringComparator.h:316
OpenMS::StringList
std::vector< String > StringList
Vector of String.
Definition: ListUtils.h:70
main
int main(int argc, const char **argv)
Definition: INIFileEditor.cpp:73
OpenMS::FuzzyStringComparator::matched_whitelist_
std::vector< std::pair< std::string, std::string > > matched_whitelist_
Alternative Whitelist.
Definition: FuzzyStringComparator.h:373
OpenMS::FuzzyStringComparator::log_dest_
std::ostream * log_dest_
Log and results output goes here.
Definition: FuzzyStringComparator.h:260
FuzzyStringComparator.h
String.h
OpenMS::FuzzyStringComparator::PrefixInfo_::line_column
int line_column
Definition: FuzzyStringComparator.h:347
OpenMS::FuzzyStringComparator::input_line_1_
InputLine input_line_1_
Definition: FuzzyStringComparator.h:294
OpenMS::FuzzyStringComparator::StreamElement_::is_space
bool is_space
Definition: FuzzyStringComparator.h:324
OpenMS::FuzzyStringComparator::PrefixInfo_::prefix_whitespaces
OpenMS::String prefix_whitespaces
Definition: FuzzyStringComparator.h:346
OpenMS::FuzzyStringComparator::element_1_
StreamElement_ element_1_
Stores information about characters, numbers, and white spaces loaded from the first input stream.
Definition: FuzzyStringComparator.h:338
OpenMS::FuzzyStringComparator::InputLine
Stores information about the current input line (i.e., stream for the line and the current position i...
Definition: FuzzyStringComparator.h:268
TOPPBase.h
OpenMS::FuzzyStringComparator::line_str_2_max_
std::string line_str_2_max_
Definition: FuzzyStringComparator.h:304
OpenMS::FuzzyStringComparator::is_absdiff_small_
bool is_absdiff_small_
Definition: FuzzyStringComparator.h:352