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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2018 - (show annotations) (download) (as text)
Wed Apr 29 03:38:10 2009 UTC (15 years, 2 months ago) by jpye
File MIME type: text/x-csrc
File size: 6898 byte(s)
Fixed compile for new header file locations <ascend/compiler/xxx.h> etc.
1 /* 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 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 *//*
23 by John Pye, 15 Feb 2007.
24 */
25
26 #include <ascend/packages/defaultall.h>
27
28 #include <ascend/compiler/proc.h>
29 #include <ascend/compiler/name.h>
30
31 #include <ascend/utilities/ascConfig.h>
32 #include <ascend/utilities/ascPrint.h>
33 #include <ascend/utilities/ascPanic.h>
34
35 #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
45 /* #define DEFAULT_DEBUG */
46
47 /*------------------------------------------------------------------------------
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 /**
70 Find atom children in the present model and set them to their ATOM DEFAULT
71 values.
72 */
73 static
74 int defaultself_visit_childatoms1(struct Instance *inst){
75 int i,n;
76 struct Instance *c;
77 struct TypeDescription *type;
78
79 /* default any child atoms' values */
80 n = NumberChildren(inst);
81 for(i = 1; i <= n; ++i){
82 #ifdef DEFAULT_DEBUG
83 CONSOLE_DEBUG("Child %d...", i);
84 #endif
85 c = InstanceChild(inst,i);
86 if(c==NULL)continue;
87
88 type = InstanceTypeDesc(c);
89 if(BaseTypeIsAtomic(type)){
90 if(!AtomDefaulted(type))continue;
91 switch(GetBaseType(type)){
92 case real_type:
93 #ifdef DEFAULT_DEBUG
94 CONSOLE_DEBUG("Setting to atom default = %f",GetRealDefault(type));
95 #endif
96 SetRealAtomValue(c, GetRealDefault(type), 0);
97 break;
98 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 #ifdef DEFAULT_DEBUG
105 CONSOLE_DEBUG("Reset atom to default value");
106 #endif
107 }else if(GetBaseType(type)==array_type){
108 /* descend into arrays */
109 defaultself_visit_childatoms1(c);
110 }
111 }
112 #ifdef DEFAULT_DEBUG
113 CONSOLE_DEBUG("defaultself_visit_childatoms1 returning %d",0);
114 #endif
115 return 0;
116 }
117
118 /*------------------------------------------------------------------------------
119 visit submodels, running 'default_self' on each
120 */
121
122 struct DefaultAll_data{
123 symchar *method_name;
124 };
125
126 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 struct DefaultAll_data data;
134 data.method_name = AddSymbol("default_self");
135
136 /* arglist is a list of gllist of instances */
137 if (arglist == NULL ||
138 gl_length(arglist) == 0L ||
139 gl_length((struct gl_list_t *)gl_fetch(arglist,1)) != 1 ||
140 gl_fetch((struct gl_list_t *)gl_fetch(arglist,1),1) == NULL) {
141 return defaultself_visit_submodels1(root,&data);
142 }else{
143 return defaultself_visit_submodels1(
144 (struct Instance *)gl_fetch( (struct gl_list_t *)gl_fetch(arglist,1),1 )
145 , &data
146 );
147 }
148 }
149
150 /**
151 Find child models in the present model and run their 'default_self' methods
152 */
153 static
154 int defaultself_visit_submodels1(struct Instance *inst
155 , struct DefaultAll_data *data
156 ){
157 int i, n, err = 0;
158 struct Instance *c;
159 struct TypeDescription *type;
160 struct InitProcedure *method;
161 enum Proc_enum pe;
162
163 type = InstanceTypeDesc(inst);
164
165 /* loop through child atoms */
166 n = NumberChildren(inst);
167 for(i = 1; i <= n; ++i){
168 c = InstanceChild(inst,i);
169 if(c==NULL)continue;
170
171 type = InstanceTypeDesc(c);
172 if(model_type == GetBaseType(type)){
173 /* run 'default_all' for all child models */
174 method = FindMethod(type,data->method_name);
175 if(method){
176 #ifdef DEFAULT_DEBUG
177 CONSOLE_DEBUG("Running METHOD %s on '%s'",SCP(data->method_name),SCP(GetName(type)));
178 #endif
179 pe = Initialize(c , CreateIdName(ProcName(method))
180 , SCP(data->method_name)
181 ,ASCERR
182 ,0, NULL, NULL
183 );
184 if(pe!=Proc_all_ok)err += 1;
185 }else{
186 #ifdef DEFAULT_DEBUG
187 CONSOLE_DEBUG("Recursing into array...");
188 #endif
189 ERROR_REPORTER_HERE(ASC_PROG_ERR,"No '%s' found for type '%s'",SCP(data->method_name),SCP(GetName(type)));
190 return 1;
191 }
192 }else if(array_type == GetBaseType(type)){
193 if(defaultself_visit_submodels1(c,data))err += 1;
194 }
195 }
196
197 #ifdef DEFAULT_DEBUG
198 CONSOLE_DEBUG("defaultself_visit_submodels1 return ing %d",err);
199 #endif
200 return err;
201 }
202
203 #if 0
204 /**
205 NOTE YET IMPLEMENTED: we need to be able to pass string constants to
206 methods, which I don't think is possible yet.
207 */
208 int Asc_VisitSubmodels(struct Instance *root
209 , struct gl_list_t *arglist, void *userdata
210 ){
211 struct DefaultAll_data data;
212 (void)userdata;
213
214 ERROR_REPORTER_HERE(ASC_USER_ERROR,"not implemented");
215 return 1;
216 if (arglist == NULL
217 || gl_length(arglist) == 0L
218 || gl_length((struct gl_list_t *)gl_fetch(arglist,1)) != 2
219 || gl_fetch((struct gl_list_t *)gl_fetch(arglist,1),1) == NULL
220 || gl_fetch((struct gl_list_t *)gl_fetch(arglist,1),2) == NULL
221 ){
222 ERROR_REPORTER_HERE(ASC_USER_ERROR,"EXTERNAL visit_submodels(SELF,'methodname') called with bad argument list");
223 return 1;
224 }
225
226 data.method_name = AddSymbol("default_all");
227
228 return Asc_DefaultAll1(
229 (struct Instance *)gl_fetch( (struct gl_list_t *)gl_fetch(arglist,1),1 )
230 , &data
231 );
232 }
233 #endif

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