Class: Sfp::Runtime

Inherits:
Object
  • Object
show all
Defined in:
lib/sfpagent/runtime.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ Runtime

Returns a new instance of Runtime.



6
7
8
9
10
11
# File 'lib/sfpagent/runtime.rb', line 6

def initialize(model)
	@mutex_procedure = Mutex.new
	@mutex_get_state = Mutex.new
	@root = nil
	set_model(model)
end

Instance Attribute Details

#rootObject (readonly)

Returns the value of attribute root.



4
5
6
# File 'lib/sfpagent/runtime.rb', line 4

def root
  @root
end

Instance Method Details

#execute_action(action) ⇒ Object

Raises:

  • (Exception)


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/sfpagent/runtime.rb', line 18

def execute_action(action)
	return false if !defined?(@root) or @root.nil?

	def normalise_parameters(params)
		p = {}
		params.each { |k,v| p[k[2,k.length-2]] = v }
		p
	end

	module_path, method_name = action['name'].pop_ref
	mod = @root.at?(module_path)[:_self]
	raise Exception, "Module #{module_path} cannot be found!" if mod.nil?
	raise Exception, "Cannot execute #{action['name']}!" if not mod.respond_to?(method_name)

	params = normalise_parameters(action['parameters'])
	if mod.synchronized.rindex(method_name)
		@mutex_procedure.synchronize { mod.send method_name.to_sym, params }
	else
		mod.send method_name.to_sym, params
	end

	# TODO - check post-execution state for verification
end

#get_state(as_sfp = false) ⇒ Object



55
56
57
58
59
60
# File 'lib/sfpagent/runtime.rb', line 55

def get_state(as_sfp=false)
	@mutex_get_state.synchronize {
		update_state(@root)
		get_object_state(@root, @model)
	}
end

#normalise_parameters(params) ⇒ Object



21
22
23
24
25
# File 'lib/sfpagent/runtime.rb', line 21

def normalise_parameters(params)
	p = {}
	params.each { |k,v| p[k[2,k.length-2]] = v }
	p
end

#set_model(model) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/sfpagent/runtime.rb', line 42

def set_model(model)
	@mutex_get_state.synchronize {
		@model = model
		if @model.is_a?(Hash)
			root_model = Sfp::Helper.deep_clone(@model)
			root_model.accept(SFPtoRubyValueConverter)
			root_model.accept(ParentEliminator)
			@root = update_model(root_model, root_model, '$')
			@root.accept(ParentGenerator)
		end
	}
end

#whoami?Boolean

Returns:

  • (Boolean)


13
14
15
16
# File 'lib/sfpagent/runtime.rb', line 13

def whoami?
	@model.each { |key,value| return key if key[0,1] != '_' and value['_context'] == 'object' } if !@model.nil?
	nil
end