#  Draco Econometrics
#  Copyright (C) 2006-2008 Approximatrix, LLC
#  http://www.approximatrix.com
#  
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Script assumes that:
# - $model contains the StatTableModel
# - $expr contains the expression to evaluate (String)
# - $result accepts the result (List(Number))

require 'model/data'
require 'model/variable'
require 'math/dracomath'

# Mix in Math module
extend Math
extend DracoMath

$rmodel = Model::Data.new($model)

# This code is an ugly kludge to get around a ruby standard
# that all identifiers that begin with a capitalized letter
# are actually constants.  Since some variables may begin
# with a capital letter, we extend the class to contain
# a constant for each variable.
ext_module = "Extend_Constants_#{rand(65535)}"
$rmodel.variables.each { |nv|
	exts = <<END_OF_STRING___ 
	    module #{ext_module}
	        #{nv} = $rmodel.get_variable('#{nv}')
	    end
END_OF_STRING___
	eval(exts)	    
}
eval("$rmodel.extend #{ext_module}") 

$rmodel.evaluate($expr) { |val| 
  val = val.value if val.kind_of? Model::Variable
  $result << java.lang.Double.new(val) if val
  $result << nil unless val
}

$rmodel = nil