mod bellman_ford;
mod dispatchable;
mod explanation;
pub(crate) mod floyd_warshall;
mod incremental;
mod minimal;
mod p3c;
use alloc::vec::Vec;
pub use crate::TimePoint;
use crate::dispatch::DispatchableNetwork;
use crate::{Budget, Error, Ticks};
pub use explanation::{Bound, CycleEdge, NegativeCycle};
pub use incremental::IncrementalStn;
pub use minimal::MinimalNetwork;
pub use p3c::ChordalNetwork;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ConstraintId(pub usize);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Constraint {
pub from: TimePoint,
pub to: TimePoint,
pub lo: Option<Ticks>,
pub hi: Option<Ticks>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Verdict<T> {
Consistent(T),
Inconsistent(NegativeCycle),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Schedule {
times: Vec<Ticks>,
}
impl Schedule {
#[must_use]
pub fn time(&self, tp: TimePoint) -> Option<Ticks> {
self.times.get(tp.0).copied()
}
#[must_use]
pub fn times(&self) -> &[Ticks] {
&self.times
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct Edge {
from: usize,
to: usize,
weight: Ticks,
source: CycleEdge,
}
#[derive(Debug, Clone, Default)]
pub struct Stn {
time_points: usize,
constraints: Vec<Constraint>,
edges: Vec<Edge>,
}
impl Stn {
#[must_use]
pub fn new() -> Self {
Self::default()
}
pub fn add_time_point(&mut self) -> Result<TimePoint, Error> {
let tp = TimePoint(self.time_points);
self.time_points = self.time_points.checked_add(1).ok_or(Error::Overflow)?;
Ok(tp)
}
#[must_use]
pub fn time_point_count(&self) -> usize {
self.time_points
}
#[must_use]
pub fn constraints(&self) -> &[Constraint] {
&self.constraints
}
#[must_use]
pub fn constraint(&self, id: ConstraintId) -> Option<&Constraint> {
self.constraints.get(id.0)
}
pub fn add_constraint(
&mut self,
from: TimePoint,
to: TimePoint,
lo: Option<Ticks>,
hi: Option<Ticks>,
) -> Result<ConstraintId, Error> {
if from.0 >= self.time_points || to.0 >= self.time_points {
return Err(Error::UnknownTimePoint);
}
let neg_lo = lo
.map(|l| l.checked_neg().ok_or(Error::Overflow))
.transpose()?;
let id = ConstraintId(self.constraints.len());
if let Some(w) = hi {
self.push_edge(from, to, w, CycleEdge::new(id, Bound::Upper));
}
if let Some(w) = neg_lo {
self.push_edge(to, from, w, CycleEdge::new(id, Bound::Lower));
}
self.constraints.push(Constraint { from, to, lo, hi });
Ok(id)
}
pub fn check_consistency(&self, budget: &Budget) -> Result<Verdict<Schedule>, Error> {
let mut meter = budget.meter();
bellman_ford::run(self, &mut meter)
}
pub fn minimal_network(&self, budget: &Budget) -> Result<Verdict<MinimalNetwork>, Error> {
let mut meter = budget.meter();
if let Some(dist) = floyd_warshall::run(self, budget, &mut meter)? {
return MinimalNetwork::new(self.time_points, dist).map(Verdict::Consistent);
}
match bellman_ford::run(self, &mut meter)? {
Verdict::Inconsistent(cycle) => Ok(Verdict::Inconsistent(cycle)),
Verdict::Consistent(_) => Err(Error::Internal),
}
}
pub fn partial_path_consistency(
&self,
budget: &Budget,
) -> Result<Verdict<ChordalNetwork>, Error> {
let mut meter = budget.meter();
let overflowed = match p3c::run(self, budget, &mut meter) {
Ok(Some(net)) => return Ok(Verdict::Consistent(net)),
Ok(None) => false,
Err(Error::Overflow) => true,
Err(e) => return Err(e),
};
match bellman_ford::run(self, &mut meter)? {
Verdict::Inconsistent(cycle) => Ok(Verdict::Inconsistent(cycle)),
Verdict::Consistent(_) if overflowed => Err(Error::Overflow),
Verdict::Consistent(_) => Err(Error::Internal),
}
}
pub fn dispatchable(&self, budget: &Budget) -> Result<Verdict<DispatchableNetwork>, Error> {
match self.minimal_network(budget)? {
Verdict::Consistent(min) => min.dispatchable(budget).map(Verdict::Consistent),
Verdict::Inconsistent(cycle) => Ok(Verdict::Inconsistent(cycle)),
}
}
fn push_edge(&mut self, from: TimePoint, to: TimePoint, weight: Ticks, source: CycleEdge) {
self.edges.push(Edge {
from: from.0,
to: to.0,
weight,
source,
});
}
}
pub(crate) fn steps(len: usize) -> Result<u64, Error> {
u64::try_from(len).map_err(|_| Error::Overflow)
}