#   This program demonstrates using a virtual file syatem to generate a data file 
#   which can be used by glpsol without the data being physically present on the
#   computer.

#  Nigel_Galloway@operamail.com
#  July 22nd., 2008.
#

require 'fusefs'

StringRTM = <<RTEND;
/*Arithmetic Mean of a large number of random Integers
   between 1 and 9 inclusive should be 5. The sum of each
   (random Integer - 5) should be 0, is it for glpk's psudo random generator?
  - or - another excuse to solve a very large constraint matrix
         over 1.75 million rows and columns with 2GB of memory.
  Nigel_Galloway@operamail.com
  July 18th., 2008.
*/

set Sample;
param RI {x in Sample},integer;
param Mean := 5;
var E {x in Sample},integer;

/* Mean + variance[n] = Sample[n] */
variances{z in Sample}: Mean + E[z] = RI[z];

solve;

printf "%d", sum{x in Sample} E[x];

data;

param:
Sample:   RI :=
  1       9
  2       8
  3       7
  4       6
  5       5
  6       4
  7       3
  8       2
  9       1
  ; 
end;
RTEND


class GLPK_vfs
  def contents(path)
    ['randomtest.mathprog',
     'randomtest.data']
  end
  def file?(path)
    if(path == '/randomtest.mathprog') then return true end
    if(path == '/randomtest.data') then return true end
    return false
  end
  def read_file(path)
    if(path == '/randomtest.mathprog') then aString = StringRTM end
    if(path == '/randomtest.data')
      aString = "data;\nparam:\nSample:   RI :=\n"
      (1..Riz).each{|z|
        aString << z.to_s << "          " << (rand(9)+1).to_s << "\n"
      }
      aString << ";\nend;\n"
    end
    return aString
  end
end

if (ARGV.size != 2)
  puts "Usage: #{$0} <directory> <number of random integers to test>"
  exit
end

dirname = ARGV.shift
Riz = ARGV.shift.to_i

unless File.directory?(dirname)
  puts "Usage: #{$0} <directory> <number of random integers to test>"
  exit
end

glpk_vfs = GLPK_vfs.new()
FuseFS.set_root( glpk_vfs )

# Mount under a directory given on the command line.
FuseFS.mount_under dirname
FuseFS.run
