cirrocumulus 0.4.6 → 0.5.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,205 +0,0 @@
1
- module Agent
2
- class AgentInfo
3
- attr_accessor :identifier
4
- attr_accessor :default_ontology
5
- attr_accessor :last_seen_at
6
-
7
- def initialize
8
- @identifier = @default_ontology = @last_seen_at = nil
9
- end
10
- end
11
-
12
- class NetworkMap
13
-
14
- INVALIDATE_PERIOD = 600*2
15
-
16
- attr_reader :agents
17
- attr_accessor :version
18
- attr_reader :valid
19
-
20
- def initialize(agent)
21
- Log4r::Logger['agent'].debug "initializing empty network map"
22
- @agent = agent
23
- @agents = []
24
- @version = 0
25
- @valid = 0
26
- end
27
-
28
- def tick(cm)
29
- @valid -= 1
30
- if @valid <= 0
31
- Log4r::Logger['agent'].debug "invalidating network map"
32
- msg = Cirrocumulus::Message.new(nil, 'request', [:update, [:map, @version.to_s]])
33
- msg.ontology = 'cirrocumulus-map'
34
- cm.send(msg)
35
- @valid = INVALIDATE_PERIOD
36
- end
37
- end
38
-
39
- def handle_message(message, cm)
40
- if message.ontology == 'cirrocumulus-map'
41
- return if !message.receiver.blank? && (message.receiver != @agent.identifier)
42
- #p message
43
-
44
- if message.act == 'inform'
45
- if message.content.first == :"="
46
- ontology = message.content[2].first
47
- agent = @agents.find {|a| a.identifier == message.sender}
48
- if agent
49
- agent.default_ontology = ontology
50
- agent.last_seen_at = Time.now.to_i
51
- @version = Time.now.to_i
52
- Log4r::Logger['agent'].info "got neighbour details: #{agent.inspect}"
53
- end
54
- else
55
- agent_info = message.content
56
- agent = AgentInfo.new
57
- agent_info.each do |param|
58
- if param.first == :identifier
59
- agent.identifier = param.second
60
- elsif param.first == :default_ontology
61
- agent.default_ontology = param.second
62
- elsif param.first == :last_seen_at
63
- agent.last_seen_at = param.second.to_i
64
- end
65
- end
66
-
67
- my_agent = @agents.find {|a| a.identifier == agent.identifier}
68
- if my_agent.nil?
69
- my_agent = agent
70
- @agents << agent
71
- else
72
- my_agent.last_seen_at = [my_agent.last_seen_at, agent.last_seen_at].max
73
- my_agent.default_ontology = agent.default_ontology || my_agent.default_ontology
74
- end
75
-
76
- Log4r::Logger['agent'].info "neighbour updated: #{my_agent.inspect}"
77
- @version = Time.now.to_i
78
- end
79
- elsif message.act == 'request'
80
- map_request = message.content
81
- foreign_map = map_request.second
82
- foreign_map_version = foreign_map.second.to_i
83
- if @version >= foreign_map_version
84
- @agents.each do |agent|
85
- next if agent.default_ontology.blank?
86
- msg = Cirrocumulus::Message.new(nil, 'inform', [
87
- [:identifier, agent.identifier],
88
- [:default_ontology, agent.default_ontology],
89
- [:last_seen_at, agent.last_seen_at.to_s]
90
- ])
91
- msg.receiver = message.sender
92
- msg.ontology = 'cirrocumulus-map'
93
- cm.send(msg)
94
- end
95
- end
96
-
97
- process_agent(message, cm)
98
- elsif message.act == 'query-ref'
99
- #p message.content
100
- if message.content.first == :default_ontology
101
- msg = Cirrocumulus::Message.new(nil, 'inform', [:'=', [:default_ontology], [@agent.default_ontology]])
102
- msg.receiver = message.sender
103
- msg.ontology = 'cirrocumulus-map'
104
- cm.send(msg)
105
- end
106
- end
107
- else
108
- process_agent(message, cm)
109
- end
110
- end
111
-
112
- private
113
-
114
- def process_agent(message, cm)
115
- agent_id = message.sender
116
- return if agent_id == @agent.identifier
117
-
118
- agent = @agents.find {|a| a.identifier == agent_id}
119
- if agent
120
- agent.last_seen_at = Time.now.to_i
121
- @version = Time.now.to_i
122
- else
123
- agent = AgentInfo.new
124
- agent.identifier = agent_id
125
- agent.last_seen_at = Time.now.to_i
126
- Log4r::Logger['agent'].info "discovered neighbour: #{agent.identifier}"
127
- @agents << agent
128
- @version = Time.now.to_i
129
-
130
- msg = Cirrocumulus::Message.new(nil, 'query-ref', [:default_ontology])
131
- msg.receiver = agent.identifier
132
- msg.ontology = 'cirrocumulus-map'
133
- cm.send(msg)
134
- end
135
- end
136
- end
137
-
138
- class Base
139
- attr_reader :identifier
140
- attr_reader :network_map
141
-
142
- def initialize(cm)
143
- Log4r::Logger['agent'].info('Initializing new agent')
144
-
145
- @cm = cm
146
- @identifier = cm.jid
147
- @ontologies = []
148
- @network_map = NetworkMap.new(self)
149
- end
150
-
151
- def load_ontologies(ontologies_list)
152
- ontologies_list.each do |ontology_name|
153
- ontology = eval("#{ontology_name}.new(self)")
154
- self.ontologies << ontology
155
- end
156
- end
157
-
158
- def default_ontology
159
- self.ontologies.size == 1 ? self.ontologies.first.name : nil
160
- end
161
-
162
- def handles_ontology?(ontology_name)
163
- self.ontologies.any? {|ontology| ontology.name == ontology_name}
164
- end
165
-
166
- def restore_state()
167
- self.ontologies.each do |ontology|
168
- begin
169
- ontology.restore_state()
170
- rescue Exception => e
171
- Log4r::Logger['agent'].warn "failed to restore state for ontology %s" % ontology.name
172
- Log4r::Logger['agent'].warn e.backtrace.to_s
173
- end
174
- end
175
- end
176
-
177
- def send_message(message)
178
- @cm.send(message)
179
- end
180
-
181
- def tick()
182
- @network_map.tick(@cm)
183
-
184
- self.ontologies.each {|ontology| ontology.tick() }
185
- end
186
-
187
- def handle_message(message, kb)
188
- @network_map.handle_message(message, @cm)
189
- self.ontologies.each {|ontology|
190
- if message.ontology == ontology.name || ontology.sagas.any? {|saga| saga.id == message.in_reply_to}
191
- ontology.handle_incoming_message(message, kb)
192
- end
193
- }
194
- rescue Exception => e
195
- Log4r::Logger['agent'].warn "failed to handle incoming message: %s" % e.to_s
196
- puts e.backtrace.to_s
197
- end
198
-
199
- protected
200
-
201
- attr_reader :cm
202
- attr_reader :ontologies
203
-
204
- end
205
- end
@@ -1,56 +0,0 @@
1
- require 'cirrocumulus/rule_engine.rb'
2
- require 'cirrocumulus/rule_server.rb'
3
-
4
- class Test < RuleEngine::Base
5
- rule 'convert', [[:temperature, :X, 'F']] do |engine, params|
6
- puts "qqq"
7
- x = params[:X].to_i
8
- engine.retract([:temperature, x, 'F'])
9
- y = 5*(x - 32)/9
10
- engine.assert([:temperature, y, 'C'])
11
- end
12
-
13
- rule 'guest_powered_off', [[:guest, :X, :powered_off]] do |engine, params|
14
- puts "guest_powered_off"
15
- end
16
-
17
- rule 'monitor_md', [[:virtual_disk, :X, :active], [:mdraid, :X, :failed]] do |engine, params|
18
- # md devices is failed, but virtual disk should be up
19
- p params
20
- puts "virtual disk #{params[:X]} should be up, but corresponding md devices is failed!"
21
- end
22
- end
23
-
24
- #RuleEngine::Server.run()
25
-
26
- e = Test.new
27
- e.assert [:guest, "233bed174ab0802fd908f981d64d185b", :powered_off]
28
- e.assert [:guest, "233bed174ab0802fd908f981d64d185b", :running]
29
- e.assert [:guest, "233bed174ab0802fd908f981d64d185b", :state, :powered_on]
30
- e.replace [:guest, "233bed174ab0802fd908f981d64d185b", :state, :STATE], :powered_off
31
-
32
- p e.match [:guest, "233bed174ab0802fd908f981d64d185b", :running]
33
- gets
34
- exit(0)
35
- e.assert [:virtual_disk, 163, :active]
36
- #e.assert [:virtual_disk, 139, :active]
37
- #e.assert [:virtual_disk, 145, :active]
38
- #e.assert [:virtual_disk, 146, :active]
39
- #e.assert [:virtual_disk, 149, :active]
40
- e.assert [:virtual_disk, 153, :active]
41
- #e.assert [:virtual_disk, 154, :active]
42
- #e.assert [:virtual_disk, 156, :active]
43
- e.assert [:virtual_disk, 158, :active]
44
- #e.assert [:virtual_disk, 137, :active]
45
- #e.assert [:virtual_disk, 135, :active]
46
- #e.assert [:virtual_disk, 159, :active]
47
- #e.assert [:virtual_disk, 103, :active]
48
- #e.assert [:virtual_disk, 102, :active]
49
- #e.assert [:virtual_disk, 20, :active]
50
- #e.assert [:virtual_disk, 2, :active]
51
- #e.assert [:virtual_disk, 777, :active]
52
- #e.assert [:virtual_disk, 90, :active]
53
- e.assert [:mdraid, 153, :failed], true
54
- e.assert [:mdraid, 158, :failed], true
55
- e.execute()
56
- gets