Class: PgVerify::Interpret::ComponentContext

Inherits:
Object
  • Object
show all
Defined in:
lib/pg-verify/interpret/component_context.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, parent_graph) ⇒ ComponentContext

Returns a new instance of ComponentContext.



31
32
33
34
35
36
37
38
# File 'lib/pg-verify/interpret/component_context.rb', line 31

def initialize(name, parent_graph)
    @name, @parent_graph = name, parent_graph
    @states_list, @transition_list = [], []
    @owned_variables = []
    @init_expressions = []
    @represents_fault = false
    @source_location = parent_graph.parent_script.find_source_location()
end

Instance Attribute Details

#init_expressionsObject

Returns the value of attribute init_expressions.



29
30
31
# File 'lib/pg-verify/interpret/component_context.rb', line 29

def init_expressions
  @init_expressions
end

#nameObject

Name of this component as a symbol



8
9
10
# File 'lib/pg-verify/interpret/component_context.rb', line 8

def name
  @name
end

#owned_variablesObject

The list of variables which is component has declared and thus owns



18
19
20
# File 'lib/pg-verify/interpret/component_context.rb', line 18

def owned_variables
  @owned_variables
end

#parent_graphObject

Back reference to the parent graph



21
22
23
# File 'lib/pg-verify/interpret/component_context.rb', line 21

def parent_graph
  @parent_graph
end

#represents_faultObject

Flag on whether this component is used to represent a fault.



24
25
26
# File 'lib/pg-verify/interpret/component_context.rb', line 24

def represents_fault
  @represents_fault
end

#source_locationObject

The source location of this components definition



27
28
29
# File 'lib/pg-verify/interpret/component_context.rb', line 27

def source_location
  @source_location
end

#states_listObject

The list of states of this component as symbols



11
12
13
# File 'lib/pg-verify/interpret/component_context.rb', line 11

def states_list
  @states_list
end

#transition_listObject

The list of transitions of this component



14
15
16
# File 'lib/pg-verify/interpret/component_context.rb', line 14

def transition_list
  @transition_list
end

Instance Method Details

#init(string) ⇒ Object



96
97
98
# File 'lib/pg-verify/interpret/component_context.rb', line 96

def init(string)
    @init_expressions << string
end

#state(state) ⇒ Object

DSL method for the definition of one state



77
78
79
# File 'lib/pg-verify/interpret/component_context.rb', line 77

def state(state)
    states(state)
end

#states(*states, init: nil) ⇒ Object

DSL method to define states which this component can have



66
67
68
69
70
71
72
73
74
# File 'lib/pg-verify/interpret/component_context.rb', line 66

def states(*states, init: nil)
    states.each { |state|
        raise InvalidDSL_state.new("State '#{state}' is neither a string or symbol") unless state.is_a?(String) || state.is_a?(Symbol)
        raise InvalidDSL_state.new("State '#{state}' was already declared for component '#{name}'") if @states_list.include?(state)
        raise InvalidDSL_state.new("Initial state '#{init}' is none of #{state}") unless init.nil? || states.include?(init)
    }
    @states_list += states.map(&:to_sym)
    self.init("#{@name} == #{init}") unless init.nil?
end

#to_modelObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/pg-verify/interpret/component_context.rb', line 100

def to_model()
    params = {}
    # Use the name as is
    params[:name] = @name
    # Use states as is
    params[:states] = @states_list
    # Convert the transitions to their model representation
    params[:transitions] = @transition_list.map(&:to_model)
    params[:represents_fault] = @represents_fault
    # Pass the source location
    params[:source_location] = @source_location
    # Pass the init expression
    unless @init_expressions.empty?
        init_expression = @init_expressions.map { |e| "( #{e} )" }.join(" && ")
        init_expression = Model::ParsedExpression.new(init_expression, Model::ParsedExpression::TYPE_PL) 
        params[:init_expression] = init_expression
    end
    
    # Create a model component using these params
    Model::Component.new(params)
end

#transition(hash, &blk) ⇒ Object

DSL method for the decleration of a transition between states of this component



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/pg-verify/interpret/component_context.rb', line 82

def transition(hash, &blk)
    raise InvalidDSL_transition.new("Invalid argument '#{hash}'") unless hash.is_a?(Hash)
    from = hash.keys.first
    to =  hash[from]
    [ from, to ].each { |state| 
        next if @states_list.include?(state)
        raise NoSuchStateError.new(state, self)
    }
    transition = TransitionContext.new(self, from, to)
    transition.instance_eval(&blk) unless blk.nil?
    @transition_list << transition
    return transition
end

#var(hash) ⇒ Object

DSL method for declaring a variable to be owned by this component

Raises:



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/pg-verify/interpret/component_context.rb', line 41

def var(hash)
    raise InvalidDSL_var.new("Invalid argument '#{hash}'") unless hash.is_a?(Hash)
    name = hash.keys.first
    range = hash[name]
    raise "Variable name must be different to the component that owns it" if name.to_sym == @name
    raise InvalidDSL_var.new("Name '#{name}' is not a symbol") unless name.is_a?(Symbol)
    raise InvalidDSL_var.new("Range '#{range}' is not a range or array") unless (range.is_a?(Range) || range.is_a?(Array)) && range.first.is_a?(Integer)
    sloc = @parent_graph.parent_script.find_source_location()
    variable = Model::Variable.new(name, range, @name, sloc)

    # The "init" argument can be an expression, or a value out of the
    # range of that variable
    if hash.key?(:init)
        init_expr = variable.values().include?(hash[:init]) \
            ? "#{variable.name} == #{hash[:init]}" \
            : hash[:init]
        raise "Neither a string, nor a valid initial value: #{hash[:init]}" unless init_expr.is_a?(String)
        variable.init_expression = Model::ParsedExpression.new(init_expr, Model::ParsedExpression::TYPE_PL)
    end

    @owned_variables << variable
    return variable
end