aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
O3prmReader.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright 2005-2020 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,
87  std::ostream& output,
88  std::string module = "");
89  /**
90  * @brief This methods defines the list of paths to look for o3prm
91  * files.
92  *
93  * Use / for path separator ! Even on Windows !
94  *
95  * @param class_path A semicolon separated list of paths.
96  */
97  void setClassPath(const std::string& class_path);
98 
99  /**
100  * @brief Add a list of paths to look for o3prm
101  * files.
102  *
103  * Use / for path separator ! Even on Windows !
104  *
105  * @param class_path A semicolon separated list of paths.
106  */
107  void addClassPath(const std::string& class_path);
108 
109  gum::prm::PRM< GUM_SCALAR >* prm() { return prm__; }
110  const gum::prm::PRM< GUM_SCALAR >* prm() const { return prm__; }
111 
112  /// @{
113  /// publishing Errors API
114 
115  /// # of errors
116  Size errors() const;
117  /// # of errors
118  Size warnings() const;
119 
120  ///
121  const ErrorsContainer& errorsContainer() const;
122 
123  /// line of ith error or warning
124  Idx errLine(Idx i) const;
125  /// col of ith error or warning
126  Idx errCol(Idx i) const;
127  /// filename of ith error or warning
128  std::wstring errFilename(Idx i) const;
129  /// type of ith error or warning
130  bool errIsError(Idx i) const;
131  /// message of ith error or warning
132  std::string errMsg(Idx i) const;
133 
134  /// send on std::cerr the list of errors
135  void showElegantErrors(std::ostream& o = std::cerr) const;
136 
137  /// send on std::cerr the list of errors or warnings
138  void showElegantErrorsAndWarnings(std::ostream& o = std::cerr) const;
139 
140  /// send on std::cerr the number of errors and the number of warnings
141  void showErrorCounts(std::ostream& o = std::cerr) const;
142  /// @}
143 
144  private:
145  PRM< GUM_SCALAR >* prm__;
146  std::unique_ptr< O3PRM > o3_prm__;
147  std::vector< std::string > class_path__;
148  Set< std::string > imported__;
149 
150  // Needed when file can't be parse (can not open it for exemple)
151  ErrorsContainer errors__;
152 
153  void readStream__(std::istream& input,
154  const std::string& file,
155  std::string module = "");
156 
157  void parseImport__(const O3Import& i, const std::string& module_path);
158 
159  void parseStream__(std::istream& input,
160  const std::string& filename,
161  const std::string& module);
162 
163  std::vector< const O3Import* > copyImports__();
164 
165  std::string clean__(std::string text) const;
166  std::string print__(const ParseError& err) const;
167  std::string readStream__(std::istream& input);
168  };
169 
170  } // namespace o3prm
171  } // namespace prm
172 } // namespace gum
173 
174 // always include the implementation of the templates
175 #include <agrum/PRM/o3prm/O3prmReader_tpl.h>
176 
177 
178 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
179 extern template class gum::prm::o3prm::O3prmReader< double >;
180 #endif
181 
182 
183 #endif // GUM_PRM_O3PRM_O3PRM_FACTORY_H