1 #ifndef NODE_H_
2 #define NODE_H_
3 
4 #include <list>
5 #include <vector>
6 #include <string>
7 
8 namespace jags {
9 
10 struct RNG;
11 class StochasticNode;
12 class DeterministicNode;
13 class Graph;
14 
15 /**
16  * @short Random variable status of nodes
17  *
18  * This enumeration is used by Node#randomVariableStatus to classify
19  * nodes into one of three categories.
20  *
21  * Some nodes are considered as random variables, and among those that
22  * are, they may be observed or unobserved. Conversely, a node that is
23  * not a random variable may not be observed.
24  */
25 enum RVStatus {RV_FALSE, RV_TRUE_UNOBSERVED, RV_TRUE_OBSERVED};
26 
27 /**
28  * @short Node in a directed acyclic graph
29  */
30 class Node {
31     std::vector<Node const *> _parents;
32     std::list<StochasticNode*> *_stoch_children;
33     std::list<DeterministicNode *> *_dtrm_children;
34 
35     /* Forbid copying of Node objects */
36     Node(Node const &orig);
37     Node &operator=(Node const &rhs);
38 
39 protected:
40     std::vector<unsigned int> const &_dim;
41     const unsigned int _length;
42     const unsigned int _nchain;
43     double *_data;
44 
45 public:
46     /**
47      * Constucts a Node with no parents.
48      * @param dim Dimension of new Node.
49      * @param nchain Number of chains that the Node will contain data on.
50      */
51     Node(std::vector<unsigned int> const &dim, unsigned int nchain);
52     /**
53      * Constructs a node with parents.  Each parent must contain data on
54      * the same number of chains. Subclasses of Node may give specific
55      * meaning to the ordering of the parents.
56      *
57      * @param dim Dimension of new Node.
58      *
59      * @param parents vector of parent nodes. A node may not be its own
60      * parent.
61      */
62     Node(std::vector<unsigned int> const &dim, unsigned int nchain,
63 	 std::vector<Node const *> const &parents);
64     /**
65      * Destructor.
66      */
67     virtual ~Node();
68     /**
69      * Number of chains.
70      */
71     unsigned int nchain() const;
72     /**
73      * Vector of parents.
74      */
75     std::vector<Node const *> const &parents() const;
76     /**
77      * Draws a random sample from the node's prior distribution.
78      * @param rng Pointer to random number generator
79      * @param chain Number of chain from which to draw sample
80      */
81     virtual void randomSample(RNG *rng, unsigned int chain) = 0;
82     /**
83      * Calculates a value for the node based on its parents' values.
84      * @param chain Number of chain from which to draw sample
85      */
86     virtual void deterministicSample(unsigned int chain) = 0;
87     /**
88      * Checks whether the parents of the Node have valid values.
89      */
90     virtual bool checkParentValues(unsigned int chain) const = 0;
91     /**
92      * Returns the stochastic children of the node
93      */
94     std::list<StochasticNode*> const *stochasticChildren();
95     /**
96      * Returns the deterministic children of the node
97      */
98     std::list<DeterministicNode*> const *deterministicChildren();
99     /**
100      * Initializes the node for the given chain. The value array of a
101      * newly constructed Node consists of missing values (denoted by
102      * the special value JAGS_NA).  This function sets the value of
103      * the node by forward sampling from its parents.  If the Node has
104      * previously had its value set, the function will do nothing and
105      * return the value true.  Initialization will fail if any of the
106      * parent nodes is uninitialized, and in this case the return
107      * value is false.
108      *
109      * @param chain Index number of chain to initialize.
110      *
111      * @returns a logical value indicating success
112      */
113     bool initialize(unsigned int chain);
114     /**
115      * Returns the BUGS-language representation of the node, based on the
116      * names of its parents
117      *
118      * @param parents Vector of names of parent nodes
119      */
120     virtual std::string
121 	deparse(std::vector<std::string> const &parents) const = 0;
122     /**
123      * Returns the random variable status of a node.
124      */
125     virtual RVStatus randomVariableStatus() const = 0;
126     /**
127      * Indicates whether the value of the node is fixed.
128      */
129     virtual bool isFixed() const = 0;
130     /**
131      * Sets the value of the node for a given chain
132      * @param value Array of values to be assigned
133      * @param length Length of the value array
134      * @param chain number of chain (starting from zero) to modify
135      *
136      * @see SArray#setValue
137      */
138     void setValue(double const *value, unsigned int length, unsigned int chain);
139     /**
140      * Indicates whether a node is discrete-valued or not.
141      * @see SArray#isDiscreteValued
142      */
143     virtual bool isDiscreteValued() const = 0;
144     /**
145      * Returns a pointer to the start of the array of values for
146      * the given chain.
147      */
148     double const *value(unsigned int chain) const;
149     /**
150      * Returns the length of the value array
151      */
152     unsigned int length() const;
153     /**
154      * Returns the dimensions of the Node
155      */
156     std::vector<unsigned int> const &dim() const;
157     /**
158      * Swaps the values in the given chains
159      */
160     void swapValue(unsigned int chain1, unsigned int chain2);
161 
162     void addChild(StochasticNode *node) const;
163     void removeChild(StochasticNode *node) const;
164     void addChild(DeterministicNode *node) const;
165     void removeChild(DeterministicNode *node) const;
166     virtual void unlinkParents() = 0;
167 };
168 
169 /**
170  * Calculate the number of chains of parameter vector. Returns 0
171  * if the parameters are inconsistent
172  */
173 unsigned int countChains(std::vector<Node const *> const &parameters);
174 
175 } /* namespace jags */
176 
177 #endif /* NODE_H_ */
178