pub enum Goal {
OperatorGoal(Operator),
BuiltInGoal(BuiltInPredicate),
ComplexGoal(Unifiable),
Nil,
}
Variants§
OperatorGoal(Operator)
Holds an Operator, such as And, Or, Time etc.
BuiltInGoal(BuiltInPredicate)
Holds a built-in predicate, such as print(), append(), etc.
ComplexGoal(Unifiable)
Holds a complex term (SComplex).
Nil
Variant for Rules which don’t have a body (ie. facts).
Implementations§
Source§impl Goal
impl Goal
Sourcepub fn recreate_variables(self, vars: &mut VarMap) -> Goal
pub fn recreate_variables(self, vars: &mut VarMap) -> Goal
Recreates logic variables to give them unique IDs.
Logic variables in the knowledge base have an ID of 0, but when a rule is fetched from the knowledge base, the logic variables must be given unique IDs.
§Arguments
- self
- vars - Map of previously recreated variable IDs.
§Return
- new goal
§Usage
use suiron::*;
clear_id();
// Create an And goal.
let goal = generate_goal("father($X, $Z), mother($Z, $Y)");
match goal {
Ok(goal) => {
// var_map records previously recreated variables.
let mut var_map = VarMap::new();
let goal = goal.recreate_variables(&mut var_map);
println!("{}", goal);
},
Err(msg) => { println!("{}", msg); },
}
// Prints: father($X_1, $Z_2), mother($Z_2, $Y_3)
Sourcepub fn replace_variables(&self, ss: &SubstitutionSet<'_>) -> Unifiable
pub fn replace_variables(&self, ss: &SubstitutionSet<'_>) -> Unifiable
Replaces logic variables with their ground terms from the substitution set.
This method is useful for displaying the results of a query.
For example, if the query loves(Leonard, $Whom)
has a solution,
calling replace_variables()
will produce a new term which shows
the solution, eg. loves(Leonard, Penny)
.
§Arguments
- self
- substitution_set
§Return
- new term - should contain no variables
§Panics
- If the goal is not a ComplexGoal
§Usage
use std::rc::Rc;
use suiron::*;
// Setup test knowledge base and base solution node.
let kb = test_kb();
let query = parse_query("loves(Leonard, $Whom)").unwrap();
let q = Rc::new(query);
let base = make_base_node(Rc::clone(&q), &kb);
// Find a solution.
let solution = next_solution(base);
match solution {
Some(ss) => { println!("{}", q.replace_variables(&ss)); },
None => { println!("No more."); },
}
// Prints: loves(Leonard, Penny)
Sourcepub fn key(&self) -> String
pub fn key(&self) -> String
Creates a key (= predicate name) for indexing into a knowledge base.
The name of a predicate consists of its functor plus its arity,
separated by a slash. For example, for the fact loves(Chandler, Monica)
,
the functor is loves
and the arity is 2, therefore the name of the
predicate is loves/2
.
§Panics
- If self is not a complex term.
§Usage
use suiron::*;
let goal = parse_subgoal("loves(Chandler, Monica)").unwrap();
let key = goal.key();
println!("{}", key); // Should print: loves/2
Sourcepub fn get_ground_term<'a>(
&self,
index: usize,
ss: Rc<SubstitutionSet<'a>>,
) -> Option<Unifiable>
pub fn get_ground_term<'a>( &self, index: usize, ss: Rc<SubstitutionSet<'a>>, ) -> Option<Unifiable>
Gets a ground term of a query-goal.
This method is useful for getting the results of a query.
§Arguments
- self
- index of term
- SubstitutionSet (containing solution of query)
§Returns
- Unifiable or None
§Usage
use suiron::*;
use std::rc::Rc;
let kb = test_kb();
let query = parse_query("loves(Penny, $Whom)").unwrap();
let query = Rc::new(query);
let base_node = make_base_node(Rc::clone(&query), &kb);
let solution = next_solution(base_node);
match solution {
Some(ss) => {
// Get the result.
// The index of the variable $Whom is 2.
match query.get_ground_term(2, ss) {
Some(result) => { println!("{}", result); },
None => { println!("No solution."); },
}
},
None => { println!("No solution."); },
} // match solution
// Prints: Leonard