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 #include <CoinMP.h>
24
25 #include "solver.hxx"
26 #include "solver.hrc"
27
28 #include <com/sun/star/beans/XPropertySet.hpp>
29 #include <com/sun/star/container/XIndexAccess.hpp>
30 #include <com/sun/star/frame/XModel.hpp>
31 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
32 #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
33 #include <com/sun/star/sheet/XSpreadsheet.hpp>
34 #include <com/sun/star/table/CellAddress.hpp>
35 #include <com/sun/star/table/CellRangeAddress.hpp>
36 #include <com/sun/star/text/XTextRange.hpp>
37
38 #include <rtl/math.hxx>
39 #include <rtl/ustrbuf.hxx>
40 #include <cppuhelper/factory.hxx>
41 #include <vector>
42 #include <hash_map>
43
44 #include <tools/resmgr.hxx>
45
46 using namespace com::sun::star;
47
48 using ::rtl::OUString;
49
50 #define C2U(constAsciiStr) (::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( constAsciiStr ) ))
51
52 #define STR_NONNEGATIVE "NonNegative"
53 #define STR_INTEGER "Integer"
54 #define STR_TIMEOUT "Timeout"
55 #define STR_EPSILONLEVEL "EpsilonLevel"
56 #define STR_LIMITBBDEPTH "LimitBBDepth"
57 #define STR_NONLINEARTEST "NonLinearTest"
58
59 // -----------------------------------------------------------------------
60 // Resources from tools are used for translated strings
61
62 static ResMgr* pSolverResMgr = NULL;
63
lcl_GetResourceString(sal_uInt32 nId)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 PROP_NONLINEARTEST
84 };
85 }
86
87 // -----------------------------------------------------------------------
88
89 // hash map for the coefficients of a dependent cell (objective or constraint)
90 // The size of each vector is the number of columns (variable cells) plus one, first entry is initial value.
91
92 struct ScSolverCellHash
93 {
operator ()ScSolverCellHash94 size_t operator()( const table::CellAddress& rAddress ) const
95 {
96 return ( rAddress.Sheet << 24 ) | ( rAddress.Column << 16 ) | rAddress.Row;
97 }
98 };
99
AddressEqual(const table::CellAddress & rAddr1,const table::CellAddress & rAddr2)100 inline bool AddressEqual( const table::CellAddress& rAddr1, const table::CellAddress& rAddr2 )
101 {
102 return rAddr1.Sheet == rAddr2.Sheet && rAddr1.Column == rAddr2.Column && rAddr1.Row == rAddr2.Row;
103 }
104
105 struct ScSolverCellEqual
106 {
operator ()ScSolverCellEqual107 bool operator()( const table::CellAddress& rAddr1, const table::CellAddress& rAddr2 ) const
108 {
109 return AddressEqual( rAddr1, rAddr2 );
110 }
111 };
112
113 typedef std::hash_map< table::CellAddress, std::vector<double>, ScSolverCellHash, ScSolverCellEqual > ScSolverCellHashMap;
114
115 // -----------------------------------------------------------------------
116
lcl_GetCell(const uno::Reference<sheet::XSpreadsheetDocument> & xDoc,const table::CellAddress & rPos)117 uno::Reference<table::XCell> lcl_GetCell( const uno::Reference<sheet::XSpreadsheetDocument>& xDoc,
118 const table::CellAddress& rPos )
119 {
120 uno::Reference<container::XIndexAccess> xSheets( xDoc->getSheets(), uno::UNO_QUERY );
121 uno::Reference<sheet::XSpreadsheet> xSheet( xSheets->getByIndex( rPos.Sheet ), uno::UNO_QUERY );
122 return xSheet->getCellByPosition( rPos.Column, rPos.Row );
123 }
124
lcl_SetValue(const uno::Reference<sheet::XSpreadsheetDocument> & xDoc,const table::CellAddress & rPos,double fValue)125 void lcl_SetValue( const uno::Reference<sheet::XSpreadsheetDocument>& xDoc,
126 const table::CellAddress& rPos, double fValue )
127 {
128 lcl_GetCell( xDoc, rPos )->setValue( fValue );
129 }
130
lcl_GetValue(const uno::Reference<sheet::XSpreadsheetDocument> & xDoc,const table::CellAddress & rPos)131 double lcl_GetValue( const uno::Reference<sheet::XSpreadsheetDocument>& xDoc,
132 const table::CellAddress& rPos )
133 {
134 return lcl_GetCell( xDoc, rPos )->getValue();
135 }
136
137 // -------------------------------------------------------------------------
138
SolverComponent(const uno::Reference<uno::XComponentContext> &)139 SolverComponent::SolverComponent( const uno::Reference<uno::XComponentContext>& /* rSMgr */ ) :
140 OPropertyContainer( GetBroadcastHelper() ),
141 mbMaximize( sal_True ),
142 mbNonNegative( sal_False ),
143 mbInteger( sal_False ),
144 mnTimeout( 120 ),
145 mnEpsilonLevel( 0 ),
146 mbLimitBBDepth( sal_True ),
147 mbNonLinearTest( sal_True ),
148 mbSuccess( sal_False ),
149 mfResultValue( 0.0 )
150 {
151 // for XPropertySet implementation:
152 registerProperty( C2U(STR_NONNEGATIVE), PROP_NONNEGATIVE, 0, &mbNonNegative, getCppuType( &mbNonNegative ) );
153 registerProperty( C2U(STR_INTEGER), PROP_INTEGER, 0, &mbInteger, getCppuType( &mbInteger ) );
154 registerProperty( C2U(STR_TIMEOUT), PROP_TIMEOUT, 0, &mnTimeout, getCppuType( &mnTimeout ) );
155 registerProperty( C2U(STR_EPSILONLEVEL), PROP_EPSILONLEVEL, 0, &mnEpsilonLevel, getCppuType( &mnEpsilonLevel ) );
156 registerProperty( C2U(STR_LIMITBBDEPTH), PROP_LIMITBBDEPTH, 0, &mbLimitBBDepth, getCppuType( &mbLimitBBDepth ) );
157 registerProperty( C2U(STR_NONLINEARTEST), PROP_NONLINEARTEST, 0, &mbNonLinearTest, getCppuType( &mbNonLinearTest ) );
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()
180 {
181 return createPropertySetInfo( getInfoHelper() );
182 }
183
184 // XSolverDescription
185
getComponentDescription()186 OUString SAL_CALL SolverComponent::getComponentDescription()
187 {
188 return lcl_GetResourceString( RID_SOLVER_COMPONENT );
189 }
190
getStatusDescription()191 OUString SAL_CALL SolverComponent::getStatusDescription()
192 {
193 return maStatus;
194 }
195
getPropertyDescription(const OUString & rPropertyName)196 OUString SAL_CALL SolverComponent::getPropertyDescription( const OUString& rPropertyName )
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 case PROP_NONLINEARTEST:
218 nResId = RID_PROPERTY_NONLINEARTEST;
219 break;
220 default:
221 {
222 // unknown - leave empty
223 }
224 }
225 OUString aRet;
226 if ( nResId )
227 aRet = lcl_GetResourceString( nResId );
228 return aRet;
229 }
230
231 // XSolver: settings
232
getDocument()233 uno::Reference<sheet::XSpreadsheetDocument> SAL_CALL SolverComponent::getDocument()
234 {
235 return mxDoc;
236 }
237
setDocument(const uno::Reference<sheet::XSpreadsheetDocument> & _document)238 void SAL_CALL SolverComponent::setDocument( const uno::Reference<sheet::XSpreadsheetDocument>& _document )
239 {
240 mxDoc = _document;
241 }
242
getObjective()243 table::CellAddress SAL_CALL SolverComponent::getObjective()
244 {
245 return maObjective;
246 }
247
setObjective(const table::CellAddress & _objective)248 void SAL_CALL SolverComponent::setObjective( const table::CellAddress& _objective )
249 {
250 maObjective = _objective;
251 }
252
getVariables()253 uno::Sequence<table::CellAddress> SAL_CALL SolverComponent::getVariables()
254 {
255 return maVariables;
256 }
257
setVariables(const uno::Sequence<table::CellAddress> & _variables)258 void SAL_CALL SolverComponent::setVariables( const uno::Sequence<table::CellAddress>& _variables )
259 {
260 maVariables = _variables;
261 }
262
getConstraints()263 uno::Sequence<sheet::SolverConstraint> SAL_CALL SolverComponent::getConstraints()
264 {
265 return maConstraints;
266 }
267
setConstraints(const uno::Sequence<sheet::SolverConstraint> & _constraints)268 void SAL_CALL SolverComponent::setConstraints( const uno::Sequence<sheet::SolverConstraint>& _constraints )
269 {
270 maConstraints = _constraints;
271 }
272
getMaximize()273 sal_Bool SAL_CALL SolverComponent::getMaximize()
274 {
275 return mbMaximize;
276 }
277
setMaximize(sal_Bool _maximize)278 void SAL_CALL SolverComponent::setMaximize( sal_Bool _maximize )
279 {
280 mbMaximize = _maximize;
281 }
282
283 // XSolver: get results
284
getSuccess()285 sal_Bool SAL_CALL SolverComponent::getSuccess()
286 {
287 return mbSuccess;
288 }
289
getResultValue()290 double SAL_CALL SolverComponent::getResultValue()
291 {
292 return mfResultValue;
293 }
294
getSolution()295 uno::Sequence<double> SAL_CALL SolverComponent::getSolution()
296 {
297 return maSolution;
298 }
299
300 // -------------------------------------------------------------------------
301
solve()302 void SAL_CALL SolverComponent::solve()
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 if ( mbNonLinearTest )
374 {
375 bool bLinear ( sal_True );
376 bLinear = rtl::math::approxEqual( fTwo, fInitial + 2.0 * fCoeff ) ||
377 rtl::math::approxEqual( fInitial, fTwo - 2.0 * fCoeff );
378 // second comparison is needed in case fTwo is zero
379 if ( !bLinear )
380 maStatus = lcl_GetResourceString( RID_ERROR_NONLINEAR );
381 }
382 }
383
384 lcl_SetValue( mxDoc, *aVarIter, 0.0 ); // set back to zero for examining next variable
385 }
386
387 xModel->unlockControllers();
388
389 if ( maStatus.getLength() )
390 return;
391
392 //
393 // build parameter arrays for CoinMP
394 //
395
396 // set objective function
397
398 const std::vector<double>& rObjCoeff = aCellsHash[maObjective];
399 double* pObjectCoeffs = new double[nVariables];
400 for (nVar=0; nVar<nVariables; nVar++)
401 pObjectCoeffs[nVar] = rObjCoeff[nVar+1];
402 double nObjectConst = rObjCoeff[0]; // constant term of objective
403
404 // add rows
405
406 size_t nRows = maConstraints.getLength();
407 size_t nCompSize = nVariables * nRows;
408 double* pCompMatrix = new double[nCompSize]; // first collect all coefficients, row-wise
409 for (size_t i=0; i<nCompSize; i++)
410 pCompMatrix[i] = 0.0;
411
412 double* pRHS = new double[nRows];
413 char* pRowType = new char[nRows];
414 for (size_t i=0; i<nRows; i++)
415 {
416 pRHS[i] = 0.0;
417 pRowType[i] = 'N';
418 }
419
420 for (sal_Int32 nConstrPos = 0; nConstrPos < maConstraints.getLength(); ++nConstrPos)
421 {
422 // integer constraints are set later
423 sheet::SolverConstraintOperator eOp = maConstraints[nConstrPos].Operator;
424 if ( eOp == sheet::SolverConstraintOperator_LESS_EQUAL ||
425 eOp == sheet::SolverConstraintOperator_GREATER_EQUAL ||
426 eOp == sheet::SolverConstraintOperator_EQUAL )
427 {
428 double fDirectValue = 0.0;
429 bool bRightCell = false;
430 table::CellAddress aRightAddr;
431 const uno::Any& rRightAny = maConstraints[nConstrPos].Right;
432 if ( rRightAny >>= aRightAddr )
433 bRightCell = true; // cell specified as right-hand side
434 else
435 rRightAny >>= fDirectValue; // constant value
436
437 table::CellAddress aLeftAddr = maConstraints[nConstrPos].Left;
438
439 const std::vector<double>& rLeftCoeff = aCellsHash[aLeftAddr];
440 double* pValues = &pCompMatrix[nConstrPos * nVariables];
441 for (nVar=0; nVar<nVariables; nVar++)
442 pValues[nVar] = rLeftCoeff[nVar+1];
443
444 // if left hand cell has a constant term, put into rhs value
445 double fRightValue = -rLeftCoeff[0];
446
447 if ( bRightCell )
448 {
449 const std::vector<double>& rRightCoeff = aCellsHash[aRightAddr];
450 // modify pValues with rhs coefficients
451 for (nVar=0; nVar<nVariables; nVar++)
452 pValues[nVar] -= rRightCoeff[nVar+1];
453
454 fRightValue += rRightCoeff[0]; // constant term
455 }
456 else
457 fRightValue += fDirectValue;
458
459 switch ( eOp )
460 {
461 case sheet::SolverConstraintOperator_LESS_EQUAL: pRowType[nConstrPos] = 'L'; break;
462 case sheet::SolverConstraintOperator_GREATER_EQUAL: pRowType[nConstrPos] = 'G'; break;
463 case sheet::SolverConstraintOperator_EQUAL: pRowType[nConstrPos] = 'E'; break;
464 default:
465 OSL_ENSURE( false, "unexpected enum type" );
466 }
467 pRHS[nConstrPos] = fRightValue;
468 }
469 }
470
471 // Find non-zero coefficients, column-wise
472
473 int* pMatrixBegin = new int[nVariables+1];
474 int* pMatrixCount = new int[nVariables];
475 double* pMatrix = new double[nCompSize]; // not always completely used
476 int* pMatrixIndex = new int[nCompSize];
477 int nMatrixPos = 0;
478 for (nVar=0; nVar<nVariables; nVar++)
479 {
480 int nBegin = nMatrixPos;
481 for (size_t nRow=0; nRow<nRows; nRow++)
482 {
483 double fCoeff = pCompMatrix[ nRow * nVariables + nVar ]; // row-wise
484 if ( fCoeff != 0.0 )
485 {
486 pMatrix[nMatrixPos] = fCoeff;
487 pMatrixIndex[nMatrixPos] = nRow;
488 ++nMatrixPos;
489 }
490 }
491 pMatrixBegin[nVar] = nBegin;
492 pMatrixCount[nVar] = nMatrixPos - nBegin;
493 }
494 pMatrixBegin[nVariables] = nMatrixPos;
495 delete[] pCompMatrix;
496 pCompMatrix = NULL;
497
498 // apply settings to all variables
499
500 double* pLowerBounds = new double[nVariables];
501 double* pUpperBounds = new double[nVariables];
502 for (nVar=0; nVar<nVariables; nVar++)
503 {
504 pLowerBounds[nVar] = mbNonNegative ? 0.0 : -DBL_MAX;
505 pUpperBounds[nVar] = DBL_MAX;
506
507 // bounds could possibly be further restricted from single-cell constraints
508 }
509
510 char* pColType = new char[nVariables];
511 for (nVar=0; nVar<nVariables; nVar++)
512 pColType[nVar] = mbInteger ? 'I' : 'C';
513
514 // apply single-var integer constraints
515
516 for (sal_Int32 nConstrPos = 0; nConstrPos < maConstraints.getLength(); ++nConstrPos)
517 {
518 sheet::SolverConstraintOperator eOp = maConstraints[nConstrPos].Operator;
519 if ( eOp == sheet::SolverConstraintOperator_INTEGER ||
520 eOp == sheet::SolverConstraintOperator_BINARY )
521 {
522 table::CellAddress aLeftAddr = maConstraints[nConstrPos].Left;
523 // find variable index for cell
524 for (nVar=0; nVar<nVariables; nVar++)
525 if ( AddressEqual( aVariableCells[nVar], aLeftAddr ) )
526 {
527 if ( eOp == sheet::SolverConstraintOperator_INTEGER )
528 pColType[nVar] = 'I';
529 else
530 {
531 pColType[nVar] = 'B';
532 pLowerBounds[nVar] = 0.0;
533 pUpperBounds[nVar] = 1.0;
534 }
535 }
536 }
537 }
538
539 int nObjectSense = mbMaximize ? SOLV_OBJSENS_MAX : SOLV_OBJSENS_MIN;
540
541 HPROB hProb = CoinCreateProblem("");
542 int nResult = CoinLoadProblem( hProb, nVariables, nRows, nMatrixPos, 0,
543 nObjectSense, nObjectConst, pObjectCoeffs,
544 pLowerBounds, pUpperBounds, pRowType, pRHS, NULL,
545 pMatrixBegin, pMatrixCount, pMatrixIndex, pMatrix,
546 NULL, NULL, NULL );
547 nResult = CoinLoadInteger( hProb, pColType );
548
549 delete[] pColType;
550 delete[] pMatrixIndex;
551 delete[] pMatrix;
552 delete[] pMatrixCount;
553 delete[] pMatrixBegin;
554 delete[] pUpperBounds;
555 delete[] pLowerBounds;
556 delete[] pRowType;
557 delete[] pRHS;
558 delete[] pObjectCoeffs;
559
560 CoinSetRealOption( hProb, COIN_REAL_MAXSECONDS, mnTimeout );
561 CoinSetRealOption( hProb, COIN_REAL_MIPMAXSEC, mnTimeout );
562
563 // TODO: handle (or remove) settings: epsilon, B&B depth
564
565 // solve model
566
567 nResult = CoinCheckProblem( hProb );
568 if (nResult != SOLV_CALL_SUCCESS)
569 {
570 // report invalid model
571
572 maStatus = lcl_GetResourceString( RID_ERROR_INVALIDMODEL );
573 CoinUnloadProblem(hProb);
574 return;
575 }
576 nResult = CoinOptimizeProblem( hProb, 0 );
577
578 mbSuccess = ( nResult == SOLV_CALL_SUCCESS );
579 if ( mbSuccess )
580 {
581 // get solution
582
583 maSolution.realloc( nVariables );
584 CoinGetSolutionValues( hProb, maSolution.getArray(), NULL, NULL, NULL );
585 mfResultValue = CoinGetObjectValue( hProb );
586 }
587 else
588 {
589 int nSolutionStatus = CoinGetSolutionStatus( hProb );
590 if ( nSolutionStatus == 1 )
591 maStatus = lcl_GetResourceString( RID_ERROR_INFEASIBLE );
592 else if ( nSolutionStatus == 2 )
593 maStatus = lcl_GetResourceString( RID_ERROR_UNBOUNDED );
594 else if ( nSolutionStatus == 3 )
595 maStatus = lcl_GetResourceString( RID_ERROR_ITERATIONLIMIT );
596 else if ( nSolutionStatus == 4 )
597 maStatus = lcl_GetResourceString( RID_ERROR_SOLVERERROR );
598 else if ( nSolutionStatus == 5 )
599 maStatus = lcl_GetResourceString( RID_ERROR_USERSTOP );
600 else if ( nSolutionStatus >= 6 )
601 maStatus = lcl_GetResourceString( RID_ERROR_UNKNOWN );
602
603 }
604
605 CoinUnloadProblem( hProb );
606 }
607
608 // -------------------------------------------------------------------------
609
610 // XServiceInfo
611
SolverComponent_getSupportedServiceNames()612 uno::Sequence< OUString > SolverComponent_getSupportedServiceNames()
613 {
614 uno::Sequence< OUString > aServiceNames( 1 );
615 aServiceNames[ 0 ] = OUString::createFromAscii( "com.sun.star.sheet.Solver" );
616 return aServiceNames;
617 }
618
SolverComponent_getImplementationName()619 OUString SolverComponent_getImplementationName()
620 {
621 return OUString::createFromAscii( "com.sun.star.comp.Calc.Solver" );
622 }
623
getImplementationName()624 OUString SAL_CALL SolverComponent::getImplementationName()
625 {
626 return SolverComponent_getImplementationName();
627 }
628
supportsService(const OUString & rServiceName)629 sal_Bool SAL_CALL SolverComponent::supportsService( const OUString& rServiceName )
630 {
631 const uno::Sequence< OUString > aServices = SolverComponent_getSupportedServiceNames();
632 const OUString* pArray = aServices.getConstArray();
633 const OUString* pArrayEnd = pArray + aServices.getLength();
634 return ::std::find( pArray, pArrayEnd, rServiceName ) != pArrayEnd;
635 }
636
getSupportedServiceNames()637 uno::Sequence<OUString> SAL_CALL SolverComponent::getSupportedServiceNames()
638 {
639 return SolverComponent_getSupportedServiceNames();
640 }
641
SolverComponent_createInstance(const uno::Reference<uno::XComponentContext> & rSMgr)642 uno::Reference<uno::XInterface> SolverComponent_createInstance( const uno::Reference<uno::XComponentContext>& rSMgr )
643 {
644 return (cppu::OWeakObject*) new SolverComponent( rSMgr );
645 }
646
647 // -------------------------------------------------------------------------
648
649 extern "C"
650 {
component_getImplementationEnvironment(const sal_Char ** ppEnvTypeName,uno_Environment **)651 SAL_DLLPUBLIC_EXPORT void SAL_CALL component_getImplementationEnvironment(
652 const sal_Char ** ppEnvTypeName, uno_Environment ** )
653 {
654 *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
655 }
656
657 // -------------------------------------------------------------------------
658
component_getFactory(const sal_Char * pImplName,void * pServiceManager,void *)659 SAL_DLLPUBLIC_EXPORT void* SAL_CALL component_getFactory( const sal_Char * pImplName, void * pServiceManager, void * /*pRegistryKey*/ )
660 {
661 OUString aImplName( OUString::createFromAscii( pImplName ) );
662 void* pRet = 0;
663
664 if( pServiceManager )
665 {
666 uno::Reference< lang::XSingleComponentFactory > xFactory;
667 if( aImplName.equals( SolverComponent_getImplementationName() ) )
668 xFactory = cppu::createSingleComponentFactory(
669 SolverComponent_createInstance,
670 OUString::createFromAscii( pImplName ),
671 SolverComponent_getSupportedServiceNames() );
672
673 if( xFactory.is() )
674 {
675 xFactory->acquire();
676 pRet = xFactory.get();
677 }
678 }
679 return pRet;
680 }
681 }
682