Skip to content

Commit bcb9d80

Browse files
committed
added a fix for multi condition if rules
1 parent 3b7c57b commit bcb9d80

File tree

2 files changed

+27
-26
lines changed

2 files changed

+27
-26
lines changed

R/detect_contradicting_if_rules.R

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -61,31 +61,21 @@ check_condition <- function(cond_expr, x){
6161
if (length(clauses) <= 1){
6262
return(NULL)
6363
}
64-
# to do for %in statement and replace with multiple "=="
65-
neg_clauses <- lapply(clauses, invert_or_negate)
64+
# browser()
65+
# take last clause as consequent, and the rest as the condition
66+
cond <-
67+
utils::head(clauses, -1) |>
68+
lapply(invert_or_negate)
6669

67-
l <- list()
68-
for (neg in neg_clauses){
69-
v <- x + do.call(validate::validator, list(.test = neg))
70-
if (is_feasible(v)){
71-
next
72-
}
73-
v1 <- is_contradicted_by(v, ".test")
74-
l[[deparse(neg)]] <- v1
75-
# op <- op_to_s(neg)
76-
# if (op == "=="){
77-
# .values <- list(neg[[3]]) |> setNames(as.character(neg[[2]]))
78-
# test_rules <- substitute_values(x, .values = .values)
79-
# v <- detect_infeasible_rules(test_rules)
80-
# if (is.null(v)){
81-
# next
82-
# }
83-
# v1 <- is_contradicted_by(test_rules, v)
84-
# v <- c(v,v1)
85-
# l[[deparse(neg)]] <- v
86-
# }
87-
88-
# TODO expand %in% statement here
70+
names(cond) <- paste0(".test", seq_along(cond))
71+
cond_s <- sapply(cond, deparse_all) |> paste0(collapse = " && ")
72+
73+
v <- x + do.call(validate::validator, cond)
74+
if (is_feasible(v)){
75+
return(NULL)
8976
}
77+
l <- list()
78+
v1 <- is_contradicted_by(v, names(cond))
79+
l[[cond_s]] <- v1
9080
l
9181
}

tests/testthat/test-detect_contradicting_if_rules.R

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,25 @@ describe("detect contradictory categorical if clauses", {
5454
expect_equal(a, list("income > 0" = c("V2", "V1")))
5555
})
5656

57-
it("detects multi conditions", {
57+
it("detects multi conditions 1", {
5858
v <- validator(
5959
if (nace == "a" && export > 0) international == TRUE,
6060
if (nace == "a") international == FALSE
6161
)
6262

6363
a <- detect_contradicting_if_rules(v, verbose=FALSE)
64-
skip("Need to fix multi conditions")
64+
expect_equal(a, list("nace == \"a\" && export > 0" = c("V2", "V1")))
6565
})
6666

67+
it("detects multi conditions 2", {
68+
v <- validator(
69+
if (income > 0) status == "working" | status == "self-employed",
70+
if (status == "unemployed") income > 0
71+
)
72+
73+
a <- detect_contradicting_if_rules(v, verbose=FALSE)
74+
expect_equal(a, list("status == \"unemployed\"" = c("V1", "V2")))
75+
})
76+
77+
6778
})

0 commit comments

Comments
 (0)