aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
O3prmReader.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 /**
23  * @file
24  * @brief Headers for the O3prmReader class.
25  *
26  * @author Christophe GONZALES(@AMU) and Pierre-Henri WUILLEMIN(@LIP6)
27  * @author Lionel TORTI
28  */
29 
30 #ifndef GUM_PRM_O3PRM_O3PRM_FACTORY_H
31 #define GUM_PRM_O3PRM_O3PRM_FACTORY_H
32 
33 #include <cstdint>
34 #include <fstream>
35 #include <iostream>
36 #include <sstream>
37 #include <string>
38 #include <vector>
39 
40 #include <agrum/PRM/PRM.h>
41 #include <agrum/PRM/o3prm/O3ClassFactory.h>
42 #include <agrum/PRM/o3prm/O3InterfaceFactory.h>
43 #include <agrum/PRM/o3prm/O3NameSolver.h>
44 #include <agrum/PRM/o3prm/O3SystemFactory.h>
45 #include <agrum/PRM/o3prm/O3TypeFactory.h>
46 #include <agrum/PRM/o3prm/O3prm.h>
47 #include <agrum/PRM/o3prm/cocoR/Parser.h>
48 #include <agrum/PRM/o3prm/cocoR/Scanner.h>
49 #include <agrum/agrum.h>
50 #include <agrum/tools/core/errorsContainer.h>
51 #include <agrum/tools/core/utils_string.h>
52 
53 namespace gum {
54  namespace prm {
55  namespace o3prm {
56 
57  /**
58  * @class O3prmReader
59  * @headerfile O3prmReader.h <agrum/PRM/o3prm/O3prmReader.h>
60  * @ingroup o3prm_group
61  *
62  * @brief This class read O3PRM files and creates the corresponding
63  * gum::prm::PRM.
64  *
65  * @tparam GUM_SCALAR The scalar type to use with the gum::prm::PRM.
66  */
67  template < typename GUM_SCALAR >
68  class O3prmReader {
69  public:
70  O3prmReader();
71  explicit O3prmReader(PRM< GUM_SCALAR >& prm);
72  O3prmReader(const O3prmReader& src);
73  O3prmReader(O3prmReader&& src);
74  ~O3prmReader();
75  O3prmReader& operator=(const O3prmReader& src);
76  O3prmReader& operator=(O3prmReader&& src);
77 
78  /// Read file and load its content using a PRMFactory.
79  /// The package parameter set the file's content package.
80  Size readFile(const std::string& file, const std::string& module = "");
81 
82  /// With readString method, you must set the current path
83  /// to search from import yourself, using addClassPath.
84  Size readString(const std::string& string);
85 
86  void parseStream(std::istream& input, std::ostream& output, std::string module = "");
87  /**
88  * @brief This methods defines the list of paths to look for o3prm
89  * files.
90  *
91  * Use / for path separator ! Even on Windows !
92  *
93  * @param class_path A semicolon separated list of paths.
94  */
95  void setClassPath(const std::string& class_path);
96 
97  /**
98  * @brief Add a list of paths to look for o3prm
99  * files.
100  *
101  * Use / for path separator ! Even on Windows !
102  *
103  * @param class_path A semicolon separated list of paths.
104  */
105  void addClassPath(const std::string& class_path);
106 
107  gum::prm::PRM< GUM_SCALAR >* prm() { return _prm_; }
108  const gum::prm::PRM< GUM_SCALAR >* prm() const { return _prm_; }
109 
110  /// @{
111  /// publishing Errors API
112 
113  /// # of errors
114  Size errors() const;
115  /// # of errors
116  Size warnings() const;
117 
118  ///
119  const ErrorsContainer& errorsContainer() const;
120 
121  /// line of ith error or warning
122  Idx errLine(Idx i) const;
123  /// col of ith error or warning
124  Idx errCol(Idx i) const;
125  /// filename of ith error or warning
126  std::wstring errFilename(Idx i) const;
127  /// type of ith error or warning
128  bool errIsError(Idx i) const;
129  /// message of ith error or warning
130  std::string errMsg(Idx i) const;
131 
132  /// send on std::cerr the list of errors
133  void showElegantErrors(std::ostream& o = std::cerr) const;
134 
135  /// send on std::cerr the list of errors or warnings
136  void showElegantErrorsAndWarnings(std::ostream& o = std::cerr) const;
137 
138  /// send on std::cerr the number of errors and the number of warnings
139  void showErrorCounts(std::ostream& o = std::cerr) const;
140  /// @}
141 
142  private:
143  PRM< GUM_SCALAR >* _prm_;
144  std::unique_ptr< O3PRM > _o3_prm_;
145  std::vector< std::string > _class_path_;
146  Set< std::string > _imported_;
147 
148  // Needed when file can't be parse (can not open it for exemple)
149  ErrorsContainer _errors_;
150 
151  void _readStream_(std::istream& input, const std::string& file, std::string module = "");
152 
153  void _parseImport_(const O3Import& i, const std::string& module_path);
154 
155  void _parseStream_(std::istream& input,
156  const std::string& filename,
157  const std::string& module);
158 
159  std::vector< const O3Import* > _copyImports_();
160 
161  std::string _clean_(std::string text) const;
162  std::string _print_(const ParseError& err) const;
163  std::string _readStream_(std::istream& input);
164  };
165 
166  } // namespace o3prm
167  } // namespace prm
168 } // namespace gum
169 
170 // always include the implementation of the templates
171 #include <agrum/PRM/o3prm/O3prmReader_tpl.h>
172 
173 
174 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
175 extern template class gum::prm::o3prm::O3prmReader< double >;
176 #endif
177 
178 
179 #endif // GUM_PRM_O3PRM_O3PRM_FACTORY_H