/[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 2074 - (hide annotations) (download) (as text)
Fri Sep 4 06:50:08 2009 UTC (9 years, 1 month ago) by arijit
Original Path: trunk/pygtk/canvas/blockcanvas.py
File MIME type: text/x-python
File size: 3949 byte(s)
Google SoC 2009 Project : Implementation of canvas based modeller for energy systems
1 jpye 1942 from gaphas import Canvas
2 jpye 1968 from gaphas.item import Line
3 arijit 2074 import re
4 jpye 1942
5 arijit 2074 UNITS_RE = re.compile("([-+]?(\d+(\.\d*)?|\d*\.d+)([eE][-+]?\d+)?)\s*(.*)");
6    
7     saved_model = None
8    
9 jpye 1942 class BlockCanvas(Canvas):
10 arijit 2074
11     def __init__(self):
12     super(BlockCanvas, self).__init__()
13     self.filestate = 0
14     self.canvasmodelstate = 'Unsolved'
15     self.model_library = 'test/canvas/blocktypes.a4c'
16     self.saved_data = None
17     self.filename = None
18    
19 jpye 1961 def update_constraints(self, items):
20     """
21     Update constraints. Also variables may be marked as dirty before the
22     constraint solver kicks in.
23     """
24     # request solving of external constraints associated with dirty items
25     request_resolve = self._solver.request_resolve
26     for item in items:
27 jpye 1970 if hasattr(item,'ports'):
28     for p in item._ports:
29     if hasattr(p,"point"):
30     request_resolve(p.point.x)
31     request_resolve(p.point.y)
32 jpye 1942
33 jpye 1961 super(BlockCanvas,self).update_constraints(items)
34 jpye 1942
35 jpye 1961 def reattach_ascend(self, library, notesdb):
36 jpye 1967 """
37     After unpickling a canvas, this method gives a way of reattaching
38     the model to an ASCEND library, by connecting the types named in the
39     pickle with the typenames present in the Library.
40     """
41     # FIXME need to set ASCEND to *load* the missing types if they're not
42     # already in the Library.
43 jpye 1959 items = []
44 jpye 1961 for item in self.get_all_items():
45     if not hasattr(item, 'blockinstance'):
46 jpye 1959 continue
47 jpye 1961 bi = item.blockinstance
48     if bi.blocktype.type is None:
49     bi.blocktype.reattach_ascend(library, notesdb)
50 jpye 1954
51 jpye 1967 def __str__(self):
52     """
53     Create ASCEND code corresponding to canvas model, return the
54     code as a string.
55     """
56 jpye 2005
57     # FIXME it should be possible to perform this function at an
58     # application layer, without using routines from Gaphas...
59 jpye 1967 s = "\n(* automatically generated model from blocklist.py *)\n";
60 arijit 2074 s += 'REQUIRE "blocktypes.a4c";\n'
61 jpye 1967 s += "MODEL canvasmodel;\n"
62 jpye 2005
63     # Add blocks to the model via IS_A statements
64 jpye 1967 for item in self.get_all_items():
65     if not hasattr(item, 'blockinstance'):
66     continue
67 jpye 1968 s += str(item.blockinstance)
68 jpye 1967
69 jpye 2005 # Add LINES as ARE_THE_SAME statements
70 jpye 1973 for line in self.get_all_items():
71     if not isinstance(line, Line):
72 jpye 1967 continue
73 jpye 1979 s += str(line.lineinstance)
74 jpye 2005
75     # Set PARAMETERS using the ON_LOAD METHOD.
76     p = ""
77 arijit 2074 uc = ""
78     s += "METHODS\nMETHOD on_load;\n";
79 jpye 2005 for item in self.get_all_items():
80     if not hasattr(item, 'blockinstance'):
81     continue
82     n = item.blockinstance.name
83 arijit 2074 uc += item.blockinstance.usercode
84 jpye 2005 for k,v in item.blockinstance.params.iteritems():
85 arijit 2074 if v.fix == 1:
86     p += "\tFIX %s.%s;\n" % (n,v.name)
87     _param_value = self.checkEntry(v.value)
88     p += "\t%s.%s := %s;\n" % (n,v.name,_param_value)
89    
90 jpye 2005 if not p == "":
91     s += p
92 arijit 2074 #if ucflag == 1:
93     # s += "RUN user_code;\n"
94     #if ucflag == 1:
95     # s += usercode
96    
97     if uc == "":
98 jpye 2005 s += "END on_load;\n"
99 arijit 2074 else:
100     s += "RUN user_code;\n"
101     s += "END on_load;\n"
102     s += "METHOD user_code;\n"
103     s += uc
104     s += "END user_code;"
105 jpye 2005
106 arijit 2074
107     s += "\nEND canvasmodel;\n";
108 jpye 1967 return s
109    
110 jpye 1965 def __getstate__(self):
111 jpye 1967 """
112     Placeholder for any special pickling stuff that we want to do with
113     our canvas.
114     """
115 arijit 2074
116 jpye 1965 return super(BlockCanvas,self).__getstate__()
117 arijit 2074
118 jpye 1965 def __setstate__(self, state):
119 jpye 1967 """
120     Placeholder for any special pickling stuff that we want to do with
121     our canvas.
122     """
123 jpye 1965 super(BlockCanvas,self).__setstate__(state)
124    
125 arijit 2074 def checkEntry(self, _param_value):
126    
127     try:
128     # match a float with option text afterwards, optionally separated by whitespace
129     _match = re.match(UNITS_RE,_param_value)
130     print "done"
131     if not _match:
132     print _param_val
133     #raise InputError("Not a valid value-and-optional-units")
134     #parent.reporter.reportError("Not a valid value-and-optional-units")
135     _val = _match.group(1) # the numerical part of the input
136     _units = _match.group(5) # the text entered for units
137     return str(_val)+'{'+str(_units)+'}'
138    
139     except:
140     print "Unable to generate Model Code. Please re-check parameter values."

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