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

Contents of /trunk/pygtk/library.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 534 - (show annotations) (download) (as text)
Tue Apr 25 13:25:04 2006 UTC (13 years, 9 months ago) by johnpye
File MIME type: text/x-c++src
File size: 8787 byte(s)
Working on platform-independent pathnames for the IMPORT command.
Added 'ospath.c' to base/generic/general for this purpose.
Patched kvalues and sensitivity to use the 'IMPORT "libname";' syntax
instead of 'IMPORT registerfn FROM libname;'.
Fixed pathnames in create.nsi.
Added GPL header to coupla files.
Added quoting to 'IMPORT' syntax in ascParse.y.
Removed 'PackageOption' from Tcl/Tk related Scons options so that missing Tcl/Tk
doesn't cause breakage.
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 }
40
41 #include "library.h"
42 #include "simulation.h"
43 #include "solver.h"
44
45 Library::Library(const char *defaultpath){
46 static int have_init;
47 if(!have_init){
48 //cerr << "Initialising ASCEND library..." << endl;
49 Asc_RedirectCompilerDefault(); // Ensure that error message reach stderr
50 Asc_CompilerInit(1);
51 Asc_ImportPathList(PATHENVIRONMENTVAR);
52 char *x = Asc_GetEnv(PATHENVIRONMENTVAR);
53 if(x==NULL || strcmp(x,"")==0){
54 string s = string(PATHENVIRONMENTVAR "=") + defaultpath;
55 cerr << "Setting " << s << endl;
56 Asc_PutEnv(s.c_str());
57 }
58 Asc_ImportPathList(PATHENVIRONMENTVAR);
59 cerr << PATHENVIRONMENTVAR << " = " << x << endl;
60 //cerr << "Created LIBRARY" << endl;
61 cerr << "Registering solvers..." << endl;
62 registerStandardSolvers();
63 }else{
64 std::cerr << "Reusing LIBRARY" << std::endl;
65 }
66 have_init=1;
67 }
68
69 Library::~Library(){
70 //ERROR_REPORTER_NOLINE(ASC_PROG_WARNING,"DESTROYED LIBRARY!");
71 //DestroyLibrary();
72 // ... need to use some kind of reference counting before you can do that...
73 }
74
75 /**
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 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 case -2:
112 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 std::cerr << "Note: Beginning parse of " << Asc_ModuleName(m) << "..." << std::endl;
131 zz_parse();
132 std::cerr << "Note: ...yyparse of " << Asc_ModuleName(m) << " completed." << std::endl;
133
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 /**
139 Return a vector of all the Modules which have been loaded into
140 the current library.
141 */
142 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 /**
165 Output to stderr the names of the modules loaded into the current Library.
166 */
167 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 //CONSOLE_DEBUG("Found type %s",name);
230 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 vector<ExtMethod>
240 Library::getExtMethods(){
241 // Clear the vector
242 extmethod_vector = vector<ExtMethod>();
243
244 // Traverse the vector
245 TraverseExtFuncLibrary(Library::extMethodTraverse, (void *)this);
246
247 return extmethod_vector;
248 }
249
250 /**
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 void
257 Library::extMethodTraverse(void *a1, void *a2){
258 Library *self = (Library *)a2;
259 self->appendToExtMethodVector(a1);
260 }
261
262 void
263 Library::appendToExtMethodVector(void *a1){
264 struct ExternalFunc *e = (struct ExternalFunc *)a1;
265 extmethod_vector.push_back(ExtMethod(e));
266 }
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 ERROR_REPORTER_NOLINE(ASC_PROG_NOTE,"Destroying simulations...\n");
288 Asc_DestroySimulations();
289
290 ERROR_REPORTER_NOLINE(ASC_PROG_NOTE,"Clearing library...\n");
291 DestroyNotesDatabase(LibraryNote());
292 SetUniversalProcedureList(NULL);
293 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 ERROR_REPORTER_NOLINE(ASC_PROG_WARNING,"LIBRARY CLEARED!");
302 }
303
304

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