HARPgroup / openmi-om

A lightweight implementation of the OpenMI framework for building Object-oriented Meta-models.
0 stars 1 forks source link

R implementation of modelObject, modelSubObject and basic equation object OM #55

Open rburghol opened 6 years ago

rburghol commented 6 years ago

Equation Evaluation


    evaluate = function(){
      value <- tryCatch(
        {
          safe_envir <- c(self$data, self$arithmetic_operators)
          #value <-eval(parse(text=self$equation), envir=safe_envir)
          value <-eval(self$eq, envir=safe_envir)
        },
        error=function(cond) {
          # if no. of errors not exceeded,
          self$numnull <- self$numnull + 1
          if (self$numnull <= 5) {
            message(paste("Could not evaluate"))
            message(cond)
          }
          # Choose a return value in case of error
          value <- self$defaultvalue
        }
      )
      if (typeof(value) == 'closure') {
        # Choose a return value in case of error
        self$numnull <- self$numnull + 1
        if (self$numnull <= 5) {
          message(paste("Eval returned a function - check equation names for reserved words with undefined local values"))
          message(self$data)
        }
        value <- self$defaultvalue
      }
      return(value)
    }

References

Objects

rburghol commented 6 years ago

Advanced R expressions: http://adv-r.had.co.nz/Expressions.html