Skip to content

Commit f0e5855

Browse files
committed
updating documentation
1 parent 7392624 commit f0e5855

18 files changed

+110
-50
lines changed

R/contradicted.R

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
#' @example ./examples/feasible.R
77
#' @param x [validate::validator()] object with rules.
88
#' @param rule_name `character` with the names of the rules that are causing infeasibility.
9+
#' @param verbose if `TRUE` prints the
910
#' @family feasibility
1011
#' @return `character` with conflicting rules.
11-
is_contradicted_by <- function(x, rule_name){
12+
is_contradicted_by <- function(x, rule_name, verbose = interactive()){
1213
rn <- rule_name %in% names(x)
1314

1415
if (any(!rn)){
@@ -23,11 +24,21 @@ is_contradicted_by <- function(x, rule_name){
2324

2425

2526
res <- character()
26-
contra <- detect_infeasible_rules(x, weight = weight)
27+
contra <- detect_infeasible_rules(x, weight = weight, verbose = FALSE)
2728
while (length(contra) && !any(contra %in% names(weight))){
2829
res <- c(res, contra)
2930
weight[contra] <- N
30-
contra <- detect_infeasible_rules(x, weight = weight)
31+
contra <- detect_infeasible_rules(x, weight = weight, verbose = FALSE)
32+
}
33+
if (isTRUE(verbose) && length(res)){
34+
v <- x[rule_name] |> to_exprs() |> lapply(deparse_all)
35+
v_cont <- x[res] |> to_exprs() |> lapply(deparse_all)
36+
message(
37+
"Rule(s): \n",
38+
paste0(" ", names(v), ": ", v, collapse = "\n"),
39+
"\ncontradicted by:\n",
40+
paste0(" ", names(v_cont), ": ", v_cont, collapse = "\n")
41+
)
3142
}
3243
res
3344
}

R/detect_contradicting_if_rules.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#' @family feasibility
1414
#' @example ./examples/detect_contradicting_if_rules.R
1515
#' @export
16-
detect_contradicting_if_rules <- function(x, ..., verbose = TRUE){
16+
detect_contradicting_if_rules <- function(x, ..., verbose = interactive()){
1717
res <- detect_if_clauses(x, ...)
1818
if (length(res) == 0){
1919
if (verbose){
@@ -79,7 +79,7 @@ check_condition <- function(cond_expr, x){
7979
return(NULL)
8080
}
8181
l <- list()
82-
v1 <- is_contradicted_by(v, names(cond))
82+
v1 <- is_contradicted_by(v, names(cond), verbose = FALSE)
8383
l[[cond_s]] <- v1
8484
l
8585
}

R/feasible.R

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,12 @@ is_feasible <- function(x, ...){
3838
#' @export
3939
#' @param x [validate::validator()] object with the validation rules.
4040
#' @param ... passed to [detect_infeasible_rules()]
41+
#' @param verbose if `TRUE` print information to the console
4142
#' @family feasibility
4243
#' @example ./examples/feasible.R
4344
#' @return [validate::validator()] object with feasible rules.
44-
make_feasible <- function(x, ...){
45-
dropping <- detect_infeasible_rules(x, ...)
45+
make_feasible <- function(x, ..., verbose = interactive()){
46+
dropping <- detect_infeasible_rules(x, ..., verbose = verbose)
4647

4748
if (length(dropping) == 0){
4849
message("No infeasibility found, returning original rule set")
@@ -65,8 +66,9 @@ make_feasible <- function(x, ...){
6566
#' weight `1`.
6667
#' @family feasibility
6768
#' @param ... not used
69+
#' @param verbose if `TRUE` it prints the infeasible rules that have been found.
6870
#' @return `character` with the names of the rules that are causing infeasibility.
69-
detect_infeasible_rules <- function(x, weight = numeric(), ...){
71+
detect_infeasible_rules <- function(x, weight = numeric(), ..., verbose = interactive()){
7072
# browser()
7173
if (!is_infeasible(x)){
7274
return(character())
@@ -145,6 +147,10 @@ detect_infeasible_rules <- function(x, weight = numeric(), ...){
145147
names(rules) <- sub("^\\.delta_", "", names(rules))
146148

147149
dropping <- names(rules)[rules == 1]
150+
if (isTRUE(verbose)){
151+
v <- x[dropping] |> to_exprs() |> lapply(deparse_all)
152+
message("Found: \n", paste0(" ", names(v), ": ", v, collapse = "\n"))
153+
}
148154
dropping
149155
} else {
150156
stop("No solution found to make system feasible.", call. = FALSE)

R/implied_by.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ is_implied_by <- function(x, rule_name, ...){
4141
# names(weight) <- names(negated_rules)
4242
# detect_infeasible_rules(test_rules, weight)
4343

44-
is_contradicted_by(test_rules, names(negated_rules))
44+
is_contradicted_by(test_rules, names(negated_rules), verbose = FALSE)
4545
}
4646

4747
# rules <- x <- validator(r1 = x > 1, r2 = x > 2)

R/redundancy.R

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,31 @@
88
#' @example ./examples/redundancy.R
99
#' @param x [validate::validator()] object with the validation rules.
1010
#' @param ... not used.
11+
#' @param verbose if `TRUE` print the redundant rule(s) to the console
1112
#'
1213
#' @family redundancy
1314
#'
1415
#' @export
15-
detect_redundancy <- function(x, ...){
16+
detect_redundancy <- function(x, ..., verbose=interactive()){
1617
x <- check_validator(x)
1718
can_be_checked <- is_linear(x) | is_categorical(x) | is_conditional(x)
1819
vals <- to_exprs(x)
1920
dnf_set <- lapply(vals[can_be_checked], as_dnf)
2021
are_redundant <- sapply(seq_along(dnf_set), function(i){
2122
is_redundant(dnf_set, i)
2223
})
23-
idx <- which(can_be_checked)[are_redundant]
2424

25+
idx <- which(can_be_checked)[are_redundant]
2526
ret <- logical(length = length(vals))
2627
names(ret) <- names(vals)
2728
ret[idx] <- TRUE
29+
if (isTRUE(verbose) && any(ret)){
30+
v <- x[names(ret)[ret]] |> to_exprs() |> lapply(deparse_all)
31+
message(
32+
"Redundant rule(s):\n",
33+
paste0(" ", names(v), ": ", v, collapse = "\n")
34+
)
35+
}
2836
ret
2937
}
3038

@@ -35,22 +43,32 @@ detect_redundancy <- function(x, ...){
3543
#' @example ./examples/redundancy.R
3644
#' @param x [validate::validator()] object with validation rules.
3745
#' @param ... not used
46+
#' @param verbose if `TRUE` print the remove rules to the console.
3847
#'
3948
#' @family redundancy
4049
#'
4150
#' @return simplified [validate::validator()] object, in which redundant rules are removed.
42-
remove_redundancy <- function(x, ...){
51+
remove_redundancy <- function(x, ..., verbose = interactive()){
4352
x <- check_validator(x)
4453

4554
can_be_checked <- is_linear(x) | is_categorical(x) | is_conditional(x)
4655

4756
vals <- to_exprs(x)
4857
dnf_set <- lapply(vals[can_be_checked], as_dnf)
58+
red <- character()
4959
for (i in rev(seq_along(dnf_set))){ # remove later rules before older rules
5060
if (is_redundant(dnf_set, i)){
61+
red <- c(red, names(dnf_set)[i])
5162
dnf_set[[i]] <- list()
5263
}
5364
}
65+
if (length(red) && isTRUE(verbose)){
66+
v <- x[red] |> to_exprs() |> lapply(deparse_all)
67+
message(
68+
"Removed redundant rule(s):\n",
69+
paste0(" ", names(v), ": ", v, collapse = "\n")
70+
)
71+
}
5472
vals[can_be_checked] <- lapply(dnf_set, as.expression)
5573
vals <- unlist(vals) # this removes empty expressions
5674
do.call(validate::validator, vals)

R/simplify_rules.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ simplify_rules <- function(.x, .values = list(...), ...){
2222
.x <- substitute_values(.x, .values)
2323
.x <- simplify_fixed_variables(.x)
2424
.x <- simplify_conditional(.x)
25-
.x <- remove_redundancy(.x)
25+
.x <- remove_redundancy(.x, verbose=FALSE)
2626
.x
2727
}
2828

README.Rmd

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,11 @@ rules <- validator( rule1 = x > 0
6060
)
6161
is_infeasible(rules)
6262
63-
detect_infeasible_rules(rules)
64-
make_feasible(rules)
63+
detect_infeasible_rules(rules, verbose=TRUE)
64+
make_feasible(rules, verbose=TRUE)
6565
6666
# find out the conflict with this rule
67-
is_contradicted_by(rules, "rule1")
67+
is_contradicted_by(rules, "rule1", verbose=TRUE)
6868
```
6969

7070
### Finding contradicting if rules
@@ -154,15 +154,15 @@ rules <- validator( rule1 = age > 12
154154
)
155155
156156
# rule1 is superfluous
157-
remove_redundancy(rules)
157+
remove_redundancy(rules, verbose=TRUE)
158158
159159
rules <- validator( rule1 = age > 12
160160
, rule2 = age > 12
161161
)
162162
163163
# standout: rule1 and rule2, first rule wins
164-
remove_redundancy(rules)
164+
remove_redundancy(rules, verbose=TRUE)
165165
166166
# Note that detection signifies both rules!
167-
detect_redundancy(rules)
167+
detect_redundancy(rules, verbose=TRUE)
168168
```

README.md

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,24 @@ rules <- validator( rule1 = x > 0
5858
is_infeasible(rules)
5959
#> [1] TRUE
6060

61-
detect_infeasible_rules(rules)
61+
detect_infeasible_rules(rules, verbose=TRUE)
62+
#> Found:
63+
#> rule1: x > 0
6264
#> [1] "rule1"
63-
make_feasible(rules)
65+
make_feasible(rules, verbose=TRUE)
66+
#> Found:
67+
#> rule1: x > 0
6468
#> Dropping rule(s): "rule1"
6569
#> Object of class 'validator' with 1 elements:
6670
#> rule2: x < 0
6771
#> Rules are evaluated using locally defined options
6872

6973
# find out the conflict with this rule
70-
is_contradicted_by(rules, "rule1")
74+
is_contradicted_by(rules, "rule1", verbose=TRUE)
75+
#> Rule(s):
76+
#> rule1: x > 0
77+
#> contradicted by:
78+
#> rule2: x < 0
7179
#> [1] "rule2"
7280
```
7381

@@ -190,7 +198,9 @@ rules <- validator( rule1 = age > 12
190198
)
191199

192200
# rule1 is superfluous
193-
remove_redundancy(rules)
201+
remove_redundancy(rules, verbose=TRUE)
202+
#> Removed redundant rule(s):
203+
#> rule1: age > 12
194204
#> Object of class 'validator' with 1 elements:
195205
#> rule2: age > 18
196206

@@ -199,12 +209,17 @@ rules <- validator( rule1 = age > 12
199209
)
200210

201211
# standout: rule1 and rule2, first rule wins
202-
remove_redundancy(rules)
212+
remove_redundancy(rules, verbose=TRUE)
213+
#> Removed redundant rule(s):
214+
#> rule2: age > 12
203215
#> Object of class 'validator' with 1 elements:
204216
#> rule1: age > 12
205217

206218
# Note that detection signifies both rules!
207-
detect_redundancy(rules)
219+
detect_redundancy(rules, verbose=TRUE)
220+
#> Redundant rule(s):
221+
#> rule1: age > 12
222+
#> rule2: age > 12
208223
#> rule1 rule2
209224
#> TRUE TRUE
210225
```

examples/detect_contradicting_if_rules.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ rules <- validator(
33
if (nace == "a") export == "n"
44
)
55

6-
conflicts <- detect_contradicting_if_rules(rules)
6+
conflicts <- detect_contradicting_if_rules(rules, verbose=TRUE)
77

88
print(conflicts)
99

examples/feasible.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ rules <- validator( rule1 = x > 0
88

99
is_infeasible(rules)
1010

11-
detect_infeasible_rules(rules)
12-
make_feasible(rules)
11+
detect_infeasible_rules(rules, verbose=TRUE)
12+
make_feasible(rules, verbose = TRUE)
1313

1414
# find out the conflict with this rule
15-
is_contradicted_by(rules, "rule1")
15+
is_contradicted_by(rules, "rule1", verbose = TRUE)

0 commit comments

Comments
 (0)