Class: ValueGraphTransformation::Context

Inherits:
Graph::Graph show all
Defined in:
lib/value_graph_transformation/context.rb

Overview

A context consist of a set of functions, values and their relationships.

Specifications

is a Graph

Instance Attribute Summary

Attributes inherited from Graph::Graph

#edges, #vertices

Instance Method Summary (collapse)

Methods inherited from Graph::Graph

#add, #connect, #delete, #disconnect

Constructor Details

- (Context) initialize

Returns a new instance of Context



5
6
7
8
# File 'lib/value_graph_transformation/context.rb', line 5

def initialize
  super
  @identifiers = {}
end

Instance Method Details

- (Context) apply(&arithmetic)

Applies a block with arithmetic operations in this Context.

Parameters:

  • arithmetic (Proc)

    the block that will be invoked within this Context.

Returns:

See Also:



45
46
47
# File 'lib/value_graph_transformation/context.rb', line 45

def apply(&arithmetic)
  Arithmetic.apply(self, &arithmetic)
end

- (Object) function(type, input, output)

Parameters:

  • type (Class)

    the Function class to instantiate.

  • input (Array<Value>)

    the input values.

  • output (Array<Value>)

    the output values.

Specifications:

should fail unless the type is a class that inherits from Function

should fail unless all input objects are Value objects

should fail unless all output objects are Value objects

should return a Function

should connect the input and output vertices

should add the function to the context's list of vertices



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/value_graph_transformation/context.rb', line 13

def function(type, input, output)
  fail "'#{type}' is not a function." unless type <= Function
  fail "input not Value objects" unless input.all?{|value| value.is_a?(Value)}
  fail "output not Value objects" unless output.all?{|value| value.is_a?(Value)}

  function = type.new
  add(function)
  input.each{|value| connect(value, function) }
  output.each{|value| connect(function, value) }
  function
end

- (Object) to_dot

return [String] the context in dot code.



50
51
52
# File 'lib/value_graph_transformation/context.rb', line 50

def to_dot
  DotCompiler.new(self).to_dot
end

- (Value) value_for(identifier = nil)

Creates a Value node, or returns the existing one if the given identifier is already defined in this Context. If no identifier is given it will create a new Value and return that.

Parameters:

  • identifier (Object) (defaults to: nil)

    the identifier.

Returns:

  • (Value)

    the Value node for the identifier in this Context.

Specifications:

should return a Value object

should return the same Value if identifier has been given earlier

should always create new Value objects if nil is given as identifier



31
32
33
34
35
36
37
38
39
# File 'lib/value_graph_transformation/context.rb', line 31

def value_for(identifier=nil)
  value = @identifiers[identifier]
  if value.nil? then
    value = Value.new(identifier)
    add(value)
    @identifiers[identifier] = value unless identifier.nil?
  end
  value
end