/[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 2822 - (hide annotations) (download) (as text)
Tue Feb 17 06:36:00 2015 UTC (3 years, 8 months ago) by jpye
Original Path: branches/pallav/pygtk/canvas/blockcanvas.py
File MIME type: text/x-python
File size: 4302 byte(s)
restoring branches...

1 jpye 1942 from gaphas import Canvas
2 jpye 1968 from gaphas.item import Line
3 arijit 2074 import re
4 saheb 2686 from blockitem import DefaultBlockItem, GraphicalBlockItem
5 jpye 2394 from blockline import BlockLine
6 grivan 2628 from blockstream import BlockStream
7 jpye 1942
8 arijit 2074 UNITS_RE = re.compile("([-+]?(\d+(\.\d*)?|\d*\.d+)([eE][-+]?\d+)?)\s*(.*)");
9    
10     saved_model = None
11    
12 jpye 1942 class BlockCanvas(Canvas):
13 arijit 2074
14     def __init__(self):
15     super(BlockCanvas, self).__init__()
16     self.filestate = 0
17     self.canvasmodelstate = 'Unsolved'
18 jpye 2394 self.model_library = ''
19 arijit 2074 self.saved_data = None
20     self.filename = None
21 jpye 2394 self.user_code = ''
22 arijit 2074
23 jpye 1961 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 jpye 1970 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 jpye 1942
37 jpye 1961 super(BlockCanvas,self).update_constraints(items)
38 jpye 1942
39 jpye 2394 def reattach_ascend(self, ascwrap, notesdb):
40 jpye 1967 """
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 jpye 1959 items = []
48 jpye 1961 for item in self.get_all_items():
49     if not hasattr(item, 'blockinstance'):
50 jpye 1959 continue
51 jpye 1961 bi = item.blockinstance
52 jpye 2394 bi.reattach_ascend(ascwrap,notesdb)
53    
54     #if bi.blocktype.type is None:
55     # bi.blocktype.reattach_ascend(ascwrap, notesdb)
56 grivan 2628 '''
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 jpye 1967 def __str__(self):
66     """
67     Create ASCEND code corresponding to canvas model, return the
68     code as a string.
69 jpye 2394 Note: This function uses Python's advanced string formatting capability.
70 jpye 1967 """
71 jpye 2005
72     # FIXME it should be possible to perform this function at an
73     # application layer, without using routines from Gaphas...
74 jpye 2394
75     string = '''
76 grivan 2628 (* automatically generated model*)
77 jpye 2394 REQUIRE "{lib_name}";
78    
79     MODEL canvasmodel;
80     {is_a}
81 grivan 2628 {streams}
82 jpye 2394 {are_the_same}
83 grivan 2628 {canvas_user_code}
84 jpye 2394 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 grivan 2628 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 jpye 2005
100 jpye 2394 items = self.get_all_items()
101    
102     def parse(item):
103 saheb 2686 if type(item)==DefaultBlockItem or type(item)==GraphicalBlockItem:
104 jpye 2394 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 grivan 2628 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 jpye 2394 try:
115 grivan 2628 replacement_fields['parameter_code']+=\
116     reduce(lambda x,y:x+y,specify)
117     except TypeError:
118 jpye 2394 pass
119     try:
120 grivan 2628 replacement_fields['parameter_code']+=\
121     reduce(lambda x,y:x+y,fix)
122     except TypeError:
123 jpye 2394 pass
124 saheb 2686
125 jpye 2394 if type(item)==BlockLine:
126     replacement_fields['are_the_same']+=str(item.lineinstance)
127 saheb 2686
128 jpye 2394 map(parse,items)
129 arijit 2074
130 jpye 2394 replacement_fields['canvas_user_code'] = self.user_code
131     return string.format(**replacement_fields)
132 jpye 1967
133 jpye 1965 def __getstate__(self):
134 jpye 1967 """
135     Placeholder for any special pickling stuff that we want to do with
136     our canvas.
137     """
138 jpye 1965 return super(BlockCanvas,self).__getstate__()
139 arijit 2074
140 jpye 1965 def __setstate__(self, state):
141 jpye 1967 """
142     Placeholder for any special pickling stuff that we want to do with
143     our canvas.
144     """
145 saheb 2686 super(BlockCanvas,self).__setstate__(state)
146    
147     # vim: set ts=4 noet:

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