Class: CTioga2::Data::DataStack

Inherits:
Object
  • Object
show all
Includes:
Log
Defined in:
lib/ctioga2/data/stack.rb

Overview

This is the central class for acquisition and handling of Dataset objects, retrieved from from a Backends::BackendFactory.

TODO: provide real stack manipulation functions such as

  • interpolation: pops the last object from the stack and add its interpolated values on the element before.

  • mathematical functions on each column (DataColumn)

  • other stack-based operations.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Log

debug, error, fatal, #format_exception, #identify, info, init_logger, logger, set_level, #spawn, warn

Constructor Details

#initializeDataStack

Creates a new DataStack object.



68
69
70
71
72
73
74
75
# File 'lib/ctioga2/data/stack.rb', line 68

def initialize
  @stack = Array.new

  @named_datasets = Hash.new

  # Defaults to the 'text' backend
  @backend_factory = Data::Backends::BackendFactory.new('text')
end

Instance Attribute Details

#backend_factoryObject

The BackendFactory used for retrieving data from named sets.



50
51
52
# File 'lib/ctioga2/data/stack.rb', line 50

def backend_factory
  @backend_factory
end

#dataset_hookObject

A hook executed every time a dataset is pushed unto the stack using #add_dataset.

TODO: this string is parsed for each call to #add_dataset. Perhaps it would be good to provide a way to record a Command call, without parsing it from scratch ???

Although, with variables, that could be interesting to reparse everytime, since any change in the variables would be taken into account.



65
66
67
# File 'lib/ctioga2/data/stack.rb', line 65

def dataset_hook
  @dataset_hook
end

#named_datasetsObject

Named datasets



53
54
55
# File 'lib/ctioga2/data/stack.rb', line 53

def named_datasets
  @named_datasets
end

#stackObject

The array containing all the Dataset used so far.



47
48
49
# File 'lib/ctioga2/data/stack.rb', line 47

def stack
  @stack
end

Instance Method Details

#add_dataset(dataset, ignore_hooks = false) ⇒ Object

Adds a Dataset object onto the stack, running hooks if necessary.

Makes use of Plotmaker.plotmaker



142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/ctioga2/data/stack.rb', line 142

def add_dataset(dataset, ignore_hooks = false)
  @stack << dataset
  if @dataset_hook && (! ignore_hooks)
    # TODO: error handling
    begin
      PlotMaker.plotmaker.interpreter.run_commands(@dataset_hook)
    rescue Exception => e
      error "There was a problem running the dataset hook '#{@dataset_hook}', disabling it"
      @dataset_hook = nil
      info "-> '#{format_exception e}'"
    end
  end
end

#add_to_dataset_hook(commands) ⇒ Object

Appends a set of commands to the dataset hook



157
158
159
160
161
162
163
# File 'lib/ctioga2/data/stack.rb', line 157

def add_to_dataset_hook(commands)
  if @dataset_hook
    @dataset_hook << "\n#{commands}"
  else
    @dataset_hook = commands
  end
end

#concatenate_datasets(n = 2) ⇒ Object

Pops the last n datasets off the stack



176
177
178
179
180
181
182
183
184
185
# File 'lib/ctioga2/data/stack.rb', line 176

def concatenate_datasets(n = 2)
  ds = @stack.pop
  raise "Nothing on the stack" unless ds
  (n-1).times do
    ds2 = @stack.pop
    raise "Not enough datasets on the stack" unless ds2
    ds << ds2
  end
  @stack.push(ds)
end

#get_datasets(set, options = {}) ⇒ Object

Performs expansion on the given set with the current backend, retrieves corresponding Dataset objects, pushes them onto the stack and returns them.

options is a Hash that can contain the options available to the ‘load’ command:

  • ‘name’ to name each element added to the stack. A %d will be replaced by the number of the dataset within the ones just added.

Additional members of the Hash are simply ignored.



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/ctioga2/data/stack.rb', line 89

def get_datasets(set, options = {})
  backend = @backend_factory.current
  retval = []
  i = 0
  for s in backend.expand_sets(set)
    ds = backend.dataset(s)
    add_dataset(ds, options['ignore_hooks'])

    # Selection
    if options['where']
      ds.select_formula!(options['where'])
    end

    retval << ds
    if options['name']
      @named_datasets[options['name'] % [i]] = ds
    end
    i += 1
  end
  return retval
end

#lastObject

Returns the last Dataset pushed onto the stack.



188
189
190
# File 'lib/ctioga2/data/stack.rb', line 188

def last
  return @stack.last
end

Writes the contents of the the given dataset (a DataSet object) to the given io stream.



167
168
169
170
171
172
173
# File 'lib/ctioga2/data/stack.rb', line 167

def print_dataset(dataset, io)
  io.puts "# #{dataset.name}"
  io.puts "# #{dataset.column_names.join("\t")}"
  dataset.each_values do |i, *vals|
    io.puts vals.join("\t")
  end
end

#specified_dataset(options) ⇒ Object

Gets a dataset from the given options hash. If a ‘which’ key is present, it is used as an argument for #stored_dataset; else, -1 is used.



129
130
131
132
133
134
135
136
# File 'lib/ctioga2/data/stack.rb', line 129

def specified_dataset(options)
  spec = if options && options['which']
           options['which']
         else
           -1
         end
  return stored_dataset(spec)
end

#stored_dataset(spec) ⇒ Object

Returns the stored dataset, either using its index in the stack, or its name in the dataset.



113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/ctioga2/data/stack.rb', line 113

def stored_dataset(spec)
  if spec.is_a? Numeric or spec =~ /^\s*-?\d+\s*$/
    spec = spec.to_i
    return @stack[spec]
  else
    if @named_datasets.key? spec
      return @named_datasets[spec]
    else
      raise "Unkown named dataset from the stack: '#{spec}'"
    end
  end
end