use crate::formulas::EncodedFormula;
use crate::handlers::handler::ComputationHandler;
pub trait FactorizationHandler: ComputationHandler {
fn performed_distribution(&mut self) -> Result<(), FactorizationError> {
Ok(())
}
fn created_clause(&mut self, _clause: EncodedFormula) -> Result<(), FactorizationError> {
Ok(())
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FactorizationError {
ClauseLimitReached,
DistributionLimitReached,
}
pub struct NopFactorizationHandler {}
impl ComputationHandler for NopFactorizationHandler {}
impl FactorizationHandler for NopFactorizationHandler {}
pub struct ClauseLimitFactorizationHandler {
pub aborted: bool,
pub dists: u64,
pub clauses: u64,
dists_limit: u64,
clauses_limit: u64,
}
impl ClauseLimitFactorizationHandler {
pub const fn new(dists_limit: u64, clauses_limit: u64) -> Self {
Self { aborted: false, dists: 0, clauses: 0, dists_limit, clauses_limit }
}
}
impl ComputationHandler for ClauseLimitFactorizationHandler {
fn started(&mut self) {
self.aborted = false;
self.dists = 0;
self.clauses = 0;
}
fn aborted(&self) -> bool {
self.aborted
}
}
impl FactorizationHandler for ClauseLimitFactorizationHandler {
fn performed_distribution(&mut self) -> Result<(), FactorizationError> {
self.dists += 1;
self.aborted = self.dists > self.dists_limit;
if self.aborted {
Err(FactorizationError::DistributionLimitReached)
} else {
Ok(())
}
}
fn created_clause(&mut self, _clause: EncodedFormula) -> Result<(), FactorizationError> {
self.clauses += 1;
self.aborted = self.clauses > self.clauses_limit;
if self.aborted {
Err(FactorizationError::ClauseLimitReached)
} else {
Ok(())
}
}
}