# ~/Downloads/bnlearn/genDfs.R
library(bnlearn)
dir = "/Users/ruiqil/Downloads/bnlearn/"
set.seed(42)

data(asia)
force(asia)
write.csv(asia, paste0(dir, "asia.csv"), row.names = FALSE)
data(coronary)
force(coronary)
write.csv(coronary, paste0(dir, "coronary.csv"), row.names = FALSE)
data(alarm)
force(alarm)
write.csv(alarm, paste0(dir, "alarm.csv"), row.names = FALSE)
data(hailfinder)
force(hailfinder)
write.csv(hailfinder, paste0(dir, "hailfinder.csv"), row.names = FALSE)
data(insurance)
force(insurance)
write.csv(insurance, paste0(dir, "insurance.csv"), row.names = FALSE)

net_names <- c("cancer", "barley", "child", "mildew", "water", "hepar2", "win95pts")
base_url <- "https://www.bnlearn.com/bnrepository"
n <- 20000
for (i in seq_along(net_names)) {
  net_name <- net_names[i]
  url <- sprintf("%s/%s/%s.rds", base_url, net_name, net_name)
  net <- readRDS(url(url))
  sim <- rbn(net, n)
  write.csv(sim, paste0(dir, net_name, "_", n, ".csv"), row.names = FALSE)
}