Skip to content

Commit

Permalink
improved caching, made the code more compact, and removed computation…
Browse files Browse the repository at this point in the history
…s of unnecessary information
  • Loading branch information
raabh committed Aug 19, 2022
1 parent 214cd7e commit 8ae3c39
Show file tree
Hide file tree
Showing 6 changed files with 200 additions and 263 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ddnnf_reasoning"
version = "0.2.0"
version = "0.4.0"
authors = ["Heiko Raab; heiko.raab@uni-ulm-de", "Chico Sundermann; [email protected]"]
edition = "2018"
license = "GNU LGPL-2.1"
Expand Down
6 changes: 4 additions & 2 deletions src/bin/ddnnife.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ fn main() {
println!(
"Ddnnf overall count: {:#?}\n
Elapsed time for parsing and overall count in seconds: {:.3}s.",
ddnnf.nodes[ddnnf.number_of_nodes - 1].count.clone(), elapsed_time
ddnnf.nodes[ddnnf.number_of_nodes - 1].count.clone(),
elapsed_time
);
} else {
ddnnf = dparser::build_ddnnf_tree_with_extras(
Expand All @@ -129,7 +130,8 @@ fn main() {
println!(
"Ddnnf overall count: {:#?}\n
Elapsed time for parsing and overall count in seconds: {:.3}s.",
ddnnf.nodes[ddnnf.number_of_nodes - 1].count.clone(), elapsed_time
ddnnf.nodes[ddnnf.number_of_nodes - 1].count.clone(),
elapsed_time
);
}

Expand Down
4 changes: 2 additions & 2 deletions src/data_structure/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ static mut COUNTER: u64 = 0;
impl Node {
#[inline]
/// Creates a new And node
pub fn new_and(children: Vec<usize>, overall_count: Integer) -> Node {
pub fn new_and(overall_count: Integer, children: Vec<usize>) -> Node {
Node {
marker: false,
count: overall_count,
Expand All @@ -69,8 +69,8 @@ impl Node {
/// Creates a new Or node
pub fn new_or(
var_number: i32,
children: Vec<usize>,
overall_count: Integer,
children: Vec<usize>,
) -> Node {
Node {
marker: false,
Expand Down
64 changes: 18 additions & 46 deletions src/parser/d4_lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use nom::{
branch::alt,
bytes::complete::tag,
character::complete::{char, digit1, space1},
combinator::{map, recognize},
combinator::{map, recognize, value},
multi::many_m_n,
sequence::{pair, preceded, terminated},
IResult,
Expand All @@ -12,13 +12,13 @@ use nom::{
/// Every token gets an enum instance for the D4lexing progress
pub enum D4Token {
/// An inner node that contains atleast one child
And { position: i32 },
/// An inner node that contains exactly two child nodes
Or { position: i32 },
And,
/// An inner node that contains atleast one child
Or,
/// A True node which is the sink of of the DAG
True { position: i32 },
True,
/// A False node can exist, but is rather an exception than the norm
False { position: i32 },
False,
/// An Edge between two nodes, with
Edge {
from: i32,
Expand Down Expand Up @@ -74,50 +74,22 @@ fn neg_digit1(line: &str) -> IResult<&str, &str> {

// Lexes an And node which is a inner node with the format "a N 0" with N as Node number.
fn lex_and(line: &str) -> IResult<&str, D4Token> {
map(preceded(tag("a "), digit1), |out: &str| D4Token::And {
position: out.parse::<i32>().unwrap_or_else(|_| {
panic!(
"Was not able to parse i32 after \"a \". String was :{}",
out
)
}),
})(line)
value(D4Token::And, preceded(tag("a "), digit1))(line)
}

// Lexes an Or node which is a inner node with the format "o N 0" with N as Node number.
fn lex_or(line: &str) -> IResult<&str, D4Token> {
map(preceded(tag("o "), digit1), |out: &str| D4Token::Or {
position: out.parse::<i32>().unwrap_or_else(|_| {
panic!(
"Was not able to parse i32 after \"o \". String was: {}",
out
)
}),
})(line)
value(D4Token::Or, preceded(tag("o "), digit1))(line)
}

// Lexes a True node which is a leaf node with the format "t N 0" with N as Node number.
fn lex_true(line: &str) -> IResult<&str, D4Token> {
map(preceded(tag("t "), digit1), |out: &str| D4Token::True {
position: out.parse::<i32>().unwrap_or_else(|_| {
panic!(
"Was not able to parse i32 after \"t \". String was: {}",
out
)
}),
})(line)
value(D4Token::True, preceded(tag("t "), digit1))(line)
}

// Lexes a False node which is a leaf node with the format "f N 0" with N as Node number.
fn lex_false(line: &str) -> IResult<&str, D4Token> {
map(preceded(tag("f "), digit1), |out: &str| D4Token::False {
position: out.parse::<i32>().unwrap_or_else(|_| {
panic!(
"Was not able to parse i32 after \"f \". String was: {}",
out
)
}),
})(line)
value(D4Token::False, preceded(tag("f "), digit1))(line)
}

#[cfg(test)]
Expand All @@ -132,33 +104,33 @@ mod test {
let false_str = "f 4 0";
let edge_str = "2 3 4 -5 0";

assert_eq!(lex_and(and_str).unwrap().1, D4Token::And { position: 1 },);
assert_eq!(lex_and(and_str).unwrap().1, D4Token::And);
assert_eq!(
lex_line_d4(and_str).unwrap().1,
D4Token::And { position: 1 },
D4Token::And,
);

assert_eq!(lex_or(or_str).unwrap().1, D4Token::Or { position: 2 },);
assert_eq!(lex_line_d4(or_str).unwrap().1, D4Token::Or { position: 2 },);
assert_eq!(lex_or(or_str).unwrap().1, D4Token::Or);
assert_eq!(lex_line_d4(or_str).unwrap().1, D4Token::Or);

assert_eq!(
lex_true(true_str).unwrap().1,
D4Token::True { position: 3 },
D4Token::True,
);

assert_eq!(
lex_line_d4(true_str).unwrap().1,
D4Token::True { position: 3 },
D4Token::True,
);

assert_eq!(
lex_false(false_str).unwrap().1,
D4Token::False { position: 4 },
D4Token::False,
);

assert_eq!(
lex_line_d4(false_str).unwrap().1,
D4Token::False { position: 4 },
D4Token::False,
);

assert_eq!(
Expand Down
Loading

0 comments on commit 8ae3c39

Please sign in to comment.