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

Annotation of /trunk/pygtk/library.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 803 - (hide annotations) (download) (as text)
Fri Aug 4 05:51:31 2006 UTC (16 years 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 johnpye 132 #include <iostream>
2     #include <stdexcept>
3     #include <sstream>
4     using namespace std;
5    
6     #undef NDEBUG
7    
8 johnpye 480 #include "config.h"
9 johnpye 507
10 johnpye 132 extern "C"{
11 johnpye 190 #include <utilities/ascConfig.h>
12 johnpye 485 #include <compiler/compiler.h>
13 johnpye 132 #include <general/list.h>
14     #include <compiler/slist.h>
15 johnpye 190 #include <compiler/ascCompiler.h>
16     #include <compiler/fractions.h>
17 johnpye 132 #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 johnpye 164 #include <compiler/simlist.h>
37 johnpye 480 #include <compiler/parser.h>
38 johnpye 485 #include <utilities/error.h>
39 johnpye 692 #include <general/env.h>
40 johnpye 132 }
41    
42     #include "library.h"
43     #include "simulation.h"
44     #include "solver.h"
45    
46 johnpye 481 Library::Library(const char *defaultpath){
47 johnpye 132 static int have_init;
48     if(!have_init){
49 johnpye 500 //cerr << "Initialising ASCEND library..." << endl;
50 johnpye 132 Asc_RedirectCompilerDefault(); // Ensure that error message reach stderr
51     Asc_CompilerInit(1);
52 johnpye 692 env_import(PATHENVIRONMENTVAR,getenv,Asc_PutEnv);
53 johnpye 132 char *x = Asc_GetEnv(PATHENVIRONMENTVAR);
54 johnpye 341 if(x==NULL || strcmp(x,"")==0){
55 johnpye 692 if(defaultpath==NULL){
56     ERROR_REPORTER_NOLINE(ASC_PROG_WARNING,"Using default "
57     PATHENVIRONMENTVAR " = '" DEFAULT_ASCENDLIBRARY "'"
58     );
59     defaultpath = DEFAULT_ASCENDLIBRARY;
60     }
61    
62 johnpye 341 string s = string(PATHENVIRONMENTVAR "=") + defaultpath;
63 johnpye 803 //ERROR_REPORTER_HERE(ASC_PROG_NOTE,"Setting %s",s.c_str());;
64 johnpye 341 Asc_PutEnv(s.c_str());
65     }
66     Asc_ImportPathList(PATHENVIRONMENTVAR);
67 johnpye 803 //cerr << PATHENVIRONMENTVAR << " = " << x << endl;
68 johnpye 500 //cerr << "Created LIBRARY" << endl;
69 johnpye 803 //cerr << "Registering solvers..." << endl;
70 johnpye 132 registerStandardSolvers();
71 johnpye 480 }else{
72 johnpye 132 std::cerr << "Reusing LIBRARY" << std::endl;
73 johnpye 480 }
74 johnpye 132 have_init=1;
75     }
76    
77     Library::~Library(){
78 johnpye 190 //ERROR_REPORTER_NOLINE(ASC_PROG_WARNING,"DESTROYED LIBRARY!");
79 johnpye 175 //DestroyLibrary();
80 johnpye 190 // ... need to use some kind of reference counting before you can do that...
81 johnpye 132 }
82    
83 johnpye 180 /**
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 johnpye 132 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 johnpye 190 case -2:
120 johnpye 132 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 johnpye 412 std::cerr << "Note: Beginning parse of " << Asc_ModuleName(m) << "..." << std::endl;
139     zz_parse();
140 johnpye 392 std::cerr << "Note: ...yyparse of " << Asc_ModuleName(m) << " completed." << std::endl;
141 johnpye 132
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 johnpye 180 /**
147     Return a vector of all the Modules which have been loaded into
148     the current library.
149     */
150 johnpye 132 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 johnpye 180 /**
173     Output to stderr the names of the modules loaded into the current Library.
174     */
175 johnpye 132 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 johnpye 782 char *type = NULL;
193 johnpye 132 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 johnpye 150 //CONSOLE_DEBUG("Found type %s",name);
238 johnpye 132 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 johnpye 213 vector<ExtMethod>
248     Library::getExtMethods(){
249 johnpye 132 // Clear the vector
250 johnpye 213 extmethod_vector = vector<ExtMethod>();
251 johnpye 132
252     // Traverse the vector
253 johnpye 213 TraverseExtFuncLibrary(Library::extMethodTraverse, (void *)this);
254 johnpye 132
255 johnpye 213 return extmethod_vector;
256 johnpye 132 }
257    
258 johnpye 180 /**
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 johnpye 132 void
265 johnpye 213 Library::extMethodTraverse(void *a1, void *a2){
266 johnpye 132 Library *self = (Library *)a2;
267 johnpye 213 self->appendToExtMethodVector(a1);
268 johnpye 132 }
269    
270     void
271 johnpye 213 Library::appendToExtMethodVector(void *a1){
272 johnpye 132 struct ExternalFunc *e = (struct ExternalFunc *)a1;
273 johnpye 213 extmethod_vector.push_back(ExtMethod(e));
274 johnpye 132 }
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 johnpye 711 \*SetUniversalProcedureList(NULL);
294 johnpye 132 */
295 johnpye 190 ERROR_REPORTER_NOLINE(ASC_PROG_NOTE,"Destroying simulations...\n");
296 johnpye 164 Asc_DestroySimulations();
297    
298 johnpye 190 ERROR_REPORTER_NOLINE(ASC_PROG_NOTE,"Clearing library...\n");
299 johnpye 132 DestroyNotesDatabase(LibraryNote());
300 johnpye 161 SetUniversalProcedureList(NULL);
301 johnpye 132 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 johnpye 190 ERROR_REPORTER_NOLINE(ASC_PROG_WARNING,"LIBRARY CLEARED!");
310 johnpye 132 }
311    
312    

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