/[ascend]/trunk/pygtk/type.cpp
ViewVC logotype

Contents of /trunk/pygtk/type.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1699 - (show annotations) (download) (as text)
Sat Jan 5 02:49:48 2008 UTC (12 years, 6 months ago) by jpye
File MIME type: text/x-c++src
File size: 5475 byte(s)
Added operator< to SymChar class (C++).
Added toString method to Dimensions class (C++).
Fixed close-button behaviour in Solver Reporter dialog (Python).
Added Library::getUnits to get list of all units of measurement in use (C++).
Added Library::getRealAtomTypes to get list of all real atomic types (C++).
Removed duplicated class definition for Dimensions in ascpy.i (C++).
Added __str__ to Python wrapping of Dimensions type.
Started working on populating data fields in UnitsDialog (Python).
Default constructor for Dimensions object now creates 'wildcard' dimension.
Cleared contents of library.i (should delete it actually).
Added Type::isFundamental (C++).
Tidied find.h formatting, #includes.

1 #include <Python.h>
2
3 #include <iostream>
4 #include <stdexcept>
5 #include <string>
6 #include <sstream>
7 using namespace std;
8
9 extern "C"{
10 #include <utilities/ascConfig.h>
11 #include <utilities/ascSignal.h>
12 #include <general/dstring.h>
13 #include <compiler/instance_enum.h>
14 #include <compiler/fractions.h>
15
16 #include <compiler/dimen.h>
17 #include <compiler/symtab.h>
18 #include <compiler/instance_io.h>
19 #include <compiler/type_desc.h>
20 #include <compiler/bintoken.h>
21 #include <compiler/library.h>
22 #include <linear/mtx.h>
23 #include <system/calc.h>
24 #include <system/relman.h>
25 #include <system/slv_client.h>
26 #include <system/system.h>
27 #include <compiler/simlist.h>
28 }
29
30 #include "type.h"
31 #include "simulation.h"
32 #include "library.h"
33 #include "dimensions.h"
34 #include "name.h"
35 #include "compiler.h"
36
37 /**
38 @TODO FIXME for some reason there are a lot of empty Type objects being created
39 */
40 Type::Type(){
41 //cerr << "CREATED EMPTY TYPE" << endl;
42 // throw runtime_error("Type::Type: Can't create new Types via C++ interface");
43 }
44
45 Type::Type(const TypeDescription *t) : t(t){
46 //cerr << "CREATED TYPE '" << getName() << "'" << endl;
47 }
48
49 const SymChar
50 Type::getName() const{
51 if(t==NULL){
52 throw runtime_error("Type::getName: t is NULL");
53 }
54 switch(GetBaseType(t)){
55 case array_type:
56 return SymChar("array");
57 case relation_type:
58 return SymChar("relation");
59 case logrel_type:
60 return SymChar("logrel");
61 case when_type:
62 return SymChar("when");
63 case set_type:
64 return SymChar("set");
65 default:
66 symchar *sym = GetName(t);
67 if(sym==NULL){
68 throw runtime_error("Unnamed type");
69 }
70 return SymChar(SCP(sym));
71 }
72 }
73
74 const int
75 Type::getParameterCount() const{
76 return GetModelParameterCount(t);
77 }
78
79 const TypeDescription *
80 Type::getInternalType() const{
81 return t;
82 }
83
84 const Dimensions
85 Type::getDimensions() const{
86 if( isRefinedConstant() ){
87 return Dimensions( GetConstantDimens(getInternalType()) );
88 }else if( isRefinedReal() ){
89 return Dimensions( GetRealDimens(getInternalType()) );
90 }else{
91 if( !isRefinedAtom() )throw runtime_error("Type::getDimensions: called with non-atom type");
92 throw runtime_error("Type::getDimensions: unrecognised type");
93 }
94 }
95
96 const bool
97 Type::isRefinedAtom() const{
98 return BaseTypeIsAtomic(t);
99 }
100
101 const bool
102 Type::isRefinedReal() const{
103 return BaseTypeIsReal(t);
104 }
105
106 const bool
107 Type::isRefinedConstant() const{
108 return BaseTypeIsConstant(t);
109 }
110
111 /**
112 Instantiate a type. This *can be* expensive, if you have selected to
113 compile your model into C-code and load a dynamic library with native
114 machine-code versions of your equations.
115
116 Once you have an instance of your model, you can start
117 to eliminate variables and attempt to solve it, see Instanc.
118
119 Note that there is some kind of dastardly underhand reference to the
120 Compiler class implicit here: the model instantiation call refers to
121 g_use_copyanon which is sort-of owned by the Compiler class.
122 */
123 Simulation
124 Type::getSimulation(const SymChar &sym
125 , const bool &rundefaultmethod
126 ){
127 /* notify the compiler of our bintoken options, if nec */
128 Compiler::instance()->sendBinaryCompilationOptions();
129
130 ERROR_REPORTER_HERE(ASC_PROG_NOTE,"Starting tree...\n");
131 error_reporter_tree_start();
132 /* ERROR_REPORTER_HERE(ASC_PROG_NOTE,"Started tree\n"); */
133
134 Instance *i = SimsCreateInstance(getInternalType()->name, sym.getInternalType(), e_normal, NULL);
135 Simulation sim(i,sym);
136
137 bool has_error;
138 if(error_reporter_tree_has_error()){
139 has_error = TRUE;
140 }else{
141 has_error = FALSE;
142 }
143
144 error_reporter_tree_end();
145
146 if(has_error){
147 stringstream ss;
148 ss << "Error(s) during instantiation of type '" << getName() << "'";
149 throw runtime_error(ss.str());
150 }else{
151 ERROR_REPORTER_HERE(ASC_USER_NOTE,"Instantiated %s",SCP(getInternalType()->name));
152 }
153
154 if(i==NULL){
155 throw runtime_error("Failed to create instance");
156 }
157
158 if(rundefaultmethod){
159 CONSOLE_DEBUG("RUNNING DEFAULT METHOD");
160 sim.runDefaultMethod();
161 }
162
163 return sim;
164 }
165
166 vector<Method>
167 Type::getMethods() const{
168 vector<Method> v;
169 struct gl_list_t *l = GetInitializationList(getInternalType());
170 if(l==NULL) return v;
171 for(int i=1, end=gl_length(l); i<=end; ++i){
172 v.push_back(Method((struct InitProcedure *)gl_fetch(l,i)));
173 }
174 return v;
175 }
176
177 Method
178 Type::getMethod(const SymChar &name) const{
179 if(GetBaseType(t)!=model_type){
180 stringstream ss;
181 ss << "Type '" << getName() << "' is not a MODEL";
182 throw runtime_error(ss.str());
183 }
184
185 struct InitProcedure *m;
186 m = FindMethod(t,name.getInternalType());
187
188 if(m==NULL){
189 stringstream ss;
190 ss << "No method named '" << name << "' in type '" << getName() << "'";
191 throw runtime_error(ss.str());
192 return NULL;
193 }
194
195 return Method(m);
196 }
197
198 const bool
199 Type::isRefinedSolverVar() const{
200 const TypeDescription *solver_var_type;
201 Type t1 = Library().findType(SymChar("solver_var"));
202 solver_var_type=t1.getInternalType();
203
204 if(MoreRefined(t, solver_var_type)==t){
205 //cerr << getName() << " IS A REFINED SOLVER_VAR" << endl;
206 return true;
207 }
208 //cerr << getName() << "IS *NOT* A REFINED SOLVER_VAR" << endl;
209 return false;
210 }
211
212 const bool
213 Type::isFundamental() const{
214 return CheckFundamental(getName().getInternalType());
215 }
216
217 const bool
218 Type::hasParameters() const{
219 return TypeHasParameterizedInsts(t);
220 }
221
222 bool
223 Type::operator<(const Type &other) const{
224 // modelled on the Unit_CmpAtomName function from UnitsProc.c in Tcl code...
225 if(!getInternalType() || !other.getInternalType() || this->isFundamental()){
226 return false;
227 }
228 return (this->getName() < other.getName());
229 }
230
231

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