use serde::{Deserialize, Serialize};
use shifty_algebra::{Shape, ShapeArena, ShapeId};
#[derive(Clone, Copy, PartialEq, Eq, Debug, Serialize, Deserialize)]
pub enum Polarity {
Positive,
Negative,
}
impl Polarity {
pub fn sign(self) -> i8 {
match self {
Polarity::Positive => 1,
Polarity::Negative => -1,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct DepEdge {
pub from: ShapeId,
pub to: ShapeId,
pub polarity: Polarity,
}
pub fn dependency_edges(arena: &ShapeArena) -> Vec<DepEdge> {
let mut edges = Vec::new();
for i in 0..arena.len() {
let from = ShapeId(i as u32);
match arena.get(from) {
Shape::Not(c) => edges.push(DepEdge {
from,
to: *c,
polarity: Polarity::Negative,
}),
Shape::And(cs) | Shape::Or(cs) => {
for c in cs {
edges.push(DepEdge {
from,
to: *c,
polarity: Polarity::Positive,
});
}
}
Shape::Count {
min,
max,
qualifier,
..
} => {
if min.is_some() {
edges.push(DepEdge {
from,
to: *qualifier,
polarity: Polarity::Positive,
});
}
if max.is_some() {
edges.push(DepEdge {
from,
to: *qualifier,
polarity: Polarity::Negative,
});
}
}
_ => {}
}
}
edges
}