1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
22 #include <com/sun/star/sheet/XSolver.hpp>
23 #include <com/sun/star/sheet/XSolverDescription.hpp>
24 #include <com/sun/star/sheet/SensitivityReport.hpp>
25 #include <com/sun/star/table/CellAddress.hpp>
26 #include <com/sun/star/lang/XServiceInfo.hpp>
27 #include <cppuhelper/implbase.hxx>
28 #include <comphelper/compbase.hxx>
29 #include <comphelper/propertycontainer2.hxx>
30 #include <comphelper/proparrhlp.hxx>
31 #include <unotools/resmgr.hxx>
33 #include <unordered_map>
35 namespace com::sun::star::table
{ class XCell
; }
37 // hash map for the coefficients of a dependent cell (objective or constraint)
38 // The size of each vector is the number of columns (variable cells) plus one, first entry is initial value.
40 struct ScSolverCellHash
42 size_t operator()( const css::table::CellAddress
& rAddress
) const;
45 inline bool AddressEqual( const css::table::CellAddress
& rAddr1
, const css::table::CellAddress
& rAddr2
)
47 return rAddr1
.Sheet
== rAddr2
.Sheet
&& rAddr1
.Column
== rAddr2
.Column
&& rAddr1
.Row
== rAddr2
.Row
;
50 struct ScSolverCellEqual
52 bool operator()( const css::table::CellAddress
& rAddr1
, const css::table::CellAddress
& rAddr2
) const;
55 typedef std::unordered_map
< css::table::CellAddress
, std::vector
<double>, ScSolverCellHash
, ScSolverCellEqual
> ScSolverCellHashMap
;
57 typedef comphelper::WeakImplHelper
<
59 css::sheet::XSolverDescription
,
60 css::lang::XServiceInfo
>
63 class SolverComponent
: public comphelper::OPropertyContainer2
,
64 public comphelper::OPropertyArrayUsageHelper
< SolverComponent
>,
65 public SolverComponent_Base
69 css::uno::Reference
< css::sheet::XSpreadsheetDocument
> mxDoc
;
70 css::table::CellAddress maObjective
;
71 css::uno::Sequence
< css::table::CellAddress
> maVariables
;
72 css::uno::Sequence
< css::sheet::SolverConstraint
> maConstraints
;
74 // set via XPropertySet
78 sal_Int32 mnEpsilonLevel
;
80 bool mbGenSensitivity
;
84 css::uno::Sequence
< double > maSolution
;
88 css::uno::Sequence
<double> m_aObjCoefficients
;
89 css::uno::Sequence
<double> m_aObjDecrease
;
90 css::uno::Sequence
<double> m_aObjIncrease
;
91 css::uno::Sequence
<double> m_aObjRedCost
;
92 css::uno::Sequence
<double> m_aConstrValue
;
93 css::uno::Sequence
<double> m_aConstrRHS
;
94 css::uno::Sequence
<double> m_aConstrDual
;
95 css::uno::Sequence
<double> m_aConstrIncrease
;
96 css::uno::Sequence
<double> m_aConstrDecrease
;
97 css::sheet::SensitivityReport m_aSensitivityReport
;
99 static OUString
GetResourceString(TranslateId aId
);
100 static css::uno::Reference
<css::table::XCell
> GetCell(
101 const css::uno::Reference
<css::sheet::XSpreadsheetDocument
>& xDoc
,
102 const css::table::CellAddress
& rPos
);
103 static void SetValue(
104 const css::uno::Reference
<css::sheet::XSpreadsheetDocument
>& xDoc
,
105 const css::table::CellAddress
& rPos
, double fValue
);
106 static double GetValue(
107 const css::uno::Reference
<css::sheet::XSpreadsheetDocument
>& xDoc
,
108 const css::table::CellAddress
& rPos
);
112 virtual ~SolverComponent() override
;
115 DECLARE_XTYPEPROVIDER()
117 virtual css::uno::Reference
< css::beans::XPropertySetInfo
> SAL_CALL
getPropertySetInfo() override
;
118 virtual ::cppu::IPropertyArrayHelper
& getInfoHelper() override
; // from OPropertySetHelper
119 virtual ::cppu::IPropertyArrayHelper
* createArrayHelper() const override
; // from OPropertyArrayUsageHelper
122 virtual css::uno::Reference
< css::sheet::XSpreadsheetDocument
> SAL_CALL
getDocument() override
;
123 virtual void SAL_CALL
setDocument( const css::uno::Reference
<
124 css::sheet::XSpreadsheetDocument
>& _document
) override
;
125 virtual css::table::CellAddress SAL_CALL
getObjective() override
;
126 virtual void SAL_CALL
setObjective( const css::table::CellAddress
& _objective
) override
;
127 virtual css::uno::Sequence
< css::table::CellAddress
> SAL_CALL
getVariables() override
;
128 virtual void SAL_CALL
setVariables( const css::uno::Sequence
<
129 css::table::CellAddress
>& _variables
) override
;
130 virtual css::uno::Sequence
< css::sheet::SolverConstraint
> SAL_CALL
getConstraints() override
;
131 virtual void SAL_CALL
setConstraints( const css::uno::Sequence
<
132 css::sheet::SolverConstraint
>& _constraints
) override
;
133 virtual sal_Bool SAL_CALL
getMaximize() override
;
134 virtual void SAL_CALL
setMaximize( sal_Bool _maximize
) override
;
136 virtual sal_Bool SAL_CALL
getSuccess() override
;
137 virtual double SAL_CALL
getResultValue() override
;
138 virtual css::uno::Sequence
< double > SAL_CALL
getSolution() override
;
140 virtual void SAL_CALL
solve() override
= 0;
142 // XSolverDescription
143 virtual OUString SAL_CALL
getComponentDescription() override
= 0;
144 virtual OUString SAL_CALL
getStatusDescription() override
;
145 virtual OUString SAL_CALL
getPropertyDescription( const OUString
& aPropertyName
) override
;
148 virtual OUString SAL_CALL
getImplementationName() override
= 0;
149 virtual sal_Bool SAL_CALL
supportsService( const OUString
& ServiceName
) override
;
150 virtual css::uno::Sequence
< OUString
> SAL_CALL
getSupportedServiceNames() override
;
153 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */