Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Moved clock out.

  • Loading branch information...
commit 5e814460af91367e22f6309d6d40dbc4bfdd1374 1 parent c44269d
@rik0 authored
View
2  examples/SIR_model.py
@@ -6,8 +6,8 @@
from pynetsym import Node
from pynetsym import Agent
from pynetsym.activator import Activator
+from pynetsym.clock import BaseClock
-from pynetsym.simulation import BaseClock
from pynetsym.configurators import NXGraphConfigurator
from pynetsym.termination.conditions import always_true
View
3  pynetsym/__init__.py
@@ -17,12 +17,11 @@
__all__.extend(configurators.__all__)
from .core import Agent, AgentError, MinimalAgentRuntime, Logger, get_logger
+from .clock import AsyncClock, Clock
from .addressing import AddressingError
from .agent_db import MissingNode, AgentDB
from .node_manager import NodeManager
from .nodes import Node
from .simulation import Simulation
-from .simulation import Clock, AsyncClock
-from .configurators import IConfigurator, AbstractConfigurator, BasicConfigurator, Either
from .error import PyNetSymError
from .activator import Activator
View
9 pynetsym/activator.py
@@ -1,8 +1,9 @@
-from pynetsym import core, NodeManager
-from pynetsym.termination import TerminationChecker
-from pynetsym.util import gather_from_ancestors, extract_subdictionary, SequenceAsyncResult
+from .core import Agent
+from .node_manager import NodeManager
+from .termination import TerminationChecker
+from .util import gather_from_ancestors, extract_subdictionary, SequenceAsyncResult
-class Activator(core.Agent):
+class Activator(Agent):
"""
The Activator chooses what happens at each step of the simulation.
View
80 pynetsym/clock.py
@@ -0,0 +1,80 @@
+import gevent
+from gevent.greenlet import LinkedCompleted
+from traits.api import false, Instance, true, List, Int
+
+from .core import Agent
+from .termination import TerminationChecker
+from .activator import Activator
+
+class BaseClock(Agent):
+ name = 'clock'
+ activator_can_terminate = false()
+ clock_loop_g = Instance(gevent.Greenlet)
+
+ active = true(transient=True)
+ observers = List
+
+ def register_observer(self, name):
+ self.observers.append(name)
+
+ def unregister_observer(self, name):
+ self.observers.remove(name)
+
+ def join(self):
+ try:
+ return super(BaseClock, self).join()
+ except LinkedCompleted:
+ return True
+
+ def start_clock(self):
+ """
+ Here the clock actually starts ticking.
+ """
+ self.active = True
+ self.clock_loop_g = gevent.spawn_link(self.clock_loop)
+
+ def positive_termination(self, originator, motive):
+ if originator == TerminationChecker.name:
+ self.clock_loop_g.join()
+
+ def clock_loop(self):
+ raise NotImplementedError()
+
+ def send_tick(self):
+ for observer in self.observers:
+ self.send(observer, 'ticked')
+ return self.send(Activator.name, 'tick')
+
+ def send_simulation_ended(self):
+ return self.send(Activator.name, 'simulation_ended')
+
+ def simulation_end(self):
+ self.active = False
+ self.send_simulation_ended().get()
+
+ def ask_to_terminate(self):
+ return self.send(
+ TerminationChecker.name, 'check',
+ requester=self.name)
+
+
+class AsyncClock(BaseClock):
+ remaining_ticks = Int
+
+ def clock_loop(self):
+ while self.remaining_ticks:
+ self.send_tick()
+ self.remaining_ticks -= 1
+ else:
+ self.simulation_end()
+
+
+class Clock(BaseClock):
+ def clock_loop(self):
+ while self.active:
+ waiting = self.send_tick()
+ waiting.get()
+ should_terminate = self.ask_to_terminate()
+ self.active = not should_terminate.get()
+ else:
+ self.simulation_end()
View
5 pynetsym/configurators/basic.py
@@ -1,13 +1,12 @@
-import collections
from traits.has_traits import implements
from traits.trait_types import Dict, Str, Int, Any
-from .. import core
+from ..core import Agent
from ..util import gather_from_ancestors, extract_subdictionary, SequenceAsyncResult
from ..node_manager import NodeManager
from .interface import IConfigurator
-class AbstractConfigurator(core.Agent):
+class AbstractConfigurator(Agent):
implements(IConfigurator)
name = 'configurator'
View
89 pynetsym/simulation.py
@@ -2,15 +2,6 @@
import sys
import operator
-import gevent
-from gevent.greenlet import LinkedCompleted
-
-from traits.api import Int
-from traits.api import List
-from traits.api import true
-from traits.api import false
-from traits.api import Instance
-
from pynetsym import addressing
from pynetsym import graph
from pynetsym import agent_db
@@ -18,7 +9,9 @@
from pynetsym import core
from pynetsym import termination
from pynetsym import timing
+
from pynetsym.activator import Activator
+from pynetsym.clock import Clock
from pynetsym.util import gather_from_ancestors
from pynetsym.util import classproperty
@@ -31,84 +24,9 @@
__all__ = [
'Simulation',
- 'AsyncClock',
- 'Clock'
]
-class BaseClock(core.Agent):
- name = 'clock'
- activator_can_terminate = false()
- clock_loop_g = Instance(gevent.Greenlet)
-
- active = true(transient=True)
- observers = List
-
- def register_observer(self, name):
- self.observers.append(name)
-
- def unregister_observer(self, name):
- self.observers.remove(name)
-
- def join(self):
- try:
- return super(BaseClock, self).join()
- except LinkedCompleted:
- return True
-
- def start_clock(self):
- """
- Here the clock actually starts ticking.
- """
- self.active = True
- self.clock_loop_g = gevent.spawn_link(self.clock_loop)
-
- def positive_termination(self, originator, motive):
- if originator == TerminationChecker.name:
- self.clock_loop_g.join()
-
- def clock_loop(self):
- raise NotImplementedError()
-
- def send_tick(self):
- for observer in self.observers:
- self.send(observer, 'ticked')
- return self.send(Activator.name, 'tick')
-
- def send_simulation_ended(self):
- return self.send(Activator.name, 'simulation_ended')
-
- def simulation_end(self):
- self.active = False
- self.send_simulation_ended().get()
-
- def ask_to_terminate(self):
- return self.send(
- termination.TerminationChecker.name, 'check',
- requester=self.name)
-
-
-class AsyncClock(BaseClock):
- remaining_ticks = Int
-
- def clock_loop(self):
- while self.remaining_ticks:
- self.send_tick()
- self.remaining_ticks -= 1
- else:
- self.simulation_end()
-
-
-class Clock(BaseClock):
- def clock_loop(self):
- while self.active:
- waiting = self.send_tick()
- waiting.get()
- should_terminate = self.ask_to_terminate()
- self.active = not should_terminate.get()
- else:
- self.simulation_end()
-
class Simulation(object):
"""
A subclass of Simulation describes a specific kind of simulation.
@@ -375,9 +293,6 @@ def run(self, args=None, force_cli=False, **kwargs):
def exception_hook(self, node):
raise node.exception
- def output_processor(self, processor, *additional_arguments):
- self.graph.output_processor(processor, *additional_arguments)
-
@property
def handle(self):
return self.graph.handle
View
3  pynetsym/util/animation.py
@@ -5,6 +5,7 @@
from pynetsym import simulation
from matplotlib import pyplot as plt
+from pynetsym.clock import Clock
from pynetsym.util import sna, degrees_to_hist
@@ -20,7 +21,7 @@ def create_directory(self):
os.mkdir(self.directory)
def register_to_clock(self):
- self.send(simulation.Clock.name, 'register_observer', self.id)
+ self.send(Clock.name, 'register_observer', self.id)
def ticked(self):
filename = 'frame%d.png' % self.counter
Please sign in to comment.
Something went wrong with that request. Please try again.