aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
netReader_tpl.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 #ifndef DOXYGEN_SHOULD_SKIP_THIS
23 
24 # include <agrum/BN/io/net/netReader.h>
25 
26 namespace gum {
27 
28  template < typename GUM_SCALAR >
29  NetReader< GUM_SCALAR >::NetReader(BayesNet< GUM_SCALAR >* bn,
30  const std::string& filename) :
33  bn__ = bn;
35  parseDone__ = false;
36 
38 
39  ioerror__ = false;
40 
41  try {
43  parser__ = new net::Parser(scanner__);
45  } catch (IOError&) { ioerror__ = true; }
46  }
47 
48  template < typename GUM_SCALAR >
51 
52  if (!ioerror__) {
53  // this could lead to memory leak !!
54  if (parser__) delete (parser__);
55 
56  if (scanner__) delete (scanner__);
57  }
58 
59  if (factory__) delete (factory__);
60  }
61 
62  template < typename GUM_SCALAR >
64  if (ioerror__) { GUM_ERROR(gum::IOError, "No such file " + streamName()); }
65 
66  return *scanner__;
67  }
68 
69  template < typename GUM_SCALAR >
70  INLINE const std::string& NetReader< GUM_SCALAR >::streamName() const {
71  return streamName__;
72  }
73 
74  template < typename GUM_SCALAR >
75  INLINE bool NetReader< GUM_SCALAR >::trace() const {
76  return traceScanning__;
77  }
78 
79  template < typename GUM_SCALAR >
80  INLINE void NetReader< GUM_SCALAR >::trace(bool b) {
82  scanner().setTrace(b);
83  }
84 
85  template < typename GUM_SCALAR >
87  if (ioerror__) { GUM_ERROR(gum::IOError, "No such file " + streamName()); }
88 
89  if (!parseDone__) {
90  try {
91  parser__->Parse();
92  } catch (gum::Exception& e) {
94  return 1 + parser__->errors().error_count;
95  }
96 
97  parseDone__ = true;
98  }
99 
100  return (parser__->errors().error_count);
101  }
102 
103  // @{
104  // publishing Errors API
105  template < typename GUM_SCALAR >
107  if (parseDone__)
108  return parser__->errors().error(i).line;
109  else {
110  GUM_ERROR(OperationNotAllowed, "Net file not parsed yet");
111  }
112  }
113 
114  template < typename GUM_SCALAR >
116  if (parseDone__)
117  return parser__->errors().error(i).column;
118  else {
119  GUM_ERROR(OperationNotAllowed, "Net file not parsed yet");
120  }
121  }
122 
123  template < typename GUM_SCALAR >
125  if (parseDone__)
126  return parser__->errors().error(i).is_error;
127  else {
128  GUM_ERROR(OperationNotAllowed, "Net file not parsed yet");
129  }
130  }
131 
132  template < typename GUM_SCALAR >
134  if (parseDone__)
135  return parser__->errors().error(i).msg;
136  else {
137  GUM_ERROR(OperationNotAllowed, "Net file not parsed yet");
138  }
139  }
140 
141  template < typename GUM_SCALAR >
143  if (parseDone__)
145  else {
146  GUM_ERROR(OperationNotAllowed, "Net file not parsed yet");
147  }
148  }
149 
150  template < typename GUM_SCALAR >
151  INLINE void
153  if (parseDone__)
155  else {
156  GUM_ERROR(OperationNotAllowed, "Net file not parsed yet");
157  }
158  }
159 
160  template < typename GUM_SCALAR >
162  if (parseDone__)
164  else {
165  GUM_ERROR(OperationNotAllowed, "Net file not parsed yet");
166  }
167  }
168 
169  template < typename GUM_SCALAR >
171  if (parseDone__)
173  else {
174  GUM_ERROR(OperationNotAllowed, "Net file not parsed yet");
175  }
176  }
177 
178  template < typename GUM_SCALAR >
180  return (!parseDone__) ? (Size)0 : parser__->errors().error_count;
181  }
182 
183  template < typename GUM_SCALAR >
185  return (!parseDone__) ? (Size)0 : parser__->errors().warning_count;
186  }
187 
188  // @}
189 } // namespace gum
190 
191 #endif // DOXYGEN_SHOULD_SKIP_THIS
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669