Skip to content

Commit

Permalink
Fixes #541 extend set parameter value by path (#543)
Browse files Browse the repository at this point in the history
  • Loading branch information
msevestre authored Jul 1, 2021
1 parent d8d32bb commit 1b9bc04
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 11 deletions.
10 changes: 8 additions & 2 deletions R/utilities-parameter.R
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ setParameterValues <- function(parameters, values) {
#' of numeric values, if the value of more than one parameter should be changed. Must have the same
#' length as 'parameterPaths'
#' @param simulation Simulation uses to retrieve parameter instances from given paths.
#' @param stopIfNotFound Boolean. If \code{TRUE} (default) and no parameter exists for the given path,
#' an error is thrown. If \code{FALSE}, a warning is shown to the user
#'
#' @examples
#'
#' simPath <- system.file("extdata", "simple.pkml", package = "ospsuite")
Expand All @@ -119,9 +122,12 @@ setParameterValues <- function(parameters, values) {
#'
#' setParameterValuesByPath(c("Organism|Liver|Volume", "Organism|Volume"), c(2, 3), sim)
#' @export
setParameterValuesByPath <- function(parameterPaths, values, simulation) {
setParameterValuesByPath <- function(parameterPaths, values, simulation, stopIfNotFound = TRUE) {
setQuantityValuesByPath(
quantityPaths = parameterPaths, values = values, simulation = simulation
quantityPaths = parameterPaths,
values = values,
simulation = simulation,
stopIfNotFound = stopIfNotFound
)
}

Expand Down
12 changes: 9 additions & 3 deletions R/utilities-quantity.R
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ setQuantityValues <- function(quantities, values) {
#' of numeric values, if the value of more than one quantity should be changed. Must have the same
#' length as 'quantityPaths'
#' @param simulation Simulation uses to retrieve quantity instances from given paths.
#' @param stopIfNotFound Boolean. If \code{TRUE} (default) and no qyantuty exists for the given path,
#' an error is thrown. If \code{FALSE}, a warning is shown to the user

#' @examples
#'
#' simPath <- system.file("extdata", "simple.pkml", package = "ospsuite")
Expand All @@ -111,7 +114,7 @@ setQuantityValues <- function(quantities, values) {
#'
#' setParameterValuesByPath(list("Organism|Liver|Volume", "Organism|Liver|A"), c(2, 3), sim)
#' @export
setQuantityValuesByPath <- function(quantityPaths, values, simulation) {
setQuantityValuesByPath <- function(quantityPaths, values, simulation, stopIfNotFound = TRUE) {
validateIsString(quantityPaths)
validateIsNumeric(values)
validateIsSameLength(quantityPaths, values)
Expand All @@ -120,8 +123,11 @@ setQuantityValuesByPath <- function(quantityPaths, values, simulation) {
task <- getContainerTask()
for (i in seq_along(quantityPaths)) {
rClr::clrCall(
task, "SetValueByPath", simulation$ref,
enc2utf8(quantityPaths[[i]]), values[[i]]
task, "SetValueByPath",
simulation$ref,
enc2utf8(quantityPaths[[i]]),
values[[i]],
stopIfNotFound
)
}
}
Expand Down
Binary file modified inst/lib/OSPSuite.Assets.dll
Binary file not shown.
Binary file modified inst/lib/OSPSuite.Core.dll
Binary file not shown.
Binary file modified inst/lib/OSPSuite.Infrastructure.Autofac.dll
Binary file not shown.
Binary file modified inst/lib/OSPSuite.Infrastructure.Import.dll
Binary file not shown.
Binary file modified inst/lib/OSPSuite.Infrastructure.dll
Binary file not shown.
Binary file modified inst/lib/OSPSuite.R.dll
Binary file not shown.
12 changes: 6 additions & 6 deletions packages.config
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="OSPSuite.Assets" version="10.0.227"/>
<package id="OSPSuite.Core" version="10.0.227"/>
<package id="OSPSuite.Infrastructure" version="10.0.227"/>
<package id="OSPSuite.Infrastructure.Import" version="10.0.227"/>
<package id="OSPSuite.Infrastructure.Autofac" version="10.0.227"/>
<package id="OSPSuite.R" version="10.0.227"/>
<package id="OSPSuite.Assets" version="10.0.263"/>
<package id="OSPSuite.Core" version="10.0.263"/>
<package id="OSPSuite.Infrastructure" version="10.0.263"/>
<package id="OSPSuite.Infrastructure.Import" version="10.0.263"/>
<package id="OSPSuite.Infrastructure.Autofac" version="10.0.263"/>
<package id="OSPSuite.R" version="10.0.263"/>
<package id="OSPSuite.Serializer" version="3.0.0.1"/>
<package id="OSPSuite.Utility" version="4.0.0.4"/>
<package id="LumenWorksCsvReader" version="4.0.0"/>
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/test-utilities-quantity.R
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,11 @@ test_that("It throws an exception when setting values for a quantity that does n
parameterPath <- "Organism|Liver|NOPE|Volume"
expect_that(setQuantityValuesByPath(parameterPath, 100, sim), throws_error())
})

test_that("It does not throw an exception when setting values for a quantity that does not exist and the stopIfnotFound flag is set to false", {
sim <- loadTestSimulation("S1", loadFromCache = TRUE)
parameterPath <- "Organism|Liver|NOPE|Volume"
setQuantityValuesByPath(parameterPath, 100, sim, FALSE)
expect_false(is.null(sim))
})

0 comments on commit 1b9bc04

Please sign in to comment.