| 1 | package PIANOS.datastructures; |
| 2 | import java.util.LinkedList; |
| 3 | |
| 4 | |
| 5 | /** A representation for a stochastic or functional variable/parameter in a statistical PIANOS model. |
| 6 | |
| 7 | @author Group Linja |
| 8 | */ |
| 9 | public class Variable{ |
| 10 | |
| 11 | private String name; |
| 12 | private String algorithm; |
| 13 | private String proposalStrategy; |
| 14 | |
| 15 | private Distribution proposal; |
| 16 | private Distribution distribution; |
| 17 | private Equation equation; |
| 18 | |
| 19 | private LinkedList<Variable> affects; |
| 20 | private LinkedList<Variable> depends; |
| 21 | private Entity belongsTo; |
| 22 | |
| 23 | private int column; |
| 24 | private int missingValues; |
| 25 | private int updates; |
| 26 | |
| 27 | private boolean data; |
| 28 | private boolean functional; |
| 29 | private boolean spatial; |
| 30 | private boolean typeInteger; |
| 31 | private boolean printed; |
| 32 | |
| 33 | /** Constructs a new Variable from scratch, ready to take in field values. |
| 34 | */ |
| 35 | public Variable (){ |
| 36 | distribution = null; |
| 37 | printed = false; |
| 38 | typeInteger = false; |
| 39 | missingValues = 0; |
| 40 | column = -1; |
| 41 | proposalStrategy = null; |
| 42 | affects = new LinkedList<Variable>(); |
| 43 | depends = new LinkedList<Variable>(); |
| 44 | spatial = false; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | @return A list of Variables this variable affects in the model. |
| 49 | */ |
| 50 | public LinkedList<Variable> getAffectsList (){ |
| 51 | return affects; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | Adds a new Variable at the end of the list of Variables that this Variable affects. |
| 56 | */ |
| 57 | public void addAffected (Variable variable){ |
| 58 | affects.add(variable); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | @return a list of Variables that this Variable depends on. |
| 63 | */ |
| 64 | public LinkedList<Variable> getDependsList (){ |
| 65 | return depends; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | Adds a new Variable at the end of the list of Variables that this Variables depends on. |
| 70 | */ |
| 71 | public void addDependence (Variable variable){ |
| 72 | depends.add(variable); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | @return the update algorithm used for this Variable (currently not implemented), defaults to M-H |
| 77 | */ |
| 78 | public String getAlgorithm (){ |
| 79 | return algorithm; |
| 80 | } |
| 81 | |
| 82 | /** (currently not implemented) This will set the update algorithm for this Variable. |
| 83 | Choices would include Metropolis-Hastings and Gibbs sampling. |
| 84 | */ |
| 85 | public void setAlgorithm (String algorithm){ |
| 86 | this.algorithm = algorithm; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | @return the column number for the variable if it is found in data, or -1 if the variable spans the whole |
| 91 | data matrix (and as such all the columns) |
| 92 | */ |
| 93 | public int getColumn (){ |
| 94 | return column; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | Sets the data column number for the Variable (See getColumn()) |
| 99 | */ |
| 100 | public void setColumn (int column){ |
| 101 | this.column = column; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | @return if this Variable can be found in the data of this model. |
| 106 | */ |
| 107 | public boolean isData (){ |
| 108 | return data; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | @param data set this to true if the Variable is found in data. |
| 113 | */ |
| 114 | public void setData(boolean data) { |
| 115 | this.data = data; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | @return The Equation used for this Variable if it is functional (see Equation) |
| 120 | */ |
| 121 | public Equation getEquation (){ |
| 122 | return equation; |
| 123 | } |
| 124 | |
| 125 | public void setEquation (Equation equation){ |
| 126 | this.equation = equation; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | @return The Distribution used for this Variable if it is stochastic (not functional) (see Distribution) |
| 131 | */ |
| 132 | public Distribution getDistribution (){ |
| 133 | return distribution; |
| 134 | } |
| 135 | |
| 136 | public void setDistribution (Distribution distribution){ |
| 137 | this.distribution = distribution; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | @return true if this Variable is functional. |
| 142 | */ |
| 143 | public boolean isFunctional (){ |
| 144 | return functional; |
| 145 | } |
| 146 | |
| 147 | public void setFunctional (boolean functional){ |
| 148 | this.functional = functional; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | @return true if this Variable is a functional variable that has an expression |
| 153 | that consists of the SUM or COUNT statements and a spatial dependence in them, such as |
| 154 | COUNT(&x) |
| 155 | */ |
| 156 | public boolean isSpatial (){ |
| 157 | return spatial; |
| 158 | } |
| 159 | |
| 160 | public void setSpatial (boolean spatial){ |
| 161 | this.spatial = spatial; |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | @return The name used in the model for this variable and to be used in the Fortran program. |
| 166 | */ |
| 167 | public String getName (){ |
| 168 | return name; |
| 169 | } |
| 170 | |
| 171 | public void setName (String name){ |
| 172 | this.name = name; |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | Sets the proposal Distribution for this variable (see the manual) |
| 177 | */ |
| 178 | public void setProposal (Distribution proposal){ |
| 179 | this.proposal = proposal; |
| 180 | } |
| 181 | |
| 182 | public Distribution getProposal(){ |
| 183 | return proposal; |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | Sets the update strategy for this variable {sequential, random} |
| 188 | */ |
| 189 | public void setStrategy (String strategy){ |
| 190 | this.proposalStrategy = strategy; |
| 191 | } |
| 192 | |
| 193 | public String getStrategy (){ |
| 194 | return proposalStrategy; |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | Sets the number of updates this variable should have before end of simulation. |
| 199 | */ |
| 200 | public void setUpdates (int updates){ |
| 201 | this.updates = updates; |
| 202 | } |
| 203 | |
| 204 | public int getUpdates (){ |
| 205 | return updates; |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | @return How many missing values this variable has (if it's a data variable that has missing values), |
| 210 | otherwise returns 0. |
| 211 | */ |
| 212 | public int getMissingValueCount (){ |
| 213 | return missingValues; |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | Adds one to the number of missing values. Used by the Parser. |
| 218 | */ |
| 219 | public void incrementMissingValues (){ |
| 220 | missingValues++; |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | @return true if this variable should have integer values. |
| 225 | */ |
| 226 | public boolean isInteger (){ |
| 227 | return typeInteger; |
| 228 | } |
| 229 | |
| 230 | public void setType (boolean typeInteger){ |
| 231 | this.typeInteger = typeInteger; |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | @return The entity this Variable belongs to. |
| 236 | */ |
| 237 | public Entity getEntity (){ |
| 238 | return belongsTo; |
| 239 | } |
| 240 | |
| 241 | public void setEntity (Entity belongsTo){ |
| 242 | this.belongsTo = belongsTo; |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | Sets that this Variable should be printed. |
| 247 | */ |
| 248 | public void setPrinted (){ |
| 249 | printed = true; |
| 250 | } |
| 251 | |
| 252 | public boolean isPrinted (){ |
| 253 | return printed; |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | Runs this Variable through a health check. Returns true if it is alright. Called by the parser at end of reading. |
| 258 | @return true if this variable is valid. |
| 259 | */ |
| 260 | public boolean isOk (){ |
| 261 | if (data && missingValues > 0 && proposal == null) |
| 262 | return false; |
| 263 | if (data && missingValues > 0 && distribution == null) |
| 264 | return false; |
| 265 | if (!functional && !data && proposal == null) |
| 266 | return false; |
| 267 | if (functional && equation == null) |
| 268 | return false; |
| 269 | if (!functional && !data && distribution == null) |
| 270 | return false; |
| 271 | |
| 272 | if (belongsTo != null && !belongsTo.isMatrix()){ |
| 273 | for (Variable var : affects){ |
| 274 | |
| 275 | if (var.getEntity() == null){ |
| 276 | //One dimensional affects a global |
| 277 | System.out.println("one-dimensional affects a global"); |
| 278 | return false; |
| 279 | } |
| 280 | if (!var.getEntity().isMatrix() && var.getEntity() != belongsTo){ |
| 281 | //Affected is one dimensional and the entity is not the same as the one this variable belongs to |
| 282 | System.out.println("one-dimensional affects another one-dimensional in a different entity"); |
| 283 | return false; |
| 284 | } |
| 285 | if (var.getEntity().isMatrix()) |
| 286 | //Affected is a matrix |
| 287 | if (var.getEntity().getXCoordinate() != belongsTo && var.getEntity().getYCoordinate() != belongsTo){ |
| 288 | //This variable's entity isn't either X or Y coordinate of the matrix |
| 289 | System.out.println("one-dimensional affects an independent intersection"); |
| 290 | return false; |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | }else if (belongsTo != null && belongsTo.isMatrix()){ |
| 295 | for (Variable var : affects){ |
| 296 | |
| 297 | if (var.getEntity() == null){ |
| 298 | //Matrix affects a global |
| 299 | System.out.println("Two-dim affects a global"); |
| 300 | return false; |
| 301 | } |
| 302 | if (!var.getEntity().isMatrix()){ |
| 303 | //Affected is one dimensional |
| 304 | System.out.println("Intersection variable "+ this +" affects the one-dimensional variable "+ var); |
| 305 | return false; |
| 306 | } |
| 307 | if (var.getEntity().isMatrix() && var.getEntity() != belongsTo){ |
| 308 | //Affected is a matrix and the entity is not the same as the one this variable belongs to |
| 309 | System.out.println("two-dim affects an independent intersection"); |
| 310 | return false; |
| 311 | } |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | //Check that spatial matrix exists |
| 316 | if (spatial){ |
| 317 | |
| 318 | if (belongsTo == null) |
| 319 | //Global variable can not be spatial |
| 320 | return false; |
| 321 | |
| 322 | if (!belongsTo.isMatrix() && !isSpatial()) |
| 323 | //Variable is one dimensional, but the Entity doesn't have a spatial matrix |
| 324 | return false; |
| 325 | |
| 326 | if (belongsTo.isMatrix()) |
| 327 | //Matrix... |
| 328 | if (!belongsTo.getXCoordinate().isSpatial() && !belongsTo.getYCoordinate().isSpatial()) |
| 329 | //Neither the X or Y cordinate is spatial |
| 330 | return false; |
| 331 | } |
| 332 | |
| 333 | return true; |
| 334 | } |
| 335 | |
| 336 | /** Prints a limited amount of information about this Variable similar to a model |
| 337 | file variable definition line. |
| 338 | */ |
| 339 | public String toString(){ |
| 340 | String toReturn = ""; |
| 341 | if (this.isInteger()) |
| 342 | toReturn += "INTEGER "; |
| 343 | else |
| 344 | toReturn += "REAL "; |
| 345 | |
| 346 | toReturn += this.getName(); |
| 347 | if (this.isData()){ |
| 348 | toReturn += "("; |
| 349 | if (this.getColumn() != -1) |
| 350 | toReturn += this.getColumn(); |
| 351 | else |
| 352 | toReturn += "*"; |
| 353 | toReturn += ")"; |
| 354 | } |
| 355 | if (this.isFunctional()){ |
| 356 | toReturn += " = "; |
| 357 | toReturn += this.getEquation(); |
| 358 | }else{ |
| 359 | if (this.getDistribution() != null){ |
| 360 | toReturn += " ~ "; |
| 361 | toReturn += this.getDistribution(); |
| 362 | } |
| 363 | else if (this.isData()){ |
| 364 | toReturn += " is plain data"; |
| 365 | }else |
| 366 | toReturn += " is not data, stochastic or functional ???"; |
| 367 | } |
| 368 | |
| 369 | if (!this.isFunctional()){ |
| 370 | if (this.getProposal() != null){ |
| 371 | toReturn += " proposal distribution: " + this.getProposal(); |
| 372 | }else |
| 373 | toReturn += " no proposal distribution "; |
| 374 | } |
| 375 | |
| 376 | if (this.isPrinted()) |
| 377 | toReturn += ", is printed"; |
| 378 | |
| 379 | if (this.getUpdates() != 0) |
| 380 | toReturn += ", updates=" + this.getUpdates(); |
| 381 | |
| 382 | if (this.getMissingValueCount() != 0) |
| 383 | toReturn += ", " + this.getMissingValueCount() + " missing values"; |
| 384 | |
| 385 | return toReturn; |
| 386 | } |
| 387 | |
| 388 | } |