#  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/>.

include Java

import com.approximatrix.draco.datamodel.StatTableModel

module Model
  class Data
    def initialize(mdl)
      @stmodel = mdl
      @variables = []
      @stmodel.getColumnCount().times { |i| 
        @variables << Model::Variable.new(@stmodel.getVariable(i))
      }
    end
    
    def update_index(index)
      @variables.each do |var|
        var.update_index(index)
      end
    end
    
    def evaluate(string)
      @stmodel.getRowCount().times do |i|
        update_index(i)
        begin
          yield instance_eval(string)
        rescue
          yield nil
        end
      end
    end
    
    def get_variable(string)
      ret = nil
      @variables.each do |var|
        ret = var if var.name == string
      end
      ret
    end
    
    def variables
      ret = []
      @variables.each do |var|
        ret << var.name
      end
      ret
    end
    
    def method_missing(method, *args)
      # puts method.to_s
      # puts args
      
      var = get_variable(method.to_s) 
      
      return var if var
      
      if args.length > 0
        var = args.first if args.first.is_a? Variable
        var.send(method,*args[1..args.length]) if var
      else
        # Still here?
        super.call(method,args)
      end
    end
    
  end # class Data
end # module model
