xref: /aoo4110/main/sccomp/source/solver/solver.cxx (revision b1cdbd2c)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 #ifdef SYSTEM_COINMP
24 #include <coin/CoinMP.h>
25 #else
26 #include <coinmp/CoinMP.h>
27 #endif
28 
29 #include "solver.hxx"
30 #include "solver.hrc"
31 
32 #include <com/sun/star/beans/XPropertySet.hpp>
33 #include <com/sun/star/container/XIndexAccess.hpp>
34 #include <com/sun/star/frame/XModel.hpp>
35 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
36 #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
37 #include <com/sun/star/sheet/XSpreadsheet.hpp>
38 #include <com/sun/star/table/CellAddress.hpp>
39 #include <com/sun/star/table/CellRangeAddress.hpp>
40 #include <com/sun/star/text/XTextRange.hpp>
41 
42 #include <rtl/math.hxx>
43 #include <rtl/ustrbuf.hxx>
44 #include <cppuhelper/factory.hxx>
45 #include <vector>
46 #include <hash_map>
47 
48 #include <tools/resmgr.hxx>
49 
50 using namespace com::sun::star;
51 
52 using ::rtl::OUString;
53 
54 #define C2U(constAsciiStr) (::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( constAsciiStr ) ))
55 
56 #define STR_NONNEGATIVE   "NonNegative"
57 #define STR_INTEGER       "Integer"
58 #define STR_TIMEOUT       "Timeout"
59 #define STR_EPSILONLEVEL  "EpsilonLevel"
60 #define STR_LIMITBBDEPTH  "LimitBBDepth"
61 
62 // -----------------------------------------------------------------------
63 //  Resources from tools are used for translated strings
64 
65 static ResMgr* pSolverResMgr = NULL;
66 
lcl_GetResourceString(sal_uInt32 nId)67 OUString lcl_GetResourceString( sal_uInt32 nId )
68 {
69     if (!pSolverResMgr)
70         pSolverResMgr = CREATEVERSIONRESMGR( solver );
71 
72     return String( ResId( nId, *pSolverResMgr ) );
73 }
74 
75 // -----------------------------------------------------------------------
76 
77 namespace
78 {
79     enum
80     {
81         PROP_NONNEGATIVE,
82         PROP_INTEGER,
83         PROP_TIMEOUT,
84         PROP_EPSILONLEVEL,
85         PROP_LIMITBBDEPTH
86     };
87 }
88 
89 // -----------------------------------------------------------------------
90 
91 // hash map for the coefficients of a dependent cell (objective or constraint)
92 // The size of each vector is the number of columns (variable cells) plus one, first entry is initial value.
93 
94 struct ScSolverCellHash
95 {
operator ()ScSolverCellHash96     size_t operator()( const table::CellAddress& rAddress ) const
97     {
98         return ( rAddress.Sheet << 24 ) | ( rAddress.Column << 16 ) | rAddress.Row;
99     }
100 };
101 
AddressEqual(const table::CellAddress & rAddr1,const table::CellAddress & rAddr2)102 inline bool AddressEqual( const table::CellAddress& rAddr1, const table::CellAddress& rAddr2 )
103 {
104     return rAddr1.Sheet == rAddr2.Sheet && rAddr1.Column == rAddr2.Column && rAddr1.Row == rAddr2.Row;
105 }
106 
107 struct ScSolverCellEqual
108 {
operator ()ScSolverCellEqual109     bool operator()( const table::CellAddress& rAddr1, const table::CellAddress& rAddr2 ) const
110     {
111         return AddressEqual( rAddr1, rAddr2 );
112     }
113 };
114 
115 typedef std::hash_map< table::CellAddress, std::vector<double>, ScSolverCellHash, ScSolverCellEqual > ScSolverCellHashMap;
116 
117 // -----------------------------------------------------------------------
118 
lcl_GetCell(const uno::Reference<sheet::XSpreadsheetDocument> & xDoc,const table::CellAddress & rPos)119 uno::Reference<table::XCell> lcl_GetCell( const uno::Reference<sheet::XSpreadsheetDocument>& xDoc,
120                                           const table::CellAddress& rPos )
121 {
122     uno::Reference<container::XIndexAccess> xSheets( xDoc->getSheets(), uno::UNO_QUERY );
123     uno::Reference<sheet::XSpreadsheet> xSheet( xSheets->getByIndex( rPos.Sheet ), uno::UNO_QUERY );
124     return xSheet->getCellByPosition( rPos.Column, rPos.Row );
125 }
126 
lcl_SetValue(const uno::Reference<sheet::XSpreadsheetDocument> & xDoc,const table::CellAddress & rPos,double fValue)127 void lcl_SetValue( const uno::Reference<sheet::XSpreadsheetDocument>& xDoc,
128                    const table::CellAddress& rPos, double fValue )
129 {
130     lcl_GetCell( xDoc, rPos )->setValue( fValue );
131 }
132 
lcl_GetValue(const uno::Reference<sheet::XSpreadsheetDocument> & xDoc,const table::CellAddress & rPos)133 double lcl_GetValue( const uno::Reference<sheet::XSpreadsheetDocument>& xDoc,
134                      const table::CellAddress& rPos )
135 {
136     return lcl_GetCell( xDoc, rPos )->getValue();
137 }
138 
139 // -------------------------------------------------------------------------
140 
SolverComponent(const uno::Reference<uno::XComponentContext> &)141 SolverComponent::SolverComponent( const uno::Reference<uno::XComponentContext>& /* rSMgr */ ) :
142     OPropertyContainer( GetBroadcastHelper() ),
143     mbMaximize( sal_True ),
144     mbNonNegative( sal_False ),
145     mbInteger( sal_False ),
146     mnTimeout( 100 ),
147     mnEpsilonLevel( 0 ),
148     mbLimitBBDepth( sal_True ),
149     mbSuccess( sal_False ),
150     mfResultValue( 0.0 )
151 {
152     // for XPropertySet implementation:
153     registerProperty( C2U(STR_NONNEGATIVE),  PROP_NONNEGATIVE,  0, &mbNonNegative,  getCppuType( &mbNonNegative )  );
154     registerProperty( C2U(STR_INTEGER),      PROP_INTEGER,      0, &mbInteger,      getCppuType( &mbInteger )      );
155     registerProperty( C2U(STR_TIMEOUT),      PROP_TIMEOUT,      0, &mnTimeout,      getCppuType( &mnTimeout )      );
156     registerProperty( C2U(STR_EPSILONLEVEL), PROP_EPSILONLEVEL, 0, &mnEpsilonLevel, getCppuType( &mnEpsilonLevel ) );
157     registerProperty( C2U(STR_LIMITBBDEPTH), PROP_LIMITBBDEPTH, 0, &mbLimitBBDepth, getCppuType( &mbLimitBBDepth ) );
158 }
159 
~SolverComponent()160 SolverComponent::~SolverComponent()
161 {
162 }
163 
IMPLEMENT_FORWARD_XINTERFACE2(SolverComponent,SolverComponent_Base,OPropertyContainer)164 IMPLEMENT_FORWARD_XINTERFACE2( SolverComponent, SolverComponent_Base, OPropertyContainer )
165 IMPLEMENT_FORWARD_XTYPEPROVIDER2( SolverComponent, SolverComponent_Base, OPropertyContainer )
166 
167 cppu::IPropertyArrayHelper* SolverComponent::createArrayHelper() const
168 {
169     uno::Sequence<beans::Property> aProps;
170     describeProperties( aProps );
171     return new cppu::OPropertyArrayHelper( aProps );
172 }
173 
getInfoHelper()174 cppu::IPropertyArrayHelper& SAL_CALL SolverComponent::getInfoHelper()
175 {
176     return *getArrayHelper();
177 }
178 
getPropertySetInfo()179 uno::Reference<beans::XPropertySetInfo> SAL_CALL SolverComponent::getPropertySetInfo() throw(uno::RuntimeException)
180 {
181     return createPropertySetInfo( getInfoHelper() );
182 }
183 
184 // XSolverDescription
185 
getComponentDescription()186 OUString SAL_CALL SolverComponent::getComponentDescription() throw (uno::RuntimeException)
187 {
188     return lcl_GetResourceString( RID_SOLVER_COMPONENT );
189 }
190 
getStatusDescription()191 OUString SAL_CALL SolverComponent::getStatusDescription() throw (uno::RuntimeException)
192 {
193     return maStatus;
194 }
195 
getPropertyDescription(const OUString & rPropertyName)196 OUString SAL_CALL SolverComponent::getPropertyDescription( const OUString& rPropertyName ) throw (uno::RuntimeException)
197 {
198     sal_uInt32 nResId = 0;
199 	sal_Int32 nHandle = getInfoHelper().getHandleByName( rPropertyName );
200     switch (nHandle)
201     {
202         case PROP_NONNEGATIVE:
203             nResId = RID_PROPERTY_NONNEGATIVE;
204             break;
205         case PROP_INTEGER:
206             nResId = RID_PROPERTY_INTEGER;
207             break;
208         case PROP_TIMEOUT:
209             nResId = RID_PROPERTY_TIMEOUT;
210             break;
211         case PROP_EPSILONLEVEL:
212             nResId = RID_PROPERTY_EPSILONLEVEL;
213             break;
214         case PROP_LIMITBBDEPTH:
215             nResId = RID_PROPERTY_LIMITBBDEPTH;
216             break;
217         default:
218             {
219                 // unknown - leave empty
220             }
221     }
222     OUString aRet;
223     if ( nResId )
224         aRet = lcl_GetResourceString( nResId );
225     return aRet;
226 }
227 
228 // XSolver: settings
229 
getDocument()230 uno::Reference<sheet::XSpreadsheetDocument> SAL_CALL SolverComponent::getDocument() throw(uno::RuntimeException)
231 {
232     return mxDoc;
233 }
234 
setDocument(const uno::Reference<sheet::XSpreadsheetDocument> & _document)235 void SAL_CALL SolverComponent::setDocument( const uno::Reference<sheet::XSpreadsheetDocument>& _document )
236                                 throw(uno::RuntimeException)
237 {
238     mxDoc = _document;
239 }
240 
getObjective()241 table::CellAddress SAL_CALL SolverComponent::getObjective() throw(uno::RuntimeException)
242 {
243     return maObjective;
244 }
245 
setObjective(const table::CellAddress & _objective)246 void SAL_CALL SolverComponent::setObjective( const table::CellAddress& _objective ) throw(uno::RuntimeException)
247 {
248     maObjective = _objective;
249 }
250 
getVariables()251 uno::Sequence<table::CellAddress> SAL_CALL SolverComponent::getVariables() throw(uno::RuntimeException)
252 {
253     return maVariables;
254 }
255 
setVariables(const uno::Sequence<table::CellAddress> & _variables)256 void SAL_CALL SolverComponent::setVariables( const uno::Sequence<table::CellAddress>& _variables )
257                                 throw(uno::RuntimeException)
258 {
259     maVariables = _variables;
260 }
261 
getConstraints()262 uno::Sequence<sheet::SolverConstraint> SAL_CALL SolverComponent::getConstraints() throw(uno::RuntimeException)
263 {
264     return maConstraints;
265 }
266 
setConstraints(const uno::Sequence<sheet::SolverConstraint> & _constraints)267 void SAL_CALL SolverComponent::setConstraints( const uno::Sequence<sheet::SolverConstraint>& _constraints )
268                                 throw(uno::RuntimeException)
269 {
270     maConstraints = _constraints;
271 }
272 
getMaximize()273 sal_Bool SAL_CALL SolverComponent::getMaximize() throw(uno::RuntimeException)
274 {
275     return mbMaximize;
276 }
277 
setMaximize(sal_Bool _maximize)278 void SAL_CALL SolverComponent::setMaximize( sal_Bool _maximize ) throw(uno::RuntimeException)
279 {
280     mbMaximize = _maximize;
281 }
282 
283 // XSolver: get results
284 
getSuccess()285 sal_Bool SAL_CALL SolverComponent::getSuccess() throw(uno::RuntimeException)
286 {
287     return mbSuccess;
288 }
289 
getResultValue()290 double SAL_CALL SolverComponent::getResultValue() throw(uno::RuntimeException)
291 {
292     return mfResultValue;
293 }
294 
getSolution()295 uno::Sequence<double> SAL_CALL SolverComponent::getSolution() throw(uno::RuntimeException)
296 {
297     return maSolution;
298 }
299 
300 // -------------------------------------------------------------------------
301 
solve()302 void SAL_CALL SolverComponent::solve() throw(uno::RuntimeException)
303 {
304     uno::Reference<frame::XModel> xModel( mxDoc, uno::UNO_QUERY );
305     if ( !xModel.is() )
306         throw uno::RuntimeException();
307 
308     maStatus = OUString();
309     mbSuccess = false;
310 
311     xModel->lockControllers();
312 
313     // collect variables in vector (?)
314 
315     std::vector<table::CellAddress> aVariableCells;
316     for (sal_Int32 nPos=0; nPos<maVariables.getLength(); nPos++)
317         aVariableCells.push_back( maVariables[nPos] );
318     size_t nVariables = aVariableCells.size();
319     size_t nVar = 0;
320 
321     // collect all dependent cells
322 
323     ScSolverCellHashMap aCellsHash;
324     aCellsHash[maObjective].reserve( nVariables + 1 );                  // objective function
325 
326     for (sal_Int32 nConstrPos = 0; nConstrPos < maConstraints.getLength(); ++nConstrPos)
327     {
328         table::CellAddress aCellAddr = maConstraints[nConstrPos].Left;
329         aCellsHash[aCellAddr].reserve( nVariables + 1 );                // constraints: left hand side
330 
331         if ( maConstraints[nConstrPos].Right >>= aCellAddr )
332             aCellsHash[aCellAddr].reserve( nVariables + 1 );            // constraints: right hand side
333     }
334 
335     // set all variables to zero
336     //! store old values?
337     //! use old values as initial values?
338     std::vector<table::CellAddress>::const_iterator aVarIter;
339     for ( aVarIter = aVariableCells.begin(); aVarIter != aVariableCells.end(); ++aVarIter )
340     {
341         lcl_SetValue( mxDoc, *aVarIter, 0.0 );
342     }
343 
344     // read initial values from all dependent cells
345     ScSolverCellHashMap::iterator aCellsIter;
346     for ( aCellsIter = aCellsHash.begin(); aCellsIter != aCellsHash.end(); ++aCellsIter )
347     {
348         double fValue = lcl_GetValue( mxDoc, aCellsIter->first );
349         aCellsIter->second.push_back( fValue );                         // store as first element, as-is
350     }
351 
352     // loop through variables
353     for ( aVarIter = aVariableCells.begin(); aVarIter != aVariableCells.end(); ++aVarIter )
354     {
355         lcl_SetValue( mxDoc, *aVarIter, 1.0 );      // set to 1 to examine influence
356 
357         // read value change from all dependent cells
358         for ( aCellsIter = aCellsHash.begin(); aCellsIter != aCellsHash.end(); ++aCellsIter )
359         {
360             double fChanged = lcl_GetValue( mxDoc, aCellsIter->first );
361             double fInitial = aCellsIter->second.front();
362             aCellsIter->second.push_back( fChanged - fInitial );
363         }
364 
365         lcl_SetValue( mxDoc, *aVarIter, 2.0 );      // minimal test for linearity
366 
367         for ( aCellsIter = aCellsHash.begin(); aCellsIter != aCellsHash.end(); ++aCellsIter )
368         {
369             double fInitial = aCellsIter->second.front();
370             double fCoeff   = aCellsIter->second.back();       // last appended: coefficient for this variable
371             double fTwo     = lcl_GetValue( mxDoc, aCellsIter->first );
372 
373             bool bLinear = rtl::math::approxEqual( fTwo, fInitial + 2.0 * fCoeff ) ||
374                            rtl::math::approxEqual( fInitial, fTwo - 2.0 * fCoeff );
375             // second comparison is needed in case fTwo is zero
376             if ( !bLinear )
377                 maStatus = lcl_GetResourceString( RID_ERROR_NONLINEAR );
378         }
379 
380         lcl_SetValue( mxDoc, *aVarIter, 0.0 );      // set back to zero for examining next variable
381     }
382 
383     xModel->unlockControllers();
384 
385     if ( maStatus.getLength() )
386         return;
387 
388     //
389     // build parameter arrays for CoinMP
390     //
391 
392     // set objective function
393 
394     const std::vector<double>& rObjCoeff = aCellsHash[maObjective];
395     double* pObjectCoeffs = new double[nVariables];
396     for (nVar=0; nVar<nVariables; nVar++)
397         pObjectCoeffs[nVar] = rObjCoeff[nVar+1];
398     double nObjectConst = rObjCoeff[0];             // constant term of objective
399 
400     // add rows
401 
402     size_t nRows = maConstraints.getLength();
403     size_t nCompSize = nVariables * nRows;
404     double* pCompMatrix = new double[nCompSize];    // first collect all coefficients, row-wise
405     for (size_t i=0; i<nCompSize; i++)
406         pCompMatrix[i] = 0.0;
407 
408     double* pRHS = new double[nRows];
409     char* pRowType = new char[nRows];
410     for (size_t i=0; i<nRows; i++)
411     {
412         pRHS[i] = 0.0;
413         pRowType[i] = 'N';
414     }
415 
416     for (sal_Int32 nConstrPos = 0; nConstrPos < maConstraints.getLength(); ++nConstrPos)
417     {
418         // integer constraints are set later
419         sheet::SolverConstraintOperator eOp = maConstraints[nConstrPos].Operator;
420         if ( eOp == sheet::SolverConstraintOperator_LESS_EQUAL ||
421              eOp == sheet::SolverConstraintOperator_GREATER_EQUAL ||
422              eOp == sheet::SolverConstraintOperator_EQUAL )
423         {
424             double fDirectValue = 0.0;
425             bool bRightCell = false;
426             table::CellAddress aRightAddr;
427             const uno::Any& rRightAny = maConstraints[nConstrPos].Right;
428             if ( rRightAny >>= aRightAddr )
429                 bRightCell = true;                  // cell specified as right-hand side
430             else
431                 rRightAny >>= fDirectValue;         // constant value
432 
433             table::CellAddress aLeftAddr = maConstraints[nConstrPos].Left;
434 
435             const std::vector<double>& rLeftCoeff = aCellsHash[aLeftAddr];
436             double* pValues = &pCompMatrix[nConstrPos * nVariables];
437             for (nVar=0; nVar<nVariables; nVar++)
438                 pValues[nVar] = rLeftCoeff[nVar+1];
439 
440             // if left hand cell has a constant term, put into rhs value
441             double fRightValue = -rLeftCoeff[0];
442 
443             if ( bRightCell )
444             {
445                 const std::vector<double>& rRightCoeff = aCellsHash[aRightAddr];
446                 // modify pValues with rhs coefficients
447                 for (nVar=0; nVar<nVariables; nVar++)
448                     pValues[nVar] -= rRightCoeff[nVar+1];
449 
450                 fRightValue += rRightCoeff[0];      // constant term
451             }
452             else
453                 fRightValue += fDirectValue;
454 
455             switch ( eOp )
456             {
457                 case sheet::SolverConstraintOperator_LESS_EQUAL:    pRowType[nConstrPos] = 'L'; break;
458                 case sheet::SolverConstraintOperator_GREATER_EQUAL: pRowType[nConstrPos] = 'G'; break;
459                 case sheet::SolverConstraintOperator_EQUAL:         pRowType[nConstrPos] = 'E'; break;
460                 default:
461                     OSL_ENSURE( false, "unexpected enum type" );
462             }
463             pRHS[nConstrPos] = fRightValue;
464         }
465     }
466 
467     // Find non-zero coefficients, column-wise
468 
469     int* pMatrixBegin = new int[nVariables+1];
470     int* pMatrixCount = new int[nVariables];
471     double* pMatrix = new double[nCompSize];    // not always completely used
472     int* pMatrixIndex = new int[nCompSize];
473     int nMatrixPos = 0;
474     for (nVar=0; nVar<nVariables; nVar++)
475     {
476         int nBegin = nMatrixPos;
477         for (size_t nRow=0; nRow<nRows; nRow++)
478         {
479             double fCoeff = pCompMatrix[ nRow * nVariables + nVar ];    // row-wise
480             if ( fCoeff != 0.0 )
481             {
482                 pMatrix[nMatrixPos] = fCoeff;
483                 pMatrixIndex[nMatrixPos] = nRow;
484                 ++nMatrixPos;
485             }
486         }
487         pMatrixBegin[nVar] = nBegin;
488         pMatrixCount[nVar] = nMatrixPos - nBegin;
489     }
490     pMatrixBegin[nVariables] = nMatrixPos;
491     delete[] pCompMatrix;
492     pCompMatrix = NULL;
493 
494     // apply settings to all variables
495 
496     double* pLowerBounds = new double[nVariables];
497     double* pUpperBounds = new double[nVariables];
498     for (nVar=0; nVar<nVariables; nVar++)
499     {
500         pLowerBounds[nVar] = mbNonNegative ? 0.0 : -DBL_MAX;
501         pUpperBounds[nVar] = DBL_MAX;
502 
503         // bounds could possibly be further restricted from single-cell constraints
504     }
505 
506     char* pColType = new char[nVariables];
507     for (nVar=0; nVar<nVariables; nVar++)
508         pColType[nVar] = mbInteger ? 'I' : 'C';
509 
510     // apply single-var integer constraints
511 
512     for (sal_Int32 nConstrPos = 0; nConstrPos < maConstraints.getLength(); ++nConstrPos)
513     {
514         sheet::SolverConstraintOperator eOp = maConstraints[nConstrPos].Operator;
515         if ( eOp == sheet::SolverConstraintOperator_INTEGER ||
516              eOp == sheet::SolverConstraintOperator_BINARY )
517         {
518             table::CellAddress aLeftAddr = maConstraints[nConstrPos].Left;
519             // find variable index for cell
520             for (nVar=0; nVar<nVariables; nVar++)
521                 if ( AddressEqual( aVariableCells[nVar], aLeftAddr ) )
522                 {
523                     if ( eOp == sheet::SolverConstraintOperator_INTEGER )
524                         pColType[nVar] = 'I';
525                     else
526                     {
527                         pColType[nVar] = 'B';
528                         pLowerBounds[nVar] = 0.0;
529                         pUpperBounds[nVar] = 1.0;
530                     }
531                 }
532         }
533     }
534 
535     int nObjectSense = mbMaximize ? SOLV_OBJSENS_MAX : SOLV_OBJSENS_MIN;
536 
537     HPROB hProb = CoinCreateProblem("");
538     int nResult = CoinLoadProblem( hProb, nVariables, nRows, nMatrixPos, 0,
539                     nObjectSense, nObjectConst, pObjectCoeffs,
540                     pLowerBounds, pUpperBounds, pRowType, pRHS, NULL,
541                     pMatrixBegin, pMatrixCount, pMatrixIndex, pMatrix,
542                     NULL, NULL, NULL );
543     nResult = CoinLoadInteger( hProb, pColType );
544 
545     delete[] pColType;
546     delete[] pMatrixIndex;
547     delete[] pMatrix;
548     delete[] pMatrixCount;
549     delete[] pMatrixBegin;
550     delete[] pUpperBounds;
551     delete[] pLowerBounds;
552     delete[] pRowType;
553     delete[] pRHS;
554     delete[] pObjectCoeffs;
555 
556     CoinSetRealOption( hProb, COIN_REAL_MAXSECONDS, mnTimeout );
557     CoinSetRealOption( hProb, COIN_REAL_MIPMAXSEC, mnTimeout );
558 
559     // TODO: handle (or remove) settings: epsilon, B&B depth
560 
561     // solve model
562 
563     nResult = CoinCheckProblem( hProb );
564     nResult = CoinOptimizeProblem( hProb, 0 );
565 
566     mbSuccess = ( nResult == SOLV_CALL_SUCCESS );
567     if ( mbSuccess )
568     {
569         // get solution
570 
571         maSolution.realloc( nVariables );
572         CoinGetSolutionValues( hProb, maSolution.getArray(), NULL, NULL, NULL );
573         mfResultValue = CoinGetObjectValue( hProb );
574     }
575     else
576     {
577         int nSolutionStatus = CoinGetSolutionStatus( hProb );
578         if ( nSolutionStatus == 1 )
579             maStatus = lcl_GetResourceString( RID_ERROR_INFEASIBLE );
580         else if ( nSolutionStatus == 2 )
581             maStatus = lcl_GetResourceString( RID_ERROR_UNBOUNDED );
582         // TODO: detect timeout condition and report as RID_ERROR_TIMEOUT
583         // (currently reported as infeasible)
584     }
585 
586     CoinUnloadProblem( hProb );
587 }
588 
589 // -------------------------------------------------------------------------
590 
591 // XServiceInfo
592 
SolverComponent_getSupportedServiceNames()593 uno::Sequence< OUString > SolverComponent_getSupportedServiceNames()
594 {
595     uno::Sequence< OUString > aServiceNames( 1 );
596     aServiceNames[ 0 ] = OUString::createFromAscii( "com.sun.star.sheet.Solver" );
597     return aServiceNames;
598 }
599 
SolverComponent_getImplementationName()600 OUString SolverComponent_getImplementationName()
601 {
602     return OUString::createFromAscii( "com.sun.star.comp.Calc.Solver" );
603 }
604 
getImplementationName()605 OUString SAL_CALL SolverComponent::getImplementationName() throw(uno::RuntimeException)
606 {
607     return SolverComponent_getImplementationName();
608 }
609 
supportsService(const OUString & rServiceName)610 sal_Bool SAL_CALL SolverComponent::supportsService( const OUString& rServiceName ) throw(uno::RuntimeException)
611 {
612     const uno::Sequence< OUString > aServices = SolverComponent_getSupportedServiceNames();
613     const OUString* pArray = aServices.getConstArray();
614     const OUString* pArrayEnd = pArray + aServices.getLength();
615     return ::std::find( pArray, pArrayEnd, rServiceName ) != pArrayEnd;
616 }
617 
getSupportedServiceNames()618 uno::Sequence<OUString> SAL_CALL SolverComponent::getSupportedServiceNames() throw(uno::RuntimeException)
619 {
620     return SolverComponent_getSupportedServiceNames();
621 }
622 
SolverComponent_createInstance(const uno::Reference<uno::XComponentContext> & rSMgr)623 uno::Reference<uno::XInterface> SolverComponent_createInstance( const uno::Reference<uno::XComponentContext>& rSMgr )
624     throw(uno::Exception)
625 {
626 	return (cppu::OWeakObject*) new SolverComponent( rSMgr );
627 }
628 
629 // -------------------------------------------------------------------------
630 
631 extern "C"
632 {
component_getImplementationEnvironment(const sal_Char ** ppEnvTypeName,uno_Environment **)633     SAL_DLLPUBLIC_EXPORT void SAL_CALL component_getImplementationEnvironment(
634         const sal_Char ** ppEnvTypeName, uno_Environment ** )
635     {
636         *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
637     }
638 
639     // -------------------------------------------------------------------------
640 
component_getFactory(const sal_Char * pImplName,void * pServiceManager,void *)641     SAL_DLLPUBLIC_EXPORT void* SAL_CALL component_getFactory( const sal_Char * pImplName, void * pServiceManager, void * /*pRegistryKey*/ )
642     {
643         OUString    aImplName( OUString::createFromAscii( pImplName ) );
644         void*       pRet = 0;
645 
646         if( pServiceManager )
647         {
648             uno::Reference< lang::XSingleComponentFactory > xFactory;
649             if( aImplName.equals( SolverComponent_getImplementationName() ) )
650                 xFactory = cppu::createSingleComponentFactory(
651                         SolverComponent_createInstance,
652                         OUString::createFromAscii( pImplName ),
653                         SolverComponent_getSupportedServiceNames() );
654 
655             if( xFactory.is() )
656             {
657                 xFactory->acquire();
658                 pRet = xFactory.get();
659             }
660         }
661         return pRet;
662     }
663 }
664 
665