reactive_graph_std_logical/behaviour/entity/gate/
function.rs

1use std::sync::Arc;
2use std::sync::LazyLock;
3
4use reactive_graph_behaviour_model_impl::entity::EntityBehaviourFactoryCreator;
5use reactive_graph_behaviour_model_impl::entity::EntityBehaviourFunctions;
6use reactive_graph_behaviour_model_impl::entity::EntityBehaviourFunctionsStorage;
7
8use crate::behaviour::entity::gate::LogicalGateFactory;
9use reactive_graph_std_logical_model::NAMESPACE_LOGICAL;
10
11pub type LogicalGateFunction = fn(bool, bool) -> bool;
12
13pub const FN_AND: LogicalGateFunction = |lhs, rhs| lhs && rhs;
14pub const FN_NAND: LogicalGateFunction = |lhs, rhs| !(lhs && rhs);
15pub const FN_NOR: LogicalGateFunction = |lhs, rhs| !(lhs || rhs);
16pub const FN_OR: LogicalGateFunction = |lhs, rhs| lhs || rhs;
17pub const FN_XOR: LogicalGateFunction = |lhs, rhs| lhs ^ rhs;
18pub const FN_XNOR: LogicalGateFunction = |lhs, rhs| !(lhs ^ rhs);
19
20const FACTORY_CREATOR: EntityBehaviourFactoryCreator<LogicalGateFunction> = |ty, f| Arc::new(LogicalGateFactory::new(ty.clone(), f));
21
22pub static LOGICAL_GATES: EntityBehaviourFunctionsStorage<LogicalGateFunction> = LazyLock::new(|| {
23    EntityBehaviourFunctions::<LogicalGateFunction>::with_namespace(NAMESPACE_LOGICAL, FACTORY_CREATOR)
24        .behaviour("and", FN_AND)
25        .behaviour("nand", FN_NAND)
26        .behaviour("nor", FN_NOR)
27        .behaviour("or", FN_OR)
28        .behaviour("xor", FN_XOR)
29        .behaviour("xnor", FN_XNOR)
30        .get()
31});