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

Annotation of /trunk/pygtk/interface/library.cpp

Parent Directory Parent Directory | Revision Log Revision Log


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

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