Kaydet (Commit) 077bdc73 authored tarafından Matúš Kukan's avatar Matúš Kukan

sccomp: Share common code and have only one library.

And use constructor feature for UNO implementations.

Change-Id: I42b6a6f417049cc8e2d44b74c7adc552680b1f2d
üst dae3e183
...@@ -150,7 +150,6 @@ $(eval $(call gb_Helper_register_libraries_for_install,OOOLIBS,base, \ ...@@ -150,7 +150,6 @@ $(eval $(call gb_Helper_register_libraries_for_install,OOOLIBS,base, \
$(eval $(call gb_Helper_register_libraries_for_install,OOOLIBS,calc, \ $(eval $(call gb_Helper_register_libraries_for_install,OOOLIBS,calc, \
analysis \ analysis \
calc \ calc \
$(if $(ENABLE_COINMP),coinmpsolver) \
date \ date \
pricing \ pricing \
sc \ sc \
...@@ -158,7 +157,7 @@ $(eval $(call gb_Helper_register_libraries_for_install,OOOLIBS,calc, \ ...@@ -158,7 +157,7 @@ $(eval $(call gb_Helper_register_libraries_for_install,OOOLIBS,calc, \
scfilt \ scfilt \
scui \ scui \
$(if $(ENABLE_OPENCL),scopencl) \ $(if $(ENABLE_OPENCL),scopencl) \
$(if $(ENABLE_LPSOLVE),solver) \ $(if $(ENABLE_COINMP)$(ENABLE_LPSOLVE),solver) \
$(if $(DISABLE_SCRIPTING),,vbaobj) \ $(if $(DISABLE_SCRIPTING),,vbaobj) \
$(if $(ENABLE_TELEPATHY),tubes) \ $(if $(ENABLE_TELEPATHY),tubes) \
)) ))
......
...@@ -2020,6 +2020,10 @@ $(eval $(call gb_Helper_register_packages_for_install,ooo,\ ...@@ -2020,6 +2020,10 @@ $(eval $(call gb_Helper_register_packages_for_install,ooo,\
endif # SYSTEM_LPSOLVE endif # SYSTEM_LPSOLVE
else
gb_LinkTarget__use_lpsolve :=
endif # ENABLE_LPSOLVE endif # ENABLE_LPSOLVE
ifneq ($(ENABLE_COINMP),) ifneq ($(ENABLE_COINMP),)
...@@ -2052,6 +2056,10 @@ $(eval $(call gb_Helper_register_packages_for_install,ooo,\ ...@@ -2052,6 +2056,10 @@ $(eval $(call gb_Helper_register_packages_for_install,ooo,\
coinmp \ coinmp \
)) ))
else
gb_LinkTarget__use_coinmp :=
endif # ENABLE_COINMP endif # ENABLE_COINMP
ifeq ($(ENABLE_GIO),TRUE) ifeq ($(ENABLE_GIO),TRUE)
......
...@@ -113,7 +113,7 @@ $(eval $(call gb_Rdb_add_components,services,\ ...@@ -113,7 +113,7 @@ $(eval $(call gb_Rdb_add_components,services,\
sccomp/source/solver/coinmpsolver \ sccomp/source/solver/coinmpsolver \
) \ ) \
$(if $(ENABLE_LPSOLVE), \ $(if $(ENABLE_LPSOLVE), \
sccomp/source/solver/solver \ sccomp/source/solver/lpsolvesolver \
) \ ) \
writerfilter/util/writerfilter \ writerfilter/util/writerfilter \
writerperfect/source/draw/wpftdraw \ writerperfect/source/draw/wpftdraw \
......
# -*- Mode: makefile-gmake; tab-width: 4; indent-tabs-mode: t -*-
#
# This file is part of the LibreOffice project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
$(eval $(call gb_Library_Library,coinmpsolver))
$(eval $(call gb_Library_set_componentfile,coinmpsolver,sccomp/source/solver/coinmpsolver))
$(eval $(call gb_Library_use_sdk_api,coinmpsolver))
$(eval $(call gb_Library_use_libraries,coinmpsolver,\
comphelper \
cppu \
cppuhelper \
sal \
tl \
i18nlangtag \
$(gb_UWINAPI) \
))
$(eval $(call gb_Library_use_externals,coinmpsolver,\
boost_headers \
coinmp \
))
$(eval $(call gb_Library_add_exception_objects,coinmpsolver,\
sccomp/source/solver/solver \
))
# vim: set noet sw=4 ts=4:
...@@ -19,7 +19,8 @@ ...@@ -19,7 +19,8 @@
$(eval $(call gb_Library_Library,solver)) $(eval $(call gb_Library_Library,solver))
$(eval $(call gb_Library_set_componentfile,solver,sccomp/source/solver/solver)) $(if $(ENABLE_COINMP),$(eval $(call gb_Library_set_componentfile,solver,sccomp/source/solver/coinmpsolver)))
$(if $(ENABLE_LPSOLVE),$(eval $(call gb_Library_set_componentfile,solver,sccomp/source/solver/lpsolvesolver)))
$(eval $(call gb_Library_use_sdk_api,solver)) $(eval $(call gb_Library_use_sdk_api,solver))
...@@ -35,11 +36,14 @@ $(eval $(call gb_Library_use_libraries,solver,\ ...@@ -35,11 +36,14 @@ $(eval $(call gb_Library_use_libraries,solver,\
$(eval $(call gb_Library_use_externals,solver,\ $(eval $(call gb_Library_use_externals,solver,\
boost_headers \ boost_headers \
coinmp \
lpsolve \ lpsolve \
)) ))
$(eval $(call gb_Library_add_exception_objects,solver,\ $(eval $(call gb_Library_add_exception_objects,solver,\
sccomp/source/solver/solver-lpsolve \ sccomp/source/solver/SolverComponent \
$(if $(ENABLE_COINMP), sccomp/source/solver/CoinMPSolver) \
$(if $(ENABLE_LPSOLVE), sccomp/source/solver/LpsolveSolver) \
)) ))
# vim: set noet sw=4 ts=4: # vim: set noet sw=4 ts=4:
...@@ -20,8 +20,7 @@ ...@@ -20,8 +20,7 @@
$(eval $(call gb_Module_Module,sccomp)) $(eval $(call gb_Module_Module,sccomp))
$(eval $(call gb_Module_add_targets,sccomp,\ $(eval $(call gb_Module_add_targets,sccomp,\
$(if $(ENABLE_COINMP), Library_coinmpsolver) \ $(if $(ENABLE_COINMP)$(ENABLE_LPSOLVE), Library_solver) \
$(if $(ENABLE_LPSOLVE), Library_solver) \
)) ))
$(eval $(call gb_Module_add_l10n_targets,sccomp,\ $(eval $(call gb_Module_add_l10n_targets,sccomp,\
......
...@@ -19,276 +19,34 @@ ...@@ -19,276 +19,34 @@
#include <CoinMP.h> #include <CoinMP.h>
#include "solver.hxx" #include "SolverComponent.hxx"
#include "solver.hrc" #include "solver.hrc"
#include <com/sun/star/beans/XPropertySet.hpp>
#include <com/sun/star/container/XIndexAccess.hpp>
#include <com/sun/star/frame/XModel.hpp> #include <com/sun/star/frame/XModel.hpp>
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
#include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
#include <com/sun/star/sheet/XSpreadsheet.hpp>
#include <com/sun/star/table/CellAddress.hpp> #include <com/sun/star/table/CellAddress.hpp>
#include <com/sun/star/table/CellRangeAddress.hpp> #include <com/sun/star/uno/XComponentContext.hpp>
#include <com/sun/star/text/XTextRange.hpp>
#include <rtl/math.hxx> #include <rtl/math.hxx>
#include <rtl/ustrbuf.hxx>
#include <cppuhelper/factory.hxx>
#include <cppuhelper/supportsservice.hxx>
#include <vector> #include <vector>
#include <boost/unordered_map.hpp>
#include <tools/resmgr.hxx>
using namespace com::sun::star; using namespace com::sun::star;
class CoinMPSolver : public SolverComponent
#define STR_NONNEGATIVE "NonNegative"
#define STR_INTEGER "Integer"
#define STR_TIMEOUT "Timeout"
#define STR_EPSILONLEVEL "EpsilonLevel"
#define STR_LIMITBBDEPTH "LimitBBDepth"
// Resources from tools are used for translated strings
static ResMgr* pSolverResMgr = NULL;
static OUString lcl_GetResourceString( sal_uInt32 nId )
{
if (!pSolverResMgr)
pSolverResMgr = ResMgr::CreateResMgr("solver");
return ResId(nId, *pSolverResMgr).toString();
}
namespace
{ {
enum public:
{ CoinMPSolver() {}
PROP_NONNEGATIVE, virtual ~CoinMPSolver() SAL_OVERRIDE {}
PROP_INTEGER,
PROP_TIMEOUT,
PROP_EPSILONLEVEL,
PROP_LIMITBBDEPTH
};
}
private:
virtual void SAL_CALL solve() throw(css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
// hash map for the coefficients of a dependent cell (objective or constraint) virtual OUString SAL_CALL getImplementationName()
// The size of each vector is the number of columns (variable cells) plus one, first entry is initial value. throw(css::uno::RuntimeException, std::exception) SAL_OVERRIDE
struct ScSolverCellHash
{
size_t operator()( const table::CellAddress& rAddress ) const
{ {
return ( rAddress.Sheet << 24 ) | ( rAddress.Column << 16 ) | rAddress.Row; return OUString("com.sun.star.comp.Calc.CoinMPSolver");
} }
}; };
inline bool AddressEqual( const table::CellAddress& rAddr1, const table::CellAddress& rAddr2 ) void SAL_CALL CoinMPSolver::solve() throw(uno::RuntimeException, std::exception)
{
return rAddr1.Sheet == rAddr2.Sheet && rAddr1.Column == rAddr2.Column && rAddr1.Row == rAddr2.Row;
}
struct ScSolverCellEqual
{
bool operator()( const table::CellAddress& rAddr1, const table::CellAddress& rAddr2 ) const
{
return AddressEqual( rAddr1, rAddr2 );
}
};
typedef boost::unordered_map< table::CellAddress, std::vector<double>, ScSolverCellHash, ScSolverCellEqual > ScSolverCellHashMap;
static uno::Reference<table::XCell> lcl_GetCell( const uno::Reference<sheet::XSpreadsheetDocument>& xDoc,
const table::CellAddress& rPos )
{
uno::Reference<container::XIndexAccess> xSheets( xDoc->getSheets(), uno::UNO_QUERY );
uno::Reference<sheet::XSpreadsheet> xSheet( xSheets->getByIndex( rPos.Sheet ), uno::UNO_QUERY );
return xSheet->getCellByPosition( rPos.Column, rPos.Row );
}
static void lcl_SetValue( const uno::Reference<sheet::XSpreadsheetDocument>& xDoc,
const table::CellAddress& rPos, double fValue )
{
lcl_GetCell( xDoc, rPos )->setValue( fValue );
}
static double lcl_GetValue( const uno::Reference<sheet::XSpreadsheetDocument>& xDoc,
const table::CellAddress& rPos )
{
return lcl_GetCell( xDoc, rPos )->getValue();
}
SolverComponent::SolverComponent( const uno::Reference<uno::XComponentContext>& /* rSMgr */ ) :
OPropertyContainer( GetBroadcastHelper() ),
mbMaximize( sal_True ),
mbNonNegative( sal_False ),
mbInteger( sal_False ),
mnTimeout( 100 ),
mnEpsilonLevel( 0 ),
mbLimitBBDepth( sal_True ),
mbSuccess( sal_False ),
mfResultValue( 0.0 )
{
// for XPropertySet implementation:
registerProperty( STR_NONNEGATIVE, PROP_NONNEGATIVE, 0, &mbNonNegative, getCppuType( &mbNonNegative ) );
registerProperty( STR_INTEGER, PROP_INTEGER, 0, &mbInteger, getCppuType( &mbInteger ) );
registerProperty( STR_TIMEOUT, PROP_TIMEOUT, 0, &mnTimeout, getCppuType( &mnTimeout ) );
registerProperty( STR_EPSILONLEVEL, PROP_EPSILONLEVEL, 0, &mnEpsilonLevel, getCppuType( &mnEpsilonLevel ) );
registerProperty( STR_LIMITBBDEPTH, PROP_LIMITBBDEPTH, 0, &mbLimitBBDepth, getCppuType( &mbLimitBBDepth ) );
}
SolverComponent::~SolverComponent()
{
}
IMPLEMENT_FORWARD_XINTERFACE2( SolverComponent, SolverComponent_Base, OPropertyContainer )
IMPLEMENT_FORWARD_XTYPEPROVIDER2( SolverComponent, SolverComponent_Base, OPropertyContainer )
cppu::IPropertyArrayHelper* SolverComponent::createArrayHelper() const
{
uno::Sequence<beans::Property> aProps;
describeProperties( aProps );
return new cppu::OPropertyArrayHelper( aProps );
}
cppu::IPropertyArrayHelper& SAL_CALL SolverComponent::getInfoHelper()
{
return *getArrayHelper();
}
uno::Reference<beans::XPropertySetInfo> SAL_CALL SolverComponent::getPropertySetInfo() throw(uno::RuntimeException, std::exception)
{
return createPropertySetInfo( getInfoHelper() );
}
// XSolverDescription
OUString SAL_CALL SolverComponent::getComponentDescription() throw (uno::RuntimeException, std::exception)
{
return lcl_GetResourceString( RID_COINMP_SOLVER_COMPONENT );
}
OUString SAL_CALL SolverComponent::getStatusDescription() throw (uno::RuntimeException, std::exception)
{
return maStatus;
}
OUString SAL_CALL SolverComponent::getPropertyDescription( const OUString& rPropertyName ) throw (uno::RuntimeException, std::exception)
{
sal_uInt32 nResId = 0;
sal_Int32 nHandle = getInfoHelper().getHandleByName( rPropertyName );
switch (nHandle)
{
case PROP_NONNEGATIVE:
nResId = RID_PROPERTY_NONNEGATIVE;
break;
case PROP_INTEGER:
nResId = RID_PROPERTY_INTEGER;
break;
case PROP_TIMEOUT:
nResId = RID_PROPERTY_TIMEOUT;
break;
case PROP_EPSILONLEVEL:
nResId = RID_PROPERTY_EPSILONLEVEL;
break;
case PROP_LIMITBBDEPTH:
nResId = RID_PROPERTY_LIMITBBDEPTH;
break;
default:
{
// unknown - leave empty
}
}
OUString aRet;
if ( nResId )
aRet = lcl_GetResourceString( nResId );
return aRet;
}
// XSolver: settings
uno::Reference<sheet::XSpreadsheetDocument> SAL_CALL SolverComponent::getDocument() throw(uno::RuntimeException, std::exception)
{
return mxDoc;
}
void SAL_CALL SolverComponent::setDocument( const uno::Reference<sheet::XSpreadsheetDocument>& _document )
throw(uno::RuntimeException, std::exception)
{
mxDoc = _document;
}
table::CellAddress SAL_CALL SolverComponent::getObjective() throw(uno::RuntimeException, std::exception)
{
return maObjective;
}
void SAL_CALL SolverComponent::setObjective( const table::CellAddress& _objective ) throw(uno::RuntimeException, std::exception)
{
maObjective = _objective;
}
uno::Sequence<table::CellAddress> SAL_CALL SolverComponent::getVariables() throw(uno::RuntimeException, std::exception)
{
return maVariables;
}
void SAL_CALL SolverComponent::setVariables( const uno::Sequence<table::CellAddress>& _variables )
throw(uno::RuntimeException, std::exception)
{
maVariables = _variables;
}
uno::Sequence<sheet::SolverConstraint> SAL_CALL SolverComponent::getConstraints() throw(uno::RuntimeException, std::exception)
{
return maConstraints;
}
void SAL_CALL SolverComponent::setConstraints( const uno::Sequence<sheet::SolverConstraint>& _constraints )
throw(uno::RuntimeException, std::exception)
{
maConstraints = _constraints;
}
sal_Bool SAL_CALL SolverComponent::getMaximize() throw(uno::RuntimeException, std::exception)
{
return mbMaximize;
}
void SAL_CALL SolverComponent::setMaximize( sal_Bool _maximize ) throw(uno::RuntimeException, std::exception)
{
mbMaximize = _maximize;
}
// XSolver: get results
sal_Bool SAL_CALL SolverComponent::getSuccess() throw(uno::RuntimeException, std::exception)
{
return mbSuccess;
}
double SAL_CALL SolverComponent::getResultValue() throw(uno::RuntimeException, std::exception)
{
return mfResultValue;
}
uno::Sequence<double> SAL_CALL SolverComponent::getSolution() throw(uno::RuntimeException, std::exception)
{
return maSolution;
}
void SAL_CALL SolverComponent::solve() throw(uno::RuntimeException, std::exception)
{ {
uno::Reference<frame::XModel> xModel( mxDoc, uno::UNO_QUERY ); uno::Reference<frame::XModel> xModel( mxDoc, uno::UNO_QUERY );
if ( !xModel.is() ) if ( !xModel.is() )
...@@ -327,46 +85,46 @@ void SAL_CALL SolverComponent::solve() throw(uno::RuntimeException, std::excepti ...@@ -327,46 +85,46 @@ void SAL_CALL SolverComponent::solve() throw(uno::RuntimeException, std::excepti
std::vector<table::CellAddress>::const_iterator aVarIter; std::vector<table::CellAddress>::const_iterator aVarIter;
for ( aVarIter = aVariableCells.begin(); aVarIter != aVariableCells.end(); ++aVarIter ) for ( aVarIter = aVariableCells.begin(); aVarIter != aVariableCells.end(); ++aVarIter )
{ {
lcl_SetValue( mxDoc, *aVarIter, 0.0 ); SolverComponent::SetValue( mxDoc, *aVarIter, 0.0 );
} }
// read initial values from all dependent cells // read initial values from all dependent cells
ScSolverCellHashMap::iterator aCellsIter; ScSolverCellHashMap::iterator aCellsIter;
for ( aCellsIter = aCellsHash.begin(); aCellsIter != aCellsHash.end(); ++aCellsIter ) for ( aCellsIter = aCellsHash.begin(); aCellsIter != aCellsHash.end(); ++aCellsIter )
{ {
double fValue = lcl_GetValue( mxDoc, aCellsIter->first ); double fValue = SolverComponent::GetValue( mxDoc, aCellsIter->first );
aCellsIter->second.push_back( fValue ); // store as first element, as-is aCellsIter->second.push_back( fValue ); // store as first element, as-is
} }
// loop through variables // loop through variables
for ( aVarIter = aVariableCells.begin(); aVarIter != aVariableCells.end(); ++aVarIter ) for ( aVarIter = aVariableCells.begin(); aVarIter != aVariableCells.end(); ++aVarIter )
{ {
lcl_SetValue( mxDoc, *aVarIter, 1.0 ); // set to 1 to examine influence SolverComponent::SetValue( mxDoc, *aVarIter, 1.0 ); // set to 1 to examine influence
// read value change from all dependent cells // read value change from all dependent cells
for ( aCellsIter = aCellsHash.begin(); aCellsIter != aCellsHash.end(); ++aCellsIter ) for ( aCellsIter = aCellsHash.begin(); aCellsIter != aCellsHash.end(); ++aCellsIter )
{ {
double fChanged = lcl_GetValue( mxDoc, aCellsIter->first ); double fChanged = SolverComponent::GetValue( mxDoc, aCellsIter->first );
double fInitial = aCellsIter->second.front(); double fInitial = aCellsIter->second.front();
aCellsIter->second.push_back( fChanged - fInitial ); aCellsIter->second.push_back( fChanged - fInitial );
} }
lcl_SetValue( mxDoc, *aVarIter, 2.0 ); // minimal test for linearity SolverComponent::SetValue( mxDoc, *aVarIter, 2.0 ); // minimal test for linearity
for ( aCellsIter = aCellsHash.begin(); aCellsIter != aCellsHash.end(); ++aCellsIter ) for ( aCellsIter = aCellsHash.begin(); aCellsIter != aCellsHash.end(); ++aCellsIter )
{ {
double fInitial = aCellsIter->second.front(); double fInitial = aCellsIter->second.front();
double fCoeff = aCellsIter->second.back(); // last appended: coefficient for this variable double fCoeff = aCellsIter->second.back(); // last appended: coefficient for this variable
double fTwo = lcl_GetValue( mxDoc, aCellsIter->first ); double fTwo = SolverComponent::GetValue( mxDoc, aCellsIter->first );
bool bLinear = rtl::math::approxEqual( fTwo, fInitial + 2.0 * fCoeff ) || bool bLinear = rtl::math::approxEqual( fTwo, fInitial + 2.0 * fCoeff ) ||
rtl::math::approxEqual( fInitial, fTwo - 2.0 * fCoeff ); rtl::math::approxEqual( fInitial, fTwo - 2.0 * fCoeff );
// second comparison is needed in case fTwo is zero // second comparison is needed in case fTwo is zero
if ( !bLinear ) if ( !bLinear )
maStatus = lcl_GetResourceString( RID_ERROR_NONLINEAR ); maStatus = SolverComponent::GetResourceString( RID_ERROR_NONLINEAR );
} }
lcl_SetValue( mxDoc, *aVarIter, 0.0 ); // set back to zero for examining next variable SolverComponent::SetValue( mxDoc, *aVarIter, 0.0 ); // set back to zero for examining next variable
} }
xModel->unlockControllers(); xModel->unlockControllers();
...@@ -565,9 +323,9 @@ void SAL_CALL SolverComponent::solve() throw(uno::RuntimeException, std::excepti ...@@ -565,9 +323,9 @@ void SAL_CALL SolverComponent::solve() throw(uno::RuntimeException, std::excepti
{ {
int nSolutionStatus = CoinGetSolutionStatus( hProb ); int nSolutionStatus = CoinGetSolutionStatus( hProb );
if ( nSolutionStatus == 1 ) if ( nSolutionStatus == 1 )
maStatus = lcl_GetResourceString( RID_ERROR_INFEASIBLE ); maStatus = SolverComponent::GetResourceString( RID_ERROR_INFEASIBLE );
else if ( nSolutionStatus == 2 ) else if ( nSolutionStatus == 2 )
maStatus = lcl_GetResourceString( RID_ERROR_UNBOUNDED ); maStatus = SolverComponent::GetResourceString( RID_ERROR_UNBOUNDED );
// TODO: detect timeout condition and report as RID_ERROR_TIMEOUT // TODO: detect timeout condition and report as RID_ERROR_TIMEOUT
// (currently reported as infeasible) // (currently reported as infeasible)
} }
...@@ -575,65 +333,12 @@ void SAL_CALL SolverComponent::solve() throw(uno::RuntimeException, std::excepti ...@@ -575,65 +333,12 @@ void SAL_CALL SolverComponent::solve() throw(uno::RuntimeException, std::excepti
CoinUnloadProblem( hProb ); CoinUnloadProblem( hProb );
} }
// XServiceInfo extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface * SAL_CALL
com_sun_star_comp_Calc_CoinMPSolver_get_implementation(
uno::Sequence< OUString > SolverComponent_getSupportedServiceNames() css::uno::XComponentContext *,
{ css::uno::Sequence<css::uno::Any> const &)
uno::Sequence< OUString > aServiceNames( 1 );
aServiceNames[ 0 ] = "com.sun.star.sheet.Solver";
return aServiceNames;
}
OUString SolverComponent_getImplementationName()
{
return OUString("com.sun.star.comp.Calc.CoinMPSolver");
}
OUString SAL_CALL SolverComponent::getImplementationName() throw(uno::RuntimeException, std::exception)
{
return SolverComponent_getImplementationName();
}
sal_Bool SAL_CALL SolverComponent::supportsService( const OUString& rServiceName ) throw(uno::RuntimeException, std::exception)
{ {
return cppu::supportsService(this, rServiceName); return cppu::acquire(new CoinMPSolver());
}
uno::Sequence<OUString> SAL_CALL SolverComponent::getSupportedServiceNames() throw(uno::RuntimeException, std::exception)
{
return SolverComponent_getSupportedServiceNames();
}
uno::Reference<uno::XInterface> SolverComponent_createInstance( const uno::Reference<uno::XComponentContext>& rSMgr )
throw(uno::Exception)
{
return (cppu::OWeakObject*) new SolverComponent( rSMgr );
}
extern "C"
{
SAL_DLLPUBLIC_EXPORT void* SAL_CALL coinmp_component_getFactory( const sal_Char * pImplName, void * pServiceManager, void * /*pRegistryKey*/ )
{
OUString aImplName( OUString::createFromAscii( pImplName ) );
void* pRet = 0;
if( pServiceManager )
{
uno::Reference< lang::XSingleComponentFactory > xFactory;
if( aImplName.equals( SolverComponent_getImplementationName() ) )
xFactory = cppu::createSingleComponentFactory(
SolverComponent_createInstance,
OUString::createFromAscii( pImplName ),
SolverComponent_getSupportedServiceNames() );
if( xFactory.is() )
{
xFactory->acquire();
pRet = xFactory.get();
}
}
return pRet;
}
} }
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
...@@ -52,276 +52,34 @@ ...@@ -52,276 +52,34 @@
#endif #endif
#undef LANGUAGE_NONE #undef LANGUAGE_NONE
#include "solver.hxx" #include "SolverComponent.hxx"
#include "solver.hrc" #include "solver.hrc"
#include <com/sun/star/beans/XPropertySet.hpp>
#include <com/sun/star/container/XIndexAccess.hpp>
#include <com/sun/star/frame/XModel.hpp> #include <com/sun/star/frame/XModel.hpp>
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
#include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
#include <com/sun/star/sheet/XSpreadsheet.hpp>
#include <com/sun/star/table/CellAddress.hpp> #include <com/sun/star/table/CellAddress.hpp>
#include <com/sun/star/table/CellRangeAddress.hpp> #include <com/sun/star/uno/XComponentContext.hpp>
#include <com/sun/star/text/XTextRange.hpp>
#include <rtl/math.hxx> #include <rtl/math.hxx>
#include <rtl/ustrbuf.hxx>
#include <cppuhelper/factory.hxx>
#include <cppuhelper/supportsservice.hxx> #include <cppuhelper/supportsservice.hxx>
#include <vector> #include <vector>
#include <boost/unordered_map.hpp>
#include <tools/resmgr.hxx>
using namespace com::sun::star; using namespace com::sun::star;
class LpsolveSolver : public SolverComponent
#define STR_NONNEGATIVE "NonNegative"
#define STR_INTEGER "Integer"
#define STR_TIMEOUT "Timeout"
#define STR_EPSILONLEVEL "EpsilonLevel"
#define STR_LIMITBBDEPTH "LimitBBDepth"
// Resources from tools are used for translated strings
static ResMgr* pSolverResMgr = NULL;
static OUString lcl_GetResourceString( sal_uInt32 nId )
{ {
if (!pSolverResMgr) public:
pSolverResMgr = ResMgr::CreateResMgr("solver"); LpsolveSolver() {}
virtual ~LpsolveSolver() SAL_OVERRIDE {}
return ResId(nId, *pSolverResMgr).toString();
}
private:
virtual void SAL_CALL solve() throw(css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
namespace virtual OUString SAL_CALL getImplementationName()
{ throw(css::uno::RuntimeException, std::exception) SAL_OVERRIDE
enum
{ {
PROP_NONNEGATIVE, return OUString("com.sun.star.comp.Calc.LpsolveSolver");
PROP_INTEGER,
PROP_TIMEOUT,
PROP_EPSILONLEVEL,
PROP_LIMITBBDEPTH
};
}
// hash map for the coefficients of a dependent cell (objective or constraint)
// The size of each vector is the number of columns (variable cells) plus one, first entry is initial value.
struct ScSolverCellHash
{
size_t operator()( const table::CellAddress& rAddress ) const
{
return ( rAddress.Sheet << 24 ) | ( rAddress.Column << 16 ) | rAddress.Row;
}
};
inline bool AddressEqual( const table::CellAddress& rAddr1, const table::CellAddress& rAddr2 )
{
return rAddr1.Sheet == rAddr2.Sheet && rAddr1.Column == rAddr2.Column && rAddr1.Row == rAddr2.Row;
}
struct ScSolverCellEqual
{
bool operator()( const table::CellAddress& rAddr1, const table::CellAddress& rAddr2 ) const
{
return AddressEqual( rAddr1, rAddr2 );
} }
}; };
typedef boost::unordered_map< table::CellAddress, std::vector<double>, ScSolverCellHash, ScSolverCellEqual > ScSolverCellHashMap; void SAL_CALL LpsolveSolver::solve() throw(uno::RuntimeException, std::exception)
static uno::Reference<table::XCell> lcl_GetCell( const uno::Reference<sheet::XSpreadsheetDocument>& xDoc,
const table::CellAddress& rPos )
{
uno::Reference<container::XIndexAccess> xSheets( xDoc->getSheets(), uno::UNO_QUERY );
uno::Reference<sheet::XSpreadsheet> xSheet( xSheets->getByIndex( rPos.Sheet ), uno::UNO_QUERY );
return xSheet->getCellByPosition( rPos.Column, rPos.Row );
}
static void lcl_SetValue( const uno::Reference<sheet::XSpreadsheetDocument>& xDoc,
const table::CellAddress& rPos, double fValue )
{
lcl_GetCell( xDoc, rPos )->setValue( fValue );
}
static double lcl_GetValue( const uno::Reference<sheet::XSpreadsheetDocument>& xDoc,
const table::CellAddress& rPos )
{
return lcl_GetCell( xDoc, rPos )->getValue();
}
SolverComponent::SolverComponent( const uno::Reference<uno::XComponentContext>& /* rSMgr */ ) :
OPropertyContainer( GetBroadcastHelper() ),
mbMaximize( sal_True ),
mbNonNegative( sal_False ),
mbInteger( sal_False ),
mnTimeout( 100 ),
mnEpsilonLevel( 0 ),
mbLimitBBDepth( sal_True ),
mbSuccess( sal_False ),
mfResultValue( 0.0 )
{
// for XPropertySet implementation:
registerProperty( STR_NONNEGATIVE, PROP_NONNEGATIVE, 0, &mbNonNegative, getCppuType( &mbNonNegative ) );
registerProperty( STR_INTEGER, PROP_INTEGER, 0, &mbInteger, getCppuType( &mbInteger ) );
registerProperty( STR_TIMEOUT, PROP_TIMEOUT, 0, &mnTimeout, getCppuType( &mnTimeout ) );
registerProperty( STR_EPSILONLEVEL, PROP_EPSILONLEVEL, 0, &mnEpsilonLevel, getCppuType( &mnEpsilonLevel ) );
registerProperty( STR_LIMITBBDEPTH, PROP_LIMITBBDEPTH, 0, &mbLimitBBDepth, getCppuType( &mbLimitBBDepth ) );
}
SolverComponent::~SolverComponent()
{
}
IMPLEMENT_FORWARD_XINTERFACE2( SolverComponent, SolverComponent_Base, OPropertyContainer )
IMPLEMENT_FORWARD_XTYPEPROVIDER2( SolverComponent, SolverComponent_Base, OPropertyContainer )
cppu::IPropertyArrayHelper* SolverComponent::createArrayHelper() const
{
uno::Sequence<beans::Property> aProps;
describeProperties( aProps );
return new cppu::OPropertyArrayHelper( aProps );
}
cppu::IPropertyArrayHelper& SAL_CALL SolverComponent::getInfoHelper()
{
return *getArrayHelper();
}
uno::Reference<beans::XPropertySetInfo> SAL_CALL SolverComponent::getPropertySetInfo() throw(uno::RuntimeException, std::exception)
{
return createPropertySetInfo( getInfoHelper() );
}
// XSolverDescription
OUString SAL_CALL SolverComponent::getComponentDescription() throw (uno::RuntimeException, std::exception)
{
return lcl_GetResourceString( RID_SOLVER_COMPONENT );
}
OUString SAL_CALL SolverComponent::getStatusDescription() throw (uno::RuntimeException, std::exception)
{
return maStatus;
}
OUString SAL_CALL SolverComponent::getPropertyDescription( const OUString& rPropertyName ) throw (uno::RuntimeException, std::exception)
{
sal_uInt32 nResId = 0;
sal_Int32 nHandle = getInfoHelper().getHandleByName( rPropertyName );
switch (nHandle)
{
case PROP_NONNEGATIVE:
nResId = RID_PROPERTY_NONNEGATIVE;
break;
case PROP_INTEGER:
nResId = RID_PROPERTY_INTEGER;
break;
case PROP_TIMEOUT:
nResId = RID_PROPERTY_TIMEOUT;
break;
case PROP_EPSILONLEVEL:
nResId = RID_PROPERTY_EPSILONLEVEL;
break;
case PROP_LIMITBBDEPTH:
nResId = RID_PROPERTY_LIMITBBDEPTH;
break;
default:
{
// unknown - leave empty
}
}
OUString aRet;
if ( nResId )
aRet = lcl_GetResourceString( nResId );
return aRet;
}
// XSolver: settings
uno::Reference<sheet::XSpreadsheetDocument> SAL_CALL SolverComponent::getDocument() throw(uno::RuntimeException, std::exception)
{
return mxDoc;
}
void SAL_CALL SolverComponent::setDocument( const uno::Reference<sheet::XSpreadsheetDocument>& _document )
throw(uno::RuntimeException, std::exception)
{
mxDoc = _document;
}
table::CellAddress SAL_CALL SolverComponent::getObjective() throw(uno::RuntimeException, std::exception)
{
return maObjective;
}
void SAL_CALL SolverComponent::setObjective( const table::CellAddress& _objective ) throw(uno::RuntimeException, std::exception)
{
maObjective = _objective;
}
uno::Sequence<table::CellAddress> SAL_CALL SolverComponent::getVariables() throw(uno::RuntimeException, std::exception)
{
return maVariables;
}
void SAL_CALL SolverComponent::setVariables( const uno::Sequence<table::CellAddress>& _variables )
throw(uno::RuntimeException, std::exception)
{
maVariables = _variables;
}
uno::Sequence<sheet::SolverConstraint> SAL_CALL SolverComponent::getConstraints() throw(uno::RuntimeException, std::exception)
{
return maConstraints;
}
void SAL_CALL SolverComponent::setConstraints( const uno::Sequence<sheet::SolverConstraint>& _constraints )
throw(uno::RuntimeException, std::exception)
{
maConstraints = _constraints;
}
sal_Bool SAL_CALL SolverComponent::getMaximize() throw(uno::RuntimeException, std::exception)
{
return mbMaximize;
}
void SAL_CALL SolverComponent::setMaximize( sal_Bool _maximize ) throw(uno::RuntimeException, std::exception)
{
mbMaximize = _maximize;
}
// XSolver: get results
sal_Bool SAL_CALL SolverComponent::getSuccess() throw(uno::RuntimeException, std::exception)
{
return mbSuccess;
}
double SAL_CALL SolverComponent::getResultValue() throw(uno::RuntimeException, std::exception)
{
return mfResultValue;
}
uno::Sequence<double> SAL_CALL SolverComponent::getSolution() throw(uno::RuntimeException, std::exception)
{
return maSolution;
}
void SAL_CALL SolverComponent::solve() throw(uno::RuntimeException, std::exception)
{ {
uno::Reference<frame::XModel> xModel( mxDoc, uno::UNO_QUERY ); uno::Reference<frame::XModel> xModel( mxDoc, uno::UNO_QUERY );
if ( !xModel.is() ) if ( !xModel.is() )
...@@ -332,7 +90,7 @@ void SAL_CALL SolverComponent::solve() throw(uno::RuntimeException, std::excepti ...@@ -332,7 +90,7 @@ void SAL_CALL SolverComponent::solve() throw(uno::RuntimeException, std::excepti
if ( mnEpsilonLevel < EPS_TIGHT || mnEpsilonLevel > EPS_BAGGY ) if ( mnEpsilonLevel < EPS_TIGHT || mnEpsilonLevel > EPS_BAGGY )
{ {
maStatus = lcl_GetResourceString( RID_ERROR_EPSILONLEVEL ); maStatus = SolverComponent::GetResourceString( RID_ERROR_EPSILONLEVEL );
return; return;
} }
...@@ -366,46 +124,46 @@ void SAL_CALL SolverComponent::solve() throw(uno::RuntimeException, std::excepti ...@@ -366,46 +124,46 @@ void SAL_CALL SolverComponent::solve() throw(uno::RuntimeException, std::excepti
std::vector<table::CellAddress>::const_iterator aVarIter; std::vector<table::CellAddress>::const_iterator aVarIter;
for ( aVarIter = aVariableCells.begin(); aVarIter != aVariableCells.end(); ++aVarIter ) for ( aVarIter = aVariableCells.begin(); aVarIter != aVariableCells.end(); ++aVarIter )
{ {
lcl_SetValue( mxDoc, *aVarIter, 0.0 ); SolverComponent::SetValue( mxDoc, *aVarIter, 0.0 );
} }
// read initial values from all dependent cells // read initial values from all dependent cells
ScSolverCellHashMap::iterator aCellsIter; ScSolverCellHashMap::iterator aCellsIter;
for ( aCellsIter = aCellsHash.begin(); aCellsIter != aCellsHash.end(); ++aCellsIter ) for ( aCellsIter = aCellsHash.begin(); aCellsIter != aCellsHash.end(); ++aCellsIter )
{ {
double fValue = lcl_GetValue( mxDoc, aCellsIter->first ); double fValue = SolverComponent::GetValue( mxDoc, aCellsIter->first );
aCellsIter->second.push_back( fValue ); // store as first element, as-is aCellsIter->second.push_back( fValue ); // store as first element, as-is
} }
// loop through variables // loop through variables
for ( aVarIter = aVariableCells.begin(); aVarIter != aVariableCells.end(); ++aVarIter ) for ( aVarIter = aVariableCells.begin(); aVarIter != aVariableCells.end(); ++aVarIter )
{ {
lcl_SetValue( mxDoc, *aVarIter, 1.0 ); // set to 1 to examine influence SolverComponent::SetValue( mxDoc, *aVarIter, 1.0 ); // set to 1 to examine influence
// read value change from all dependent cells // read value change from all dependent cells
for ( aCellsIter = aCellsHash.begin(); aCellsIter != aCellsHash.end(); ++aCellsIter ) for ( aCellsIter = aCellsHash.begin(); aCellsIter != aCellsHash.end(); ++aCellsIter )
{ {
double fChanged = lcl_GetValue( mxDoc, aCellsIter->first ); double fChanged = SolverComponent::GetValue( mxDoc, aCellsIter->first );
double fInitial = aCellsIter->second.front(); double fInitial = aCellsIter->second.front();
aCellsIter->second.push_back( fChanged - fInitial ); aCellsIter->second.push_back( fChanged - fInitial );
} }
lcl_SetValue( mxDoc, *aVarIter, 2.0 ); // minimal test for linearity SolverComponent::SetValue( mxDoc, *aVarIter, 2.0 ); // minimal test for linearity
for ( aCellsIter = aCellsHash.begin(); aCellsIter != aCellsHash.end(); ++aCellsIter ) for ( aCellsIter = aCellsHash.begin(); aCellsIter != aCellsHash.end(); ++aCellsIter )
{ {
double fInitial = aCellsIter->second.front(); double fInitial = aCellsIter->second.front();
double fCoeff = aCellsIter->second.back(); // last appended: coefficient for this variable double fCoeff = aCellsIter->second.back(); // last appended: coefficient for this variable
double fTwo = lcl_GetValue( mxDoc, aCellsIter->first ); double fTwo = SolverComponent::GetValue( mxDoc, aCellsIter->first );
bool bLinear = rtl::math::approxEqual( fTwo, fInitial + 2.0 * fCoeff ) || bool bLinear = rtl::math::approxEqual( fTwo, fInitial + 2.0 * fCoeff ) ||
rtl::math::approxEqual( fInitial, fTwo - 2.0 * fCoeff ); rtl::math::approxEqual( fInitial, fTwo - 2.0 * fCoeff );
// second comparison is needed in case fTwo is zero // second comparison is needed in case fTwo is zero
if ( !bLinear ) if ( !bLinear )
maStatus = lcl_GetResourceString( RID_ERROR_NONLINEAR ); maStatus = SolverComponent::GetResourceString( RID_ERROR_NONLINEAR );
} }
lcl_SetValue( mxDoc, *aVarIter, 0.0 ); // set back to zero for examining next variable SolverComponent::SetValue( mxDoc, *aVarIter, 0.0 ); // set back to zero for examining next variable
} }
xModel->unlockControllers(); xModel->unlockControllers();
...@@ -557,75 +315,22 @@ void SAL_CALL SolverComponent::solve() throw(uno::RuntimeException, std::excepti ...@@ -557,75 +315,22 @@ void SAL_CALL SolverComponent::solve() throw(uno::RuntimeException, std::excepti
mfResultValue = get_objective( lp ); mfResultValue = get_objective( lp );
} }
else if ( nResult == INFEASIBLE ) else if ( nResult == INFEASIBLE )
maStatus = lcl_GetResourceString( RID_ERROR_INFEASIBLE ); maStatus = SolverComponent::GetResourceString( RID_ERROR_INFEASIBLE );
else if ( nResult == UNBOUNDED ) else if ( nResult == UNBOUNDED )
maStatus = lcl_GetResourceString( RID_ERROR_UNBOUNDED ); maStatus = SolverComponent::GetResourceString( RID_ERROR_UNBOUNDED );
else if ( nResult == TIMEOUT || nResult == SUBOPTIMAL ) else if ( nResult == TIMEOUT || nResult == SUBOPTIMAL )
maStatus = lcl_GetResourceString( RID_ERROR_TIMEOUT ); maStatus = SolverComponent::GetResourceString( RID_ERROR_TIMEOUT );
// SUBOPTIMAL is assumed to be caused by a timeout, and reported as an error // SUBOPTIMAL is assumed to be caused by a timeout, and reported as an error
delete_lp( lp ); delete_lp( lp );
} }
// XServiceInfo extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface * SAL_CALL
com_sun_star_comp_Calc_LpsolveSolver_get_implementation(
uno::Sequence< OUString > SolverComponent_getSupportedServiceNames() css::uno::XComponentContext *,
{ css::uno::Sequence<css::uno::Any> const &)
uno::Sequence< OUString > aServiceNames( 1 );
aServiceNames[ 0 ] = "com.sun.star.sheet.Solver";
return aServiceNames;
}
OUString SolverComponent_getImplementationName()
{
return OUString("com.sun.star.comp.Calc.Solver");
}
OUString SAL_CALL SolverComponent::getImplementationName() throw(uno::RuntimeException, std::exception)
{
return SolverComponent_getImplementationName();
}
sal_Bool SAL_CALL SolverComponent::supportsService( const OUString& rServiceName ) throw(uno::RuntimeException, std::exception)
{ {
return cppu::supportsService(this, rServiceName); return cppu::acquire(new LpsolveSolver());
}
uno::Sequence<OUString> SAL_CALL SolverComponent::getSupportedServiceNames() throw(uno::RuntimeException, std::exception)
{
return SolverComponent_getSupportedServiceNames();
}
uno::Reference<uno::XInterface> SolverComponent_createInstance( const uno::Reference<uno::XComponentContext>& rSMgr )
throw(uno::Exception)
{
return (cppu::OWeakObject*) new SolverComponent( rSMgr );
}
extern "C"
{
SAL_DLLPUBLIC_EXPORT void* SAL_CALL solver_component_getFactory( const sal_Char * pImplName, void * pServiceManager, void * /*pRegistryKey*/ )
{
OUString aImplName( OUString::createFromAscii( pImplName ) );
void* pRet = 0;
if( pServiceManager )
{
uno::Reference< lang::XSingleComponentFactory > xFactory;
if( aImplName.equals( SolverComponent_getImplementationName() ) )
xFactory = cppu::createSingleComponentFactory(
SolverComponent_createInstance,
OUString::createFromAscii( pImplName ),
SolverComponent_getSupportedServiceNames() );
if( xFactory.is() )
{
xFactory->acquire();
pRet = xFactory.get();
}
}
return pRet;
}
} }
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include "SolverComponent.hxx"
#include "solver.hrc"
#include <com/sun/star/container/XIndexAccess.hpp>
#include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
#include <com/sun/star/sheet/XSpreadsheet.hpp>
#include <com/sun/star/table/CellAddress.hpp>
#include <cppuhelper/supportsservice.hxx>
#include <vector>
#include <tools/resmgr.hxx>
using namespace com::sun::star;
#define STR_NONNEGATIVE "NonNegative"
#define STR_INTEGER "Integer"
#define STR_TIMEOUT "Timeout"
#define STR_EPSILONLEVEL "EpsilonLevel"
#define STR_LIMITBBDEPTH "LimitBBDepth"
// Resources from tools are used for translated strings
ResMgr* SolverComponent::pSolverResMgr = NULL;
OUString SolverComponent::GetResourceString( sal_uInt32 nId )
{
if (!pSolverResMgr)
pSolverResMgr = ResMgr::CreateResMgr("solver");
return ResId(nId, *pSolverResMgr).toString();
}
size_t ScSolverCellHash::operator()( const css::table::CellAddress& rAddress ) const
{
return ( rAddress.Sheet << 24 ) | ( rAddress.Column << 16 ) | rAddress.Row;
}
bool ScSolverCellEqual::operator()( const css::table::CellAddress& rAddr1, const css::table::CellAddress& rAddr2 ) const
{
return AddressEqual( rAddr1, rAddr2 );
}
namespace
{
enum
{
PROP_NONNEGATIVE,
PROP_INTEGER,
PROP_TIMEOUT,
PROP_EPSILONLEVEL,
PROP_LIMITBBDEPTH
};
}
uno::Reference<table::XCell> SolverComponent::GetCell( const uno::Reference<sheet::XSpreadsheetDocument>& xDoc,
const table::CellAddress& rPos )
{
uno::Reference<container::XIndexAccess> xSheets( xDoc->getSheets(), uno::UNO_QUERY );
uno::Reference<sheet::XSpreadsheet> xSheet( xSheets->getByIndex( rPos.Sheet ), uno::UNO_QUERY );
return xSheet->getCellByPosition( rPos.Column, rPos.Row );
}
void SolverComponent::SetValue( const uno::Reference<sheet::XSpreadsheetDocument>& xDoc,
const table::CellAddress& rPos, double fValue )
{
SolverComponent::GetCell( xDoc, rPos )->setValue( fValue );
}
double SolverComponent::GetValue( const uno::Reference<sheet::XSpreadsheetDocument>& xDoc,
const table::CellAddress& rPos )
{
return SolverComponent::GetCell( xDoc, rPos )->getValue();
}
SolverComponent::SolverComponent() :
OPropertyContainer( GetBroadcastHelper() ),
mbMaximize( sal_True ),
mbNonNegative( sal_False ),
mbInteger( sal_False ),
mnTimeout( 100 ),
mnEpsilonLevel( 0 ),
mbLimitBBDepth( sal_True ),
mbSuccess( sal_False ),
mfResultValue( 0.0 )
{
// for XPropertySet implementation:
registerProperty( STR_NONNEGATIVE, PROP_NONNEGATIVE, 0, &mbNonNegative, getCppuType( &mbNonNegative ) );
registerProperty( STR_INTEGER, PROP_INTEGER, 0, &mbInteger, getCppuType( &mbInteger ) );
registerProperty( STR_TIMEOUT, PROP_TIMEOUT, 0, &mnTimeout, getCppuType( &mnTimeout ) );
registerProperty( STR_EPSILONLEVEL, PROP_EPSILONLEVEL, 0, &mnEpsilonLevel, getCppuType( &mnEpsilonLevel ) );
registerProperty( STR_LIMITBBDEPTH, PROP_LIMITBBDEPTH, 0, &mbLimitBBDepth, getCppuType( &mbLimitBBDepth ) );
}
SolverComponent::~SolverComponent()
{
}
IMPLEMENT_FORWARD_XINTERFACE2( SolverComponent, SolverComponent_Base, OPropertyContainer )
IMPLEMENT_FORWARD_XTYPEPROVIDER2( SolverComponent, SolverComponent_Base, OPropertyContainer )
cppu::IPropertyArrayHelper* SolverComponent::createArrayHelper() const
{
uno::Sequence<beans::Property> aProps;
describeProperties( aProps );
return new cppu::OPropertyArrayHelper( aProps );
}
cppu::IPropertyArrayHelper& SAL_CALL SolverComponent::getInfoHelper()
{
return *getArrayHelper();
}
uno::Reference<beans::XPropertySetInfo> SAL_CALL SolverComponent::getPropertySetInfo() throw(uno::RuntimeException, std::exception)
{
return createPropertySetInfo( getInfoHelper() );
}
// XSolverDescription
OUString SAL_CALL SolverComponent::getComponentDescription() throw (uno::RuntimeException, std::exception)
{
return SolverComponent::GetResourceString( RID_SOLVER_COMPONENT );
}
OUString SAL_CALL SolverComponent::getStatusDescription() throw (uno::RuntimeException, std::exception)
{
return maStatus;
}
OUString SAL_CALL SolverComponent::getPropertyDescription( const OUString& rPropertyName ) throw (uno::RuntimeException, std::exception)
{
sal_uInt32 nResId = 0;
sal_Int32 nHandle = getInfoHelper().getHandleByName( rPropertyName );
switch (nHandle)
{
case PROP_NONNEGATIVE:
nResId = RID_PROPERTY_NONNEGATIVE;
break;
case PROP_INTEGER:
nResId = RID_PROPERTY_INTEGER;
break;
case PROP_TIMEOUT:
nResId = RID_PROPERTY_TIMEOUT;
break;
case PROP_EPSILONLEVEL:
nResId = RID_PROPERTY_EPSILONLEVEL;
break;
case PROP_LIMITBBDEPTH:
nResId = RID_PROPERTY_LIMITBBDEPTH;
break;
default:
{
// unknown - leave empty
}
}
OUString aRet;
if ( nResId )
aRet = SolverComponent::GetResourceString( nResId );
return aRet;
}
// XSolver: settings
uno::Reference<sheet::XSpreadsheetDocument> SAL_CALL SolverComponent::getDocument() throw(uno::RuntimeException, std::exception)
{
return mxDoc;
}
void SAL_CALL SolverComponent::setDocument( const uno::Reference<sheet::XSpreadsheetDocument>& _document )
throw(uno::RuntimeException, std::exception)
{
mxDoc = _document;
}
table::CellAddress SAL_CALL SolverComponent::getObjective() throw(uno::RuntimeException, std::exception)
{
return maObjective;
}
void SAL_CALL SolverComponent::setObjective( const table::CellAddress& _objective ) throw(uno::RuntimeException, std::exception)
{
maObjective = _objective;
}
uno::Sequence<table::CellAddress> SAL_CALL SolverComponent::getVariables() throw(uno::RuntimeException, std::exception)
{
return maVariables;
}
void SAL_CALL SolverComponent::setVariables( const uno::Sequence<table::CellAddress>& _variables )
throw(uno::RuntimeException, std::exception)
{
maVariables = _variables;
}
uno::Sequence<sheet::SolverConstraint> SAL_CALL SolverComponent::getConstraints() throw(uno::RuntimeException, std::exception)
{
return maConstraints;
}
void SAL_CALL SolverComponent::setConstraints( const uno::Sequence<sheet::SolverConstraint>& _constraints )
throw(uno::RuntimeException, std::exception)
{
maConstraints = _constraints;
}
sal_Bool SAL_CALL SolverComponent::getMaximize() throw(uno::RuntimeException, std::exception)
{
return mbMaximize;
}
void SAL_CALL SolverComponent::setMaximize( sal_Bool _maximize ) throw(uno::RuntimeException, std::exception)
{
mbMaximize = _maximize;
}
// XSolver: get results
sal_Bool SAL_CALL SolverComponent::getSuccess() throw(uno::RuntimeException, std::exception)
{
return mbSuccess;
}
double SAL_CALL SolverComponent::getResultValue() throw(uno::RuntimeException, std::exception)
{
return mfResultValue;
}
uno::Sequence<double> SAL_CALL SolverComponent::getSolution() throw(uno::RuntimeException, std::exception)
{
return maSolution;
}
// XServiceInfo
sal_Bool SAL_CALL SolverComponent::supportsService( const OUString& rServiceName ) throw(uno::RuntimeException, std::exception)
{
return cppu::supportsService(this, rServiceName);
}
uno::Sequence<OUString> SAL_CALL SolverComponent::getSupportedServiceNames() throw(uno::RuntimeException, std::exception)
{
uno::Sequence< OUString > aServiceNames( 1 );
aServiceNames[ 0 ] = "com.sun.star.sheet.Solver";
return aServiceNames;
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
...@@ -22,13 +22,38 @@ ...@@ -22,13 +22,38 @@
#include <com/sun/star/sheet/XSolver.hpp> #include <com/sun/star/sheet/XSolver.hpp>
#include <com/sun/star/sheet/XSolverDescription.hpp> #include <com/sun/star/sheet/XSolverDescription.hpp>
#include <com/sun/star/table/CellAddress.hpp>
#include <com/sun/star/table/XCell.hpp>
#include <com/sun/star/lang/XServiceInfo.hpp> #include <com/sun/star/lang/XServiceInfo.hpp>
#include <com/sun/star/uno/XComponentContext.hpp>
#include <cppuhelper/implbase3.hxx> #include <cppuhelper/implbase3.hxx>
#include <comphelper/broadcasthelper.hxx> #include <comphelper/broadcasthelper.hxx>
#include <comphelper/propertycontainer.hxx> #include <comphelper/propertycontainer.hxx>
#include <comphelper/proparrhlp.hxx> #include <comphelper/proparrhlp.hxx>
#include <boost/unordered_map.hpp>
class ResMgr;
// hash map for the coefficients of a dependent cell (objective or constraint)
// The size of each vector is the number of columns (variable cells) plus one, first entry is initial value.
struct ScSolverCellHash
{
size_t operator()( const css::table::CellAddress& rAddress ) const;
};
inline bool AddressEqual( const css::table::CellAddress& rAddr1, const css::table::CellAddress& rAddr2 )
{
return rAddr1.Sheet == rAddr2.Sheet && rAddr1.Column == rAddr2.Column && rAddr1.Row == rAddr2.Row;
}
struct ScSolverCellEqual
{
bool operator()( const css::table::CellAddress& rAddr1, const css::table::CellAddress& rAddr2 ) const;
};
typedef boost::unordered_map< css::table::CellAddress, std::vector<double>, ScSolverCellHash, ScSolverCellEqual > ScSolverCellHashMap;
typedef cppu::WeakImplHelper3< typedef cppu::WeakImplHelper3<
com::sun::star::sheet::XSolver, com::sun::star::sheet::XSolver,
com::sun::star::sheet::XSolverDescription, com::sun::star::sheet::XSolverDescription,
...@@ -40,6 +65,9 @@ class SolverComponent : public comphelper::OMutexAndBroadcastHelper, ...@@ -40,6 +65,9 @@ class SolverComponent : public comphelper::OMutexAndBroadcastHelper,
public comphelper::OPropertyArrayUsageHelper< SolverComponent >, public comphelper::OPropertyArrayUsageHelper< SolverComponent >,
public SolverComponent_Base public SolverComponent_Base
{ {
protected:
static ResMgr* pSolverResMgr;
// settings // settings
com::sun::star::uno::Reference< com::sun::star::sheet::XSpreadsheetDocument > mxDoc; com::sun::star::uno::Reference< com::sun::star::sheet::XSpreadsheetDocument > mxDoc;
com::sun::star::table::CellAddress maObjective; com::sun::star::table::CellAddress maObjective;
...@@ -58,9 +86,19 @@ class SolverComponent : public comphelper::OMutexAndBroadcastHelper, ...@@ -58,9 +86,19 @@ class SolverComponent : public comphelper::OMutexAndBroadcastHelper,
com::sun::star::uno::Sequence< double > maSolution; com::sun::star::uno::Sequence< double > maSolution;
OUString maStatus; OUString maStatus;
static OUString GetResourceString( sal_uInt32 nId );
static css::uno::Reference<css::table::XCell> GetCell(
const css::uno::Reference<css::sheet::XSpreadsheetDocument>& xDoc,
const css::table::CellAddress& rPos );
static void SetValue(
const css::uno::Reference<css::sheet::XSpreadsheetDocument>& xDoc,
const css::table::CellAddress& rPos, double fValue );
static double GetValue(
const css::uno::Reference<css::sheet::XSpreadsheetDocument>& xDoc,
const css::table::CellAddress& rPos );
public: public:
SolverComponent( const com::sun::star::uno::Reference< SolverComponent();
com::sun::star::uno::XComponentContext >& rxMSF );
virtual ~SolverComponent(); virtual ~SolverComponent();
DECLARE_XINTERFACE() DECLARE_XINTERFACE()
...@@ -98,7 +136,7 @@ public: ...@@ -98,7 +136,7 @@ public:
virtual ::com::sun::star::uno::Sequence< double > SAL_CALL getSolution() virtual ::com::sun::star::uno::Sequence< double > SAL_CALL getSolution()
throw(::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE; throw(::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
virtual void SAL_CALL solve() throw(::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE; virtual void SAL_CALL solve() throw(::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE = 0;
// XSolverDescription // XSolverDescription
virtual OUString SAL_CALL getComponentDescription() throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE; virtual OUString SAL_CALL getComponentDescription() throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
...@@ -108,7 +146,7 @@ public: ...@@ -108,7 +146,7 @@ public:
// XServiceInfo // XServiceInfo
virtual OUString SAL_CALL getImplementationName() virtual OUString SAL_CALL getImplementationName()
throw(::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE; throw(::com::sun::star::uno::RuntimeException, std::exception) = 0;
virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName )
throw(::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE; throw(::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
virtual ::com::sun::star::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() virtual ::com::sun::star::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames()
......
...@@ -7,9 +7,10 @@ ...@@ -7,9 +7,10 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
--> -->
<component loader="com.sun.star.loader.SharedLibrary" prefix="coinmp" <component loader="com.sun.star.loader.SharedLibrary" environment="@CPPU_ENV@"
xmlns="http://openoffice.org/2010/uno-components"> xmlns="http://openoffice.org/2010/uno-components">
<implementation name="com.sun.star.comp.Calc.CoinMPSolver"> <implementation name="com.sun.star.comp.Calc.CoinMPSolver"
constructor="com_sun_star_comp_Calc_CoinMPSolver_get_implementation">
<service name="com.sun.star.sheet.Solver"/> <service name="com.sun.star.sheet.Solver"/>
</implementation> </implementation>
</component> </component>
...@@ -18,8 +18,9 @@ ...@@ -18,8 +18,9 @@
--> -->
<component loader="com.sun.star.loader.SharedLibrary" environment="@CPPU_ENV@" <component loader="com.sun.star.loader.SharedLibrary" environment="@CPPU_ENV@"
prefix="solver" xmlns="http://openoffice.org/2010/uno-components"> xmlns="http://openoffice.org/2010/uno-components">
<implementation name="com.sun.star.comp.Calc.Solver"> <implementation name="com.sun.star.comp.Calc.LpsolveSolver"
constructor="com_sun_star_comp_Calc_LpsolveSolver_get_implementation">
<service name="com.sun.star.sheet.Solver"/> <service name="com.sun.star.sheet.Solver"/>
</implementation> </implementation>
</component> </component>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment