1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
use std::collections::{HashSet}; use std::fs; #[macro_use] extern crate serde_derive; extern crate serde; use toml; #[macro_use] pub mod macros; pub mod proposition; pub mod action; pub mod plangraph; pub mod solver; mod layer; mod pairset; use crate::proposition::Proposition; use crate::action::Action; use crate::plangraph::{PlanGraph, Solution}; use crate::solver::{GraphPlanSolver, SimpleSolver}; #[derive(Deserialize)] struct Config { initial: Vec<String>, goals: Vec<String>, actions: Vec<ConfigAction> } #[derive(Deserialize)] struct ConfigAction { name: String, reqs: Vec<String>, effects: Vec<String>, } pub struct GraphPlan<T: GraphPlanSolver> { pub solver: T, pub plangraph: PlanGraph, } impl<T: GraphPlanSolver> GraphPlan<T> { pub fn new(initial_props: HashSet<&Proposition>, goals: HashSet<&Proposition>, actions: HashSet<&Action>, solver: T) -> GraphPlan<T> { let pg = PlanGraph::new(initial_props, goals, actions); GraphPlan { solver: solver, plangraph: pg } } pub fn search(&mut self) -> Option<Solution>{ self.plangraph.search_with(&self.solver) } } impl GraphPlan<SimpleSolver> { pub fn from_toml(filepath: String) -> GraphPlan<SimpleSolver> { let st = fs::read_to_string(filepath).expect("Failed to read file"); let config: Config = toml::from_str(&st).expect("Fail"); let initial_props: HashSet<Proposition> = config.initial .iter() .map(|i| Proposition::new(i.to_owned(), i.starts_with("not_"))) .collect(); let goals: HashSet<Proposition> = config.goals .iter() .map(|i| Proposition::new(i.to_owned(), i.starts_with("not_"))) .collect(); let actions: HashSet<Action> = config.actions .iter() .map(|i| { let reqs: HashSet<Proposition> = i.reqs.iter() .map(|r| Proposition::new(r.clone(), r.starts_with("not_"))) .collect(); let effects: HashSet<Proposition> = i.effects.iter() .map(|e| Proposition::new(e.clone(), e.starts_with("not_"))) .collect(); Action::new( i.name.to_string(), reqs.iter().collect(), effects.iter().collect() ) }) .collect(); let solver = SimpleSolver::new(); GraphPlan::new( initial_props.iter().collect(), goals.iter().collect(), actions.iter().collect(), solver ) } } #[cfg(test)] mod integration_test { use crate::GraphPlan; use crate::proposition::Proposition; use crate::action::Action; use crate::solver::SimpleSolver; #[test] fn integration() { let p1 = Proposition::from_str("tired"); let not_p1 = p1.negate(); let p2 = Proposition::from_str("dog needs to pee"); let not_p2 = p2.negate(); let p3 = Proposition::from_str("caffeinated"); let a1 = Action::new( String::from("coffee"), hashset!{&p1}, hashset!{&p3, ¬_p1} ); let a2 = Action::new( String::from("walk dog"), hashset!{&p2, &p3}, hashset!{¬_p2}, ); let mut pg = GraphPlan::new( hashset!{&p1, &p2}, hashset!{¬_p1, ¬_p2, &p3}, hashset!{&a1, &a2}, SimpleSolver::new() ); assert!(pg.search() != None, "Solution should not be None"); } #[test] fn load_from_toml_config() { let path = String::from("resources/rocket_domain.toml"); let mut pg: GraphPlan<SimpleSolver> = GraphPlan::from_toml(path); assert!(pg.search() != None, "Solution should not be None"); } }