Class: Finitio::Generation
- Inherits:
-
Object
- Object
- Finitio::Generation
- 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
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #call(type, world = nil) ⇒ Object
- #collection_size ⇒ Object
- #flip_coin ⇒ Object
- #flip_one_out_of(n) ⇒ Object
- #generators ⇒ Object
- #heuristic ⇒ Object
-
#initialize(options = {}) ⇒ Generation
constructor
A new instance of Generation.
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 = DEFAULT_OPTIONS.merge(){|k,v1,v2| v1.is_a?(Hash) ? v1.merge(v2) : v2 } end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
55 56 57 |
# File 'lib/finitio/generation.rb', line 55 def @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_size ⇒ Object
79 80 81 82 83 |
# File 'lib/finitio/generation.rb', line 79 def collection_size size = [:collection_size] size = size.is_a?(Proc) ? size.call : size flip_one_out_of(size) end |
#flip_coin ⇒ Object
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 |
#generators ⇒ Object
61 62 63 |
# File 'lib/finitio/generation.rb', line 61 def generators [:generators] end |
#heuristic ⇒ Object
57 58 59 |
# File 'lib/finitio/generation.rb', line 57 def heuristic [:heuristic] end |