Blackboard

Type Alias Blackboard 

Source
pub type Blackboard = HashMap<Symbol, Rc<dyn Any>>;
Expand description

Blackboard is a mapping of a variable names and their values. The value is wrapped in an Any trait object, so it can be any type.

§Implementation note

You may wonder why the value is wrapped in an Rc, not a Box. It seems unnecessary to have reference count for owned values inside a blackboard. The reason is that we need to pass the copy of the variables to the subtree by copying values, but Clone trait is not object safe.

Another way to work around this is to define a new trait, like AnyClone, that can clone and be object safe at the same time. The signature for the clone method would be something like:

trait AnyClone: Any {
    fn any_clone(&self) -> Box<dyn AnyClone>;
}

The difference from Clone trait is that it returns a boxed copy of the object to avoid the requirement that returned value of a function needs to be Sized. However, requiring every type that can exist in the blackboard to implement this trait is a bit too much to ask to the users. By wrapping in an Rc, it looks like cloneable, but it doesn’t require the value type to implement anything but Any.

Interestingly, this is the exactly the same issue when you try to implement a function call in your own programming language, i.e. pass-by-value v.s. pass-by-reference. In essence, a subtree in behavior tree is a function in a programming language. The third sect in the society is copy-on-write reference, which is what Rc does.

Aliased Type§

pub struct Blackboard { /* private fields */ }