Class: NodeFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/support/node_factory.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dataSource, persister = nil) ⇒ NodeFactory

Returns a new instance of NodeFactory.



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/support/node_factory.rb', line 9

def initialize(dataSource, persister=nil)
  @dataSource = dataSource
  @suffixOffset = 0
  @configuration = {
      :leafCount => false,
      :valueDepth => false,
      :previousValue => false,
      :dataSourceBit => false
  }
  @db = persister
  self.reset
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



6
7
8
# File 'lib/support/node_factory.rb', line 6

def configuration
  @configuration
end

#dataSourceObject (readonly)

Returns the value of attribute dataSource.



5
6
7
# File 'lib/support/node_factory.rb', line 5

def dataSource
  @dataSource
end

#dbObject (readonly)

Returns the value of attribute db.



7
8
9
# File 'lib/support/node_factory.rb', line 7

def db
  @db
end

#nextNodeIdObject (readonly)

Returns the value of attribute nextNodeId.



4
5
6
# File 'lib/support/node_factory.rb', line 4

def nextNodeId
  @nextNodeId
end

#rootObject (readonly)

Returns the value of attribute root.



4
5
6
# File 'lib/support/node_factory.rb', line 4

def root
  @root
end

Instance Method Details

#addLeaf(node, value, offset) ⇒ Object

The algorithm adds leaf nodes in order



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

def addLeaf(node, value, offset)
  result = newChild(node, value, @suffixOffset, offset, Node::CURRENT_ENDING_OFFSET)

  # optional configuration based properties
  result.leafCount = 1 if (@configuration[:leafCount])
  result.previousValue = (@dataSource.valueAt(@suffixOffset - 1)) if ((@suffixOffset > 0) && @configuration[:previousValue])
  result.dataSourceBit = @dataSourceBit if @configuration[:dataSourceBit]
  @suffixOffset += 1
  if ((@nextDataSourceSwitch != nil) && ((@suffixOffset % @nextDataSourceSwitch) == 0)) then
    self.nextDataSourceBit
  end

  persist(result)
end

#extendDataSource(dataSource, startOffset) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/support/node_factory.rb', line 30

def extendDataSource(dataSource, startOffset)
  self.nextDataSourceBit
  if (@dataSource == nil) then
    @dataSource = dataSource
  else
    @dataSource.extendWith(dataSource, startOffset)
  end
end

#newRootObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/support/node_factory.rb', line 50

def newRoot
  self.reset
  result = newNode
  result.children = {}
  @root = result
  @configuration.each do |key, value|
    if (value) then
      @root.createAccessor(key.to_s)
    end
  end

  # configuration controlled accessors
  @root.valueDepth = 0 if @configuration[:valueDepth]
  @root.leafCount = 0 if @configuration[:leafCount]
  @dataSourceBit = 1 if @configuration[:dataSourceBit]
  @root.dataSourceBit = @dataSourceBit if @configuration[:dataSourceBit]

  persist(result)
end

#nextDataSourceBitObject



26
27
28
# File 'lib/support/node_factory.rb', line 26

def nextDataSourceBit
  @dataSourceBit = (@dataSourceBit << 1) if ((@configuration[:dataSourceBit]) && (@dataSource != nil))
end

#nextDataSourceSetSize(modForSwitch) ⇒ Object



39
40
41
# File 'lib/support/node_factory.rb', line 39

def nextDataSourceSetSize(modForSwitch)
  @nextDataSourceSwitch = modForSwitch
end

#resetObject



22
23
24
# File 'lib/support/node_factory.rb', line 22

def reset
  @nextNodeId = 1
end

#setConfiguration(configurationHash) ⇒ Object



43
44
45
46
47
48
# File 'lib/support/node_factory.rb', line 43

def setConfiguration configurationHash
  configurationHash.each do |key, value|
    @configuration[key] = value
  end
  self
end

#splitEdgeAt(node, incomingEdgeOffset) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/support/node_factory.rb', line 89

def splitEdgeAt(node, incomingEdgeOffset)
  result = newChild(node.parent, @dataSource.valueAt(node.incomingEdgeStartOffset), node.suffixOffset, node.incomingEdgeStartOffset, incomingEdgeOffset - 1)
  node.incomingEdgeStartOffset = incomingEdgeOffset
  addChild(result, @dataSource.valueAt(incomingEdgeOffset), node)

  # optional configuration based properties
  result.valueDepth = (result.parent.valueDepth + result.incomingEdgeLength) if @configuration[:valueDepth]
  result.dataSourceBit = (node.dataSourceBit | @dataSourceBit) if @configuration[:dataSourceBit]

  persist(node)
  persist(result)
end

#valuePath(node, delimiter = ' ') ⇒ Object

return a sequence of all values on the path to this node



105
106
107
108
109
110
111
112
113
# File 'lib/support/node_factory.rb', line 105

def valuePath(node, delimiter=' ')
  result = []
  while (node.parent != nil) do
    reverseAddValues(result, node.incomingEdgeStartOffset, node.incomingEdgeEndOffset)
    node = node.parent
  end
  result.reverse!
  return result.join(delimiter)
end