2014-05-17 20:08:50 +08:00
|
|
|
from operator import itemgetter
|
|
|
|
|
2014-05-31 01:01:27 +08:00
|
|
|
from artiq.language.units import *
|
2014-07-11 00:13:37 +08:00
|
|
|
from artiq.language import core as core_language
|
2014-05-17 20:08:50 +08:00
|
|
|
|
2014-09-05 12:03:22 +08:00
|
|
|
|
2014-05-17 20:08:50 +08:00
|
|
|
class SequentialTimeContext:
|
2014-09-05 12:03:22 +08:00
|
|
|
def __init__(self, current_time):
|
|
|
|
self.current_time = current_time
|
|
|
|
self.block_duration = 0*s
|
|
|
|
|
|
|
|
def take_time(self, amount):
|
|
|
|
self.current_time += amount
|
|
|
|
self.block_duration += amount
|
2014-05-17 20:08:50 +08:00
|
|
|
|
|
|
|
|
|
|
|
class ParallelTimeContext:
|
2014-09-05 12:03:22 +08:00
|
|
|
def __init__(self, current_time):
|
|
|
|
self.current_time = current_time
|
|
|
|
self.block_duration = 0*s
|
|
|
|
|
|
|
|
def take_time(self, amount):
|
|
|
|
if amount > self.block_duration:
|
|
|
|
self.block_duration = amount
|
2014-05-17 20:08:50 +08:00
|
|
|
|
|
|
|
|
2014-05-29 04:42:01 +08:00
|
|
|
class Manager:
|
2014-09-05 12:03:22 +08:00
|
|
|
def __init__(self):
|
|
|
|
self.stack = [SequentialTimeContext(0*s)]
|
|
|
|
self.timeline = []
|
|
|
|
|
|
|
|
def enter_sequential(self):
|
|
|
|
new_context = SequentialTimeContext(self.get_time())
|
|
|
|
self.stack.append(new_context)
|
|
|
|
|
|
|
|
def enter_parallel(self):
|
|
|
|
new_context = ParallelTimeContext(self.get_time())
|
|
|
|
self.stack.append(new_context)
|
|
|
|
|
|
|
|
def exit(self):
|
|
|
|
old_context = self.stack.pop()
|
|
|
|
self.take_time(old_context.block_duration)
|
|
|
|
|
|
|
|
def take_time(self, duration):
|
|
|
|
self.stack[-1].take_time(duration)
|
|
|
|
|
|
|
|
def get_time(self):
|
|
|
|
return self.stack[-1].current_time
|
|
|
|
|
|
|
|
def set_time(self, t):
|
|
|
|
dt = t - self.get_time()
|
|
|
|
if dt < 0*s:
|
|
|
|
raise ValueError("Attempted to go back in time")
|
|
|
|
self.take_time(dt)
|
|
|
|
|
|
|
|
def event(self, description):
|
|
|
|
self.timeline.append((self.get_time(), description))
|
|
|
|
|
|
|
|
def format_timeline(self):
|
|
|
|
r = ""
|
|
|
|
prev_time = 0*s
|
|
|
|
for time, description in sorted(self.timeline, key=itemgetter(0)):
|
|
|
|
r += "@{:10} (+{:10}) ".format(str(time), str(time-prev_time))
|
|
|
|
for item in description:
|
|
|
|
r += "{:16}".format(str(item))
|
|
|
|
r += "\n"
|
|
|
|
prev_time = time
|
|
|
|
return r
|
2014-05-17 20:08:50 +08:00
|
|
|
|
2014-05-29 04:42:01 +08:00
|
|
|
manager = Manager()
|
2014-07-11 00:13:37 +08:00
|
|
|
core_language.set_time_manager(manager)
|