1
0
mirror of https://github.com/gsi-upm/soil synced 2024-11-10 13:42:28 +00:00
soil/models/BaseBehaviour/BaseBehaviour.py

39 lines
1.0 KiB
Python
Raw Normal View History

2017-04-20 11:36:17 +00:00
import settings
from nxsim import BaseNetworkAgent
from .. import networkStatus
class BaseBehaviour(BaseNetworkAgent):
def __init__(self, environment=None, agent_id=0, state=()):
super().__init__(environment=environment, agent_id=agent_id, state=state)
self._attrs = {}
@property
def attrs(self):
now = self.env.now
if now not in self._attrs:
self._attrs[now] = {}
return self._attrs[now]
@attrs.setter
def attrs(self, value):
self._attrs[self.env.now] = value
def run(self):
while True:
self.step(self.env.now)
yield self.env.timeout(settings.network_params["timeout"])
2017-04-20 11:36:17 +00:00
def step(self, now):
networkStatus['agent_%s'% self.id] = self.to_json()
def to_json(self):
final = {}
for stamp, attrs in self._attrs.items():
for a in attrs:
if a not in final:
2017-04-21 11:55:42 +00:00
final[a] = {}
2017-04-20 11:36:17 +00:00
final[a][stamp] = attrs[a]
2017-04-21 11:55:42 +00:00
return final