use clingo::*;
use std::env;
fn print_model(model: &Model, label: &str, show: ShowType) {
print!("{}:", label);
let atoms = model
.symbols(show)
.expect("Failed to retrieve symbols in the model.");
for symbol in atoms {
print!(" {}", symbol);
}
println!();
}
fn solve(ctl: Control) {
let mut handle = ctl
.solve(SolveMode::YIELD, &[])
.expect("Failed retrieving solve handle.");
loop {
handle.resume().expect("Failed resume on solve handle.");
match handle.model() {
Ok(Some(model)) => {
let model_type = model.model_type().unwrap();
let type_string = match model_type {
ModelType::StableModel => "Stable model",
ModelType::BraveConsequences => "Brave consequences",
ModelType::CautiousConsequences => "Cautious consequences",
};
let number = model.number().unwrap();
println!("{}: {}", type_string, number);
print_model(model, " shown", ShowType::SHOWN);
print_model(model, " atoms", ShowType::ATOMS);
print_model(model, " terms", ShowType::TERMS);
print_model(model, " ~atoms", ShowType::COMPLEMENT | ShowType::ATOMS);
}
Ok(None) => {
break;
}
Err(e) => {
panic!("Error: {}", e);
}
}
}
handle.close().expect("Failed to close solve handle.");
}
fn main() {
let options = env::args().skip(1).collect();
let mut ctl = control(options).expect("Failed creating clingo_control.");
ctl.add("base", &[], "1 {a; b} 1. #show c : b. #show a/0.")
.expect("Failed to add a logic program.");
let part = Part::new("base", vec![]).unwrap();
let parts = vec![part];
ctl.ground(&parts)
.expect("Failed to ground a logic program.");
solve(ctl);
}