/[ascend]/trunk/pygtk/library.cpp
ViewVC logotype

Contents of /trunk/pygtk/library.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 803 - (show annotations) (download) (as text)
Fri Aug 4 05:51:31 2006 UTC (16 years, 10 months ago) by johnpye
File MIME type: text/x-c++src
File size: 9062 byte(s)
Added default solver preference in PyGTK GUI.
Removed some debug output from library.cpp
1 #include <iostream>
2 #include <stdexcept>
3 #include <sstream>
4 using namespace std;
5
6 #undef NDEBUG
7
8 #include "config.h"
9
10 extern "C"{
11 #include <utilities/ascConfig.h>
12 #include <compiler/compiler.h>
13 #include <general/list.h>
14 #include <compiler/slist.h>
15 #include <compiler/ascCompiler.h>
16 #include <compiler/fractions.h>
17 #include <compiler/compiler.h>
18 #include <compiler/redirectFile.h>
19 #include <compiler/module.h>
20 #include <compiler/prototype.h>
21 #include <compiler/dump.h>
22 #include <compiler/dimen.h>
23 #include <compiler/child.h>
24 #include <compiler/childio.h>
25 #include <compiler/type_desc.h>
26 #include <compiler/typedef.h>
27 #include <compiler/library.h>
28 #include <compiler/childinfo.h>
29 #include <solver/slv_types.h>
30 #include <solver/system.h>
31 #include <utilities/ascEnvVar.h>
32 #include <compiler/symtab.h>
33 #include <general/table.h>
34 #include <compiler/instance_enum.h>
35 #include <compiler/notate.h>
36 #include <compiler/simlist.h>
37 #include <compiler/parser.h>
38 #include <utilities/error.h>
39 #include <general/env.h>
40 }
41
42 #include "library.h"
43 #include "simulation.h"
44 #include "solver.h"
45
46 Library::Library(const char *defaultpath){
47 static int have_init;
48 if(!have_init){
49 //cerr << "Initialising ASCEND library..." << endl;
50 Asc_RedirectCompilerDefault(); // Ensure that error message reach stderr
51 Asc_CompilerInit(1);
52 env_import(PATHENVIRONMENTVAR,getenv,Asc_PutEnv);
53 char *x = Asc_GetEnv(PATHENVIRONMENTVAR);
54 if(x==NULL || strcmp(x,"")==0){
55 if(defaultpath==NULL){
56 ERROR_REPORTER_NOLINE(ASC_PROG_WARNING,"Using default "
57 PATHENVIRONMENTVAR " = '" DEFAULT_ASCENDLIBRARY "'"
58 );
59 defaultpath = DEFAULT_ASCENDLIBRARY;
60 }
61
62 string s = string(PATHENVIRONMENTVAR "=") + defaultpath;
63 //ERROR_REPORTER_HERE(ASC_PROG_NOTE,"Setting %s",s.c_str());;
64 Asc_PutEnv(s.c_str());
65 }
66 Asc_ImportPathList(PATHENVIRONMENTVAR);
67 //cerr << PATHENVIRONMENTVAR << " = " << x << endl;
68 //cerr << "Created LIBRARY" << endl;
69 //cerr << "Registering solvers..." << endl;
70 registerStandardSolvers();
71 }else{
72 std::cerr << "Reusing LIBRARY" << std::endl;
73 }
74 have_init=1;
75 }
76
77 Library::~Library(){
78 //ERROR_REPORTER_NOLINE(ASC_PROG_WARNING,"DESTROYED LIBRARY!");
79 //DestroyLibrary();
80 // ... need to use some kind of reference counting before you can do that...
81 }
82
83 /**
84 Load an ASCEND model file into the Library. It will be parsed such that
85 its types will be visible to Library::findType.
86
87 @param filename Filename, will be searched for relative to ASCENDLIBRARY environment
88 variable, if necessary.
89 */
90 void
91 Library::load(const char *filename){
92
93 //std::cerr << "Loading '" << filename << "'" << std::endl;
94
95 int status;
96 struct module_t *m=Asc_RequireModule(filename,&status);
97 if(m!=NULL){
98 //std::cerr << "Loaded module '" << Asc_ModuleName(m) << "'" << std::endl;
99 }else{
100 std::cerr << "Error: unable to load module '" << filename << "'." << std::endl;
101 }
102
103 char *msg;
104 switch(status){
105 case 5:
106 msg = "The module '%s' already exists. "; break;
107 case 4:
108 msg = "Caught an attempt to do a recursive require under '%s'."; break;
109 case 3:
110 msg = "A new module was created from '%s', overwriting a module's alias."; break;
111 case 2:
112 msg = "An existing module is being returned for '%s'." ; break;
113 case 1:
114 msg = "An new version of an existing module was created for '%s'."; break;
115 case 0:
116 msg = "Module for '%s' created OK."; break;
117 case -1:
118 msg = "Error: File not found for '%s'. (-1)"; break;
119 case -2:
120 msg = "Error: Unable to open '%s' for reading. (-2)";break;
121 case -3:
122 msg = "Error: Insuffient memory to create module for '%s'. (-3)"; break;
123 case -4:
124 msg = "Error: bad input, null or zero length filename in '%s'. (-4)"; break;
125 default:
126 throw std::runtime_error("Invalid status code in library.cpp");
127 }
128
129 char msg1[100];
130 sprintf(msg1,msg,filename);
131
132 if(status<0 || status>0){
133 throw std::runtime_error(msg1);
134 }else{
135 std::cerr << "Note: Module " << Asc_ModuleName(m) << ": " << msg1 << std::endl;
136 }
137
138 std::cerr << "Note: Beginning parse of " << Asc_ModuleName(m) << "..." << std::endl;
139 zz_parse();
140 std::cerr << "Note: ...yyparse of " << Asc_ModuleName(m) << " completed." << std::endl;
141
142 struct gl_list_t *l = Asc_TypeByModule(m);
143 std::cerr << "Note: " << gl_length(l) << " library entries loaded from '" << filename << "'" << std::endl;
144 }
145
146 /**
147 Return a vector of all the Modules which have been loaded into
148 the current library.
149 */
150 vector<Module>
151 Library::getModules(){
152 //cerr << "GET MODULES\n" << endl;
153
154 vector<Module> v;
155 struct gl_list_t *l = Asc_ModuleList(0);
156 for(int i=0, end=gl_length(l); i<end; ++i){
157 symchar *name = (symchar *)gl_fetch(l,i+1);
158 if(AscFindSymbol(name)==NULL){
159 throw runtime_error("Library::getModules: invalid symchar *");
160 }
161 //cerr << "GOT " << SCP( name ) << endl;
162 const module_t *m = Asc_GetModuleByName((const char *)name);
163 v.push_back(Module(m));
164 }
165 /*cerr << "LENGTH OF V IS " << v.size() << endl;
166 if(v.size()){
167 cerr << "MODULE 0's NAME IS " << v[0].getName() << endl;
168 }*/
169 return v;
170 }
171
172 /**
173 Output to stderr the names of the modules loaded into the current Library.
174 */
175 void
176 Library::listModules(const int &module_type) const{
177
178 if(module_type < 0 || module_type > 2){
179 throw std::runtime_error("Library::listModules: invalid module_type parameter");
180 }
181
182 struct gl_list_t *l;
183
184 l = Asc_ModuleList(module_type);
185
186 if(l==NULL){
187 std::cerr << "Library::listModules: list is empty" << std::endl;
188 return;
189 //throw std::runtime_error("Library::listModules: Asc_ModuleList returned NULL");
190 }
191
192 char *type = NULL;
193 switch(module_type){
194 case 0: type = "modules containing defined types"; break;
195 case 1: type = "modules with string definitions"; break;
196 case 2: type = "modules with statements"; break;
197 }
198 int n=gl_length(l);
199 if(n){
200 std::cerr << "Listing " << gl_length(l) << " " << type << std::endl;
201 gl_iterate(l,Library::displayModule);
202 }else{
203 std::cerr << "Notice: No " << type << " found in module list." << std::endl;
204 }
205 }
206
207 void
208 Library::displayModule(void *v){
209 //module_t *m = (module_t *)v;
210 std::cerr << " - " << (char *)v << std::endl;
211 }
212
213 Type &
214 Library::findType(SymChar sym){
215 TypeDescription *t = FindType(sym.getInternalType());
216 if(t==NULL){
217 stringstream ss;
218 ss << "Library::findType: type '" << sym << "' not found in library";
219 throw runtime_error(ss.str());
220 }/*else{
221 cerr << "Found something for type " << sym << endl;
222 }*/
223 Type *t2=new Type(t);
224 return *t2;
225 }
226
227 /**
228 This could be quite a bit more efficient if we could get a gl_list_t of TypeDescription rather than names
229 */
230 vector<Type>
231 Library::getModuleTypes(const Module &m){
232 //cerr << "GET MODULE TYPES\n" << endl;
233 vector<Type> v;
234 struct gl_list_t *l = Asc_TypeByModule(m.getInternalType());
235 for(int i=0,end=gl_length(l); i<end; ++i){
236 char *name = (char *)gl_fetch(l,i+1);
237 //CONSOLE_DEBUG("Found type %s",name);
238 TypeDescription *t = FindType((const symchar *)name);
239 v.push_back(Type(t));
240 }
241 return v;
242 }
243
244 /**
245 This function is kinda fighting against the Table implementation of the external function library. What we really need is some kind of iterator on the Table struct, but it doesn't seem to be implemented. Instead there is a C-style equivalent of the STL 'bind1st' function which we can use, but it's not exported from the current extfunc.h so we need to add it.
246 */
247 vector<ExtMethod>
248 Library::getExtMethods(){
249 // Clear the vector
250 extmethod_vector = vector<ExtMethod>();
251
252 // Traverse the vector
253 TraverseExtFuncLibrary(Library::extMethodTraverse, (void *)this);
254
255 return extmethod_vector;
256 }
257
258 /**
259 This method exists only to allow the TraverseExtFuncLibrary function
260 to make callbacks to the Library class from C.
261
262 @NOTE there might be issues with C/C++ linking here?
263 */
264 void
265 Library::extMethodTraverse(void *a1, void *a2){
266 Library *self = (Library *)a2;
267 self->appendToExtMethodVector(a1);
268 }
269
270 void
271 Library::appendToExtMethodVector(void *a1){
272 struct ExternalFunc *e = (struct ExternalFunc *)a1;
273 extmethod_vector.push_back(ExtMethod(e));
274 }
275
276 /**
277 Clear the library: 'DESTROY TYPES'
278
279 @TODO do this more efficiently, don't destroy the whole ASCEND compiler.
280 */
281 void
282 Library::clear(){
283 //DestroyNotesDatabase(LibraryNote());
284 /* Asc_CompilerDestroy();
285 cerr << "COMPLETED ASC_COMPILERDESTROY" << endl;
286 Asc_CompilerInit(1);
287 cerr << "... ASC_COMPILERINIT OK" << endl;
288 Asc_ImportPathList(PATHENVIRONMENTVAR);
289 registerStandardSolvers();
290 cerr << "... REGISTER_STANDARD_SOLVERS" << endl;
291 DefineFundamentalTypes();
292 cerr << "... DEFINED FUND TYPES" << endl;
293 \*SetUniversalProcedureList(NULL);
294 */
295 ERROR_REPORTER_NOLINE(ASC_PROG_NOTE,"Destroying simulations...\n");
296 Asc_DestroySimulations();
297
298 ERROR_REPORTER_NOLINE(ASC_PROG_NOTE,"Clearing library...\n");
299 DestroyNotesDatabase(LibraryNote());
300 SetUniversalProcedureList(NULL);
301 DestroyLibrary();
302 DestroyPrototype();
303 EmptyTrash();
304 Asc_DestroyModules((DestroyFunc)DestroyStatementList);
305 WriteChildMissing(NULL,NULL,NULL);
306 //Asc_CompilerInit(1)
307 DefineFundamentalTypes();
308 InitNotesDatabase(LibraryNote());
309 ERROR_REPORTER_NOLINE(ASC_PROG_WARNING,"LIBRARY CLEARED!");
310 }
311
312

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