class NoSE::Random::Network

A simple representation of a random ER diagram

Constants

FIELD_TYPES

Probabilities of selecting various field types

VARIABLE_NAMES

Random names of variables combined to create random names

Attributes

entities[R]

Public Class Methods

new(params = {}) click to toggle source
# File lib/nose/random.rb, line 11
def initialize(params = {})
  @nodes_nb = params.fetch :nodes_nb, 10
  @field_count = RandomGaussian.new params.fetch(:num_fields, 3), 1
  @neighbours = Array.new(@nodes_nb) { Set.new }
end

Public Instance Methods

inspect() click to toggle source

:nocov:

# File lib/nose/random.rb, line 18
def inspect
  @nodes.map do |node|
    @entities[node].inspect
  end.join "\n"
end

Protected Instance Methods

add_foreign_keys() click to toggle source

Add foreign key relationships for neighbouring nodes @return [void]

# File lib/nose/random.rb, line 62
def add_foreign_keys
  @neighbours.each_with_index do |other_nodes, node|
    other_nodes.each do |other_node|
      @neighbours[other_node].delete node

      if rand > 0.5
        from_node = node
        to_node = other_node
      else
        from_node = other_node
        to_node = node
      end

      from_field = Fields::ForeignKeyField.new(
        'FK' + @entities[to_node].name + 'ID',
        @entities[to_node]
      )
      to_field = Fields::ForeignKeyField.new(
        'FK' + @entities[from_node].name + 'ID',
        @entities[from_node]
      )

      from_field.reverse = to_field
      to_field.reverse = from_field

      @entities[from_node] << from_field
      @entities[to_node] << to_field
    end
  end
end
create_entity(node) click to toggle source

Create a random entity to use in the model @return [Entity]

# File lib/nose/random.rb, line 29
def create_entity(node)
  num_entities = RandomGaussian.new 10_000, 100
  entity = Entity.new('E' + random_name(node)) * num_entities.rand
  pick_fields entity

  entity
end
new_neighbour(node, neighbour) click to toggle source

Find a new neighbour for a node

# File lib/nose/random.rb, line 108
def new_neighbour(node, neighbour)
  unlinkable_nodes = [node, neighbour] + @neighbours[node].to_a
  (@nodes.to_a - unlinkable_nodes).sample
end
pick_fields(entity) click to toggle source

Select random fields for an entity @return [void]

# File lib/nose/random.rb, line 47
def pick_fields(entity)
  entity << Fields::IDField.new(entity.name + 'ID')
  0.upto(@field_count.rand).each do |field_index|
    entity << random_field(field_index)
  end
end
random_field(field_index) click to toggle source

Generate a random field to add to an entity @return [Fields::Field]

# File lib/nose/random.rb, line 56
def random_field(field_index)
  Pickup.new(FIELD_TYPES).pick.send(:new, 'F' + random_name(field_index))
end
random_name(index) click to toggle source

Generate a random name for an attribute @return [String]

# File lib/nose/random.rb, line 119
def random_name(index)
  index.to_s.chars.map(&:to_i).map { |digit| VARIABLE_NAMES[digit] }.join
end