1 /*
2  * Copyright © 2006-2020 Dynare Team
3  *
4  * This file is part of Dynare.
5  *
6  * Dynare is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU 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  * Dynare 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 General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef _STATEMENT_HH
21 #define _STATEMENT_HH
22 
23 #include <ostream>
24 #include <string>
25 #include <map>
26 #include <set>
27 
28 #include "SymbolList.hh"
29 #include "WarningConsolidation.hh"
30 
31 class ModFileStructure
32 {
33 public:
34   ModFileStructure() = default;
35   //! Whether check is present
36   bool check_present{false};
37   //! Whether steady is present
38   bool steady_present{false};
39   //! Whether a perfect_foresight_solver/simul statement is present
40   bool perfect_foresight_solver_present{false};
41   //! Whether a stoch_simul statement is present
42   bool stoch_simul_present{false};
43   //! Whether an estimation statement is present
44   bool estimation_present{false};
45   //! Whether an osr statement is present
46   bool osr_present{false};
47   //! Whether an osr params statement is present
48   bool osr_params_present{false};
49   //! Whether an optim weight statement is present
50   bool optim_weights_present{false};
51   //! Whether a ramsey_model statement is present
52   bool ramsey_model_present{false};
53   //! Whether a ramsey_policy statement is present
54   bool ramsey_policy_present{false};
55   //! Whether a discretionary_objective statement is present
56   bool discretionary_policy_present{false};
57   //! Whether a planner_objective statement is present
58   bool planner_objective_present{false};
59   //! Whether an extended_path statement is present
60   bool extended_path_present{false};
61   //! The value of the "order" option of stoch_simul, estimation, osr, ramsey_policy
62   //! Derivation order
63   /*! First initialized to zero. If user sets order option somewhere in the MOD file, it will be equal to the maximum of order options. Otherwise will default to 2 */
64   int order_option{0};
65   //! Whether a bvar_density, bvar_forecast, sbvar, ms_sbvar statement is present
66   bool bvar_present{false};
67   //! Whether an svar_identification statement is present
68   bool svar_identification_present{false};
69   //! Whether an identification statement is present or the identification option of dynare_sensitivity statement is equal to one
70   bool identification_present{false};
71   //! The maximum of the “order” option in identification statements
72   int identification_order{0};
73   //! Whether a sensitivity statement is present
74   bool sensitivity_present{false};
75   //! Whether the option analytic_derivation is given to estimation
76   bool estimation_analytic_derivation{false};
77   //! Whether the option partial_information is given to stoch_simul/estimation/osr/ramsey_policy
78   bool partial_information{false};
79   //! Whether the "k_order_solver" option is used (explictly, or implicitly if order >= 3)
80   bool k_order_solver{false};
81   //! Whether there is a calibrated measurement error
82   bool calibrated_measurement_errors{false};
83   //! Whether dsge_prior_weight was initialized as a parameter
84   bool dsge_prior_weight_initialized;
85   //! Whether dsge_prior_weight is in the estimated_params block
86   bool dsge_prior_weight_in_estimated_params{false};
87   //! Whether there is a dsge_var, with calibrated prior weight
88   string dsge_var_calibrated;
89   //! Whether there is a dsge_var, with prior weight that must be estimated
90   bool dsge_var_estimated{false};
91   //! Whether there is a bayesian_irf option passed to the estimation statement
92   bool bayesian_irf_present{false};
93   //! Whether there is a data statement present
94   bool estimation_data_statement_present{false};
95   //! Last chain number for Markov Switching statement2
96   int last_markov_switching_chain{0};
97   //! Whether a calib_smoother statement is present
98   bool calib_smoother_present{false};
99   //! Whether there is an estimated_params_init with use_calibration
100   bool estim_params_use_calib{false};
101   //! Set of parameters used within shocks blocks, inside the expressions
102   //! defining the values of covariances (stored as symbol ids)
103   set<int> parameters_within_shocks_values;
104   //! Set of estimated parameters (stored as symbol ids)
105   set<int> estimated_parameters;
106   //! Whether there is a prior statement present
107   bool prior_statement_present{false};
108   //! Whether there is a std prior statement present
109   bool std_prior_statement_present{false};
110   //! Whether there is a corr prior statement present
111   bool corr_prior_statement_present{false};
112   //! Whether there is a options statement present
113   bool options_statement_present{false};
114   //! Whether there is a std options statement present
115   bool std_options_statement_present{false};
116   //! Whether there is a corr options statement present
117   bool corr_options_statement_present{false};
118   //! Whether a Markov Switching DSGE is present
119   bool ms_dsge_present{false};
120   //! Whether occbin is present
121   bool occbin_option{false};
122   //! Stores the original number of equations in the model_block
123   int orig_eq_nbr{0};
124   //! Stores the number of equations added to the Ramsey model
125   int ramsey_eq_nbr{0};
126   //! Whether there was a steady_state_model block
127   bool steady_state_model_present{false};
128   //! Whether there is a write_latex_steady_state_model statement present
129   bool write_latex_steady_state_model_present{false};
130   //! Pac growth and discount
131   set<int> pac_params;
132   //! Instruments if ramsey_model, ramsey_policy or discretionary_policy is present
133   SymbolList instruments;
134   /* Whether any of shock_decomposition, realtime_shock_decomposition and
135      initial_condition_decomposition has the “with_epilogue” option */
136   bool with_epilogue_option{false};
137   /* Lists symbol IDs of parameters that appear in a “planner_discount” option.
138      See dynare#1173 for more details. */
139   set<int> parameters_in_planner_discount;
140 };
141 
142 class Statement
143 {
144 public:
145   Statement() = default;
146   virtual ~Statement() = default;
147 
148   Statement(const Statement &) = delete;
149   Statement(Statement &&) = delete;
150   Statement &operator=(const Statement &) = delete;
151   Statement &operator=(Statement &&) = delete;
152 
153   //! Do some internal check, and fill the ModFileStructure class
154   /*! Don't forget to update ComputingTasks.hh, Shocks.hh and
155     NumericalInitialization.hh if you modify the signature of this
156     method. Otherwise the default implementation (i.e. a no-op) will apply and
157     some checks won't be run. */
158   virtual void checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings);
159   virtual void computingPass();
160   //! Write Matlab output code
161   /*!
162     \param output is the output stream of the main matlab file
163     \param basename is the name of the modfile (without extension) which can be used to build auxiliary files
164   */
165   virtual void writeOutput(ostream &output, const string &basename, bool minimal_workspace) const = 0;
166   virtual void writeJuliaOutput(ostream &output, const string &basename);
167   virtual void writeJsonOutput(ostream &output) const;
168 };
169 
170 class NativeStatement : public Statement
171 {
172 private:
173   const string native_statement;
174 public:
175   explicit NativeStatement(string native_statement_arg);
176   void writeOutput(ostream &output, const string &basename, bool minimal_workspace) const override;
177   void writeJsonOutput(ostream &output) const override;
178 };
179 
180 class VerbatimStatement : public Statement
181 {
182 private:
183   const string verbatim_statement;
184 public:
185   explicit VerbatimStatement(string verbatim_statement_arg);
186   void writeOutput(ostream &output, const string &basename, bool minimal_workspace) const override;
187   void writeJsonOutput(ostream &output) const override;
188 };
189 
190 class OptionsList
191 {
192 public:
193   using num_options_t = map<string, string>;
194   using paired_num_options_t = map<string, pair<string, string>>;
195   using string_options_t = map<string, string>;
196   using date_options_t = map<string, string>;
197   using symbol_list_options_t = map<string, SymbolList>;
198   using vec_int_options_t = map<string, vector<int>>;
199   using vec_str_options_t = map<string, vector<string >>;
200   num_options_t num_options;
201   paired_num_options_t paired_num_options;
202   string_options_t string_options;
203   date_options_t date_options;
204   symbol_list_options_t symbol_list_options;
205   vec_int_options_t vector_int_options;
206   vec_str_options_t vector_str_options;
207   int getNumberOfOptions() const;
208   void writeOutput(ostream &output) const;
209   void writeOutput(ostream &output, const string &option_group) const;
210   void writeJsonOutput(ostream &output) const;
211   void clear();
212 };
213 
214 #endif // ! _STATEMENT_HH
215