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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

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