Class: Qrb::RelationType

Inherits:
Type
  • Object
show all
Defined in:
lib/qrb/type/relation_type.rb

Overview

The Relation type generator allows capturing sets of information facts, i.e. sets of tuples (of same heading). E.g.

ColoredPoints = {{ point: Point, color: Color }}

This class allows capturing relation types, in a way similar to TupleType:

ColoredPoints = RelationType.new( Heading[...] )

A ruby Set is used as concrete representation, and will contain hashes that are valid representations of the associated tuple type:

R(ColoredPoints) = Set[ R({...}) ] = Set[Hash[...]]

Accordingly, the dress transformation function has the signature below. It expects an Enumerable as input and fails if any duplicate is found (after tuple transformation), or if any tuple fails at being transformed.

dress :: Alpha  -> ColoredPoints   throws TypeError
dress :: Object -> Set[Hash[...]]  throws TypeError

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Type

#name, #name=, #to_s

Constructor Details

#initialize(heading, name = nil) ⇒ RelationType

Returns a new instance of RelationType.



26
27
28
29
30
31
32
33
# File 'lib/qrb/type/relation_type.rb', line 26

def initialize(heading, name = nil)
  unless heading.is_a?(Heading)
    raise ArgumentError, "Heading expected, got `#{heading}`"
  end

  super(name)
  @heading = heading
end

Instance Attribute Details

#headingObject (readonly)

Returns the value of attribute heading.



34
35
36
# File 'lib/qrb/type/relation_type.rb', line 34

def heading
  @heading
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



64
65
66
67
# File 'lib/qrb/type/relation_type.rb', line 64

def ==(other)
  return false unless other.is_a?(RelationType)
  heading == other.heading
end

#default_nameObject



36
37
38
# File 'lib/qrb/type/relation_type.rb', line 36

def default_name
  "{{#{heading.to_name}}}"
end

#dress(value, handler = DressHelper.new) ⇒ Object

Apply the corresponding TupleType’s ‘dress` to every element of `value` (any enumerable). Return a Set of transformed tuples. Fail if anything goes wrong transforming tuples or if duplicates are found.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/qrb/type/relation_type.rb', line 49

def dress(value, handler = DressHelper.new)
  handler.failed!(self, value) unless value.respond_to?(:each)

  # Up every tuple and keep results in a Set
  set = Set.new
  handler.iterate(value) do |tuple, index|
    tuple = tuple_type.dress(tuple, handler)
    handler.fail!("Duplicate tuple") if set.include?(tuple)
    set << tuple
  end

  # Return built tuples
  set
end

#hashObject



70
71
72
# File 'lib/qrb/type/relation_type.rb', line 70

def hash
  self.class.hash ^ heading.hash
end

#include?(value) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
43
44
# File 'lib/qrb/type/relation_type.rb', line 40

def include?(value)
  value.is_a?(Set) && value.all?{|tuple|
    tuple_type.include?(tuple)
  }
end