Skip to content

Commit cf241ef

Browse files
committed
Merge branch 'master' of github.com:data-cleaning/validatetools
2 parents 7004e89 + eb475aa commit cf241ef

File tree

7 files changed

+88
-0
lines changed

7 files changed

+88
-0
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ Romina Filippini and Simona Toti for reporting.
1212

1313
* fix for `is_contradicted_by` and `detect_infeasible_rules` when the rules contained
1414
if statements.
15+
16+
* added extra simplification for conditional statements (issue #10)
1517

1618
# validatetools 0.5.2
1719

R/simplify_conditional.R

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,16 @@ simplify_conditional <- function(x, ...){
1515
vals <- to_exprs(x)
1616
for (i in which(is_cond)){
1717
cond <- vals[[i]]
18+
19+
cond <- simplify_dnf(cond, vals[-i])
20+
vals[[i]] <- cond
21+
1822
cond <- simplify_non_constraining(cond, vals)
1923
vals[[i]] <- cond
24+
2025
cond <- simplify_non_relaxing(cond, vals)
2126
vals[[i]] <- cond
27+
2228
}
2329
# TODO set meta data correctly for the resulting rule set
2430
do.call(validate::validator, vals)
@@ -49,3 +55,27 @@ simplify_non_constraining <- function(cond_expr, vals){
4955
}
5056
cond_expr
5157
}
58+
59+
simplify_dnf <- function(cond_expr, vals){
60+
clauses <- as_dnf(cond_expr)
61+
rm <- integer()
62+
for (i in seq_along(clauses)){
63+
clause_neg <- invert_or_negate(clauses[[i]])
64+
for (j in seq_along(clauses)){
65+
66+
if (i == j || j %in% rm){
67+
next
68+
}
69+
70+
test_rules <- do.call(validate::validator,c(clauses[j], clause_neg))
71+
if (is_infeasible(test_rules, verbose=FALSE)){
72+
rm <- c(rm, j)
73+
next
74+
}
75+
}
76+
}
77+
if (length(rm)){
78+
return(as.expression.dnf(clauses[-rm], as_if = TRUE)[[1]])
79+
}
80+
cond_expr
81+
}

README.Rmd

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,16 @@ simplify_fixed_variables(rules)
146146
### Simplifying conditional statements
147147

148148
```{r}
149+
# superfluous conditions
150+
rules <- validator(
151+
r1 = if (age > 18) age <= 67,
152+
r2 = if (income > 0 && income > 1000) job == TRUE
153+
)
154+
# implies that age always is <= 67
155+
simplify_conditional(rules)
156+
157+
158+
149159
# non-relaxing clause
150160
rules <- validator(
151161
r1 = if (income > 0) age >= 16,

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,19 @@ simplify_fixed_variables(rules)
193193
### Simplifying conditional statements
194194

195195
``` r
196+
# superfluous conditions
197+
rules <- validator(
198+
r1 = if (age > 18) age <= 67,
199+
r2 = if (income > 0 && income > 1000) job == TRUE
200+
)
201+
# implies that age always is <= 67
202+
simplify_conditional(rules)
203+
#> Object of class 'validator' with 2 elements:
204+
#> r1: age <= 67
205+
#> r2: income <= 1000 | (job == TRUE)
206+
207+
208+
196209
# non-relaxing clause
197210
rules <- validator(
198211
r1 = if (income > 0) age >= 16,

examples/simplify_conditional.R

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
library(validate)
22

3+
# superfluous conditions
4+
rules <- validator(
5+
r1 = if (x > 0) x < 1,
6+
r2 = if (y > 0 && y > 1) z > 0
7+
)
8+
# r1 means that x > 0 is superfluous
9+
# r2 means that y > 0 is superfluous
10+
simplify_conditional(rules)
11+
312
# non-relaxing clause
413
rules <- validator( r1 = if (x > 1) y > 3
514
, r2 = y < 2

man/simplify_conditional.Rd

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-simplify_conditional.R

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,18 @@ test_that("a more complex if statement also works", {
113113
expect_equal(exprs_s$r2, exprs$r2)
114114

115115
})
116+
117+
test_that("simplify dnf works", {
118+
rules <- validator(r1 = if (x > 0) x < 1)
119+
rules_s <- simplify_conditional(rules)
120+
121+
exprs_s <- to_exprs(rules_s)
122+
expect_equal(exprs_s$r1, quote(x < 1))
123+
124+
rules <- validator(r1 = if (x > 0 && x > 1) y > 0)
125+
rules_s <- simplify_conditional(rules)
126+
127+
exprs_s <- to_exprs(rules_s)
128+
expect_equal(exprs_s$r1, quote(if (x > 1) y >0))
129+
130+
})

0 commit comments

Comments
 (0)