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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2241 - (show annotations) (download) (as text)
Sun Aug 1 03:17:50 2010 UTC (13 years, 10 months ago) by jpye
File MIME type: text/x-csrc
File size: 6920 byte(s)
Adding test case for defaultall.c errors.
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 CONSOLE_DEBUG("starting");
134 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 /**
152 Find child models in the present model and run their 'default_self' methods
153 */
154 static
155 int defaultself_visit_submodels1(struct Instance *inst
156 , struct DefaultAll_data *data
157 ){
158 int i, n, err = 0;
159 struct Instance *c;
160 struct TypeDescription *type;
161 struct InitProcedure *method;
162 enum Proc_enum pe;
163
164 type = InstanceTypeDesc(inst);
165
166 /* loop through child atoms */
167 n = NumberChildren(inst);
168 for(i = 1; i <= n; ++i){
169 c = InstanceChild(inst,i);
170 if(c==NULL)continue;
171
172 type = InstanceTypeDesc(c);
173 if(model_type == GetBaseType(type)){
174 /* run 'default_all' for all child models */
175 method = FindMethod(type,data->method_name);
176 if(method){
177 #ifdef DEFAULT_DEBUG
178 CONSOLE_DEBUG("Running METHOD %s on '%s'",SCP(data->method_name),SCP(GetName(type)));
179 #endif
180 pe = Initialize(c , CreateIdName(ProcName(method))
181 , SCP(data->method_name)
182 ,ASCERR
183 ,0, NULL, NULL
184 );
185 if(pe!=Proc_all_ok)err += 1;
186 }else{
187 #ifdef DEFAULT_DEBUG
188 CONSOLE_DEBUG("Recursing into array...");
189 #endif
190 ERROR_REPORTER_HERE(ASC_PROG_ERR,"No '%s' found for type '%s'",SCP(data->method_name),SCP(GetName(type)));
191 return 1;
192 }
193 }else if(array_type == GetBaseType(type)){
194 if(defaultself_visit_submodels1(c,data))err += 1;
195 }
196 }
197
198 #ifdef DEFAULT_DEBUG
199 CONSOLE_DEBUG("defaultself_visit_submodels1 return ing %d",err);
200 #endif
201 return err;
202 }
203
204 #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
215 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
229 return Asc_DefaultAll1(
230 (struct Instance *)gl_fetch( (struct gl_list_t *)gl_fetch(arglist,1),1 )
231 , &data
232 );
233 }
234 #endif

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