/* -*- 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/.
*/
// Creates a model using the XSolverSettings API checks if it is accessible via the API void ScSolverSettingsObj::testXSolverSettings()
{
uno::Reference<sheet::XSpreadsheetDocument> xDoc(mxComponent, uno::UNO_QUERY_THROW);
uno::Reference<container::XIndexAccess> xIndex(xDoc->getSheets(), uno::UNO_QUERY_THROW);
uno::Reference<sheet::XSpreadsheet> xSheet(xIndex->getByIndex(0), uno::UNO_QUERY_THROW);
uno::Reference<beans::XPropertySet> xPropSet(xSheet, uno::UNO_QUERY_THROW);
uno::Reference<sheet::XSolverSettings> xSolverModel(
xPropSet->getPropertyValue("SolverSettings"), uno::UNO_QUERY_THROW);
// Objective cell is in G7
table::CellAddress aObjCell(0, 6, 6);
xSolverModel->setObjectiveCell(uno::Any(aObjCell));
xSolverModel->setObjectiveType(sheet::SolverObjectiveType::MAXIMIZE);
// Run the model and check the results
xSolverModel->saveToFile();
xSolverModel->setSuppressDialog(true);
xSolverModel->solve();
// The correct solution value is 6981 (using LpsolveSolver)
CPPUNIT_ASSERT_EQUAL(static_cast<double>(6981), xSheet->getCellByPosition(6, 6)->getValue());
// Check objective function and variable cells
testCellAddress(aObjCell, xSolverModel->getObjectiveCell());
CPPUNIT_ASSERT_EQUAL(sheet::SolverObjectiveType::MAXIMIZE, xSolverModel->getObjectiveType());
uno::Sequence<uno::Any> aSeq = xSolverModel->getVariableCells();
CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), aSeq.getLength());
testCellRangeAddress(aVarCells[0], aSeq[0]);
// Save file and reload to check if solver settings are still there
saveAndReload(u"calc8"_ustr);
uno::Reference<sheet::XSpreadsheetDocument> xDoc2(mxComponent, uno::UNO_QUERY_THROW);
uno::Reference<container::XIndexAccess> xIndex2(xDoc2->getSheets(), uno::UNO_QUERY_THROW);
uno::Reference<sheet::XSpreadsheet> xSheet2(xIndex2->getByIndex(0), uno::UNO_QUERY_THROW);
uno::Reference<beans::XPropertySet> xPropSet2(xSheet2, uno::UNO_QUERY_THROW);
uno::Reference<sheet::XSolverSettings> xSolverModel2(
xPropSet2->getPropertyValue("SolverSettings"), uno::UNO_QUERY_THROW);
// // Check objective function
testCellAddress(aObjCell, xSolverModel2->getObjectiveCell());
CPPUNIT_ASSERT_EQUAL(sheet::SolverObjectiveType::MAXIMIZE, xSolverModel2->getObjectiveType());
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.