Skip to content

Commit

Permalink
refac: add tabled to pretty-print models
Browse files Browse the repository at this point in the history
  • Loading branch information
jsfpdn committed Dec 15, 2024
1 parent d96be8a commit d064b74
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
1 change: 1 addition & 0 deletions sdd-rs-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ clap = { version = "4.5.4", features = ["derive"] }
bon = "3"
rustc-hash = "2.1"
anyhow = "1.0"
tabled = "0.17.0"

[dev-dependencies]
pretty_assertions = "1.4.1"
Expand Down
25 changes: 16 additions & 9 deletions sdd-rs-lib/manager/model.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::literal::{Literal, Polarity, Variable};
use bitvec::prelude::*;
use std::fmt::Display;
use tabled::{builder::Builder, grid::config::HorizontalLine, settings::Theme};

/// All models of the knowledge base.
#[derive(Debug, PartialEq)]
Expand Down Expand Up @@ -30,15 +31,21 @@ impl Models {

impl Display for Models {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{{\n{}\n}}",
self.all_models()
.iter()
.map(|model| format!(" {model}"))
.collect::<Vec<String>>()
.join(",\n")
)
let mut builder = Builder::default();
builder.push_record(self.variables.iter().map(|v| v.label()));

for model in &self.models {
builder.push_record(
model
.iter()
.map(|assignment| if *assignment { "1" } else { "0" }),
);
}

let mut style = Theme::default();
style.insert_horizontal_line(1, HorizontalLine::full('-', '-', ' ', ' '));
let output = builder.build().with(style).to_string();
write!(f, "{output}")
}
}

Expand Down

0 comments on commit d064b74

Please sign in to comment.