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
use std::fmt;
use std::cmp::{Ordering};
use std::hash::{Hash, Hasher};
use std::collections::{HashSet};
use crate::proposition::Proposition;

#[derive(Eq, PartialEq, Clone)]
pub struct Action {
    pub name: String,
    pub reqs: HashSet<Proposition>,
    pub effects: HashSet<Proposition>,
}

impl fmt::Debug for Action {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "A:{}", self.name)
    }
}

/// Actions are hashed based on their name, that means you can't have
/// two actions of the same name in a HashSet even if they have
/// different reqs and effects
impl Hash for Action {
    fn hash<H>(&self, state: &mut H) where H: Hasher {
        self.name.hash(state);
    }
}

impl Ord for Action {
    fn cmp(&self, other: &Self) -> Ordering {
        (self.name).cmp(&(other.name))
    }
}

impl PartialOrd for Action {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Action {
    pub fn new(name: String, reqs: HashSet<&Proposition>, effects: HashSet<&Proposition>) -> Action {
        Action {
            name: name,
            reqs: reqs.into_iter().map(|i| i.to_owned()).collect(),
            effects: effects.into_iter().map(|i| i.to_owned()).collect()
        }
    }
}