Import code from my Subversion repository
[black_box_cellml.git] / sources / BlaBoC / BlaBoCImpl.hpp
blob026e84b3a42cf055669257030f4bc2b11a83ae3b
1 #include <exception>
2 #include "IfaceBlaBoC.hxx"
3 #include "IfaceCeVAS.hxx"
4 #include "IfaceCUSES.hxx"
5 #include "IfaceMaLaES.hxx"
6 #include "Utilities.hxx"
7 #include <string>
8 #include <set>
9 #include <map>
11 template<class C> class CleanupList
12 : public std::list<C>
14 public:
15 ~CleanupList()
17 typename CleanupList<C>::iterator i;
18 for (i = CleanupList<C>::begin(); i != CleanupList<C>::end(); i++)
19 (*i)->release_ref();
23 class BBABlaBoC
24 : public iface::blaboc_api::BlaBoC
26 public:
27 CDA_IMPL_REFCOUNT;
28 CDA_IMPL_ID;
29 CDA_IMPL_QI1(blaboc_api::BlaBoC);
31 BBABlaBoC(iface::cellml_api::Model* aModel);
32 ~BBABlaBoC();
34 void variableIsInput(iface::cellml_api::CellMLVariable* aVariable)
35 throw(std::exception&);
36 void variableIsOutput(iface::cellml_api::CellMLVariable* aVariable)
37 throw(std::exception&);
38 void othersAreOutput() throw();
39 void othersAreInput() throw();
40 void computeBlaBoC() throw(std::exception&);
42 wchar_t* errorMessage() throw();
43 wchar_t* classImplementation() throw(std::exception&);
45 private:
46 class BBAEquation
47 : public iface::XPCOM::IObject
49 public:
50 CDA_IMPL_QI0;
51 CDA_IMPL_ID;
52 CDA_IMPL_REFCOUNT;
54 BBAEquation(iface::cellml_api::CellMLElement* aContext,
55 iface::mathml_dom::MathMLElement* aEquation)
56 : _cda_refcount(1), mContext(aContext), mEquation(aEquation), used(false)
60 ObjRef<iface::cellml_api::CellMLElement> mContext;
61 ObjRef<iface::mathml_dom::MathMLElement> mEquation;
62 // Not owning because the model is owned the whole time.
63 // For black-boxes, the input variables.
64 std::list<iface::cellml_api::CellMLVariable*> mSourceVariables;
65 // Only used for black boxes.
66 std::list<iface::cellml_api::CellMLVariable*> mOutputs;
67 bool used;
70 void BuildEquationList(std::list<BBAEquation*>& eqns);
71 bool IsEquationExternal(iface::mathml_dom::MathMLApplyElement* mae);
72 void ClassifyEquations(std::list<BBAEquation*>& eqns,
73 std::list<BBAEquation*>& aBlackBoxes,
74 std::list<BBAEquation*>& aWhiteBoxes);
75 void getServices(
76 ObjRef<iface::cellml_services::CUSES>& cuses,
77 ObjRef<iface::cellml_services::MaLaESTransform>& mt
79 void determineEquationVariables(
80 iface::cellml_services::CUSES* cuses,
81 iface::cellml_services::MaLaESTransform * mt,
82 std::list<BBAEquation*>& eqns
84 void processConstants(
85 iface::cellml_services::MaLaESTransform* mt,
86 iface::cellml_services::CUSES* cuses,
87 std::set<iface::cellml_api::CellMLVariable*>& allSourceVariables,
88 std::set<iface::cellml_api::CellMLVariable*>& directConstants,
89 std::set<iface::cellml_api::CellMLVariable*>& allConstants,
90 std::set<iface::cellml_api::CellMLVariable*>& wanted,
91 std::list<BBAEquation*>& whiteBoxes,
92 std::wstring& aComment,
93 std::wstring& aCode,
94 std::list<std::wstring>& aSupplementary
96 void determineEvaluationDAG(
97 std::set<iface::cellml_api::CellMLVariable*>& know,
98 std::set<iface::cellml_api::CellMLVariable*>& want,
99 std::list<BBAEquation*>& equations,
100 std::multimap<iface::cellml_api::CellMLVariable*,
101 iface::cellml_api::CellMLVariable*>& forwardEdges,
102 std::multimap<iface::cellml_api::CellMLVariable*,
103 iface::cellml_api::CellMLVariable*>& backEdges
105 void dfsComputeSet(iface::cellml_services::MaLaESTransform* mt,
106 iface::cellml_services::CUSES* cuses,
107 std::set<iface::cellml_api::CellMLVariable*>& targets,
108 std::multimap<iface::cellml_api::CellMLVariable*,
109 iface::cellml_api::CellMLVariable*>& backEdges,
110 std::wstring& aCode,
111 std::list<std::wstring>& aSupplementary
113 void dfsComputeVariable
115 iface::cellml_services::MaLaESTransform* mt,
116 iface::cellml_services::CUSES* cuses,
117 iface::cellml_api::CellMLVariable* var,
118 std::multimap<iface::cellml_api::CellMLVariable*,
119 iface::cellml_api::CellMLVariable*>& backEdges,
120 std::wstring& aCode,
121 std::list<std::wstring>& aSupplementary
123 void WriteDirectAssignment
125 std::wstring& aCode,
126 std::list<std::wstring>& aSupplementary,
127 iface::cellml_services::MaLaESTransform* mt,
128 iface::cellml_services::CUSES* cuses,
129 iface::cellml_api::CellMLVariable* var,
130 iface::mathml_dom::MathMLElement* lhs,
131 iface::mathml_dom::MathMLElement* rhs
133 void determineBlackBoxIO
135 iface::cellml_services::MaLaESTransform* mt,
136 iface::cellml_services::CUSES* cuses,
137 std::list<BBAEquation*>& blackBoxes,
138 std::set<iface::cellml_api::CellMLVariable*>& blackBoxInputs,
139 std::set<iface::cellml_api::CellMLVariable*>& blackBoxOutputs,
140 std::set<iface::cellml_api::CellMLVariable*>& blackBoxIO,
141 uint32_t& tmpInMax,
142 uint32_t& tmpOutMax,
143 std::wstring& blackBoxCode
145 void addBlackBoxInput
147 iface::cellml_services::MaLaESTransform* mt,
148 iface::cellml_services::CUSES* cuses,
149 BBAEquation* eqn,
150 iface::mathml_dom::MathMLCiElement* el,
151 std::set<iface::cellml_api::CellMLVariable*>& blackBoxInputs,
152 std::set<iface::cellml_api::CellMLVariable*>& blackBoxIO
154 void addBlackBoxOutput
156 iface::cellml_services::MaLaESTransform* mt,
157 iface::cellml_services::CUSES* cuses,
158 BBAEquation* eqn,
159 iface::mathml_dom::MathMLCiElement* el,
160 std::set<iface::cellml_api::CellMLVariable*>& blackBoxOutputs,
161 std::set<iface::cellml_api::CellMLVariable*>& blackBoxIO
163 void processTraining
165 iface::cellml_services::MaLaESTransform* mt,
166 iface::cellml_services::CUSES* cuses,
167 std::set<iface::cellml_api::CellMLVariable*>& blackBoxIO,
168 std::set<iface::cellml_api::CellMLVariable*>& allConstants,
169 std::set<iface::cellml_api::CellMLVariable*>& wanted,
170 std::list<BBAEquation*>& whiteBoxes,
171 std::list<BBAEquation*>& blackBoxes,
172 std::wstring& trainingCode,
173 std::list<std::wstring>& supplements
175 void processRun
177 iface::cellml_services::MaLaESTransform* mt,
178 iface::cellml_services::CUSES* cuses,
179 std::set<iface::cellml_api::CellMLVariable*>& blackBoxInputs,
180 std::set<iface::cellml_api::CellMLVariable*>& blackBoxOutputs,
181 std::set<iface::cellml_api::CellMLVariable*>& allConstants,
182 std::set<iface::cellml_api::CellMLVariable*>& wanted,
183 std::list<BBAEquation*>& whiteBoxes,
184 std::list<BBAEquation*>& blackBoxes,
185 std::wstring& runCode,
186 std::list<std::wstring>& supplements
188 void assignVariableIndices(std::set<iface::cellml_api::CellMLVariable*>& candidates,
189 std::wstring& comment, uint32_t& count);
190 void resetSeen(std::set<iface::cellml_api::CellMLVariable*>& resetSet);
191 std::wstring EscapeString(const std::wstring& input);
193 std::wstring mMessage;
194 std::wstring mImpl;
195 ObjRef<iface::cellml_api::Model> mModel;
196 ObjRef<iface::cellml_services::CeVAS> mCeVAS;
197 ObjRef<iface::cellml_services::AnnotationSet> mAnnoSet;
198 // Not owner because mModel already owns them, and we own it.
199 std::set<iface::cellml_api::CellMLVariable*> mInputVariableSet;
200 std::set<iface::cellml_api::CellMLVariable*> mOutputVariableSet;
201 uint32_t mUniqueSolve, mInputIndex, mOutputIndex;
204 class BBABlaBoCBootstrap
205 : public iface::blaboc_api::BlaBoCBootstrap
207 public:
208 CDA_IMPL_REFCOUNT;
209 CDA_IMPL_ID;
210 CDA_IMPL_QI1(blaboc_api::BlaBoCBootstrap);
212 BBABlaBoCBootstrap()
213 : _cda_refcount(1) {}
215 iface::blaboc_api::BlaBoC* createBlaBoC(iface::cellml_api::Model* aModel)
216 throw()
218 return new BBABlaBoC(aModel);
221 char* assembleBlaBoC(const wchar_t* aSource, uint32_t* aLen) throw();