1 #ifndef SYMTAB_H_
2 #define SYMTAB_H_
3 
4 #include <model/NodeArray.h>
5 #include <sarray/SArray.h>
6 
7 #include <string>
8 #include <vector>
9 #include <map>
10 
11 namespace jags {
12 
13 /**
14  * @short Associates a NodeArray object with a BUGS-language name
15  *
16  * The SymTab class stores the names of variables used in the BUGS
17  * language representation of the model.
18  *
19  * @see NodeArray
20  */
21 class SymTab
22 {
23   Model *_model;
24   std::map<std::string, NodeArray*> _varTable;
25   std::map<Node const*, std::string> _names;
26 public:
27   /**
28    * Constructs an empty symbol table
29    *
30    * @param model Model to which newly allocated nodes are added.
31    *
32    */
33   SymTab(Model *model);
34   ~SymTab();
35   /**
36    * Adds an array variable to the symbol table. This creates a
37    * NodeArray object of the given dimension and associates it
38    * with the name, so it can be retrieved with a call to getVariable.
39    * If no dimension is given, the variable is assumed to be scalar.
40    */
41   void addVariable(std::string const &name, std::vector<unsigned int> const &dim);
42   /**
43    * Returns a pointer to the  NodeArray associated with the given
44    * name, or a NULL pointer if there is no such NodeArray.
45    */
46   NodeArray *getVariable(std::string const &name) const;
47   /**
48    * Inserts a node into the symbol table with the given name
49    * (which must correspond to a previously added variable)
50    * and range (which must be a valid sub-range for the variable).
51    */
52   void insertNode(Node *node, std::string const &name, Range const &range);
53   /**
54    * Creates constant nodes in all the arrays of the symbol table
55    * taking values from the given data table.
56    *
57    * @param data_table Data table from which values will be read.
58    *
59    * @see NodeArray#setData
60    */
61   void writeData(std::map<std::string, SArray> const &data_table);
62   /**
63    * Write values from the data table to the arrays in the symbol
64    * table with the same name. Unlike the writeData function, the
65    * Nodes are not permanently set to the supplied values, and values
66    * are only written to the given chain.
67    *
68    * @param data_table Data table from which values will be read
69    *
70    * @param chain Index number of chain to which values are written.
71    *
72    * @see NodeArray#setValue
73    */
74   void writeValues(std::map<std::string, SArray> const &data_table,
75 		   unsigned int chain);
76   /**
77    * Reads the current value of selected nodes in the symbol table and
78    * writes the result to the data table. The selection is based on
79    * a given boolean function that returns true for the selected nodes.
80    *
81    * @param data_table Data table to which results will be written.
82    * New entries will be created for the selected nodes.  However, a
83    * new entry is not created if, in the symbol table, all nodes
84    * corresponding to the selection are missing. Existing entries in
85    * the data table will be overwritten.
86    *
87    * @param condition Function that returns true if the values of a
88    * Node are to be read, and false otherwise.
89    */
90   void readValues(std::map<std::string, SArray> &data_table,
91                   unsigned int chain,
92 		  bool (*condition)(Node const *)) const;
93   /**
94    * Returns the number of variables in the symbol table
95    */
96   unsigned int size() const;
97   /**
98    * Deletes all the variables in the symbol table
99    */
100   void clear();
101   /**
102    * Gets the BUGS language name of the node if it belongs to
103    * any of the NodeArrays in the symbol table. Special rules for nested
104    * indexing also allow the names of Mixture Nodes to be calculated.
105    * If the node name is not found, an empty string is returned
106    */
107   std::string getName(Node const *node) const;
108 };
109 
110 } /* namespace jags */
111 
112 #endif /* SYMTAB_H_ */
113