Enum Goal

Source
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

Source

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)
Source

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
§Return
  • new term - should contain no variables
§Panics
§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)
Source

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
§Usage
use suiron::*;

let goal = parse_subgoal("loves(Chandler, Monica)").unwrap();
let key = goal.key();
println!("{}", key);  // Should print: loves/2
Source

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
§Returns
§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

Trait Implementations§

Source§

impl Clone for Goal

Source§

fn clone(&self) -> Goal

Returns a duplicate of the value. Read more
1.0.0 · Source§

const fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Goal

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for Goal

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for Goal

Source§

fn eq(&self, other: &Goal) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

const fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for Goal

Auto Trait Implementations§

§

impl Freeze for Goal

§

impl RefUnwindSafe for Goal

§

impl Send for Goal

§

impl Sync for Goal

§

impl Unpin for Goal

§

impl UnwindSafe for Goal

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.