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
use std::{fmt, error::Error};
use crate::{ID, Context, ExclusivelyContextual, InContext, Atomic, sat, error::AcesError};

/// An identifier of a single node used in c-e structures.
///
/// In line with the theory, the set of nodes is a shared resource
/// common to all c-e structures.  On the other hand, properties of a
/// node depend on a particular c-e structure, visualization method,
/// etc.
///
/// Therefore, there is no type `Node` in _aces_.  Instead, structural
/// information is stored in [`CEStructure`] objects and accessed
/// through structural identifiers, [`PortID`], [`LinkID`], [`ForkID`]
/// and [`JoinID`].  Remaining node-related data is retrieved through
/// `NodeID`s from [`Context`] instances (many such instances may
/// coexist in the program).
///
/// [`PortID`]: crate::PortID
/// [`LinkID`]: crate::LinkID
/// [`ForkID`]: crate::ForkID
/// [`JoinID`]: crate::JoinID
/// [`CEStructure`]: crate::CEStructure
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
#[repr(transparent)]
pub struct NodeID(pub(crate) ID);

impl NodeID {
    #[inline]
    pub const fn get(self) -> ID {
        self.0
    }
}

impl From<ID> for NodeID {
    fn from(id: ID) -> Self {
        NodeID(id)
    }
}

impl From<NodeID> for ID {
    fn from(id: NodeID) -> Self {
        id.0
    }
}

impl ExclusivelyContextual for NodeID {
    fn format_locked(&self, ctx: &Context) -> Result<String, Box<dyn Error>> {
        let name = ctx.get_node_name(*self).ok_or(AcesError::NodeMissingForID)?;
        Ok(name.to_owned())
    }
}

impl Atomic for NodeID {
    fn into_node_id(this: InContext<Self>) -> Option<NodeID> {
        Some(*this.get_thing())
    }

    fn into_sat_literal(self, negated: bool) -> sat::Literal {
        sat::Literal::from_atom_id(self.get(), negated)
    }
}

#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub enum Face {
    Tx,
    Rx,
}

impl std::ops::Not for Face {
    type Output = Face;

    fn not(self) -> Self::Output {
        match self {
            Face::Tx => Face::Rx,
            Face::Rx => Face::Tx,
        }
    }
}

impl fmt::Display for Face {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Face::Tx => write!(f, ">"),
            Face::Rx => write!(f, "<"),
        }
    }
}

/// A maximum number of tokens a node may hold.
///
/// This is the type of values of the function _cap<sub>U</sub>_,
/// which maps nodes in the carrier of a c-e structure _U_ to their
/// capacities.  Valid capacities are positive integers or _&omega;_
/// (infinite capacity).  The default value is 1.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]
#[repr(transparent)]
pub struct Capacity(u64);

impl Capacity {
    pub fn new_omega() -> Self {
        Capacity(0)
    }

    pub fn new_finite(value: u64) -> Option<Self> {
        if value > 0 {
            Some(Capacity(value))
        } else {
            None
        }
    }

    pub fn is_omega(self) -> bool {
        self.0 == 0
    }

    pub fn is_finite(self) -> bool {
        self.0 > 0
    }
}

impl Default for Capacity {
    fn default() -> Self {
        Capacity(1)
    }
}