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