Skip to content

Commit

Permalink
W.I.P.
Browse files Browse the repository at this point in the history
  • Loading branch information
Anatoliy Lischynsky committed Oct 27, 2019
1 parent fa102f3 commit 8f9ff3d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 5 deletions.
17 changes: 16 additions & 1 deletion src/facts.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::HashSet;
use std::fmt;

#[derive(Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq)]
pub struct Facts {
yes: HashSet<char>,
no: HashSet<char>,
Expand All @@ -19,13 +19,28 @@ impl Facts {
Facts { yes, no }
}

pub fn is_empty(&self) -> bool {
self.yes.is_empty() && self.no.is_empty()
}

pub fn yes(&self, c: char) -> bool {
self.yes.get(&c).is_some()
}

pub fn no(&self, c: char) -> bool {
self.no.get(&c).is_some()
}

/// Remove facts that are known in `other` from self
pub fn remove_contained(&mut self, other: &Facts) {
for fact in other.yes.iter() {
self.yes.remove(fact);
}

for fact in other.no.iter() {
self.yes.remove(fact);
}
}
}

impl fmt::Display for Facts {
Expand Down
29 changes: 27 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,32 @@ use expert_system::{parser, Facts, Query, Rule};
use rustyline::error::ReadlineError;
use std::collections::HashSet;

fn run(facts: &HashSet<Rule>, given: &Facts, find: &Facts, level: usize) -> Facts {
fn run(usable_rules: HashSet<Rule>, given: Facts, mut find: Facts, level: usize) -> Facts {
find.remove_contained(&given);

if find.is_empty() {
println!("{}search list empty, returning {}", " ".repeat(level), given);
return given;
}

if usable_rules.is_empty() {
println!("{}no more rules to use, returning {}", " ".repeat(level), given);
return given;
}

for rule in usable_rules.iter() {
if rule.can_give(&find) {
// recurse into
let mut usable_rules = usable_rules.clone();
usable_rules.remove(&rule);

println!("{}try {} with {}", " ".repeat(level), rule, given);
run(usable_rules, given.clone(), find.clone(), level + 1);
} else {
println!("{}{} can't give {}", " ".repeat(level), rule, find);
}
}

unimplemented!()
}

Expand All @@ -33,7 +58,7 @@ fn main() {
Query::Find(find) => {
println!("Find: {}", find);

let result = run(&rules, &facts, &find, 0);
let result = run(rules.clone(), facts.clone(), find.clone(), 0);
println!("Result: {}", result);
}
Query::Dump => {
Expand Down
4 changes: 2 additions & 2 deletions src/rule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::fmt;

use super::Facts;

#[derive(Debug, PartialEq, Eq, Hash)]
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum Rule {
Char(char),
Not(Box<Rule>),
Expand Down Expand Up @@ -51,7 +51,7 @@ impl Rule {
use Rule::*;

match self {
Char(ref c) => facts.no(*c) || !facts.yes(*c),
Char(ref c) => facts.no(*c) || facts.yes(*c),
Not(ref l) => l.can_give_recursive(facts),
And(ref l, ref r) => l.can_give_recursive(facts) || r.can_give_recursive(facts),
Or(ref l, ref r) => l.can_give_recursive(facts) || r.can_give_recursive(facts),
Expand Down

0 comments on commit 8f9ff3d

Please sign in to comment.