<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/> <title>C++ API frePPLe: forecastsolver.cpp Source File</title> <link href="doxygen.css" rel="stylesheet" type="text/css"> <link href="tabs.css" rel="stylesheet" type="text/css"> <link href="../styles.css" rel="stylesheet" type="text/css"> </head> <body> <div id="container"> <div id="menubar"> <div id="logo" align="center"> <br/><img src='../frepple.bmp' alt="frepple" /><br/> <a href='http://www.frepple.com/'> <strong>a Free<br/>Production Planning<br/>Library</strong> </a> </div> <div id="menu"> <br/> <h3><a href='../Main/HomePage.html'>Main</a></h3> <h3><a href='../UI/Main.html'>User Manual</a></h3> <h3><a href='../Tutorial/Main.html'>Tutorial</a></h3> <h3><a href='../Frepple/Main.html'>Reference Manual</a></h3> <h3><a href='../Main/FAQ.html'>FAQ</a></h3> <h3><a href='index.html'>C++ API</a></h3> <br/> </div> </div> <div id="content"> <br/> <!-- Generated by Doxygen 1.6.1 --> <div class="navigation" id="top"> <div class="tabs"> <ul> <li><a href="index.html"><span>Main Page</span></a></li> <li><a href="pages.html"><span>Related Pages</span></a></li> <li><a href="namespaces.html"><span>Namespaces</span></a></li> <li><a href="annotated.html"><span>Classes</span></a></li> <li class="current"><a href="files.html"><span>Files</span></a></li> <li><a href="dirs.html"><span>Directories</span></a></li> </ul> </div> <div class="tabs"> <ul> <li><a href="files.html"><span>File List</span></a></li> <li><a href="globals.html"><span>File Members</span></a></li> </ul> </div> <div class="navpath"><a class="el" href="dir_cfc6129efafb4deb1379318d01a4b532.html">modules</a> » <a class="el" href="dir_4a975c66df64d91ea50e31e1c6371883.html">forecast</a> </div> </div> <div class="contents"> <h1>forecastsolver.cpp</h1><a href="a00205.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/***************************************************************************</span> <a name="l00002"></a>00002 <span class="comment"> file : $URL: https://frepple.svn.sourceforge.net/svnroot/frepple/trunk/modules/forecast/forecastsolver.cpp $</span> <a name="l00003"></a>00003 <span class="comment"> version : $LastChangedRevision: 1315 $ $LastChangedBy: jdetaeye $</span> <a name="l00004"></a>00004 <span class="comment"> date : $LastChangedDate: 2010-07-17 18:08:53 +0200 (Sat, 17 Jul 2010) $</span> <a name="l00005"></a>00005 <span class="comment"> ***************************************************************************/</span> <a name="l00006"></a>00006 <a name="l00007"></a>00007 <span class="comment">/***************************************************************************</span> <a name="l00008"></a>00008 <span class="comment"> * *</span> <a name="l00009"></a>00009 <span class="comment"> * Copyright (C) 2007-2010 by Johan De Taeye *</span> <a name="l00010"></a>00010 <span class="comment"> * *</span> <a name="l00011"></a>00011 <span class="comment"> * This library is free software; you can redistribute it and/or modify it *</span> <a name="l00012"></a>00012 <span class="comment"> * under the terms of the GNU Lesser General Public License as published *</span> <a name="l00013"></a>00013 <span class="comment"> * by the Free Software Foundation; either version 2.1 of the License, or *</span> <a name="l00014"></a>00014 <span class="comment"> * (at your option) any later version. *</span> <a name="l00015"></a>00015 <span class="comment"> * *</span> <a name="l00016"></a>00016 <span class="comment"> * This library is distributed in the hope that it will be useful, *</span> <a name="l00017"></a>00017 <span class="comment"> * but WITHOUT ANY WARRANTY; without even the implied warranty of *</span> <a name="l00018"></a>00018 <span class="comment"> * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *</span> <a name="l00019"></a>00019 <span class="comment"> * General Public License for more details. *</span> <a name="l00020"></a>00020 <span class="comment"> * *</span> <a name="l00021"></a>00021 <span class="comment"> * You should have received a copy of the GNU Lesser General Public *</span> <a name="l00022"></a>00022 <span class="comment"> * License along with this library; if not, write to the Free Software *</span> <a name="l00023"></a>00023 <span class="comment"> * Foundation Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,*</span> <a name="l00024"></a>00024 <span class="comment"> * USA *</span> <a name="l00025"></a>00025 <span class="comment"> * *</span> <a name="l00026"></a>00026 <span class="comment"> ***************************************************************************/</span> <a name="l00027"></a>00027 <a name="l00028"></a>00028 <span class="preprocessor">#include "<a class="code" href="a00204.html" title="Header file for the module forecast.">forecast.h</a>"</span> <a name="l00029"></a>00029 <a name="l00030"></a>00030 <span class="keyword">namespace </span>module_forecast <a name="l00031"></a>00031 { <a name="l00032"></a>00032 <a name="l00033"></a>00033 <span class="keyword">const</span> <a class="code" href="a00115.html" title="This class stores metadata about the classes in the library. The stored information...">MetaClass</a> *<a class="code" href="a00075.html#a4927b0e91b356b353486a68be54411a0">ForecastSolver::metadata</a>; <a name="l00034"></a>00034 <a name="l00035"></a><a class="code" href="a00075.html#a5fd4c59863d2941f85d4f23fe3446f07">00035</a> <span class="keywordtype">int</span> <a class="code" href="a00075.html#a5fd4c59863d2941f85d4f23fe3446f07">ForecastSolver::initialize</a>() <a name="l00036"></a>00036 { <a name="l00037"></a>00037 <span class="comment">// Initialize the metadata</span> <a name="l00038"></a>00038 <a class="code" href="a00075.html#a4927b0e91b356b353486a68be54411a0">metadata</a> = <span class="keyword">new</span> <a class="code" href="a00115.html" title="This class stores metadata about the classes in the library. The stored information...">MetaClass</a>(<span class="stringliteral">"solver"</span>, <span class="stringliteral">"solver_forecast"</span>, <a name="l00039"></a>00039 Object::createString<ForecastSolver>); <a name="l00040"></a>00040 <a name="l00041"></a>00041 <span class="comment">// Initialize the Python class</span> <a name="l00042"></a>00042 <span class="keywordflow">return</span> <a class="code" href="a00075.html#a5fd4c59863d2941f85d4f23fe3446f07">FreppleClass<ForecastSolver,Solver>::initialize</a>(); <a name="l00043"></a>00043 } <a name="l00044"></a>00044 <a name="l00045"></a>00045 <a name="l00046"></a><a class="code" href="a00075.html#a00a1cb8d4cc26b2225890a23ea253f15">00046</a> <span class="keywordtype">bool</span> <a class="code" href="a00075.html#a00a1cb8d4cc26b2225890a23ea253f15">ForecastSolver::callback</a>(<a class="code" href="a00051.html" title="Represents the (independent) demand in the system. It can represent a customer order...">Demand</a>* l, <span class="keyword">const</span> <a class="code" href="a00257.html#aec6d4a238b0cd9ffc8be8f10fd9b652f">Signal</a> a) <a name="l00047"></a>00047 { <a name="l00048"></a>00048 <span class="comment">// Call the netting function</span> <a name="l00049"></a>00049 <a class="code" href="a00075.html#ae732b3df58aae09f8edf43409d288632">solve</a>(l, NULL); <a name="l00050"></a>00050 <a name="l00051"></a>00051 <span class="comment">// Always return 'okay'</span> <a name="l00052"></a>00052 <span class="keywordflow">return</span> <span class="keyword">true</span>; <a name="l00053"></a>00053 } <a name="l00054"></a>00054 <a name="l00055"></a>00055 <a name="l00056"></a><a class="code" href="a00075.html#abb0d7eac37463f328f3f6d87f95bcb06">00056</a> <span class="keywordtype">void</span> <a class="code" href="a00075.html#abb0d7eac37463f328f3f6d87f95bcb06">ForecastSolver::writeElement</a>(<a class="code" href="a00189.html" title="Base class for writing XML formatted data to an output stream.">XMLOutput</a> *o, <span class="keyword">const</span> <a class="code" href="a00096.html" title="This class defines a keyword for the frePPLe data model.">Keyword</a>& tag, <a class="code" href="a00257.html#a20c2c7e4c6b668e71c154ae3eb18f398">mode</a> m)<span class="keyword"> const</span> <a name="l00057"></a>00057 <span class="keyword"></span>{ <a name="l00058"></a>00058 <span class="comment">// Writing a reference</span> <a name="l00059"></a>00059 <span class="keywordflow">if</span> (m == <a class="code" href="a00257.html#a20c2c7e4c6b668e71c154ae3eb18f398a874240ada53cc5b608466064d86d91ae">REFERENCE</a>) <a name="l00060"></a>00060 { <a name="l00061"></a>00061 o-><a class="code" href="a00189.html#a711922eea4cfe3c33b97e4188dacc7d2">writeElement</a> <a name="l00062"></a>00062 (tag, <a class="code" href="a00177.html#aeb3841a93774dfc303e3df16304b5af2">Tags::tag_name</a>, <a class="code" href="a00182.html#a7416ca220c55147bc1276becc5b9de01">getName</a>(), <a class="code" href="a00177.html#a6745112e0237e378e2b0cbe41aa5926a">Tags::tag_type</a>, <a class="code" href="a00075.html#af578993f598ddfbcd14243a211347a75">getType</a>().<a class="code" href="a00171.html" title="This class is an implementation of the &quot;visitor&quot; design pattern. It is...">type</a>); <a name="l00063"></a>00063 <span class="keywordflow">return</span>; <a name="l00064"></a>00064 } <a name="l00065"></a>00065 <a name="l00066"></a>00066 <span class="comment">// Write the complete object</span> <a name="l00067"></a>00067 <span class="keywordflow">if</span> (m != <a class="code" href="a00257.html#a20c2c7e4c6b668e71c154ae3eb18f398a2ad5e75fe119e297630882d182832243">NOHEADER</a>) o-><a class="code" href="a00189.html#a5c1811dae2e3eae344ad366f6441a8a5">BeginObject</a> <a name="l00068"></a>00068 (tag, <a class="code" href="a00177.html#aeb3841a93774dfc303e3df16304b5af2">Tags::tag_name</a>, <a class="code" href="a00182.html#a7416ca220c55147bc1276becc5b9de01">getName</a>(), <a class="code" href="a00177.html#a6745112e0237e378e2b0cbe41aa5926a">Tags::tag_type</a>, <a class="code" href="a00075.html#af578993f598ddfbcd14243a211347a75">getType</a>().<a class="code" href="a00171.html" title="This class is an implementation of the &quot;visitor&quot; design pattern. It is...">type</a>); <a name="l00069"></a>00069 <a name="l00070"></a>00070 <span class="comment">// Write the parent class</span> <a name="l00071"></a>00071 <a class="code" href="a00075.html#abb0d7eac37463f328f3f6d87f95bcb06">Solver::writeElement</a>(o, tag, <a class="code" href="a00257.html#a20c2c7e4c6b668e71c154ae3eb18f398a2ad5e75fe119e297630882d182832243">NOHEADER</a>); <a name="l00072"></a>00072 } <a name="l00073"></a>00073 <a name="l00074"></a>00074 <a name="l00075"></a><a class="code" href="a00075.html#ae732b3df58aae09f8edf43409d288632">00075</a> <span class="keywordtype">void</span> <a class="code" href="a00075.html#ae732b3df58aae09f8edf43409d288632">ForecastSolver::solve</a>(<span class="keyword">const</span> <a class="code" href="a00051.html" title="Represents the (independent) demand in the system. It can represent a customer order...">Demand</a>* l, <span class="keywordtype">void</span>* v) <a name="l00076"></a>00076 { <a name="l00077"></a>00077 <span class="comment">// Forecast don't net themselves, and hidden demands either...</span> <a name="l00078"></a>00078 <span class="keywordflow">if</span> (!l || dynamic_cast<const Forecast*>(l) || l-><a class="code" href="a00051.html#acab54fd6080088303350b0390c35cf30">getHidden</a>()) <span class="keywordflow">return</span>; <a name="l00079"></a>00079 <a name="l00080"></a>00080 <span class="comment">// Message</span> <a name="l00081"></a>00081 <span class="keywordflow">if</span> (<a class="code" href="a00171.html#a7331226cd4ef515184dff2d118c43423">getLogLevel</a>()>0) <a name="l00082"></a>00082 <a class="code" href="a00257.html#a4af9f241e955122820c5e81ceb33edb5">logger</a> << <span class="stringliteral">" Netting of demand '"</span> << l << <span class="stringliteral">"' ('"</span> << l-><a class="code" href="a00051.html#ada8b8f6e9163a3f334d9799d14c5a7ec">getCustomer</a>() <a name="l00083"></a>00083 << <span class="stringliteral">"','"</span> << l-><a class="code" href="a00051.html#a49f5ca2d8cf91112c41c1eaa04626889">getItem</a>() << <span class="stringliteral">"', '"</span> << l-><a class="code" href="a00051.html#aa8a606ec515a77824b54bce3053bf182">getDeliveryOperation</a>() <a name="l00084"></a>00084 << <span class="stringliteral">"'): "</span> << l-><a class="code" href="a00051.html#ab21889e9a16e3701a84ba8f3aa68628b">getDue</a>() << <span class="stringliteral">", "</span> << l-><a class="code" href="a00051.html#af5fe6f1b6e8f9b2c24ce3479b6546444">getQuantity</a>() << endl; <a name="l00085"></a>00085 <a name="l00086"></a>00086 <span class="comment">// Find a matching forecast</span> <a name="l00087"></a>00087 <a class="code" href="a00072.html" title="This class represents a bucketized demand signal.">Forecast</a> *fcst = matchDemandToForecast(l); <a name="l00088"></a>00088 <a name="l00089"></a>00089 <span class="keywordflow">if</span> (!fcst) <a name="l00090"></a>00090 { <a name="l00091"></a>00091 <span class="comment">// Message</span> <a name="l00092"></a>00092 <span class="keywordflow">if</span> (<a class="code" href="a00171.html#a7331226cd4ef515184dff2d118c43423">getLogLevel</a>()>0) <a name="l00093"></a>00093 <a class="code" href="a00257.html#a4af9f241e955122820c5e81ceb33edb5">logger</a> << <span class="stringliteral">" No matching forecast available"</span> << endl; <a name="l00094"></a>00094 <span class="keywordflow">return</span>; <a name="l00095"></a>00095 } <a name="l00096"></a>00096 <span class="keywordflow">else</span> <span class="keywordflow">if</span> (<a class="code" href="a00171.html#a7331226cd4ef515184dff2d118c43423">getLogLevel</a>()>0) <a name="l00097"></a>00097 <a class="code" href="a00257.html#a4af9f241e955122820c5e81ceb33edb5">logger</a> << <span class="stringliteral">" Matching forecast: "</span> << fcst << endl; <a name="l00098"></a>00098 <a name="l00099"></a>00099 <span class="comment">// Netting the order from the forecast</span> <a name="l00100"></a>00100 netDemandFromForecast(l,fcst); <a name="l00101"></a>00101 } <a name="l00102"></a>00102 <a name="l00103"></a>00103 <a name="l00104"></a><a class="code" href="a00075.html#ac3feb97e41920fc9f83dc436b2f7db21">00104</a> <span class="keywordtype">void</span> <a class="code" href="a00075.html#ae732b3df58aae09f8edf43409d288632">ForecastSolver::solve</a>(<span class="keywordtype">void</span> *v) <a name="l00105"></a>00105 { <a name="l00106"></a>00106 <span class="comment">// Sort the demands using the same sort function as used for planning.</span> <a name="l00107"></a>00107 <span class="comment">// Note: the memory consumption of the sorted list can be significant</span> <a name="l00108"></a>00108 sortedDemandList l; <a name="l00109"></a>00109 <span class="keywordflow">for</span> (Demand::iterator i = Demand::begin(); i != <a class="code" href="a00085.html#abb9d04973550ac71037718511358f2ff">Demand::end</a>(); ++i) <a name="l00110"></a>00110 <span class="comment">// Only sort non-forecast demand.</span> <a name="l00111"></a>00111 <span class="keywordflow">if</span> (!dynamic_cast<Forecast*>(&*i) <a name="l00112"></a>00112 && !<span class="keyword">dynamic_cast<</span><a class="code" href="a00073.html" title="This class represents a forecast value in a time bucket.">ForecastBucket</a>*<span class="keyword">></span>(&*i)) <a name="l00113"></a>00113 l.insert(&*i); <a name="l00114"></a>00114 <a name="l00115"></a>00115 <span class="comment">// Netting loop</span> <a name="l00116"></a>00116 <span class="keywordflow">for</span>(sortedDemandList::iterator i = l.begin(); i != l.end(); ++i) <a name="l00117"></a>00117 <span class="keywordflow">try</span> {<a class="code" href="a00075.html#ae732b3df58aae09f8edf43409d288632">solve</a>(*i, NULL);} <a name="l00118"></a>00118 <span class="keywordflow">catch</span> (...) <a name="l00119"></a>00119 { <a name="l00120"></a>00120 <span class="comment">// Error message</span> <a name="l00121"></a>00121 <a class="code" href="a00257.html#a4af9f241e955122820c5e81ceb33edb5">logger</a> << <span class="stringliteral">"Error: Caught an exception while netting demand '"</span> <a name="l00122"></a>00122 << (*i)->getName() << <span class="stringliteral">"':"</span> << endl; <a name="l00123"></a>00123 <span class="keywordflow">try</span> {<span class="keywordflow">throw</span>;} <a name="l00124"></a>00124 <span class="keywordflow">catch</span> (bad_exception&) {<a class="code" href="a00257.html#a4af9f241e955122820c5e81ceb33edb5">logger</a> << <span class="stringliteral">" bad exception"</span> << endl;} <a name="l00125"></a>00125 <span class="keywordflow">catch</span> (exception& e) {<a class="code" href="a00257.html#a4af9f241e955122820c5e81ceb33edb5">logger</a> << <span class="stringliteral">" "</span> << e.what() << endl;} <a name="l00126"></a>00126 <span class="keywordflow">catch</span> (...) {<a class="code" href="a00257.html#a4af9f241e955122820c5e81ceb33edb5">logger</a> << <span class="stringliteral">" Unknown type"</span> << endl;} <a name="l00127"></a>00127 } <a name="l00128"></a>00128 } <a name="l00129"></a>00129 <a name="l00130"></a>00130 <a name="l00131"></a>00131 <a class="code" href="a00072.html" title="This class represents a bucketized demand signal.">Forecast</a>* ForecastSolver::matchDemandToForecast(<span class="keyword">const</span> <a class="code" href="a00051.html" title="Represents the (independent) demand in the system. It can represent a customer order...">Demand</a>* l) <a name="l00132"></a>00132 { <a name="l00133"></a>00133 pair<const Item*, const Customer*> key <a name="l00134"></a>00134 = make_pair(&*(l-><a class="code" href="a00051.html#a49f5ca2d8cf91112c41c1eaa04626889">getItem</a>()), &*(l-><a class="code" href="a00051.html#ada8b8f6e9163a3f334d9799d14c5a7ec">getCustomer</a>())); <a name="l00135"></a>00135 <a name="l00136"></a>00136 <span class="keywordflow">do</span> <span class="comment">// Loop through second dimension</span> <a name="l00137"></a>00137 { <a name="l00138"></a>00138 <span class="keywordflow">do</span> <span class="comment">// Loop through first dimension</span> <a name="l00139"></a>00139 { <a name="l00140"></a>00140 Forecast::MapOfForecasts::iterator x = Forecast::ForecastDictionary.lower_bound(key); <a name="l00141"></a>00141 <a name="l00142"></a>00142 <span class="comment">// Loop through all matching keys</span> <a name="l00143"></a>00143 <span class="keywordflow">while</span> (x != Forecast::ForecastDictionary.<a class="code" href="a00085.html#abb9d04973550ac71037718511358f2ff">end</a>() && x->first == key) <a name="l00144"></a>00144 { <a name="l00145"></a>00145 <span class="keywordflow">if</span> (!<a class="code" href="a00072.html#ade90bb03d2f7b17823c1f1bb9cd31c8b">Forecast::getMatchUsingDeliveryOperation</a>() <a name="l00146"></a>00146 || x->second->getDeliveryOperation() == l-><a class="code" href="a00051.html#aa8a606ec515a77824b54bce3053bf182">getDeliveryOperation</a>()) <a name="l00147"></a>00147 <span class="comment">// Bingo! Found a matching key, if required plus matching delivery operation</span> <a name="l00148"></a>00148 <span class="keywordflow">return</span> x->second; <a name="l00149"></a>00149 <span class="keywordflow">else</span> <a name="l00150"></a>00150 ++ x; <a name="l00151"></a>00151 } <a name="l00152"></a>00152 <span class="comment">// Not found: try a higher level match in first dimension</span> <a name="l00153"></a>00153 <span class="keywordflow">if</span> (Forecast::Customer_Then_Item_Hierarchy) <a name="l00154"></a>00154 { <a name="l00155"></a>00155 <span class="comment">// First customer hierarchy</span> <a name="l00156"></a>00156 <span class="keywordflow">if</span> (key.second) key.second = key.second->getOwner(); <a name="l00157"></a>00157 <span class="keywordflow">else</span> <span class="keywordflow">break</span>; <a name="l00158"></a>00158 } <a name="l00159"></a>00159 <span class="keywordflow">else</span> <a name="l00160"></a>00160 { <a name="l00161"></a>00161 <span class="comment">// First item hierarchy</span> <a name="l00162"></a>00162 <span class="keywordflow">if</span> (key.first) key.first = key.first->getOwner(); <a name="l00163"></a>00163 <span class="keywordflow">else</span> <span class="keywordflow">break</span>; <a name="l00164"></a>00164 } <a name="l00165"></a>00165 } <a name="l00166"></a>00166 <span class="keywordflow">while</span> (<span class="keyword">true</span>); <a name="l00167"></a>00167 <a name="l00168"></a>00168 <span class="comment">// Not found at any level in the first dimension</span> <a name="l00169"></a>00169 <a name="l00170"></a>00170 <span class="comment">// Try a new level in the second dimension</span> <a name="l00171"></a>00171 <span class="keywordflow">if</span> (Forecast::Customer_Then_Item_Hierarchy) <a name="l00172"></a>00172 { <a name="l00173"></a>00173 <span class="comment">// Second is item</span> <a name="l00174"></a>00174 <span class="keywordflow">if</span> (key.first) key.first = key.first->getOwner(); <a name="l00175"></a>00175 <span class="keywordflow">else</span> <span class="keywordflow">return</span> NULL; <a name="l00176"></a>00176 <span class="comment">// Reset to lowest level in the first dimension again</span> <a name="l00177"></a>00177 key.second = &*(l-><a class="code" href="a00051.html#ada8b8f6e9163a3f334d9799d14c5a7ec">getCustomer</a>()); <a name="l00178"></a>00178 } <a name="l00179"></a>00179 <span class="keywordflow">else</span> <a name="l00180"></a>00180 { <a name="l00181"></a>00181 <span class="comment">// Second is customer</span> <a name="l00182"></a>00182 <span class="keywordflow">if</span> (key.second) key.second = key.second->getOwner(); <a name="l00183"></a>00183 <span class="keywordflow">else</span> <span class="keywordflow">return</span> NULL; <a name="l00184"></a>00184 <span class="comment">// Reset to lowest level in the first dimension again</span> <a name="l00185"></a>00185 key.first = &*(l-><a class="code" href="a00051.html#a49f5ca2d8cf91112c41c1eaa04626889">getItem</a>()); <a name="l00186"></a>00186 } <a name="l00187"></a>00187 } <a name="l00188"></a>00188 <span class="keywordflow">while</span> (<span class="keyword">true</span>); <a name="l00189"></a>00189 } <a name="l00190"></a>00190 <a name="l00191"></a>00191 <a name="l00192"></a>00192 <span class="keywordtype">void</span> ForecastSolver::netDemandFromForecast(<span class="keyword">const</span> <a class="code" href="a00051.html" title="Represents the (independent) demand in the system. It can represent a customer order...">Demand</a>* dmd, Forecast* fcst) <a name="l00193"></a>00193 { <a name="l00194"></a>00194 <span class="comment">// Find the bucket with the due date</span> <a name="l00195"></a>00195 ForecastBucket* zerobucket = NULL; <a name="l00196"></a>00196 <span class="keywordflow">for</span> (<a class="code" href="a00083.html#af604aa38df25c5555db2cee12da24a23">Forecast::memberIterator</a> i = fcst->beginMember(); i != fcst->endMember(); ++i) <a name="l00197"></a>00197 { <a name="l00198"></a>00198 zerobucket = <span class="keyword">dynamic_cast<</span>ForecastBucket*<span class="keyword">></span>(&*i); <a name="l00199"></a>00199 <span class="keywordflow">if</span> (zerobucket && zerobucket->getDueRange().within(dmd-><a class="code" href="a00051.html#ab21889e9a16e3701a84ba8f3aa68628b">getDue</a>())) <span class="keywordflow">break</span>; <a name="l00200"></a>00200 } <a name="l00201"></a>00201 <span class="keywordflow">if</span> (!zerobucket) <a name="l00202"></a>00202 <span class="keywordflow">throw</span> <a class="code" href="a00111.html" title="An exception of this type is thrown when the library gets in an inconsistent state...">LogicException</a>(<span class="stringliteral">"Can't find forecast bucket for "</span> <a name="l00203"></a>00203 + <span class="keywordtype">string</span>(dmd-><a class="code" href="a00051.html#ab21889e9a16e3701a84ba8f3aa68628b">getDue</a>()) + <span class="stringliteral">" in forecast '"</span> + fcst->getName() + <span class="stringliteral">"'"</span>); <a name="l00204"></a>00204 <a name="l00205"></a>00205 <span class="comment">// Netting - looking for time buckets with net forecast</span> <a name="l00206"></a>00206 <span class="keywordtype">double</span> remaining = dmd-><a class="code" href="a00051.html#af5fe6f1b6e8f9b2c24ce3479b6546444">getQuantity</a>(); <a name="l00207"></a>00207 ForecastBucket* curbucket = zerobucket; <a name="l00208"></a>00208 <span class="keywordtype">bool</span> backward = <span class="keyword">true</span>; <a name="l00209"></a>00209 <span class="keywordflow">while</span> ( remaining > 0 && curbucket <a name="l00210"></a>00210 && (dmd-><a class="code" href="a00051.html#ab21889e9a16e3701a84ba8f3aa68628b">getDue</a>()-<a class="code" href="a00072.html#aa50e97b7aec516ba64c9ae86bc615c30">Forecast::getNetEarly</a>() < curbucket->getDueRange().getEnd()) <a name="l00211"></a>00211 && (dmd-><a class="code" href="a00051.html#ab21889e9a16e3701a84ba8f3aa68628b">getDue</a>()+<a class="code" href="a00072.html#a4ae1efae848a4909e634c55d1088c3e3">Forecast::getNetLate</a>() >= curbucket->getDueRange().getStart()) <a name="l00212"></a>00212 ) <a name="l00213"></a>00213 { <a name="l00214"></a>00214 <span class="comment">// Net from the current bucket</span> <a name="l00215"></a>00215 <span class="keywordtype">double</span> available = curbucket->getQuantity(); <a name="l00216"></a>00216 <span class="keywordflow">if</span> (available > 0) <a name="l00217"></a>00217 { <a name="l00218"></a>00218 <span class="keywordflow">if</span> (available >= remaining) <a name="l00219"></a>00219 { <a name="l00220"></a>00220 <span class="comment">// Partially consume a bucket</span> <a name="l00221"></a>00221 <span class="keywordflow">if</span> (<a class="code" href="a00171.html#a7331226cd4ef515184dff2d118c43423">getLogLevel</a>()>=2) <a name="l00222"></a>00222 <a class="code" href="a00257.html#a4af9f241e955122820c5e81ceb33edb5">logger</a> << <span class="stringliteral">" Consuming "</span> << remaining << <span class="stringliteral">" from bucket "</span> <a name="l00223"></a>00223 << curbucket->getDueRange() << <span class="stringliteral">" ("</span> << available <a name="l00224"></a>00224 << <span class="stringliteral">" available)"</span> << endl; <a name="l00225"></a>00225 curbucket->incConsumed(remaining); <a name="l00226"></a>00226 remaining = 0; <a name="l00227"></a>00227 } <a name="l00228"></a>00228 <span class="keywordflow">else</span> <a name="l00229"></a>00229 { <a name="l00230"></a>00230 <span class="comment">// Completely consume a bucket</span> <a name="l00231"></a>00231 <span class="keywordflow">if</span> (<a class="code" href="a00171.html#a7331226cd4ef515184dff2d118c43423">getLogLevel</a>()>=2) <a name="l00232"></a>00232 <a class="code" href="a00257.html#a4af9f241e955122820c5e81ceb33edb5">logger</a> << <span class="stringliteral">" Consuming "</span> << available << <span class="stringliteral">" from bucket "</span> <a name="l00233"></a>00233 << curbucket->getDueRange() << <span class="stringliteral">" ("</span> << available <a name="l00234"></a>00234 << <span class="stringliteral">" available)"</span> << endl; <a name="l00235"></a>00235 remaining -= available; <a name="l00236"></a>00236 curbucket->incConsumed(available); <a name="l00237"></a>00237 } <a name="l00238"></a>00238 } <a name="l00239"></a>00239 <span class="keywordflow">else</span> <span class="keywordflow">if</span> (<a class="code" href="a00171.html#a7331226cd4ef515184dff2d118c43423">getLogLevel</a>()>=2) <a name="l00240"></a>00240 <a class="code" href="a00257.html#a4af9f241e955122820c5e81ceb33edb5">logger</a> << <span class="stringliteral">" Nothing available in bucket "</span> <a name="l00241"></a>00241 << curbucket->getDueRange() << endl; <a name="l00242"></a>00242 <a name="l00243"></a>00243 <span class="comment">// Find the next forecast bucket</span> <a name="l00244"></a>00244 <span class="keywordflow">if</span> (backward) <a name="l00245"></a>00245 { <a name="l00246"></a>00246 <span class="comment">// Moving to earlier buckets</span> <a name="l00247"></a>00247 curbucket = curbucket->getPreviousBucket(); <a name="l00248"></a>00248 <span class="keywordflow">if</span> (!curbucket) <a name="l00249"></a>00249 { <a name="l00250"></a>00250 backward = <span class="keyword">false</span>; <a name="l00251"></a>00251 curbucket = zerobucket->getNextBucket(); <a name="l00252"></a>00252 } <a name="l00253"></a>00253 } <a name="l00254"></a>00254 <span class="keywordflow">else</span> <a name="l00255"></a>00255 <span class="comment">// Moving to later buckets</span> <a name="l00256"></a>00256 curbucket = curbucket->getNextBucket(); <a name="l00257"></a>00257 } <a name="l00258"></a>00258 <a name="l00259"></a>00259 <span class="comment">// Quantity for which no bucket is found</span> <a name="l00260"></a>00260 <span class="keywordflow">if</span> (remaining > 0 && <a class="code" href="a00171.html#a7331226cd4ef515184dff2d118c43423">getLogLevel</a>()>=2) <a name="l00261"></a>00261 <a class="code" href="a00257.html#a4af9f241e955122820c5e81ceb33edb5">logger</a> << <span class="stringliteral">" Remains "</span> << remaining << <span class="stringliteral">" that can't be netted"</span> << endl; <a name="l00262"></a>00262 <a name="l00263"></a>00263 } <a name="l00264"></a>00264 <a name="l00265"></a>00265 } <span class="comment">// end namespace</span> </pre></div></div> <hr size="1"/><address style="align: right;"><small>Documentation generated for frePPLe by <a href="http://www.doxygen.org/index.html"> <img src="doxygen.png" alt="doxygen" align="middle" border="0"/></a></small></address> </div> </div> </body> </html>