Class: Finitio::Generation

Inherits:
Object
  • Object
show all
Defined in:
lib/finitio/generation.rb,
lib/finitio/generation/heuristic.rb,
lib/finitio/generation/heuristic/random.rb,
lib/finitio/generation/heuristic/constant.rb

Overview

This class helps generating random data for a given Finitio type.

Example

type = Finitio.system(<<-FIO)
  .Integer
FIO
gen = Finitio::Generation.new
gen.call(type)

Random data is generated for most ruby builtin types, tuples and all sorts of collections (seq, set, relations).

You can fine-tune the random data generation through the following means (the first one wins):

  • By passing generators by type name at construction:

    Finition::Generation.new({
      generators: {
        "Name" => ->(type, g, world) { "Bernard" }
      }
    })
    
  • Through ‘examples` metadata put on type definitions:

    /- examples: {"Bernard"} -/
    Name = .String
    

Defined Under Namespace

Classes: Heuristic

Constant Summary collapse

DEFAULT_OPTIONS =
{

  heuristic: Heuristic::Random.new,

  collection_size: 1..10,

  generators: {
    "Date"     => ->(t,g,w) { g.heuristic.call(Date, g, w) },
    "Time"     => ->(t,g,w) { g.heuristic.call(Time, g, w) },
    "DateTime" => ->(t,g,w) { g.heuristic.call(DateTime, g, w) }
  }

}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Generation

Returns a new instance of Generation.



50
51
52
53
54
# File 'lib/finitio/generation.rb', line 50

def initialize(options = {})
  @options = DEFAULT_OPTIONS.merge(options){|k,v1,v2|
    v1.is_a?(Hash) ? v1.merge(v2) : v2
  }
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



55
56
57
# File 'lib/finitio/generation.rb', line 55

def options
  @options
end

Instance Method Details

#call(type, world = nil) ⇒ Object



85
86
87
88
89
90
91
92
93
# File 'lib/finitio/generation.rb', line 85

def call(type, world = nil)
  if gen = generators[type.name]
    gen.call(type, self, world)
  elsif exs = type. && type.[:examples]
    flip_one_out_of(exs)
  else
    type.generate_data(self, world)
  end
end

#collection_sizeObject



79
80
81
82
83
# File 'lib/finitio/generation.rb', line 79

def collection_size
  size = options[:collection_size]
  size = size.is_a?(Proc) ? size.call : size
  flip_one_out_of(size)
end

#flip_coinObject



65
66
67
# File 'lib/finitio/generation.rb', line 65

def flip_coin
  flip_one_out_of(2) == 1
end

#flip_one_out_of(n) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/finitio/generation.rb', line 69

def flip_one_out_of(n)
  case n
  when Integer    then Kernel.rand(n)
  when Range      then Kernel.rand(n)
  when Enumerable then n[flip_one_out_of(n.size)]
  else
    raise "Cannot flip one on #{n}"
  end
end

#generatorsObject



61
62
63
# File 'lib/finitio/generation.rb', line 61

def generators
  options[:generators]
end

#heuristicObject



57
58
59
# File 'lib/finitio/generation.rb', line 57

def heuristic
  options[:heuristic]
end