This repository has been archived by the owner on Nov 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmdcnvt
executable file
·196 lines (170 loc) · 5.63 KB
/
mdcnvt
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
#!/usr/bin/env python
#
# Copyright 2005-2013,2018 Matthew R. Wette
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 3 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the licence with this software.
# If not, see <http://www.gnu.org/licenses/>.
#from util1 import move_if_changed, replace_c_code
import sys
import pickle
import pdb # debugger
from zipfile import *
from getopt import getopt
sys.path.append(".")
from ssk1.mdreader import *
import ssk1.uanlyz
from ssk1.uml2ssk import *
from ssk1.ssk1 import index_ssk
from ssk1.sskP import SskXmlWriter
from ssk1.impl_c2 import C2Impl
def get_diag_list(model):
"""
Generate list of diagrams in the model.
"""
dlist = []
#pdb.set_trace()
for elt in model.packagedElement:
eclass = elt.__class__
if eclass == uml2.StateMachine:
if elt.name:
ename = elt.name
else:
ename = '(unmamed)'
cname = str(eclass)[5:]
dlist.append((ename,cname))
elif eclass == uml2.Package:
l = get_diag_list(elt)
dlist.extend(l)
else:
continue
return dlist
def list_diag(model):
dlist = get_diag_list(model)
for diag in dlist:
ename, cname = diag
print "%-20s %s" % (ename, cname)
def is_zipfile(filename):
rx = re.compile(r'.*\.(md)?zip$')
m = rx.match(filename)
return m
md_mf_name = 'com.nomagic.magicdraw.uml_model.model' # file w/ model
def zipbuf(filename):
# return buffer of contents of file
# 17.0.2 on: file is com.nomagic.magicdraw.uml_model.model
za = ZipFile(filename)
nl = za.namelist()
if len(nl) == 1:
# 17.0.1 and prior
buf = za.read(nl[0])
elif md_mf_name in nl:
buf = za.read(md_mf_name)
else:
print "*** mdcnvt: could not find model file in zip archive:", nl
sys.exit(1)
#return buf
return buf.encode('utf-8')
def main(argv):
"""
mdcnvt[-l] <file> <mach>
example: ./mdcnvt SSKdemo1.mdzip Demo1B2
"""
ld = False # list diagrams
mach = None # machine
file = None # filename
cn = None # codgen: class name
fb = None # codegen: file base (what for?)
diags = [] # diagrams to process
# Process options.
sopts = 'hlc:b:m:'
lopts = [ 'help', 'list', 'class-name=', 'file-base=', 'mach=' ]
opts, argv = getopt(argv[1:], sopts, lopts)
if len(argv) > 0:
file = argv[0] # file name
if len(argv) > 1:
diags = argv[1:] # diagrams
for opt in opts:
key = opt[0]; val = opt[1]
if key == '-h' or key == '--help':
print "usage: mdmain.py [-h|-l|-c <name>|-m <name>] file.mdzip"
print " -l | --list list diagrams"
print " -c <n> | --class-name=<n> tbd"
print " -b <n> | --file-base=<n> tbd"
print " -m m1,m2 | --mach=m1,m2 tbd"
print "example: mdmain.py project.mdzip stmach1"
sys.exit(0)
elif key == '-l' or key == '--list':
ld = True
elif key == '-c' or key == '--class-name':
cn = val
elif key == '-b' or key == '--file-base':
fb = val
elif key == '-m' or key == '--mach':
mach = val.split(',')
else:
print "unknown option:", key
# Generate parser.
mdh = MagicdrawFileHdlr()
h = XmiDocumentHandler(mdh)
# Generate model and extensions.
if is_zipfile(file):
xml.sax.parseString(zipbuf(file), h)
else:
# f = codec.open(file)
xml.sax.parse(file, h)
#
mdh.patchup() # see patchup call in mdreader
model = mdh.model
extns = mdh.extns
if False:
umlval = uanlyz.ModelValidator()
umlval.validate_model(mdh.model)
del h, mdh
# Get base name. (needed?)
base = re.sub(r'\.[^\.]+$', '', file)
base = re.sub(r'.*\/', '', base)
# === Generate output. =============
if ld:
list_diag(model)
# \todo Allow regexp's to be used to choose diagrams.
# This should be option to generate code. Sometimes we just gen diag.
ss = None
for machname in diags:
#pdb.set_trace()
ss = uml_to_ssk(model, machname)
if False:
f = open(base + '.pkl', 'w')
p = pickle.Pickler(f)
p.dump(ss)
f.close()
if True and ss:
index_ssk(ss)
f1 = open("demo.xml", 'w')
sp = SskXmlWriter(ss, f1)
sp.write()
f1.close()
if True and ss:
f1 = open("demo.c", 'w')
impl = C2Impl(ss)
impl.dump_body(f1)
f1.close()
if False and ss:
f1 = open("demo.pml", 'w')
impl = PmlImpl(ss)
impl.dump_body(f1)
f1.close()
if __name__ == '__main__':
main(sys.argv)
print "\nTODO:"
print " + actions are not getting through"
print " + implied transitions due to orthongal state exit"
# --- last line of mdcnvt ---