mirror of https://github.com/gsi-upm/soil
Add rescheduling for received
parent
ee0c4517cb
commit
189836408f
@ -0,0 +1,21 @@
|
|||||||
|
from soil import Agent, Environment, Simulation, state
|
||||||
|
|
||||||
|
|
||||||
|
class NoopAgent(Agent):
|
||||||
|
num_calls = 0
|
||||||
|
|
||||||
|
@state(default=True)
|
||||||
|
def unique(self):
|
||||||
|
self.num_calls += 1
|
||||||
|
|
||||||
|
class NoopEnvironment(Environment):
|
||||||
|
num_agents = 100
|
||||||
|
|
||||||
|
def init(self):
|
||||||
|
self.add_agents(NoopAgent, k=self.num_agents)
|
||||||
|
self.add_agent_reporter("num_calls")
|
||||||
|
|
||||||
|
|
||||||
|
from _config import *
|
||||||
|
|
||||||
|
run_sim(model=NoopEnvironment)
|
@ -0,0 +1,30 @@
|
|||||||
|
from soil import Agent, Environment, Simulation, state
|
||||||
|
from soil.time import SoilentActivation
|
||||||
|
|
||||||
|
|
||||||
|
class NoopAgent(Agent):
|
||||||
|
num_calls = 0
|
||||||
|
|
||||||
|
@state(default=True)
|
||||||
|
async def unique(self):
|
||||||
|
while True:
|
||||||
|
self.num_calls += 1
|
||||||
|
# yield self.delay(1)
|
||||||
|
await self.delay()
|
||||||
|
|
||||||
|
|
||||||
|
class NoopEnvironment(Environment):
|
||||||
|
num_agents = 100
|
||||||
|
schedule_class = SoilentActivation
|
||||||
|
|
||||||
|
def init(self):
|
||||||
|
self.add_agents(NoopAgent, k=self.num_agents)
|
||||||
|
self.add_agent_reporter("num_calls")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
from _config import *
|
||||||
|
|
||||||
|
res = run_sim(model=NoopEnvironment)
|
||||||
|
for r in res:
|
||||||
|
assert isinstance(r.schedule, SoilentActivation)
|
File diff suppressed because one or more lines are too long
@ -1,6 +1,42 @@
|
|||||||
|
from functools import wraps
|
||||||
|
from .time import INFINITY
|
||||||
|
|
||||||
def report(f: property):
|
def report(f: property):
|
||||||
if isinstance(f, property):
|
if isinstance(f, property):
|
||||||
setattr(f.fget, "add_to_report", True)
|
setattr(f.fget, "add_to_report", True)
|
||||||
else:
|
else:
|
||||||
setattr(f, "add_to_report", True)
|
setattr(f, "add_to_report", True)
|
||||||
return f
|
return f
|
||||||
|
|
||||||
|
|
||||||
|
def syncify(func, method=True):
|
||||||
|
_coroutine = None
|
||||||
|
|
||||||
|
@wraps(func)
|
||||||
|
def wrapped(*args, **kwargs):
|
||||||
|
if not method:
|
||||||
|
nonlocal _coroutine
|
||||||
|
else:
|
||||||
|
_coroutine = getattr(args[0], "_coroutine", None)
|
||||||
|
_coroutine = _coroutine or func(*args, **kwargs)
|
||||||
|
try:
|
||||||
|
val = _coroutine.send(None)
|
||||||
|
except StopIteration as ex:
|
||||||
|
_coroutine = None
|
||||||
|
val = ex.value
|
||||||
|
finally:
|
||||||
|
if method:
|
||||||
|
args[0]._coroutine = _coroutine
|
||||||
|
return val
|
||||||
|
|
||||||
|
return wrapped
|
||||||
|
|
||||||
|
|
||||||
|
def while_alive(func):
|
||||||
|
@wraps(func)
|
||||||
|
def wrapped(self, *args, **kwargs):
|
||||||
|
if self.alive:
|
||||||
|
return func(self, *args, **kwargs)
|
||||||
|
return INFINITY
|
||||||
|
|
||||||
|
return wrapped
|
Loading…
Reference in New Issue