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

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