xref: /aoo42x/main/sccomp/source/solver/solver.cxx (revision e74d7e62)
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 
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 {
96     size_t operator()( const table::CellAddress& rAddress ) const
97     {
98         return ( rAddress.Sheet << 24 ) | ( rAddress.Column << 16 ) | rAddress.Row;
99     }
100 };
101 
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 {
109     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 
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 
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 
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 
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( 120 ),
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 
160 SolverComponent::~SolverComponent()
161 {
162 }
163 
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 
174 cppu::IPropertyArrayHelper& SAL_CALL SolverComponent::getInfoHelper()
175 {
176     return *getArrayHelper();
177 }
178 
179 uno::Reference<beans::XPropertySetInfo> SAL_CALL SolverComponent::getPropertySetInfo() throw(uno::RuntimeException)
180 {
181     return createPropertySetInfo( getInfoHelper() );
182 }
183 
184 // XSolverDescription
185 
186 OUString SAL_CALL SolverComponent::getComponentDescription() throw (uno::RuntimeException)
187 {
188     return lcl_GetResourceString( RID_SOLVER_COMPONENT );
189 }
190 
191 OUString SAL_CALL SolverComponent::getStatusDescription() throw (uno::RuntimeException)
192 {
193     return maStatus;
194 }
195 
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 
230 uno::Reference<sheet::XSpreadsheetDocument> SAL_CALL SolverComponent::getDocument() throw(uno::RuntimeException)
231 {
232     return mxDoc;
233 }
234 
235 void SAL_CALL SolverComponent::setDocument( const uno::Reference<sheet::XSpreadsheetDocument>& _document )
236                                 throw(uno::RuntimeException)
237 {
238     mxDoc = _document;
239 }
240 
241 table::CellAddress SAL_CALL SolverComponent::getObjective() throw(uno::RuntimeException)
242 {
243     return maObjective;
244 }
245 
246 void SAL_CALL SolverComponent::setObjective( const table::CellAddress& _objective ) throw(uno::RuntimeException)
247 {
248     maObjective = _objective;
249 }
250 
251 uno::Sequence<table::CellAddress> SAL_CALL SolverComponent::getVariables() throw(uno::RuntimeException)
252 {
253     return maVariables;
254 }
255 
256 void SAL_CALL SolverComponent::setVariables( const uno::Sequence<table::CellAddress>& _variables )
257                                 throw(uno::RuntimeException)
258 {
259     maVariables = _variables;
260 }
261 
262 uno::Sequence<sheet::SolverConstraint> SAL_CALL SolverComponent::getConstraints() throw(uno::RuntimeException)
263 {
264     return maConstraints;
265 }
266 
267 void SAL_CALL SolverComponent::setConstraints( const uno::Sequence<sheet::SolverConstraint>& _constraints )
268                                 throw(uno::RuntimeException)
269 {
270     maConstraints = _constraints;
271 }
272 
273 sal_Bool SAL_CALL SolverComponent::getMaximize() throw(uno::RuntimeException)
274 {
275     return mbMaximize;
276 }
277 
278 void SAL_CALL SolverComponent::setMaximize( sal_Bool _maximize ) throw(uno::RuntimeException)
279 {
280     mbMaximize = _maximize;
281 }
282 
283 // XSolver: get results
284 
285 sal_Bool SAL_CALL SolverComponent::getSuccess() throw(uno::RuntimeException)
286 {
287     return mbSuccess;
288 }
289 
290 double SAL_CALL SolverComponent::getResultValue() throw(uno::RuntimeException)
291 {
292     return mfResultValue;
293 }
294 
295 uno::Sequence<double> SAL_CALL SolverComponent::getSolution() throw(uno::RuntimeException)
296 {
297     return maSolution;
298 }
299 
300 // -------------------------------------------------------------------------
301 
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 
374         lcl_SetValue( mxDoc, *aVarIter, 0.0 );      // set back to zero for examining next variable
375     }
376 
377     xModel->unlockControllers();
378 
379     if ( maStatus.getLength() )
380         return;
381 
382     //
383     // build parameter arrays for CoinMP
384     //
385 
386     // set objective function
387 
388     const std::vector<double>& rObjCoeff = aCellsHash[maObjective];
389     double* pObjectCoeffs = new double[nVariables];
390     for (nVar=0; nVar<nVariables; nVar++)
391         pObjectCoeffs[nVar] = rObjCoeff[nVar+1];
392     double nObjectConst = rObjCoeff[0];             // constant term of objective
393 
394     // add rows
395 
396     size_t nRows = maConstraints.getLength();
397     size_t nCompSize = nVariables * nRows;
398     double* pCompMatrix = new double[nCompSize];    // first collect all coefficients, row-wise
399     for (size_t i=0; i<nCompSize; i++)
400         pCompMatrix[i] = 0.0;
401 
402     double* pRHS = new double[nRows];
403     char* pRowType = new char[nRows];
404     for (size_t i=0; i<nRows; i++)
405     {
406         pRHS[i] = 0.0;
407         pRowType[i] = 'N';
408     }
409 
410     for (sal_Int32 nConstrPos = 0; nConstrPos < maConstraints.getLength(); ++nConstrPos)
411     {
412         // integer constraints are set later
413         sheet::SolverConstraintOperator eOp = maConstraints[nConstrPos].Operator;
414         if ( eOp == sheet::SolverConstraintOperator_LESS_EQUAL ||
415              eOp == sheet::SolverConstraintOperator_GREATER_EQUAL ||
416              eOp == sheet::SolverConstraintOperator_EQUAL )
417         {
418             double fDirectValue = 0.0;
419             bool bRightCell = false;
420             table::CellAddress aRightAddr;
421             const uno::Any& rRightAny = maConstraints[nConstrPos].Right;
422             if ( rRightAny >>= aRightAddr )
423                 bRightCell = true;                  // cell specified as right-hand side
424             else
425                 rRightAny >>= fDirectValue;         // constant value
426 
427             table::CellAddress aLeftAddr = maConstraints[nConstrPos].Left;
428 
429             const std::vector<double>& rLeftCoeff = aCellsHash[aLeftAddr];
430             double* pValues = &pCompMatrix[nConstrPos * nVariables];
431             for (nVar=0; nVar<nVariables; nVar++)
432                 pValues[nVar] = rLeftCoeff[nVar+1];
433 
434             // if left hand cell has a constant term, put into rhs value
435             double fRightValue = -rLeftCoeff[0];
436 
437             if ( bRightCell )
438             {
439                 const std::vector<double>& rRightCoeff = aCellsHash[aRightAddr];
440                 // modify pValues with rhs coefficients
441                 for (nVar=0; nVar<nVariables; nVar++)
442                     pValues[nVar] -= rRightCoeff[nVar+1];
443 
444                 fRightValue += rRightCoeff[0];      // constant term
445             }
446             else
447                 fRightValue += fDirectValue;
448 
449             switch ( eOp )
450             {
451                 case sheet::SolverConstraintOperator_LESS_EQUAL:    pRowType[nConstrPos] = 'L'; break;
452                 case sheet::SolverConstraintOperator_GREATER_EQUAL: pRowType[nConstrPos] = 'G'; break;
453                 case sheet::SolverConstraintOperator_EQUAL:         pRowType[nConstrPos] = 'E'; break;
454                 default:
455                     OSL_ENSURE( false, "unexpected enum type" );
456             }
457             pRHS[nConstrPos] = fRightValue;
458         }
459     }
460 
461     // Find non-zero coefficients, column-wise
462 
463     int* pMatrixBegin = new int[nVariables+1];
464     int* pMatrixCount = new int[nVariables];
465     double* pMatrix = new double[nCompSize];    // not always completely used
466     int* pMatrixIndex = new int[nCompSize];
467     int nMatrixPos = 0;
468     for (nVar=0; nVar<nVariables; nVar++)
469     {
470         int nBegin = nMatrixPos;
471         for (size_t nRow=0; nRow<nRows; nRow++)
472         {
473             double fCoeff = pCompMatrix[ nRow * nVariables + nVar ];    // row-wise
474             if ( fCoeff != 0.0 )
475             {
476                 pMatrix[nMatrixPos] = fCoeff;
477                 pMatrixIndex[nMatrixPos] = nRow;
478                 ++nMatrixPos;
479             }
480         }
481         pMatrixBegin[nVar] = nBegin;
482         pMatrixCount[nVar] = nMatrixPos - nBegin;
483     }
484     pMatrixBegin[nVariables] = nMatrixPos;
485     delete[] pCompMatrix;
486     pCompMatrix = NULL;
487 
488     // apply settings to all variables
489 
490     double* pLowerBounds = new double[nVariables];
491     double* pUpperBounds = new double[nVariables];
492     for (nVar=0; nVar<nVariables; nVar++)
493     {
494         pLowerBounds[nVar] = mbNonNegative ? 0.0 : -DBL_MAX;
495         pUpperBounds[nVar] = DBL_MAX;
496 
497         // bounds could possibly be further restricted from single-cell constraints
498     }
499 
500     char* pColType = new char[nVariables];
501     for (nVar=0; nVar<nVariables; nVar++)
502         pColType[nVar] = mbInteger ? 'I' : 'C';
503 
504     // apply single-var integer constraints
505 
506     for (sal_Int32 nConstrPos = 0; nConstrPos < maConstraints.getLength(); ++nConstrPos)
507     {
508         sheet::SolverConstraintOperator eOp = maConstraints[nConstrPos].Operator;
509         if ( eOp == sheet::SolverConstraintOperator_INTEGER ||
510              eOp == sheet::SolverConstraintOperator_BINARY )
511         {
512             table::CellAddress aLeftAddr = maConstraints[nConstrPos].Left;
513             // find variable index for cell
514             for (nVar=0; nVar<nVariables; nVar++)
515                 if ( AddressEqual( aVariableCells[nVar], aLeftAddr ) )
516                 {
517                     if ( eOp == sheet::SolverConstraintOperator_INTEGER )
518                         pColType[nVar] = 'I';
519                     else
520                     {
521                         pColType[nVar] = 'B';
522                         pLowerBounds[nVar] = 0.0;
523                         pUpperBounds[nVar] = 1.0;
524                     }
525                 }
526         }
527     }
528 
529     int nObjectSense = mbMaximize ? SOLV_OBJSENS_MAX : SOLV_OBJSENS_MIN;
530 
531     HPROB hProb = CoinCreateProblem("");
532     int nResult = CoinLoadProblem( hProb, nVariables, nRows, nMatrixPos, 0,
533                     nObjectSense, nObjectConst, pObjectCoeffs,
534                     pLowerBounds, pUpperBounds, pRowType, pRHS, NULL,
535                     pMatrixBegin, pMatrixCount, pMatrixIndex, pMatrix,
536                     NULL, NULL, NULL );
537     nResult = CoinLoadInteger( hProb, pColType );
538 
539     delete[] pColType;
540     delete[] pMatrixIndex;
541     delete[] pMatrix;
542     delete[] pMatrixCount;
543     delete[] pMatrixBegin;
544     delete[] pUpperBounds;
545     delete[] pLowerBounds;
546     delete[] pRowType;
547     delete[] pRHS;
548     delete[] pObjectCoeffs;
549 
550     CoinSetRealOption( hProb, COIN_REAL_MAXSECONDS, mnTimeout );
551     CoinSetRealOption( hProb, COIN_REAL_MIPMAXSEC, mnTimeout );
552 
553     // TODO: handle (or remove) settings: epsilon, B&B depth
554 
555     // solve model
556 
557     nResult = CoinCheckProblem( hProb );
558     nResult = CoinOptimizeProblem( hProb, 0 );
559 
560     mbSuccess = ( nResult == SOLV_CALL_SUCCESS );
561     if ( mbSuccess )
562     {
563         // get solution
564 
565         maSolution.realloc( nVariables );
566         CoinGetSolutionValues( hProb, maSolution.getArray(), NULL, NULL, NULL );
567         mfResultValue = CoinGetObjectValue( hProb );
568     }
569     else
570     {
571         int nSolutionStatus = CoinGetSolutionStatus( hProb );
572         if ( nSolutionStatus == 1 )
573             maStatus = lcl_GetResourceString( RID_ERROR_INFEASIBLE );
574         else if ( nSolutionStatus == 2 )
575             maStatus = lcl_GetResourceString( RID_ERROR_UNBOUNDED );
576         // TODO: detect timeout condition and report as RID_ERROR_TIMEOUT
577         // (currently reported as infeasible)
578     }
579 
580     CoinUnloadProblem( hProb );
581 }
582 
583 // -------------------------------------------------------------------------
584 
585 // XServiceInfo
586 
587 uno::Sequence< OUString > SolverComponent_getSupportedServiceNames()
588 {
589     uno::Sequence< OUString > aServiceNames( 1 );
590     aServiceNames[ 0 ] = OUString::createFromAscii( "com.sun.star.sheet.Solver" );
591     return aServiceNames;
592 }
593 
594 OUString SolverComponent_getImplementationName()
595 {
596     return OUString::createFromAscii( "com.sun.star.comp.Calc.Solver" );
597 }
598 
599 OUString SAL_CALL SolverComponent::getImplementationName() throw(uno::RuntimeException)
600 {
601     return SolverComponent_getImplementationName();
602 }
603 
604 sal_Bool SAL_CALL SolverComponent::supportsService( const OUString& rServiceName ) throw(uno::RuntimeException)
605 {
606     const uno::Sequence< OUString > aServices = SolverComponent_getSupportedServiceNames();
607     const OUString* pArray = aServices.getConstArray();
608     const OUString* pArrayEnd = pArray + aServices.getLength();
609     return ::std::find( pArray, pArrayEnd, rServiceName ) != pArrayEnd;
610 }
611 
612 uno::Sequence<OUString> SAL_CALL SolverComponent::getSupportedServiceNames() throw(uno::RuntimeException)
613 {
614     return SolverComponent_getSupportedServiceNames();
615 }
616 
617 uno::Reference<uno::XInterface> SolverComponent_createInstance( const uno::Reference<uno::XComponentContext>& rSMgr )
618     throw(uno::Exception)
619 {
620 	return (cppu::OWeakObject*) new SolverComponent( rSMgr );
621 }
622 
623 // -------------------------------------------------------------------------
624 
625 extern "C"
626 {
627     SAL_DLLPUBLIC_EXPORT void SAL_CALL component_getImplementationEnvironment(
628         const sal_Char ** ppEnvTypeName, uno_Environment ** )
629     {
630         *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
631     }
632 
633     // -------------------------------------------------------------------------
634 
635     SAL_DLLPUBLIC_EXPORT void* SAL_CALL component_getFactory( const sal_Char * pImplName, void * pServiceManager, void * /*pRegistryKey*/ )
636     {
637         OUString    aImplName( OUString::createFromAscii( pImplName ) );
638         void*       pRet = 0;
639 
640         if( pServiceManager )
641         {
642             uno::Reference< lang::XSingleComponentFactory > xFactory;
643             if( aImplName.equals( SolverComponent_getImplementationName() ) )
644                 xFactory = cppu::createSingleComponentFactory(
645                         SolverComponent_createInstance,
646                         OUString::createFromAscii( pImplName ),
647                         SolverComponent_getSupportedServiceNames() );
648 
649             if( xFactory.is() )
650             {
651                 xFactory->acquire();
652                 pRet = xFactory.get();
653             }
654         }
655         return pRet;
656     }
657 }
658 
659