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