A restricted PythonPDEVS simulator modelled in SCCD
import gzip
import cPickle as pickle
import time
import os
# We basically just interface with the basesimulator
from scheduler import Scheduler
from DEVS import directConnect, CoupledDEVS, AtomicDEVS, RootDEVS
class Breakpoint(object):
def __init__(self, breakpoint_id, function, enabled, disable_on_trigger):
self.id = breakpoint_id
self.function = function
self.enabled = enabled
self.disable_on_trigger = disable_on_trigger
# Store only transitioned models, but all of them
INCREMENTAL = 0
# Store a full copy of every model at every transition step
FULL = 1
continue simulation
# -1 --> termination condition
# else --> breakpoint
if realtime:
check_time = self.simulation_time
else:
self.compute_timeNext()
check_time = self.timeNext
# Just access the 'transitioned' dictionary
# Kind of dirty though...
if self.transitioning is None:
transitioned = set()
else:
transitioned = set(self.transitioning.keys())
if check_time[0] == float('inf'):
# Always terminate if we have reached infinity
terminate = True
elif self.termination_condition is not None:
terminate = self.termination_condition(check_time, self.root_model, transitioned)
else:
terminate = self.termination_time < check_time
if terminate:
# Always terminate, so don't check breakpoints
return -1
else:
# Have to check breakpoints for termination
for bp in self.breakpoints:
if not bp.enabled:
continue
# Include the function in the scope
exec(bp.function)
# And execute it, note that the breakpoint thus has to start with "def breakpoint("
if breakpoint(check_time, self.root_model, transitioned):
# Triggered!
return bp.id
else:
# Not triggered, so continue
continue
return -2
]]>
self.simulation_time:
break
config = self.inject_queue.pop(0)
portname = config["port"]
event = config["event"]
port = self.find_port_with_name(portname)
self.inbags.setdefault(port, []).append(event)
]]>
internal transition
# 2 --> external transition
# 3 --> confluent transition
# These codes are a legacy of efficient PyPDEVS, but is kept here for consistency
self.transitioning = {model: 1 for model in self.imminents}
for inport in self.inbags:
aDEVS = inport.hostDEVS
aDEVS.myInput[inport] = self.inbags[inport]
if aDEVS in self.transitioning:
self.transitioning[aDEVS] = 3
else:
self.transitioning[aDEVS] = 2
]]>
@ %s\n" % (type, model.getModelFullName(), model.timeLast[0]))
self.trace_file.write(" NEW STATE <%s>\n" % (model.state))
if type != "EXTERNAL":
self.trace_file.write(" OUTPUTFNC returned %s\n" % model.myOutput)
elif type != "INTERNAL":
self.trace_file.write(" inputs were %s\n" % model.myInput)
self.trace_file.write(" timeNext: %s (ta: %s)\n" % (model.timeNext[0], model.timeNext[0] - model.timeLast[0]))
]]>
0:
self.timeNext = min(model_timeNext, self.inject_queue[0]["time"])
else:
self.timeNext = model_timeNext
]]>
= self.simulation_time:
return
if self.omniscient_technique == INCREMENTAL:
# We really have something to do
# For now, just unwrap this very inefficiently
for model in self.model.componentSet:
history = self.model_history[model.model_id]
if history[-1][0] <= simulation_time:
# No need to revert
continue
counter = len(history)
for e in reversed(history):
t, m = e
if t <= simulation_time:
# We came at the end: revert to this one
if isinstance(m, int):
with gzip.open("store/%s.dms" % m) as f:
m = f.read()
state = pickle.loads(m)
self.jump_model(model, t, state)
self.model_history[model.model_id] = self.model_history[model.model_id][:counter]
break
else:
counter -= 1
if isinstance(m, str):
self.used_memory -= len(m)
elif self.omniscient_technique == FULL:
# Just go over all versions and reset all
counter = len(self.model_history)
for e in reversed(self.model_history):
if e[0] <= simulation_time and e[1] is not None:
entry = e
break
else:
counter -= 1
if isinstance(e[1], str):
self.used_memory -= len(e[1])
#print("For %s use checkpoint %s" % (simulation_time, entry[0]))
self.model_history = self.model_history[:counter]
# Got the correct full entry
if isinstance(entry[1], int):
# Load from swapped
with gzip.open("store/%s.dms" % entry[1], "rb") as f:
entry = entry[0], f.read()
d = pickle.loads(entry[1])
for model in self.model.componentSet:
tl, state = d[model.model_id]
self.jump_model(model, tl, state)
self.simulation_time = simulation_time
]]>