1  #ifndef CONSOLE_H_
2  #define CONSOLE_H_
3 
4  #include <sarray/SArray.h>
5 
6  #include <vector>
7  #include <iostream>
8  #include <string>
9  #include <map>
10  #include <cstdio>
11  #include <list>
12 
13  namespace jags {
14 
15  class BUGSModel;
16  class ParseTree;
17  struct RNG;
18  class Module;
19 
20  /**
21   * @short Flags for the function Console#dumpState
22   */
23  enum DumpType {DUMP_DATA, DUMP_PARAMETERS, DUMP_ALL};
24  /**
25   * @short Enumerates factory types in a model
26   */
27  enum FactoryType {SAMPLER_FACTORY, MONITOR_FACTORY, RNG_FACTORY};
28 
29  /**
30   * @short Interface to the JAGS library
31   */
32  class Console
33  {
34    std::ostream &_out;
35    std::ostream &_err;
36    BUGSModel *_model;
37    ParseTree *_pdata;
38    ParseTree *_prelations;
39    std::vector<ParseTree*> *_pvariables;
40    std::vector<std::string> _array_names;
41    static unsigned int &rngSeed();
42  public:
43    /**
44     * Constructor
45     *
46     * @param out Output stream to which information messages will be printed.
47     *
48     * @param err Output stream to which error messages will be printed.
49     *
50     */
51    Console(std::ostream &out, std::ostream &err);
52    ~Console();
53    /**
54     * Checks syntactic correctness of model
55     *
56     * @param file containing BUGS-language description of the model
57     *
58     * @return true on success or false on error.
59     */
60    bool checkModel(std::FILE *file);
61    /**
62     * Compiles the model.
63     *
64     * @param data_table Map relating the names of the observed variables
65     * to their values.
66     *
67     * @param nchain Number of chains in the model.
68     *
69     * @param gendata Boolean flag indicating whether the data generation
70     * sub-model should be run, if there is one.
71     *
72     * @return true on success or false on error.
73     */
74    bool compile(std::map<std::string, SArray> &data_table, unsigned int nchain,
75 		bool gendata);
76    /**
77     * @short Sets the parameters (unobserved variables) of the model.
78     *
79     * This is normally done to supply initial values to the model but
80     * may also be done at any point in the chain.
81     *
82     * @param param_table Map relating the names of the parameters to
83     * their values
84     *
85     * @param chain Number of chain (starting from 1) to apply parameter
86     * values to
87     *
88     * @return true on success, false on failure.
89     */
90    bool setParameters(std::map<std::string, SArray> const &param_table,
91 		      unsigned int chain);
92    /**
93     * Sets the name of the RNG for the given chain. The Console searches
94     * through all loaded RNGFactories to find one that will generate an
95     * RNG object with the given name.
96     *
97     * @return true on success, false on failure.
98     */
99    bool setRNGname(std::string const &name, unsigned int chain);
100    /**
101     * @short Initializes the model.
102     *
103     * Any uninitialized parameters are given values by deterministic forward
104     * sampling. Then default RNGs are chosen for all chains that have not
105     * already had their RNG set, based on the list of RNGFactory objects.
106     * Finally, the samplers are chosen based for the unobserved
107     * stochastic nodes based on the list of sampler factories.
108     *
109     * @returns true on success, false on failure.
110     *
111     * @see Model#samplerFactories, Model#rngFactories
112     */
113    bool initialize();
114    /**
115     * @short Updates the Markov chain generated by the model.
116     *
117     * @param n Number of iterations of the Markov chain.
118     *
119     * @returns true on success, false on failure.
120     */
121    bool update (unsigned int n);
122    /**
123     * Sets a monitor for a subset of the given node array
124     *
125     * @param name Name of array containing nodes to be monitored
126     *
127     * @param range Range describing subset of named array to monitor.
128     * A NULL range may be given, in which case, the whole array is
129     * monitored.
130     *
131     * @param thin Thinning interval for the monitor
132     *
133     * @param type Name of the monitor type.
134     *
135     */
136    bool setMonitor(std::string const &name, Range const &range,
137 		   unsigned int thin, std::string const &type);
138    /**
139     * @short Clears a monitor.
140     *
141     * The arguments name, range and type must correspond exactly to
142     * a previous call to setMonitor.
143     */
144    bool clearMonitor(std::string const &name, Range const &range,
145 		     std::string const &type);
146    /**
147     * @short Dumps the state of the model.
148     *
149     * Writes the current values of the variables to the data table.
150     *
151     * @param data_table Data table to receive values. This must be
152     * initially empty.
153     *
154     * @param rng_name String which will be overwritten with the name
155     * of the RNG for this chain.
156     *
157     * @param type Flag describing which values in the model to dump.
158     * DUMP_DATA dumps the observed stochastic nodes, DUMP_PARAMETERS
159     * dumps the unobserved stochastic nodes, and DUMP_ALL, dumps the values
160     * of all named nodes in the model.
161     *
162     * @param chain Number of the chain for which to dump values (starting
163     * from 1).
164     */
165    bool dumpState(std::map<std::string,SArray> &data_table,
166 		  std::string &rng_name,
167 		  DumpType type, unsigned int chain);
168    /**
169     * Returns the iteration number of the model.
170     */
171    unsigned int iter() const;
172    /**
173     * Returns a vector of variable names used by the model. This vector
174     * excludes any counters used by the model within a for loop.
175     */
176    std::vector<std::string> const &variableNames() const;
177    /**
178     * Dump the contants of monitored node in CODA format
179     *
180     * @param node Vector of monitored nodes to be dumped, each node
181     * is described by the variable name and index range. If the vector
182     * is empty then ALL monitored nodes will be dumped.
183     *
184     * @param prefix Prefix to be prepended to the output file names
185     */
186    bool coda(std::vector<std::pair<std::string, Range> > const &nodes,
187 	     std::string const &prefix);
188    bool coda(std::string const &prefix);
189    BUGSModel const *model();
190    unsigned int nchain() const;
191    bool dumpMonitors(std::map<std::string,SArray> &data_table,
192 		     std::string const &type, bool flat);
193    bool dumpSamplers(std::vector<std::vector<std::string> > &sampler_list);
194    /** Turns off adaptive mode of the model */
195    bool adaptOff();
196    /** Checks whether adaptation is complete */
197    bool checkAdaptation(bool &status);
198    /** Indicates whether model is in adaptive mode */
199    bool isAdapting() const;
200    /** Clears the model */
201    void clearModel();
202    /**
203     * Loads a module by name
204     */
205    static bool loadModule(std::string const &name);
206    /**
207     * Unloads a module by name
208     */
209    static bool unloadModule(std::string const &name);
210    /**
211     * Returns a vector containing the names of loaded modules
212     */
213    static std::vector<std::string> listModules();
214    /**
215     * Returns a vector containing the names of currently loaded factories
216     * and whether or not they are active.
217     */
218    static std::vector<std::pair<std::string, bool> >
219        listFactories(FactoryType type);
220    /**
221     * Sets a factory to be active or inactive
222     */
223    static bool setFactoryActive(std::string const &name, FactoryType type,
224 				bool active);
225    /**
226     * Sets the seed for all RNG factories.
227     *
228     * Sets the seed for all currently loaded RNGFactory objects,
229     * including the ones that are currently active. After a call to
230     * setSeed, all RNGFactory objects in subsequently loaded Modules
231     * will have their seeds set to the given value.
232     *
233     * @param seed Unsigned integer giving a random seed. The seed must
234     * be positive: a zero seed will have no effect.
235     *
236     * @see RNGFactory#setSeed
237     */
238    static void setRNGSeed(unsigned int seed);
239 };
240 
241 } /* namespace jags */
242 
243 #endif /* CONSOLE_H_ */
244