R/QueryCondition.r

# Netherlands Biodiversity API
#
# Access to the digitised Natural History collection at the Naturalis Biodiversity Center
#
# OpenAPI spec version: v2
# Contact: support@naturalis.nl
# Generated by: https://github.com/swagger-api/swagger-codegen.git

#'
#' @docType class
#'
#' @format R6 class
#'
#' @usage
#' # QueryCondition$new()
#'
#' @format
#' R6 class
#'
#' @title QueryCondition Class
#'
#' @description
#' For more information on the NBA object model, please refer to the
#' official NBA documentation at
#' \href{https://docs.biodiversitydata.nl}{https://docs.biodiversitydata.nl} and
#' the NBA model and endpoints reference at
#' \href{https://docs.biodiversitydata.nl/endpoints-reference}{https://docs.biodiversitydata.nl/endpoints-reference}.
#'
#' @details Model class for QueryCondition objects. Class modeling a query condition. A condition consists of a field name, a ComparisonOperator and a value. For example: 'name', EQUALS, 'John'. A condition can optionally have a list of sibling conditions. These are joined to the containing condition using the AND or OR operator. A condition and its siblings are strongly bound together, as though surrounded by parentheses: (condition AND sibling0 AND sibling1). Because each sibling may itself also have a list of sibling conditions, this allows you to nest logical expressions like (A AND (B OR C OR (D AND E)) AND F).
#'
#'
#' @field not  character
#'
#' @field field  character
#'
#' @field operator  character
#'
#' @field value  list
#'
#' @field and  list(QueryCondition)
#'
#' @field or  list(QueryCondition)
#'
#' @field constantScore  logical
#'
#' @field boost  numeric
#'
#'
#'
#' @section Methods:
#' \describe{
#'
#' \item{\code{$new()}}{
#'
#'   Constructor QueryCondition object.
#'
#' }
#' \item{\code{$fromList(QueryConditionList)}}{
#'
#'   Create QueryCondition object from list.
#'
#' }
#'
#' \item{\code{$toList()}}{
#'
#'   Get list representation of QueryCondition.
#'
#' }
#' \item{\code{fromJSONString(QueryConditionJson)}}{
#'
#'   Create QueryCondition object from JSON.
#'
#' }
#' \item{\code{toJSONString(pretty=TRUE)}}{
#'
#'   Get JSON representation of QueryCondition.
#'
#' }
#' }
#' @importFrom R6 R6Class
#' @importFrom jsonlite fromJSON toJSON
#' @export
QueryCondition <- R6::R6Class(
  "QueryCondition",
  public = list(
    `not` = NULL,
    `field` = NULL,
    `operator` = NULL,
    `value` = NULL,
    `and` = NULL,
    `or` = NULL,
    `constantScore` = NULL,
    `boost` = NULL,
    initialize = function(
                              `not`,
                              `field`,
                              `operator`,
                              `value`,
                              `and`,
                              `or`,
                              `constantScore`,
                              `boost`) {
      if (!missing(`not`)) {
        stopifnot(
          is.character(`not`),
          length(`not`) == 1
        )
        self[["not"]] <- `not`
      }
      if (!missing(`field`)) {
        self[["field"]] <- `field`
      }
      if (!missing(`operator`)) {
        stopifnot(
          is.character(`operator`),
          length(`operator`) == 1
        )
        self[["operator"]] <- `operator`
      }
      if (!missing(`value`)) {
        self[["value"]] <- `value`
      }
      if (!missing(`and`)) {
        stopifnot(
          is.list(`and`),
          length(`and`) != 0
        )
        lapply(
          `and`,
          function(x) stopifnot(R6::is.R6(x))
        )
        ## omit names as they should not be part of JSON representation
        self[["and"]] <- unname(`and`)
      }
      if (!missing(`or`)) {
        stopifnot(
          is.list(`or`),
          length(`or`) != 0
        )
        lapply(
          `or`,
          function(x) stopifnot(R6::is.R6(x))
        )
        ## omit names as they should not be part of JSON representation
        self[["or"]] <- unname(`or`)
      }
      if (!missing(`constantScore`)) {
        self[["constantScore"]] <- `constantScore`
      }
      if (!missing(`boost`)) {
        stopifnot(
          is.numeric(`boost`),
          length(`boost`) == 1
        )
        self[["boost"]] <- `boost`
      }
    },
    toList = function() {
      QueryConditionList <- list()
      if (!is.null(self[["not"]])) {
        QueryConditionList[["not"]] <-
          self[["not"]]
      }
      if (!is.null(self[["field"]])) {
        QueryConditionList[["field"]] <-
          self[["field"]]
      }
      if (!is.null(self[["operator"]])) {
        QueryConditionList[["operator"]] <-
          self[["operator"]]
      }
      if (!is.null(self[["value"]])) {
        QueryConditionList[["value"]] <-
          self[["value"]]
      }
      if (!is.null(self[["and"]])) {
        QueryConditionList[["and"]] <-
          lapply(self[["and"]], function(x) x$toList())
      }
      if (!is.null(self[["or"]])) {
        QueryConditionList[["or"]] <-
          lapply(self[["or"]], function(x) x$toList())
      }
      if (!is.null(self[["constantScore"]])) {
        QueryConditionList[["constantScore"]] <-
          self[["constantScore"]]
      }
      if (!is.null(self[["boost"]])) {
        QueryConditionList[["boost"]] <-
          self[["boost"]]
      }
      ## omit empty nested lists in returned list
      QueryConditionList[vapply(
        QueryConditionList,
        length,
        FUN.VALUE = integer(1)
      ) > 0]
    },
    fromList = function(QueryConditionList,
                            typeMapping = NULL) {
      self[["not"]] <-
        QueryConditionList[["not"]]
      self[["field"]] <-
        QueryConditionList[["field"]]
      self[["operator"]] <-
        QueryConditionList[["operator"]]
      self[["value"]] <-
        QueryConditionList[["value"]]
      self[["and"]] <- lapply(
        QueryConditionList[["and"]],
        function(x) {
          QueryCondition$new()$fromList(x,
            typeMapping = typeMapping
          )
        }
      )
      self[["or"]] <- lapply(
        QueryConditionList[["or"]],
        function(x) {
          QueryCondition$new()$fromList(x,
            typeMapping = typeMapping
          )
        }
      )
      self[["constantScore"]] <-
        QueryConditionList[["constantScore"]]
      self[["boost"]] <-
        QueryConditionList[["boost"]]
      invisible(self)
    },
    toJSONString = function(pretty = TRUE) {
      jsonlite::toJSON(
        self$toList(),
        simplifyVector = TRUE,
        auto_unbox = TRUE,
        pretty = pretty
      )
    },
    fromJSONString = function(QueryConditionJson,
                                  typeMapping = NULL) {
      QueryConditionList <- jsonlite::fromJSON(
        QueryConditionJson,
        simplifyVector = FALSE
      )
      self <- self$fromList(QueryConditionList)
      invisible(self)
    },
    print = function(...) {
      ## print class name
      cat("<QueryCondition>\n")
      ## print all members with values
      cat("Fields:\n")
      if (typeof(self$not) == "environment") {
        cat("\tnot:\tobject of class", paste0("<", class(self$not)[1], ">"), "\n")
      }
      else if (typeof(self$not) == "list") {
        cat("\tnot:\tlist of length", length(self$not), "\n")
      }
      else {
        cat("\tnot:\t", self$not, "\n")
      }
      if (typeof(self$field) == "environment") {
        cat("\tfield:\tobject of class", paste0("<", class(self$field)[1], ">"), "\n")
      }
      else if (typeof(self$field) == "list") {
        cat("\tfield:\tlist of length", length(self$field), "\n")
      }
      else {
        cat("\tfield:\t", self$field, "\n")
      }
      if (typeof(self$operator) == "environment") {
        cat("\toperator:\tobject of class", paste0("<", class(self$operator)[1], ">"), "\n")
      }
      else if (typeof(self$operator) == "list") {
        cat("\toperator:\tlist of length", length(self$operator), "\n")
      }
      else {
        cat("\toperator:\t", self$operator, "\n")
      }
      if (typeof(self$value) == "environment") {
        cat("\tvalue:\tobject of class", paste0("<", class(self$value)[1], ">"), "\n")
      }
      else if (typeof(self$value) == "list") {
        cat("\tvalue:\tlist of length", length(self$value), "\n")
      }
      else {
        cat("\tvalue:\t", self$value, "\n")
      }
      if (typeof(self$and) == "environment") {
        cat("\tand:\tobject of class", paste0("<", class(self$and)[1], ">"), "\n")
      }
      else if (typeof(self$and) == "list") {
        cat("\tand:\tlist of length", length(self$and), "\n")
      }
      else {
        cat("\tand:\t", self$and, "\n")
      }
      if (typeof(self$or) == "environment") {
        cat("\tor:\tobject of class", paste0("<", class(self$or)[1], ">"), "\n")
      }
      else if (typeof(self$or) == "list") {
        cat("\tor:\tlist of length", length(self$or), "\n")
      }
      else {
        cat("\tor:\t", self$or, "\n")
      }
      if (typeof(self$constantScore) == "environment") {
        cat("\tconstantScore:\tobject of class", paste0("<", class(self$constantScore)[1], ">"), "\n")
      }
      else if (typeof(self$constantScore) == "list") {
        cat("\tconstantScore:\tlist of length", length(self$constantScore), "\n")
      }
      else {
        cat("\tconstantScore:\t", self$constantScore, "\n")
      }
      if (typeof(self$boost) == "environment") {
        cat("\tboost:\tobject of class", paste0("<", class(self$boost)[1], ">"), "\n")
      }
      else if (typeof(self$boost) == "list") {
        cat("\tboost:\tlist of length", length(self$boost), "\n")
      }
      else {
        cat("\tboost:\t", self$boost, "\n")
      }
      ## print all methods
      cat("Methods:\n")
      cat("\tfromJSONString\n")
      cat("\ttoJSONString\n")
      cat("\tfromList\n")
      cat("\ttoList\n")
      cat("\tprint\n")
      invisible(self)
    }
  )
)
naturalis/nbaR documentation built on Nov. 12, 2023, 4:47 p.m.