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

Contents of /trunk/base/generic/packages/defaultall.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, 5 months ago) by johnpye
File MIME type: text/x-csrc
File size: 4761 byte(s)
Added EXTERNAL asc_default_all plus test cases.
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 <packages/defaultall.h>
27
28 #include <compiler/proc.h>
29 #include <compiler/name.h>
30
31 #include <utilities/ascConfig.h>
32 #include <utilities/ascPrint.h>
33 #include <utilities/ascPanic.h>
34
35 #include <compiler/instquery.h>
36 #include <compiler/child.h>
37 #include <compiler/type_desc.h>
38 #include <compiler/symtab.h>
39 #include <compiler/atomvalue.h>
40 #include <compiler/visitinst.h>
41 #include <compiler/parentchild.h>
42 #include <compiler/library.h>
43 #include <compiler/watchpt.h>
44 #include <compiler/initialize.h>
45
46 /**
47 Find atom children in the present model and set them to their ATOM DEFAULT
48 values.
49 */
50 static
51 int Asc_DefaultSelf1(struct Instance *inst){
52 int i,n;
53 struct Instance *c;
54 struct TypeDescription *type;
55
56 /* default any child atoms' values */
57 n = NumberChildren(inst);
58 for(i = 1; i <= n; ++i){
59 CONSOLE_DEBUG("Child %d...", i);
60 c = InstanceChild(inst,i);
61 type = InstanceTypeDesc(c);
62 if(BaseTypeIsAtomic(type)){
63 if(!AtomDefaulted(type))continue;
64 switch(GetBaseType(type)){
65 case real_type: SetRealAtomValue(c, GetRealDefault(type), 0); break;
66 case integer_type: SetIntegerAtomValue(c, GetIntDefault(type), 0); break;
67 case boolean_type: SetBooleanAtomValue(c, GetBoolDefault(type), 0); break;
68 case symbol_type: SetSymbolAtomValue(c, GetSymDefault(type)); break;
69 case set_type: /* what is the mechanism for defaulting of sets? */
70 default: ASC_PANIC("invalid type");
71 }
72 CONSOLE_DEBUG("Reset atom to default value");
73 }else if(GetBaseType(type)==array_type){
74 /* descend into arrays */
75 Asc_DefaultSelf1(c);
76 }
77 }
78 return 0;
79 }
80
81 struct DefaultAll_data{
82 symchar *default_all;
83 };
84
85 /**
86 Find child models in the present model and run their 'default_all' methods
87 */
88 static
89 int Asc_DefaultAll1(struct Instance *inst, struct DefaultAll_data *data){
90 int i, n, err = 0;
91 struct Instance *c;
92 struct TypeDescription *type;
93 struct InitProcedure *method;
94 enum Proc_enum pe;
95
96 type = InstanceTypeDesc(inst);
97
98 /* loop through child atoms */
99 n = NumberChildren(inst);
100 for(i = 1; i <= n; ++i){
101 c = InstanceChild(inst,i);
102 type = InstanceTypeDesc(c);
103 if(model_type == GetBaseType(type)){
104 /* run 'default_all' for all child models */
105 method = FindMethod(type,data->default_all);
106 if(method){
107 CONSOLE_DEBUG("Running default_all on '%s'",SCP(GetName(type)));
108 pe = Initialize(c , CreateIdName(ProcName(method)), "__not_named__"
109 ,ASCERR
110 ,0, NULL, NULL
111 );
112 if(pe!=Proc_all_ok)err += 1;
113 }else{
114 CONSOLE_DEBUG("Recursing into array...");
115 ERROR_REPORTER_HERE(ASC_PROG_ERR,"No 'default_all' found for type '%s'",SCP(GetName(type)));
116 return 1;
117 }
118 }else if(array_type == GetBaseType(type)){
119 if(Asc_DefaultAll1(c,data))err += 1;
120 }
121 }
122
123 return err;
124 }
125
126 int Asc_DefaultSelf(struct Instance *root, struct gl_list_t *arglist, void *userdata){
127 /* arglist is a list of gllist of instances */
128 if (arglist == NULL ||
129 gl_length(arglist) == 0L ||
130 gl_length((struct gl_list_t *)gl_fetch(arglist,1)) != 1 ||
131 gl_fetch((struct gl_list_t *)gl_fetch(arglist,1),1) == NULL) {
132 return Asc_DefaultSelf1(root);
133 }else{
134 return Asc_DefaultSelf1((struct Instance *)gl_fetch( (struct gl_list_t *)gl_fetch(arglist,1),1 ));
135 }
136 }
137
138 int Asc_DefaultAll(struct Instance *root, struct gl_list_t *arglist, void *userdata){
139 struct DefaultAll_data data;
140 data.default_all = AddSymbol("default_all");
141
142 /* arglist is a list of gllist of instances */
143 if (arglist == NULL ||
144 gl_length(arglist) == 0L ||
145 gl_length((struct gl_list_t *)gl_fetch(arglist,1)) != 1 ||
146 gl_fetch((struct gl_list_t *)gl_fetch(arglist,1),1) == NULL) {
147 return Asc_DefaultAll1(root,&data);
148 }else{
149 return Asc_DefaultAll1((struct Instance *)gl_fetch( (struct gl_list_t *)gl_fetch(arglist,1),1 )
150 , &data
151 );
152 }
153 }

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