forked from capocchi/DEVSimPy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevsimpy-nogui.py
More file actions
264 lines (220 loc) · 10.4 KB
/
devsimpy-nogui.py
File metadata and controls
264 lines (220 loc) · 10.4 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#!/usr/bin/env python
# -*- coding: utf-8 -*-
## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
# devsimpy-nogui.py --- DEVSimPy - The Python DEVS no GUI modeling and simulation software
# --------------------------------
# Copyright (c) 2019
# Laurent CAPOCCHI
# SPE - University of Corsica
# --------------------------------
# Version 2.9 last modified: 09/09/19
## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
#
# GENERAL NOTES AND REMARKS:
#
# Bach version of DEVSimPy (whitout GUI)
## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
#
# GLOBAL VARIABLES AND FUNCTIONS
#
## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
import os
import sys
import builtins
import json
__version__ = '4.0'
ABS_HOME_PATH = os.path.abspath(os.path.dirname(sys.argv[0]))
### specific builtin variables. (dont modify the defautls value. If you want to change it, go tot the PreferencesGUI from devsimpy interface.)
builtin_dict = {'SPLASH_PNG': os.path.join(ABS_HOME_PATH, 'splash', 'splash.png'),
'DEVSIMPY_PNG': 'iconDEVSimPy.png', # png file for devsimpy icon
'HOME_PATH': ABS_HOME_PATH,
'ICON_PATH': os.path.join(ABS_HOME_PATH, 'icons'),
'ICON_PATH_16_16': os.path.join(ABS_HOME_PATH, 'icons', '16x16'),
'SIMULATION_SUCCESS_SOUND_PATH': os.path.join(ABS_HOME_PATH,'sounds', 'Simulation-Success.wav'),
'SIMULATION_ERROR_SOUND_PATH': os.path.join(ABS_HOME_PATH,'sounds', 'Simulation-Error.wav'),
'DOMAIN_PATH': os.path.join(ABS_HOME_PATH, 'Domain'), # path of local lib directory
'NB_OPENED_FILE': 5, # number of recent files
'NB_HISTORY_UNDO': 5, # number of undo
'OUT_DIR': 'out', # name of local output directory (composed by all .dat, .txt files)
'PLUGINS_PATH': os.path.join(ABS_HOME_PATH, 'plugins'), # path of plug-ins directory
'FONT_SIZE': 12, # Block font size
'LOCAL_EDITOR': True, # for the use of local editor
'LOG_FILE': os.devnull, # log file (null by default)
'DEFAULT_SIM_STRATEGY': 'bag-based', #choose the default simulation strategy for PyDEVS
'PYDEVS_SIM_STRATEGY_DICT' : {'original':'SimStrategy1', 'bag-based':'SimStrategy2', 'direct-coupling':'SimStrategy3'}, # list of available simulation strategy for PyDEVS package
'PYPDEVS_SIM_STRATEGY_DICT' : {'classic':'SimStrategy4', 'parallel':'SimStrategy5'}, # list of available simulation strategy for PyPDEVS package
'PYPDEVS_221_SIM_STRATEGY_DICT' : {'classic':'SimStrategy4', 'parallel':'SimStrategy5'}, # list of available simulation strategy for PyPDEVS package
'HELP_PATH' : os.path.join('doc', 'html'), # path of help directory
'NTL' : False, # No Time Limit for the simulation
'DYNAMIC_STRUCTURE' : False, #Dynamic structure for PyPDEVS simulation
'REAL_TIME': False, ### PyPDEVS threaded real time simulation
'VERBOSE':False,
'TRANSPARENCY' : True, # Transparancy for DetachedFrame
'DEFAULT_PLOT_DYN_FREQ' : 100, # frequence of dynamic plot of QuickScope (to avoid overhead),
'DEFAULT_DEVS_DIRNAME':'PyDEVS', # default DEVS Kernel directory
'DEVS_DIR_PATH_DICT':{'PyDEVS':os.path.join(ABS_HOME_PATH,'DEVSKernel','PyDEVS'),
'PyPDEVS_221':os.path.join(ABS_HOME_PATH,'DEVSKernel','PyPDEVS','pypdevs221' ,'src'),
'PyPDEVS':os.path.join(ABS_HOME_PATH,'DEVSKernel','PyPDEVS','old')},
'GUI_FLAG' : True,
'INFINITY' : float('inf')
}
builtin_dict['GUI_FLAG'] = False
from InteractionYAML import YAMLHandler
def simulate(devs, duration, simu_name, is_remote):
from SimulationNoGUI import makeSimulation
if str(duration) in ('inf', 'ntl'):
builtins.__dict__['NTL'] = True
duration = 0.0
### launch simulation
makeSimulation(devs, duration, simu_name, is_remote, True)
# Sets the homepath variable to the directory where your application is located (sys.argv[0]).
builtins.__dict__.update(builtin_dict)
#-------------------------------------------------------------------
if __name__ == '__main__':
import gettext
_ = gettext.gettext
import argparse
parser = argparse.ArgumentParser(description="simulate a model unless other option is specified")
# required filename
parser.add_argument("filename", help="dsp or yaml devsimpy file")
# optional simulation_time for simulation
parser.add_argument("simulation_time", nargs='?', help="simulation time [inf|ntl]", default=10)
# optional simulation_name for remote execution
parser.add_argument("-remote", help="remote execution", action="store_true")
parser.add_argument("-name", help="simulation name", type=str, default="simu")
# optional kernel for simulation kernel
parser.add_argument("-kernel", help="simulation kernel [pyDEVS|PyPDEVS]", type=str, default="pyDEVS")
# optional real time
parser.add_argument("-rt", help="real time simulation (only for PyPDEVS)", action="store_true")
# non-simulation options
group = parser.add_mutually_exclusive_group()
group.add_argument("-js", "--javascript",help="generate JS file", action="store_true")
group.add_argument("-json", help="turn the YAML/DSP file to JSON", action="store_true")
group.add_argument("-blockslist", help="get the list of models in a master model", action="store_true")
group.add_argument("-blockargs", help="parameters of an atomic model (ex. -blockargs <label of block>)", type=str)
parser.add_argument("-updateblockargs", help="update parameters (ex. -blockargs <label of block> -updateblockargs <'''{'<key>':<val>}'''>", type=str, default="")
args = parser.parse_args()
if args.kernel:
if 'PyPDEVS' in args.kernel:
builtins.__dict__['DEFAULT_DEVS_DIRNAME'] = 'PyPDEVS_221'
builtins.__dict__['DEFAULT_SIM_STRATEGY'] = 'parallel'
### Real time only for PyPDEVS...
builtins.__dict__['REAL_TIME'] = args.rt
filename = args.filename
if not os.path.exists(filename):
sys.stderr.write(_('ERROR: devsimpy file does not exist!\n'))
sys.exit()
else:
yamlHandler = YAMLHandler(filename)
if args.javascript:
# Javascript generation
yamlHandler.getJS()
elif args.json:
# turn the YAML/DSP file to JSON
j = yamlHandler.getJSON()
sys.stdout.write(json.dumps(j))
elif args.blockslist:
# get the list of models in a master model
l = yamlHandler.getYAMLBlockModelsList()
sys.stdout.write(json.dumps(l))
elif args.blockargs:
# model block parameters read or update
label = args.blockargs
if args.updateblockargs:
# model block is updated
args = json.loads(args.updateblockargs)
if isinstance(args,str):
args = eval(args)
new_args = yamlHandler.setYAMLBlockModelArgs(label, args)
sys.stdout.write(json.dumps(new_args))
else:
args = yamlHandler.getYAMLBlockModelArgs(label)
sys.stdout.write(json.dumps(args))
else:
# simulation
duration = args.simulation_time
if not isinstance(duration, str):
duration = float(duration)
devs = yamlHandler.getDevsInstance()
if devs:
simulate(devs, duration, args.name, args.remote)
#~ yamlHandler = YAMLHandler(filename)
#~ if not yamlHandler.filename_is_valid:
#~ sys.stderr.write(_('ERROR: Invalid file!\n'))
#~ sys.exit()
#~ #sys.stdout.write(_("DEVSimPy - version %s\n"%__version__ ))
#~ nb_args = len(sys.argv)
#~ ### First argument is filename - validity check
#~ filename = sys.argv[1] if nb_args > 1 else None
#~ if not filename:
#~ sys.stderr.write(_('ERROR: Unspecified devsimpy file!\n'))
#~ sys.exit()
#~ elif not os.path.exists(filename):
#~ sys.stderr.write(_('ERROR: devsimpy file does not exist!\n'))
#~ sys.exit()
#~ yamlHandler = YAMLHandler(filename)
#~ if not yamlHandler.filename_is_valid:
#~ sys.stderr.write(_('ERROR: Invalid file!\n'))
#~ sys.exit()
#~ if nb_args == 2:
#~ ########################################################################
#~ # Simulation with default simulated duration
#~ devs = yamlHandler.getDevsInstance()
#~ if devs :
#~ simulate(master=devs, T = 10.0, socket_id="")
#~ elif nb_args >= 3:
#~ action = sys.argv[2]
#~ if action in ('-js','-javascript'):
#~ ########################################################################
#~ # Javascript generation
#~ yamlHandler.getJS()
#~ elif action in ('-json'):
#~ ########################################################################
#~ # turn the YAML/DSP file to JSON
#~ j = yamlHandler.getJSON()
#~ sys.stdout.write(json.dumps(j))
#~ elif action in ('-blockslist'):
#~ ########################################################################
#~ # get the list of models in a master model
#~ list = yamlHandler.getYAMLBlockModelsList()
#~ sys.stdout.write(json.dumps(list))
#~ elif action in ('-getblockargs'):
#~ ########################################################################
#~ # get the parameters of an atomic model
#~ if nb_args == 4:
#~ label = sys.argv[3]
#~ args = yamlHandler.getYAMLBlockModelArgs(label)
#~ sys.stdout.write(json.dumps(args))
#~ else:
#~ sys.stderr.write(_('ERROR: Unspecified label for model!\n'))
#~ sys.exit()
#~ elif action in ('-setblockargs'):
#~ ########################################################################
#~ # update the parameters of a block of a model
#~ if nb_args == 5:
#~ import json
#~ label = sys.argv[3]
#~ args = json.loads(sys.argv[4])
#~ new_args = yamlHandler.setYAMLBlockModelArgs(label, args)
#~ sys.stdout.write(json.dumps(new_args))
#~ else:
#~ sys.stderr.write(_("unexpected nb_args=" + str(nb_args)))
#~ #sys.stderr.write(_('ERROR: usage devsimpy-nogui.py dsp_or_yaml_filename -setmodelargs block_label args_as_JSON_string!\n'))
#~ #sys.exit()
#~ else:
#~ ########################################################################
#~ # Simulation without socket communication
#~ duration = sys.argv[2]
#~ if nb_args == 4:
#~ socket_id = sys.argv[3]
#~ else:
#~ socket_id = ""
#~ devs = yamlHandler.getDevsInstance()
#~ if devs :
#~ simulate(devs, duration, socket_id)
#~ else:
#~ sys.stderr.write(_('ERROR: Unspecified .dsp file!\n'))
#~ sys.stdout.write(_('USAGE: to simulate $python devsimpy-nogui.py yourfile.dsp [time=10.0|[inf|ntl]]\n'))
#~ sys.stdout.write(_('USAGE: to generate JS file $python devsimpy-nogui.py yourfile.dsp [-js|-javascript]\n'))
#~ sys.exit()