Module: SplitCat::Random

Defined in:
lib/split_cat/random.rb

Class Method Summary collapse

Class Method Details

.generate(args) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/split_cat/random.rb', line 5

def self.generate( args )
  n_subjects = args[ :n_subjects ].to_i
  n_experiments = args[ :n_experiments ].to_i
  max_items = args[ :max_items ].to_i

  tokens = []
  (1..n_subjects).each do |i|
    tokens << SplitCat::Subject.create.token
  end

  Rails.logger.info "SplitCat::Random.generate N_EXPERIMENTS=#{ n_experiments }"

  (1..n_experiments).each do |i|
    experiment = Experiment.create( :name => "test_#{ i }_#{ Time.now.to_i }" )
    n_hypotheses = 2 + rand( max_items - 2 )
    n_goals = rand( max_items )

    (0..n_hypotheses).each do |i|
      name = ('a'.ord + i).chr
      experiment.hypotheses << SplitCat::Hypothesis.create( :name => name, :weight => 1 )
    end

    (0..n_goals).each do |i|
      experiment.goals << SplitCat::Goal.create( :name => "goal_#{i}" )
    end

    Rails.logger.info "SplitCat::Random.generate SAVING EXPERIMENT: #{ experiment.name }"

    experiment.save!

    tokens.each do |token|

      Rails.logger.info "SplitCat::Random.generate ASSIGNING: #{token}"

      experiment.get_hypothesis( token )
      experiment.goals.each do |goal|
        if rand > 0.5
          experiment.record_goal( goal.name, token )
        end
      end
    end
  end

end