File: Event.py

package info (click to toggle)
cain 1.10%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 29,856 kB
  • sloc: cpp: 49,612; python: 14,988; xml: 11,654; ansic: 3,644; makefile: 133; sh: 2
file content (45 lines) | stat: -rw-r--r-- 1,538 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
"""Implements the Event class."""

from math import *

class Event:

    def __init__(self, model, assignmentString=''):
        self.model = model
        self.assignmentString = assignmentString

    def initialize(self):
        self.count = 0
        # Build the list of assignments.
        self.assignments = []
        if self.assignmentString:
            for assignment in self.assignmentString.split(';'):
                s = assignment.split('=')
                if len(s) != 2:
                    raise Exception('Bad assignment "' + assignment +
                                    '" in Event.')
                self.assignments.append((s[0].strip(),
                                         lambda m, e=self.model.decorate(s[1]):
                                         eval(e)))

    def fire(self):
        self.count += 1
        m = self.model
        for (id, f) in self.assignments:
            if id in m.species:
                m.species[id].amount = f(m)
            elif id in m.parameters:
                m.parameters[id].value = f(m)
            else:
                raise Exception('The identifier ' + id + ' in the Event'\
                                ' is neither a species nor a parameter.')

def makeTriggerTimeEvent(event):
    """From the specified event make a new event that assigns the values at
    the current time."""
    e = Event(event.model)
    e.initialize()
    for (id, f) in event.assignments:
        v = f(event.model)
        e.assignments.append((id, lambda m: v))
    return e