/[ascend]/trunk/base/generic/compiler/packages.c
ViewVC logotype

Contents of /trunk/base/generic/compiler/packages.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1291 - (show annotations) (download) (as text)
Mon Feb 26 06:20:43 2007 UTC (17 years, 4 months ago) by johnpye
File MIME type: text/x-csrc
File size: 7859 byte(s)
Added EXTERNAL asc_default_all plus test cases.
1 /* ASCEND modelling environment
2 Copyright (C) 1990, 1993, 1994 Thomas Guthrie Epperly, Kirk Abbott.
3 Copyright (C) 2006 Carnegie Mellon University
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
8 any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
19 *//**
20 @file
21 Code to support dynamic and static loading of user packages.
22
23 'Packages' are bits of code that add functionality to ASCEND, like 'plugins'
24 or 'addins' on other projects. In ASCEND we have a few that are pretty much
25 essential -- the 'built in' packges. They are only packages in the sense
26 that a 'register' function must be called to connect them correctly with
27 the rest of the system.
28
29 Next there are the 'static' packages. These ones are linked into
30 The default state is to have packages. As such it takes an explicit
31 definition of NO_PACKAGES, if packages are not to be handled.
32 An explicit definition of STATIC_PACKAGES or DYNAMIC_PACKAGES is also
33 required.
34 *//*
35 by Kirk Abbott
36 Created: July 4, 1994
37 Last in CVS: 1.14 ballan 1998/03/06 15:47:14
38 */
39
40 #if !defined(DYNAMIC_PACKAGES) && !defined(STATIC_PACKAGES) && !defined(NO_PACKAGES)
41 # error "Package linking option not set!"
42 #endif
43
44 #include <math.h>
45 #include <ctype.h> /* was compiler/actype.h */
46
47 #include <utilities/ascConfig.h>
48 #include <utilities/config.h> /* NEW */
49
50 #ifndef ASC_DEFAULTPATH
51 # error "Where is ASC_DEFAULTPATH???"
52 #endif
53
54 #include <general/ospath.h>
55
56
57 #include <utilities/ascMalloc.h>
58 #include <utilities/ascEnvVar.h>
59 #include <compiler/importhandler.h>
60 #include <utilities/ascPanic.h>
61 #include <general/list.h>
62 #include "symtab.h"
63
64
65 #include "functype.h"
66 #include "expr_types.h"
67 #include "extcall.h"
68 #include "mathinst.h"
69 #include "instance_enum.h"
70 #include "instquery.h"
71 #include "atomvalue.h"
72 #include "find.h"
73 #include "extfunc.h"
74 #include "rel_blackbox.h"
75 #include "vlist.h"
76 #include "relation.h"
77 #include "safe.h"
78 #include "relation_util.h"
79 #include "extfunc.h"
80 #include <packages/sensitivity.h>
81 #include <packages/ascFreeAllVars.h>
82 #include <packages/defaultall.h>
83 #include "module.h"
84 #include "packages.h"
85
86 /*
87 Initialise the slv data structures used when calling external fns
88 */
89 void Init_BBoxInterp(struct BBoxInterp *interp)
90 {
91 if (interp){
92 interp->status = calc_all_ok;
93 interp->user_data = NULL;
94 interp->task = bb_none;
95 }
96 }
97
98 /*---------------------------------------------
99 BUILT-IN PACKAGES...
100 */
101
102 /**
103 Load builtin packages, unless NO_PACKAGES.
104
105 @return 0 if success, 1 if failure.
106 */
107 static
108 int Builtins_Init(void){
109 int result = 0;
110
111 #ifdef NO_PACKAGES
112 ERROR_REPORTER_HERE(ASC_USER_WARNING,"Builtins_Init: DISABLED at compile-time");
113 #else
114 /* ERROR_REPORTER_DEBUG("Loading function asc_free_all_variables\n"); */
115 result = CreateUserFunctionMethod("asc_free_all_variables"
116 ,Asc_FreeAllVars
117 ,1 /* num of args */
118 ,"Unset 'fixed' flag of all items of type 'solver_var'" /* help */
119 ,NULL /* user_data */
120 ,NULL /* destroy fn */
121 );
122
123 ERROR_REPORTER_DEBUG("Registering EXTERNAL asc_default_self");
124 result = CreateUserFunctionMethod("asc_default_self"
125 ,Asc_DefaultSelf
126 ,1 /* num of args */
127 ,"Set local child atoms to their ATOMs' DEFAULT values. Also recurses into arrays." /* help */
128 ,NULL /* user_data */
129 ,NULL /* destroy fn */
130 );
131
132 ERROR_REPORTER_DEBUG("Registering EXTERNAL asc_default_all");
133 result = CreateUserFunctionMethod("asc_default_all"
134 ,Asc_DefaultAll
135 ,1 /* num of args */
136 ,"Call 'default_all' methods on sub-models, then run 'default_self' locally." /* help */
137 ,NULL /* user_data */
138 ,NULL /* destroy fn */
139 );
140 #endif
141 return result;
142 }
143
144 /* return 0 on success */
145 int LoadArchiveLibrary(CONST char *partialpath, CONST char *initfunc){
146
147 #ifdef DYNAMIC_PACKAGES
148 struct FilePath *fp1;
149 int result;
150 struct ImportHandler *handler=NULL;
151
152 /**
153 @TODO
154 * modify SearchArchiveLibraryPath to use the ImportHandler array
155 in each directory in the path.
156 * when a file is found, return information about which ImportHandler
157 should be used to open it, then make the call.
158 */
159
160 CONSOLE_DEBUG("Searching for external library '%s'",partialpath);
161
162 importhandler_createlibrary();
163
164 fp1 = importhandler_findinpath(
165 partialpath, ASC_DEFAULTPATH, PATHENVIRONMENTVAR,&handler
166 );
167 if(fp1==NULL){
168 CONSOLE_DEBUG("External library '%s' not found",partialpath);
169 ERROR_REPORTER_NOLINE(ASC_USER_ERROR,"External library '%s' not found.",partialpath);
170 return 1; /* failure */
171 }
172
173 asc_assert(handler!=NULL);
174
175 /* CONSOLE_DEBUG("About to import external library..."); */
176 /* note the import handler will deal with all the initfunc execution, etc etc */
177 result = (*(handler->importfn))(fp1,initfunc,partialpath);
178 if(result){
179 CONSOLE_DEBUG("Error %d when importing external library of type '%s'",result,handler->name);
180 ERROR_REPORTER_HERE(ASC_PROG_ERROR,"Error importing external library '%s'",partialpath);
181 ospath_free(fp1);
182 return 1;
183 }
184
185 ospath_free(fp1);
186 return 0;
187 #else
188
189 (void)partialname; (void)initfunc;
190
191 # if defined(STATIC_PACKAGES)
192 ERROR_REPORTER_HERE(ASC_PROG_NOTE,"LoadArchiveLibrary disabled: STATIC_PACKAGES, no need to load dynamically.\n");
193 return 0;
194 # elif defined(NO_PACKAGES)
195 ERROR_REPORTER_HERE(ASC_PROG_ERROR,"LoadArchiveLibrary disabled: NO_PACKAGES");
196 return 1;
197 # else
198 # error "Invalid package linking flags"
199 # endif
200
201 #endif
202 }
203
204 /*---------------------------------------------
205 STATIC_PACKAGES code only...
206
207 Declare the functions which we are expected to be able to call.
208 */
209 #ifndef NO_PACKAGES
210 # ifdef STATIC_PACKAGES
211
212 #include <packages/kvalues.h>
213 #include <packages/bboxtest.h>
214 #include <packages/bisect.h>
215 #include <packages/sensitivity.h>
216
217 # endif
218 #endif
219
220 #ifdef STATIC_PACKAGES
221 /**
222 Load all statically-linked packages
223
224 @return 0 on success, >0 if any CreateUserFunction calls failed.
225 */
226 static int StaticPackages_Init(void){
227 int result = 0;
228
229 result += sensitivity_register();
230 result += kvalues_register();
231 result += bboxtest_register();
232
233 return result;
234 }
235 #endif
236
237 /**
238 This is a general purpose function that will load whatever user
239 functions are required according to the compile-time settings.
240
241 If NO_PACKAGES, nothing will be loaded. If DYNAMIC_PACKAGES, then
242 just the builtin packages will be loaded. If STATIC_PACKAGES then
243 builtin plus those called in 'StaticPackages_Init' will be loaded.
244 */
245 void AddUserFunctions(void){
246
247 CONSOLE_DEBUG("ADDING USER FUNCTIONS");
248
249 #ifdef NO_PACKAGES
250 # ifdef __GNUC__
251 # warning "NO_PACKAGES was defined so external packages are disabled"
252 # endif
253 ERROR_REPORTER_NOLINE(ASC_PROG_NOTE,"AddUserFunctions disabled at compile-time.");
254 #else
255
256 CONSOLE_DEBUG("ADDING BUILTINS");
257 /* Builtins are always statically linked */
258 if (Builtins_Init()) {
259 ERROR_REPORTER_NOLINE(ASC_PROG_WARNING
260 ,"Problem in Builtins_Init: Some user functions not created"
261 );
262 }
263
264 # ifdef DYNAMIC_PACKAGES
265
266 CONSOLE_DEBUG("ADDING DYNAMIC PACKAGES...");
267 /* do nothing. WHY? */
268
269 # elif defined(STATIC_PACKAGES)
270 # ifdef __GNUC__
271 # warning "STATIC PACKAGES"
272 # endif
273
274 CONSOLE_DEBUG("ADDING STATIC PACKAGES FUNCTIONS...");
275 /*The following need to be reimplemented but are basically useful as is. */
276 if (StaticPackages_Init()) {
277 ERROR_REPORTER_NOLINE(ASC_PROG_WARNING
278 ,"Problem in StaticPackages_Init(): Some user functions not created"
279 );
280 }
281
282 # endif
283 #endif
284 }
285

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