Merge pull request #2203 from coasys/library-use-case

Don’t include unbound variables in results returned from run_query()
This commit is contained in:
Mark Thom
2023-12-05 13:14:30 -07:00
committed by GitHub
2 changed files with 24 additions and 6 deletions

View File

@@ -196,7 +196,12 @@ impl Machine {
let output: String = outputter.result(); let output: String = outputter.result();
// println!("Result: {} = {}", var_key.to_string(), output); // println!("Result: {} = {}", var_key.to_string(), output);
bindings.insert(var_key.to_string(), Value::try_from(output).expect("asdfs")); if var_key.to_string() != output {
bindings.insert(
var_key.to_string(),
Value::try_from(output).expect("Couldn't convert Houtput to Value"),
);
}
} }
matches.push(QueryResolutionLine::Match(bindings)); matches.push(QueryResolutionLine::Match(bindings));
@@ -338,6 +343,21 @@ mod tests {
); );
} }
#[test]
fn empty_predicate() {
let mut machine = Machine::new_lib();
machine.load_module_string(
"facts",
r#"
:- discontiguous(subject_class/2).
"#
.to_string(),
);
let result = machine.run_query(String::from("subject_class(X, _)."));
assert_eq!(result, Ok(QueryResolution::False));
}
#[test] #[test]
fn list_results() { fn list_results() {
let mut machine = Machine::new_lib(); let mut machine = Machine::new_lib();
@@ -425,7 +445,7 @@ mod tests {
} }
#[test] #[test]
fn stress_integration_test() { fn integration_test() {
let mut machine = Machine::new_lib(); let mut machine = Machine::new_lib();
// File with test commands, i.e. program code to consult and queries to run // File with test commands, i.e. program code to consult and queries to run
@@ -486,14 +506,12 @@ mod tests {
output, output,
Ok(QueryResolution::Matches(vec![QueryMatch::from( Ok(QueryResolution::Matches(vec![QueryMatch::from(
btreemap! { btreemap! {
"Predicate" => Value::from("Predicate"),
"Result" => Value::List( "Result" => Value::List(
Vec::from([ Vec::from([
Value::List([Value::from("p1"), Value::from("b")].into()), Value::List([Value::from("p1"), Value::from("b")].into()),
Value::List([Value::from("p2"), Value::from("b")].into()), Value::List([Value::from("p2"), Value::from("b")].into()),
]) ])
), ),
"Target" => Value::from("Target"),
} }
),])) ),]))
); );

View File

@@ -65,11 +65,11 @@ impl From<Vec<QueryResolutionLine>> for QueryResolution {
} }
} }
// If there is only one line, and it is an empty match, return true. // If there is only one line, and it is an empty match, return false.
if query_result_lines.len() == 1 { if query_result_lines.len() == 1 {
if let QueryResolutionLine::Match(m) = query_result_lines[0].clone() { if let QueryResolutionLine::Match(m) = query_result_lines[0].clone() {
if m.is_empty() { if m.is_empty() {
return QueryResolution::True; return QueryResolution::False;
} }
} }
} }