-
Notifications
You must be signed in to change notification settings - Fork 0
/
MiddleSample.py
executable file
·120 lines (97 loc) · 3.25 KB
/
MiddleSample.py
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# -*- coding: utf-8 -*-
"""
-------------------------------------------------------------------------------
Name: <MiddleSample.py>
Model: A model for a lamp with some basic strategy
Authors: Raed Chammam
Organization: unice Polytech Sophia
Date: <1992-02-20>
License: GPL
-------------------------------------------------------------------------------
"""
### Specific import ------------------------------------------------------------
from DomainInterface.DomainBehavior import DomainBehavior
from DomainInterface.Object import Message
from transitions import Machine
from transitions import State
### Model class ----------------------------------------------------------------
class MiddleSample(DomainBehavior):
''' DEVS Class for MiddleSample model
'''
def __init__(self):
''' Constructor.
'''
DomainBehavior.__init__(self)
self.state = { 'status': 'IDLE', 'sigma':INFINITY}
self.proc = 0
self.x = {}
self.y = {}
self.pos = [-1]*100
''' The object '''
self.lump = Lamp()
# The states
self.states = [
State(name='on', on_enter=['say_im_turned_on']),
State(name='off', on_enter=['say_im_turned_off'])
]
self.transitions = [
{'trigger': 'illuminate', 'source': 'off', 'dest': 'on'},
{'trigger': 'darken', 'source': 'on', 'dest': 'off'}
]
# Initialize
self.machine = Machine(self.lump, states=self.states, transitions=self.transitions, initial='off')
def extTransition(self):
''' DEVS external transition function.
'''
for i in xrange(len(self.IPorts)):
msg = self.peek(self.IPorts[i])
if msg:
print "---- EXTERNAL "+ str(msg) +" ----"
self.x[i] = msg
if self.state['status'] == 'ACTIVE':
self.state['sigma'] = 0
else:
self.state = { 'status': 'ACTIVE', 'sigma':self.proc}
self.y = self.x
#print "STATUS : " , str(self.state['status'])
#print "SIGMA : " , str(self.state['sigma'])
def outputFnc(self):
''' DEVS output function.
'''
print "---- OUTPUT ----"
n = len(self.y)
print "N : "+ str(n)
print "TIME !!!!!!!!! : " + str(self.y[0].time)
in0 = int(self.y[0].value[0])
in1 = int(self.y[1].value[0])
in2 = int(self.y[2].value[0])
'''INSERT strategy here [Change the state of the FMS (self.lump)]'''
if in0 == 1 and in1 == 1:
if self.lump.state == "off":
self.lump.illuminate()
else:
if self.lump.state == "on":
self.lump.darken()
''' End of strategy '''
print "LAMP STATE :" , self.lump.state
self.poke(self.OPorts[0],Message(str(int(self.lump.is_on())),str(self.y[0].time)))
print "---- END OUTPUT ----"
def intTransition(self):
''' DEVS internal transition function.
'''
print "---- INTERNAL | Status "+str(self.state['status'])+" ----"
if self.state['status'] == 'ACTIVE':
self.state = { 'status': 'IDLE', 'sigma':INFINITY}
def timeAdvance(self):
''' DEVS Time Advance function.
'''
#print "---- TIME "+ str(self.state['sigma'])+" ----"
return self.state['sigma']
def finish(self, msg):
''' Additional function which is lunched just before the end of the simulation.
'''
print "---- FINISH ----"
## Lamp class
class Lamp(object):
def say_im_turned_on(self): print("Let there be LIGHT !")
def say_im_turned_off(self): print("Hello darkness, my old friend !")