class NodeFactory

Attributes

configuration[R]
dataSource[R]
db[R]
nextNodeId[R]
root[R]

Public Class Methods

new(dataSource, persister=nil) click to toggle source
# 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

Public Instance Methods

addLeaf(node, value, offset) click to toggle source

The algorithm adds leaf nodes in order

# 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) click to toggle source
# 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
newRoot() click to toggle source
# 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
nextDataSourceBit() click to toggle source
# File lib/support/node_factory.rb, line 26
def nextDataSourceBit
  @dataSourceBit = (@dataSourceBit << 1) if ((@configuration[:dataSourceBit]) && (@dataSource != nil))
end
nextDataSourceSetSize(modForSwitch) click to toggle source
# File lib/support/node_factory.rb, line 39
def nextDataSourceSetSize(modForSwitch)
  @nextDataSourceSwitch = modForSwitch
end
reset() click to toggle source
# File lib/support/node_factory.rb, line 22
def reset
  @nextNodeId = 1
end
setConfiguration(configurationHash) click to toggle source
# 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) click to toggle source
# 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=' ') click to toggle source

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

# 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

Private Instance Methods

addChild(parentNode, value, childNode) click to toggle source
# File lib/support/node_factory.rb, line 153
def addChild(parentNode, value, childNode)
  if (parentNode.children == nil) then
    parentNode.children = {}
  end
  parentNode.children[value] = childNode
  childNode.parent = parentNode
  persist(parentNode)
  persist(childNode)
end
newChild(node, key, suffixOffset, incomingEdgeStartOffset, incomingEdgeEndOffset) click to toggle source
# File lib/support/node_factory.rb, line 133
def newChild(node, key, suffixOffset, incomingEdgeStartOffset, incomingEdgeEndOffset)
  child = newNode
  child.suffixOffset = suffixOffset
  child.incomingEdgeStartOffset = incomingEdgeStartOffset
  child.incomingEdgeEndOffset = incomingEdgeEndOffset
  addChild(node, key, child)
  child.valueDepth = 0 if @configuration[:valueDepth]
  return child
end
newNode() click to toggle source
# File lib/support/node_factory.rb, line 143
def newNode
  result = Node.new(@nextNodeId)

  # newRoot defines leafCount accessor, so that case is handled in newRoot after the node is created
  result.leafCount = 0 if (@configuration[:leafCount] && (@nextNodeId > 1))
  result.dataSourceBit = @dataSourceBit if (@configuration[:dataSourceBit] && (@nextNodeId > 1))
  @nextNodeId += 1
  return result
end
persist(node) click to toggle source
# File lib/support/node_factory.rb, line 163
def persist(node)
  if (@db != nil) then
    @db.persist(node)
  end
  node
end
reverseAddValues(result, startOffset, endOffset) click to toggle source

return edge value sequence in reverse (used when getting path to root from a node)

# File lib/support/node_factory.rb, line 121
def reverseAddValues(result, startOffset, endOffset)
  if (endOffset == Node::CURRENT_ENDING_OFFSET) then
    result << @dataSource.valueAt(startOffset)
  else
    scanner = endOffset
    while (scanner >= startOffset) do
      result << @dataSource.valueAt(scanner)
      scanner -= 1
    end
  end
end