Class: Glaemscribe::API::SheafChainIterator

Inherits:
Object
  • Object
show all
Defined in:
lib/api/sheaf_chain_iterator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sheaf_chain, cross_schema = nil) ⇒ SheafChainIterator

If a cross schema is passed, the prototype of the chain will be permutated



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/api/sheaf_chain_iterator.rb', line 32

def initialize(sheaf_chain, cross_schema = nil)
  @sheaf_chain            = sheaf_chain
  # Sizes contains the number of fragments/sheaf
  @sizes                  = sheaf_chain.sheaves.map { |sheaf| sheaf.fragments.count }
  # An array of counters, one for each sheaf, to increment on fragments
  @iterators              = Array.new(@sizes.count,0)
    
  @errors                 = []
    
  # Construct the identity array
  identity_cross_array    = []
  sheaf_count             = sheaf_chain.sheaves.count
  sheaf_count.times{|i| identity_cross_array << i+1}  

  # Construct the cross array
  if cross_schema
    @cross_array          = cross_schema.split(",").map{ |i| i.to_i }
    ca_count              = @cross_array.count
    @errors << "#{sheaf_count} sheafs found in right predicate, but #{ca_count} elements in cross rule."  if ca_count != sheaf_count  
    @errors << "Cross rule should contain each element of #{identity_cross_array} once and only once."    if identity_cross_array != @cross_array.sort
  else
    @cross_array = identity_cross_array
  end    
end

Instance Attribute Details

#cross_mapObject

Returns the value of attribute cross_map.



28
29
30
# File 'lib/api/sheaf_chain_iterator.rb', line 28

def cross_map
  @cross_map
end

#errorsObject

Returns the value of attribute errors.



29
30
31
# File 'lib/api/sheaf_chain_iterator.rb', line 29

def errors
  @errors
end

#sheaf_chainObject

Returns the value of attribute sheaf_chain.



27
28
29
# File 'lib/api/sheaf_chain_iterator.rb', line 27

def sheaf_chain
  @sheaf_chain
end

Instance Method Details

#combinationsObject

Calculate all cominations for the chain



91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/api/sheaf_chain_iterator.rb', line 91

def combinations
  resolved = []
  @iterators.each_with_index{ |counter, index|
    sheaf     = sheaf_chain.sheaves[index]
    fragment  = sheaf.fragments[counter]
    
    resolved << fragment.combinations
  }
  res = resolved[0]
  (resolved.count-1).times { |i|
    res = res.product(resolved[i+1]).map{|e1,e2| e1+e2} 
  }
  res
end

#iterateObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/api/sheaf_chain_iterator.rb', line 74

def iterate
  pos = 0
  while pos < @sizes.count do
    realpos = @cross_array[pos]-1
    @iterators[realpos] += 1
    if @iterators[realpos] >= @sizes[realpos]
      @iterators[realpos] = 0
      pos += 1
    else
      return true
    end
  end
  # Wrapped!
  return false
end

#prototypeObject

Calculate the prototype of the chain



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/api/sheaf_chain_iterator.rb', line 58

def prototype
  res   = @sizes.clone
  res2  = @sizes.clone 
  
  res.count.times{ |i| res2[i] = res[@cross_array[i]-1] }
  res   = res2
    
  # Remove all sheaves of size 1 (which are constant)
  res.delete(1)
  
  # Create a prototype string
  res = res.join("x")
  res = "1" if res.empty?
  res
end