/[ascend]/trunk/ascend/packages/defaultall.c
ViewVC logotype

Annotation of /trunk/ascend/packages/defaultall.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2241 - (hide annotations) (download) (as text)
Sun Aug 1 03:17:50 2010 UTC (13 years, 11 months ago) by jpye
File MIME type: text/x-csrc
File size: 6920 byte(s)
Adding test case for defaultall.c errors.
1 johnpye 1282 /* ASCEND modelling environment
2     Copyright (C) 2007 Carnegie Mellon University
3    
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2, or (at your option)
7     any later version.
8    
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12     GNU General Public License for more details.
13    
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 59 Temple Place - Suite 330,
17     Boston, MA 02111-1307, USA.
18     *//** @file
19 johnpye 1289 The following functions give an automatic, default form for the
20     'default_all' and 'default_self' methods usually written as explicit
21     METHODs in a model. EXPERIMENTAL.
22 johnpye 1282 *//*
23     by John Pye, 15 Feb 2007.
24     */
25    
26 jpye 2018 #include <ascend/packages/defaultall.h>
27 johnpye 1291
28 jpye 2018 #include <ascend/compiler/proc.h>
29     #include <ascend/compiler/name.h>
30 johnpye 1291
31 jpye 2018 #include <ascend/utilities/ascConfig.h>
32     #include <ascend/utilities/ascPrint.h>
33     #include <ascend/utilities/ascPanic.h>
34 johnpye 1282
35 jpye 2018 #include <ascend/compiler/instquery.h>
36     #include <ascend/compiler/child.h>
37     #include <ascend/compiler/type_desc.h>
38     #include <ascend/compiler/symtab.h>
39     #include <ascend/compiler/atomvalue.h>
40     #include <ascend/compiler/visitinst.h>
41     #include <ascend/compiler/parentchild.h>
42     #include <ascend/compiler/library.h>
43     #include <ascend/compiler/initialize.h>
44 johnpye 1282
45 jpye 2241 #define DEFAULT_DEBUG
46 johnpye 1303
47 jpye 1372 /*------------------------------------------------------------------------------
48     visit child atoms of the current model (don't visit sub models) and set
49     to ATOM default values
50     */
51    
52     static int defaultself_visit_childatoms1(struct Instance *inst);
53    
54     int defaultself_visit_childatoms(struct Instance *root, struct gl_list_t *arglist, void *userdata){
55     /* arglist is a list of gllist of instances */
56     if (arglist == NULL ||
57     gl_length(arglist) == 0L ||
58     gl_length((struct gl_list_t *)gl_fetch(arglist,1)) != 1 ||
59     gl_fetch((struct gl_list_t *)gl_fetch(arglist,1),1) == NULL) {
60     return defaultself_visit_childatoms1(root);
61     }else{
62     return defaultself_visit_childatoms1(
63     (struct Instance *)gl_fetch( (struct gl_list_t *)gl_fetch(arglist,1),1 )
64     );
65     }
66     }
67    
68    
69 johnpye 1282 /**
70 johnpye 1289 Find atom children in the present model and set them to their ATOM DEFAULT
71     values.
72     */
73     static
74 jpye 1372 int defaultself_visit_childatoms1(struct Instance *inst){
75 johnpye 1289 int i,n;
76     struct Instance *c;
77     struct TypeDescription *type;
78 johnpye 1282
79 johnpye 1289 /* default any child atoms' values */
80     n = NumberChildren(inst);
81     for(i = 1; i <= n; ++i){
82 johnpye 1303 #ifdef DEFAULT_DEBUG
83 johnpye 1289 CONSOLE_DEBUG("Child %d...", i);
84 johnpye 1303 #endif
85 johnpye 1289 c = InstanceChild(inst,i);
86 johnpye 1303 if(c==NULL)continue;
87    
88 johnpye 1289 type = InstanceTypeDesc(c);
89     if(BaseTypeIsAtomic(type)){
90     if(!AtomDefaulted(type))continue;
91     switch(GetBaseType(type)){
92 jpye 1372 case real_type:
93 jpye 1374 #ifdef DEFAULT_DEBUG
94 jpye 1372 CONSOLE_DEBUG("Setting to atom default = %f",GetRealDefault(type));
95 jpye 1374 #endif
96 jpye 1372 SetRealAtomValue(c, GetRealDefault(type), 0);
97     break;
98 johnpye 1289 case integer_type: SetIntegerAtomValue(c, GetIntDefault(type), 0); break;
99     case boolean_type: SetBooleanAtomValue(c, GetBoolDefault(type), 0); break;
100     case symbol_type: SetSymbolAtomValue(c, GetSymDefault(type)); break;
101     case set_type: /* what is the mechanism for defaulting of sets? */
102     default: ASC_PANIC("invalid type");
103     }
104 johnpye 1303 #ifdef DEFAULT_DEBUG
105 johnpye 1289 CONSOLE_DEBUG("Reset atom to default value");
106 johnpye 1303 #endif
107 johnpye 1289 }else if(GetBaseType(type)==array_type){
108     /* descend into arrays */
109 jpye 1372 defaultself_visit_childatoms1(c);
110 johnpye 1289 }
111     }
112 jpye 1374 #ifdef DEFAULT_DEBUG
113 jpye 1372 CONSOLE_DEBUG("defaultself_visit_childatoms1 returning %d",0);
114 jpye 1374 #endif
115 johnpye 1289 return 0;
116     }
117 johnpye 1282
118 jpye 1372 /*------------------------------------------------------------------------------
119     visit submodels, running 'default_self' on each
120     */
121    
122 johnpye 1291 struct DefaultAll_data{
123 jpye 1372 symchar *method_name;
124 johnpye 1291 };
125    
126 jpye 1372 static int defaultself_visit_submodels1(struct Instance *inst
127     , struct DefaultAll_data *data
128     );
129    
130     int defaultself_visit_submodels(struct Instance *root
131     , struct gl_list_t *arglist, void *userdata
132     ){
133 jpye 2241 CONSOLE_DEBUG("starting");
134 jpye 1372 struct DefaultAll_data data;
135     data.method_name = AddSymbol("default_self");
136    
137     /* arglist is a list of gllist of instances */
138     if (arglist == NULL ||
139     gl_length(arglist) == 0L ||
140     gl_length((struct gl_list_t *)gl_fetch(arglist,1)) != 1 ||
141     gl_fetch((struct gl_list_t *)gl_fetch(arglist,1),1) == NULL) {
142     return defaultself_visit_submodels1(root,&data);
143     }else{
144     return defaultself_visit_submodels1(
145     (struct Instance *)gl_fetch( (struct gl_list_t *)gl_fetch(arglist,1),1 )
146     , &data
147     );
148     }
149     }
150    
151 johnpye 1289 /**
152 jpye 1372 Find child models in the present model and run their 'default_self' methods
153 johnpye 1282 */
154     static
155 jpye 1372 int defaultself_visit_submodels1(struct Instance *inst
156     , struct DefaultAll_data *data
157     ){
158 johnpye 1291 int i, n, err = 0;
159 johnpye 1282 struct Instance *c;
160     struct TypeDescription *type;
161 johnpye 1291 struct InitProcedure *method;
162     enum Proc_enum pe;
163 johnpye 1282
164     type = InstanceTypeDesc(inst);
165    
166 johnpye 1291 /* loop through child atoms */
167 johnpye 1282 n = NumberChildren(inst);
168     for(i = 1; i <= n; ++i){
169 johnpye 1291 c = InstanceChild(inst,i);
170 johnpye 1303 if(c==NULL)continue;
171    
172 johnpye 1282 type = InstanceTypeDesc(c);
173 johnpye 1291 if(model_type == GetBaseType(type)){
174     /* run 'default_all' for all child models */
175 jpye 1372 method = FindMethod(type,data->method_name);
176 johnpye 1291 if(method){
177 johnpye 1303 #ifdef DEFAULT_DEBUG
178 jpye 1372 CONSOLE_DEBUG("Running METHOD %s on '%s'",SCP(data->method_name),SCP(GetName(type)));
179 johnpye 1303 #endif
180 jpye 1372 pe = Initialize(c , CreateIdName(ProcName(method))
181     , SCP(data->method_name)
182 johnpye 1291 ,ASCERR
183     ,0, NULL, NULL
184     );
185     if(pe!=Proc_all_ok)err += 1;
186     }else{
187 johnpye 1303 #ifdef DEFAULT_DEBUG
188 johnpye 1291 CONSOLE_DEBUG("Recursing into array...");
189 johnpye 1303 #endif
190 jpye 1372 ERROR_REPORTER_HERE(ASC_PROG_ERR,"No '%s' found for type '%s'",SCP(data->method_name),SCP(GetName(type)));
191 johnpye 1291 return 1;
192 johnpye 1282 }
193 johnpye 1291 }else if(array_type == GetBaseType(type)){
194 jpye 1372 if(defaultself_visit_submodels1(c,data))err += 1;
195 johnpye 1282 }
196     }
197    
198 jpye 1374 #ifdef DEFAULT_DEBUG
199 jpye 1372 CONSOLE_DEBUG("defaultself_visit_submodels1 return ing %d",err);
200 jpye 1374 #endif
201 johnpye 1291 return err;
202 johnpye 1282 }
203 johnpye 1289
204 jpye 1372 #if 0
205     /**
206     NOTE YET IMPLEMENTED: we need to be able to pass string constants to
207     methods, which I don't think is possible yet.
208     */
209     int Asc_VisitSubmodels(struct Instance *root
210     , struct gl_list_t *arglist, void *userdata
211     ){
212     struct DefaultAll_data data;
213     (void)userdata;
214 johnpye 1291
215 jpye 1372 ERROR_REPORTER_HERE(ASC_USER_ERROR,"not implemented");
216     return 1;
217     if (arglist == NULL
218     || gl_length(arglist) == 0L
219     || gl_length((struct gl_list_t *)gl_fetch(arglist,1)) != 2
220     || gl_fetch((struct gl_list_t *)gl_fetch(arglist,1),1) == NULL
221     || gl_fetch((struct gl_list_t *)gl_fetch(arglist,1),2) == NULL
222     ){
223     ERROR_REPORTER_HERE(ASC_USER_ERROR,"EXTERNAL visit_submodels(SELF,'methodname') called with bad argument list");
224     return 1;
225     }
226    
227     data.method_name = AddSymbol("default_all");
228 johnpye 1291
229 jpye 1372 return Asc_DefaultAll1(
230     (struct Instance *)gl_fetch( (struct gl_list_t *)gl_fetch(arglist,1),1 )
231     , &data
232     );
233 johnpye 1291 }
234 jpye 1372 #endif

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