Skip to content

Commit 7ebe24e

Browse files
committed
cond
1 parent c749e5b commit 7ebe24e

4 files changed

Lines changed: 45 additions & 27 deletions

File tree

src/builder/bdd/robdd.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,11 @@ impl<'a, T: IteTable<'a, BddPtr<'a>> + Default> BddBuilder<'a> for RobddBuilder<
8484

8585

8686
fn cond_helper(&'a self, bdd: BddPtr<'a>, lbl: VarLabel, value: bool) -> BddPtr<'a> {
87-
bdd.clear_scratch();
87+
// bdd.clear_scratch();
8888
let r = self.cond_with_alloc(bdd, lbl, value, &mut HashMap::new());
8989
// outer call clears the scratch. very messy.
90-
bdd.clear_scratch();
91-
r.clear_scratch();
90+
// bdd.clear_scratch();
91+
// r.clear_scratch();
9292
r
9393
}
9494
}

src/repr/bdd.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ impl<'a> BddPtr<'a> {
403403
};
404404
let l_s = print_bdd_helper(l_p);
405405
let h_s = print_bdd_helper(h_p);
406-
format!("({}, scratch: {:?}, {}, {})", node.var.value(), node.data, h_s, l_s)
406+
format!("({}, {}, {})", node.var.value(), h_s, l_s)
407407
}
408408
}
409409
}

src/repr/cnf.rs

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,6 @@ impl Cnf {
265265
pub fn new(clauses: &[Vec<Literal>]) -> Cnf {
266266
let clauses: Vec<Vec<Literal>> = clauses
267267
.iter()
268-
.filter(|clause| !clause.is_empty())
269268
.map(|clause| {
270269
let mut clause = clause.clone();
271270
clause.sort_by_key(|a| a.label().value());
@@ -552,30 +551,24 @@ impl Cnf {
552551
}
553552

554553
/// Updates the CNF to a new CNF that results from conditioning on the supplied literal
555-
pub fn condition(&mut self, lit: Literal) -> Cnf {
556-
let new_cnf: Vec<Vec<Literal>> = self
557-
.clauses()
558-
.iter()
559-
.filter_map(|clause| {
560-
// first, check if there is a true literal -- if there is, filter out this clause
561-
if clause
562-
.iter()
563-
.any(|outer| outer.label() == lit.label() && outer.polarity() == lit.polarity())
564-
{
565-
None
554+
pub fn condition(&self, lit: Literal) -> Cnf {
555+
let mut new_cnf : Vec<Vec<Literal>> = Vec::new();
556+
'cnf: for clause in self.clauses.iter() {
557+
let mut new_clause = Vec::new();
558+
'clause: for l in clause.iter() {
559+
if l.label() == lit.label() && l.polarity() == lit.polarity() {
560+
// skip over this whole clause
561+
continue 'cnf
562+
} else if l.label() == lit.label() && l.polarity() != lit.polarity() {
563+
// skip over this literal
564+
continue 'clause
566565
} else {
567-
// next, filter out clauses with false literals
568-
let filtered: Vec<Literal> = clause
569-
.iter()
570-
.filter(|outer| {
571-
!(lit.label() == outer.label() && lit.polarity() != outer.polarity())
572-
})
573-
.copied()
574-
.collect();
575-
Some(filtered)
566+
// push the literal
567+
new_clause.push(*l);
576568
}
577-
})
578-
.collect();
569+
}
570+
new_cnf.push(new_clause);
571+
}
579572
Cnf::new(&new_cnf)
580573
}
581574

@@ -730,3 +723,12 @@ fn test_cnf_wmc() {
730723
]);
731724
assert_eq!(cnf.wmc(&WmcParams::new(weights)), FiniteField::new(3));
732725
}
726+
727+
#[test]
728+
fn test_cond() {
729+
let v = vec![vec![
730+
Literal::new(VarLabel::new(0), false),
731+
]];
732+
let cnf = Cnf::new(&v);
733+
println!("{:?}", cnf.condition(Literal::new(VarLabel::new(0), true)));
734+
}

tests/test.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ mod test_bdd_builder {
283283
use rsdd::repr::BddPtr;
284284
use rsdd::repr::Cnf;
285285
use rsdd::repr::DTree;
286+
use rsdd::repr::Literal;
286287
use rsdd::repr::PartialModel;
287288
use rsdd::repr::VTree;
288289
use rsdd::repr::VarLabel;
@@ -311,6 +312,21 @@ mod test_bdd_builder {
311312
}
312313
}
313314

315+
quickcheck! {
316+
fn test_cond(c: Cnf, i : u64) -> bool {
317+
let builder = super::RobddBuilder::<AllIteTable<BddPtr>>::new_with_linear_order(16);
318+
let cnf = builder.compile_cnf(&c);
319+
let cond_var = VarLabel::new(i % 4);
320+
let cnf_cond = c.condition(Literal::new(cond_var, true));
321+
let bdd1 = builder.condition(cnf, cond_var, true);
322+
let bdd2 = builder.compile_cnf(&cnf_cond);
323+
println!("cnf: {}\ncond var: {:?}\ncond cnf: {}\ncond cnf debug: {:?}\ncond bdd: {}\ncond cnf bdd: {}\neq? {}\n",
324+
c, cond_var, cnf_cond, cnf_cond, bdd1.to_string_debug(), bdd2.to_string_debug(), bdd1 == bdd2);
325+
bdd1 == bdd2
326+
}
327+
}
328+
329+
314330
quickcheck! {
315331
/// check that every node in the BDD has a unique semantic hash
316332
fn qc_bdd_canonicity(c1: Cnf) -> TestResult {

0 commit comments

Comments
 (0)