Skip to content

Commit

Permalink
SolveHandle::model() returns now Result<Option<&Model>, Error>
Browse files Browse the repository at this point in the history
to distinguish Errors and nomore models=None
  • Loading branch information
sthiele committed Dec 13, 2018
1 parent bf06f1b commit 0755505
Show file tree
Hide file tree
Showing 12 changed files with 118 additions and 89 deletions.
1 change: 1 addition & 0 deletions clingo-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ fn main() {
gcc::Build::new()
.cpp(true)
.flag("-std=c++11")
.flag("-O3")
.warnings(false)
.define("NDEBUG", Some("1"))
.file("clingo/clasp/libpotassco/src/application.cpp")
Expand Down
8 changes: 5 additions & 3 deletions src/examples/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,19 @@ fn print_model(model: &Model) {

fn solve(ctl: &mut Control) {
// get a solve handle
let mut handle = ctl.solve(&SolveMode::YIELD, &[])
let mut handle = ctl
.solve(&SolveMode::YIELD, &[])
.expect("Failed retrieving solve handle.");

// loop over all models
loop {
handle.resume().expect("Failed resume on solve handle.");
match handle.model() {
// print the model
Ok(model) => print_model(model),
Ok(Some(model)) => print_model(model),
// stop if there are no more models
Err(_) => break,
Ok(None) => break,
Err(e) => panic!("Error: {}", e.as_fail()),
}
}

Expand Down
8 changes: 5 additions & 3 deletions src/examples/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,19 @@ fn print_model(model: &Model) {

fn solve(ctl: &mut Control) {
// get a solve handle
let mut handle = ctl.solve(&SolveMode::YIELD, &[])
let mut handle = ctl
.solve(&SolveMode::YIELD, &[])
.expect("Failed retrieving solve handle.");

// loop over all models
loop {
handle.resume().expect("Failed resume on solve handle.");
match handle.model() {
// print the model
Ok(model) => print_model(model),
Ok(Some(model)) => print_model(model),
// stop if there are no more models
Err(_) => break,
Ok(None) => break,
Err(e) => panic!("Error: {}", e.as_fail()),
}
}

Expand Down
10 changes: 6 additions & 4 deletions src/examples/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,19 @@ fn print_model(model: &Model) {

fn solve(ctl: &mut Control) {
// get a solve handle
let mut handle = ctl.solve(&SolveMode::YIELD, &[])
let mut handle = ctl
.solve(&SolveMode::YIELD, &[])
.expect("Failed retrieving solve handle.");

// loop over all models
loop {
handle.resume().expect("Failed resume on solve handle.");
match handle.model() {
// stop if there are no more models
Err(_) => break,
// print the model
Ok(model) => print_model(model),
Ok(Some(model)) => print_model(model),
// stop if there are no more models
Ok(None) => break,
Err(e) => panic!("Error: {}", e.as_fail()),
}
}

Expand Down
8 changes: 5 additions & 3 deletions src/examples/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,19 @@ fn print_model(model: &Model) {

fn solve(ctl: &mut Control) {
// get a solve handle
let mut handle = ctl.solve(&SolveMode::YIELD, &[])
let mut handle = ctl
.solve(&SolveMode::YIELD, &[])
.expect("Failed retrieving solve handle.");

// loop over all models
loop {
handle.resume().expect("Failed resume on solve handle.");
match handle.model() {
// print the model
Ok(model) => print_model(model),
Ok(Some(model)) => print_model(model),
// stop if there are no more models
Err(_) => break,
Ok(None) => break,
Err(e) => panic!("Error: {}", e.as_fail()),
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/examples/inject-terms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,11 @@ fn main() {
});

// solve
let handle = ctl.solve(&SolveMode::YIELD, &[])
let handle = ctl
.solve(&SolveMode::YIELD, &[])
.expect("Failed to retrieve solve handle.");

print_model(handle.model().unwrap());
print_model(handle.model().unwrap().unwrap());

handle.close().expect("Failed to close solve handle.");
}
65 changes: 37 additions & 28 deletions src/examples/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,41 +20,45 @@ fn print_model(model: &Model, label: &str, show: &ShowType) {

fn solve(ctl: &mut Control) {
// get a solve handle
let mut handle = ctl.solve(&SolveMode::YIELD, &[])
let mut handle = ctl
.solve(&SolveMode::YIELD, &[])
.expect("Failed retrieving solve handle.");

// loop over all models
loop {
handle.resume().expect("Failed resume on solve handle.");
if let Ok(model) = handle.model() {
// get model type
let model_type = model.model_type().unwrap();

let mut type_string = match model_type {
ModelType::StableModel => "Stable model",
ModelType::BraveConsequences => "Brave consequences",
ModelType::CautiousConsequences => "Cautious consequences",
};

// get running number of model
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));
} else {
// stop if there are no more models
break;
match handle.model() {
Ok(Some(model)) => {
// get model type
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",
};

// get running number of model
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) => {
// stop if there are no more models
break;
}
Err(e) => {
panic!("Error: {}", e);
}
}
}

// close the solve handle
handle
.get()
.expect("Failed to get result from solve handle.");
handle.close().expect("Failed to close solve handle.");
}

Expand All @@ -66,8 +70,13 @@ fn main() {
let mut ctl = Control::new(options).expect("Failed creating clingo_control.");

// add a logic program to the base part
ctl.add("base", &[], "1 {a; b} 1. #show c : b. #show a/0.")
.expect("Failed to add a logic program.");
// ctl.add("base", &[], "1 {a; b} 1. #show c : b. #show a/0.")
ctl.add(
"base",
&[],
"a(1..100). 1{b(X):a(X)}1. #maximize { V@2 : b(V) }.",
)
.expect("Failed to add a logic program.");

// ground the base part
let part = Part::new("base", &[]).unwrap();
Expand Down
16 changes: 8 additions & 8 deletions src/examples/propagator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,23 @@ fn print_model(model: &Model) {

fn solve(ctl: &mut Control) {
// get a solve handle
let mut handle = ctl.solve(&SolveMode::YIELD, &[])
let mut handle = ctl
.solve(&SolveMode::YIELD, &[])
.expect("Failed to retrieve solve handle.");

// loop over all models
loop {
handle.resume().expect("Failed resume on solve handle.");
match handle.model() {
// print the model
Ok(model) => print_model(model),
Ok(Some(model)) => print_model(model),
// stop if there are no more models
Err(_) => break,
Ok(None) => break,
Err(e) => panic!("Error: {}", e.as_fail()),
}
}

// close the solve handle
handle
.get()
.expect("Failed to get result from solve handle.");
handle.close().expect("Failed to close solve handle.");
}

Expand Down Expand Up @@ -249,7 +248,8 @@ fn main() {
"pigeon",
&vec!["h", "p"],
"1 { place(P,H) : H = 1..h } 1 :- P = 1..p.",
).expect("Failed to add a logic program.");
)
.expect("Failed to add a logic program.");

// ground the pigeon part

Expand All @@ -270,7 +270,7 @@ fn main() {
solve(&mut ctl);
}
Err(e) => {
println!("Error: {}", e.cause());
panic!("Error: {}", e.as_fail());
}
}
}
9 changes: 5 additions & 4 deletions src/examples/solve-async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ fn main() {
1 { p(X); q(X) } 1 :- X = 1..n.\
:- not n+1 { p(1..n); \
q(1..n) }.",
).expect("Failed to add a logic program.");
)
.expect("Failed to add a logic program.");

// ground the base part
let part = Part::new("base", &[]).unwrap();
Expand All @@ -46,9 +47,9 @@ fn main() {
};

// create a solve handle with an attached event handler
let mut handle =
ctl.solve_with_event_handler(&(SolveMode::ASYNC | SolveMode::YIELD), &[], &mut running)
.expect("Failed to retrieve solve handle.");
let mut handle = ctl
.solve_with_event_handler(&(SolveMode::ASYNC | SolveMode::YIELD), &[], &mut running)
.expect("Failed to retrieve solve handle.");

// let's approximate pi
let mut samples = 0.;
Expand Down
8 changes: 5 additions & 3 deletions src/examples/statistics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,19 @@ fn print_model(model: &Model) {

fn solve(ctl: &mut Control) {
// get a solve handle
let mut handle = ctl.solve(&SolveMode::YIELD, &[])
let mut handle = ctl
.solve(&SolveMode::YIELD, &[])
.expect("Failed retrieving solve handle.");

// loop over all models
loop {
handle.resume().expect("Failed resume on solve handle.");
match handle.model() {
// print the model
Ok(model) => print_model(model),
Ok(Some(model)) => print_model(model),
// stop if there are no more models
Err(_) => break,
Ok(None) => break,
Err(e) => panic!("Error: {}", e.as_fail()),
}
}

Expand Down
11 changes: 7 additions & 4 deletions src/examples/theory-atoms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,19 @@ fn print_model(model: &Model) {

fn solve(ctl: &mut Control) {
// get a solve handle
let mut handle = ctl.solve(&SolveMode::YIELD, &[])
let mut handle = ctl
.solve(&SolveMode::YIELD, &[])
.expect("Failed retrieving solve handle.");

// loop over all models
loop {
handle.resume().expect("Failed resume on solve handle.");
match handle.model() {
// print the model
Ok(model) => print_model(model),
Ok(Some(model)) => print_model(model),
// stop if there are no more models
Err(_) => break,
Ok(None) => break,
Err(e) => panic!("Error: {}", e.as_fail()),
}
}

Expand Down Expand Up @@ -90,7 +92,8 @@ fn main() {
}.\
x :- &a { 1+2 }.\
y :- &b(3) { } = 17.",
).expect("Failed to add a logic program.");
)
.expect("Failed to add a logic program.");

// ground the base part
let part = Part::new("base", &[]).unwrap();
Expand Down
Loading

0 comments on commit 0755505

Please sign in to comment.