Class: Axiom::Algebra::Restriction

Inherits:
Relation
  • Object
show all
Includes:
Relation::Operation::Unary
Defined in:
lib/axiom/algebra/restriction.rb

Overview

Restrict the tuples to those that match a predicate

Defined Under Namespace

Modules: Methods

Constant Summary collapse

TAUTOLOGY =
Function::Proposition::Tautology.instance

Instance Attribute Summary collapse

Attributes included from Operation::Unary

#operand

Attributes inherited from Relation

#header

Instance Method Summary collapse

Methods inherited from Relation

#==, #[], #directions, #empty?, #include?, #materialize, #materialized?, new, #replace

Methods included from Visitable

#accept

Constructor Details

#initialize(operand, predicate) ⇒ undefined

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initialize a Restriction

Parameters:

  • operand (Relation)

    the relation to restrict

  • predicate (Function, #call)

    the function to restrict the tuples with



30
31
32
33
# File 'lib/axiom/algebra/restriction.rb', line 30

def initialize(operand, predicate)
  super(operand)
  @predicate = predicate
end

Instance Attribute Details

#predicateFunction, #call (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The predicate for the relation

Returns:



18
19
20
# File 'lib/axiom/algebra/restriction.rb', line 18

def predicate
  @predicate
end

Instance Method Details

#delete(other) ⇒ Restriction

Delete a relation from the Restriction

The tuples must match the predicate to be deleted.

Examples:

new_relation = restriction.delete(other)

Parameters:

Returns:



86
87
88
89
# File 'lib/axiom/algebra/restriction.rb', line 86

def delete(other)
  other = coerce(other)
  operand.delete(other.restrict(predicate)).restrict(predicate)
end

#each {|tuple| ... } ⇒ self

Iterate over each tuple in the set

Examples:

restriction = Restriction.new(operand, predicate)
restriction.each { |tuple| ... }

Yields:

  • (tuple)

Yield Parameters:

  • tuple (Tuple)

    each tuple in the set

Returns:

  • (self)


49
50
51
52
53
54
55
# File 'lib/axiom/algebra/restriction.rb', line 49

def each
  return to_enum unless block_given?
  operand.each do |tuple|
    yield tuple if Function.extract_value(predicate, tuple).equal?(true)
  end
  self
end

#insert(other) ⇒ Restriction

Insert a relation into the Restriction

The tuples must match the predicate to be inserted.

Examples:

new_relation = restriction.insert(other)

Parameters:

Returns:



69
70
71
72
# File 'lib/axiom/algebra/restriction.rb', line 69

def insert(other)
  other = coerce(other)
  operand.insert(other.restrict(predicate)).restrict(predicate)
end