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

Annotation of /trunk/pygtk/library.cpp

Parent Directory Parent Directory | Revision Log Revision Log


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

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