/[ascend]/branches/mike/pygtk/canvas/blockcanvas.py
ViewVC logotype

Contents of /branches/mike/pygtk/canvas/blockcanvas.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2628 - (show annotations) (download) (as text)
Wed May 16 20:40:56 2012 UTC (6 years, 5 months ago) by grivan
Original Path: trunk/pygtk/canvas/blockcanvas.py
File MIME type: text/x-python
File size: 4237 byte(s)
Merge changes from grivan2 branch (r3261-4126) to trunk, so that Saheb can use it.
1 from gaphas import Canvas
2 from gaphas.item import Line
3 import re
4 from blockitem import DefaultBlockItem
5 from blockline import BlockLine
6 from blockstream import BlockStream
7
8 UNITS_RE = re.compile("([-+]?(\d+(\.\d*)?|\d*\.d+)([eE][-+]?\d+)?)\s*(.*)");
9
10 saved_model = None
11
12 class BlockCanvas(Canvas):
13
14 def __init__(self):
15 super(BlockCanvas, self).__init__()
16 self.filestate = 0
17 self.canvasmodelstate = 'Unsolved'
18 self.model_library = ''
19 self.saved_data = None
20 self.filename = None
21 self.user_code = ''
22
23 def update_constraints(self, items):
24 """
25 Update constraints. Also variables may be marked as dirty before the
26 constraint solver kicks in.
27 """
28 # request solving of external constraints associated with dirty items
29 request_resolve = self._solver.request_resolve
30 for item in items:
31 if hasattr(item,'ports'):
32 for p in item._ports:
33 if hasattr(p,"point"):
34 request_resolve(p.point.x)
35 request_resolve(p.point.y)
36
37 super(BlockCanvas,self).update_constraints(items)
38
39 def reattach_ascend(self, ascwrap, notesdb):
40 """
41 After unpickling a canvas, this method gives a way of reattaching
42 the model to an ASCEND library, by connecting the types named in the
43 pickle with the typenames present in the Library.
44 """
45 # FIXME need to set ASCEND to *load* the missing types if they're not
46 # already in the Library.
47 items = []
48 for item in self.get_all_items():
49 if not hasattr(item, 'blockinstance'):
50 continue
51 bi = item.blockinstance
52 bi.reattach_ascend(ascwrap,notesdb)
53
54 #if bi.blocktype.type is None:
55 # bi.blocktype.reattach_ascend(ascwrap, notesdb)
56 '''
57 def set_stream(self,stream):
58 items = self.get_all_items()
59 for item in items:
60 if type(item)==DefaultBlockItem:
61 bi = item.blockinstance
62 bi.stream=stream
63 '''
64
65 def __str__(self):
66 """
67 Create ASCEND code corresponding to canvas model, return the
68 code as a string.
69 Note: This function uses Python's advanced string formatting capability.
70 """
71
72 # FIXME it should be possible to perform this function at an
73 # application layer, without using routines from Gaphas...
74
75 string = '''
76 (* automatically generated model*)
77 REQUIRE "{lib_name}";
78
79 MODEL canvasmodel;
80 {is_a}
81 {streams}
82 {are_the_same}
83 {canvas_user_code}
84 METHODS
85 METHOD canvas_user_code;
86 END canvas_user_code;
87 METHOD parameter_code;
88 {parameter_code}
89 END parameter_code;
90 METHOD on_load;
91 RUN canvas_user_code;
92 RUN parameter_code;
93 END on_load;
94 END canvasmodel;
95 '''
96 replacement_fields = {'lib_name':str(self.model_library),'is_a':'','are_the_same':'',
97 'canvas_user_code':'','parameter_code':'','block_user_code':''
98 ,'streams':''}
99
100 items = self.get_all_items()
101
102 def parse(item):
103 if type(item)==DefaultBlockItem:
104 bi = item.blockinstance
105 replacement_fields['is_a']+=str(bi)
106 specify = filter(lambda param:bi.params[param].value != None,bi.params)
107 fix = filter(lambda param:bi.params[param].fix == True,bi.params)
108 specify = filter(lambda x:not (x in fix),specify)
109 specify = map(lambda param:'\t{0}.{1}:={2};\n'.
110 format(bi.name,param,bi.params[param].value),specify)
111 fix = map(lambda param:'\tFIX {0}.{1};\n\t{0}.{1}:={2}{4}{3}{5};\n'.
112 format(bi.name,param,bi.params[param].value,
113 bi.params[param].units,'{','}'),fix)
114 try:
115 replacement_fields['parameter_code']+=\
116 reduce(lambda x,y:x+y,specify)
117 except TypeError:
118 pass
119 try:
120 replacement_fields['parameter_code']+=\
121 reduce(lambda x,y:x+y,fix)
122 except TypeError:
123 pass
124
125 if type(item)==BlockLine:
126 replacement_fields['are_the_same']+=str(item.lineinstance)
127
128
129 map(parse,items)
130
131 replacement_fields['canvas_user_code'] = self.user_code
132 return string.format(**replacement_fields)
133
134 def __getstate__(self):
135 """
136 Placeholder for any special pickling stuff that we want to do with
137 our canvas.
138 """
139 return super(BlockCanvas,self).__getstate__()
140
141 def __setstate__(self, state):
142 """
143 Placeholder for any special pickling stuff that we want to do with
144 our canvas.
145 """
146 super(BlockCanvas,self).__setstate__(state)

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22