1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
use NodeType::{And, False, Literal, Or, True};
use num::BigInt;
use std::fmt::{Display, Formatter};
/// Represents all types of Nodes with its different parts
#[derive(Debug, Clone, PartialEq)]
pub struct Node {
pub(crate) marker: bool,
/// The cardinality of the node for the cardinality of a feature model
pub count: BigInt,
/// The cardinality during the different queries
pub temp: BigInt,
/// The cardinality during the different queries
pub partial_derivative: BigInt,
/// Every node excpet the root has (multiple) parent nodes
pub(crate) parents: Vec<usize>,
/// the different kinds of nodes with its additional fields
pub ntype: NodeType,
}
/// The Type of the Node declares how we handle the computation for the different types of cardinalities
#[derive(Debug, Clone, PartialEq)]
pub enum NodeType {
/// The cardinality of an And node is always the product of its childs
And { children: Vec<usize> },
/// The cardinality of an Or node is the sum of its children
Or { children: Vec<usize> },
/// The cardinality is one if not declared otherwise due to some query
Literal { literal: i32 },
/// The cardinality is one
True,
/// The cardinality is zero
False,
}
impl Node {
#[inline]
/// Creates a new node
fn new_node(count: BigInt, ntype: NodeType) -> Node {
Node {
marker: false,
count,
temp: BigInt::ZERO,
partial_derivative: BigInt::ZERO,
parents: Vec::new(),
ntype,
}
}
#[inline]
/// Creates a new And node
pub fn new_and(count: BigInt, children: Vec<usize>) -> Node {
Node::new_node(count, And { children })
}
#[inline]
/// Creates a new Or node
pub fn new_or(_decision_var: u32, count: BigInt, children: Vec<usize>) -> Node {
Node::new_node(count, Or { children })
}
#[inline]
/// Creates a new Literal node
pub fn new_literal(literal: i32) -> Node {
Node::new_node(BigInt::from(1), Literal { literal })
}
#[inline]
/// Creates either a new True or False node
pub fn new_bool(b: bool) -> Node {
if b {
Node::new_node(BigInt::from(1), True)
} else {
Node::new_node(BigInt::ZERO, False)
}
}
}
impl Display for Node {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match &self.ntype {
NodeType::And { children } => {
write!(f, "A ")?;
deconstruct_children(f, children)
}
NodeType::Or { children } => {
write!(f, "O 0 ")?;
deconstruct_children(f, children)
}
NodeType::Literal { literal } => write!(f, "L {literal}"),
NodeType::True => write!(f, "A 0"),
NodeType::False => write!(f, "O 0 0"),
}
}
}
fn deconstruct_children(f: &mut Formatter<'_>, children: &[usize]) -> std::fmt::Result {
write!(f, "{} ", &children.len().to_string())?;
children.iter().enumerate().try_for_each(|(i, child)| {
write!(f, "{child}")?;
if i < children.len() - 1 {
write!(f, " ")?;
}
Ok(())
})
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn build_nodes() {
assert_eq!(
Node::new_and(BigInt::from(42), vec![1, 5, 10]),
Node {
marker: false,
count: BigInt::from(42),
temp: BigInt::ZERO,
partial_derivative: BigInt::ZERO,
parents: vec![],
ntype: And {
children: vec![1, 5, 10]
}
}
);
assert_eq!(
Node::new_node(
BigInt::from(42),
And {
children: vec![1, 5, 10]
}
),
Node {
marker: false,
count: BigInt::from(42),
temp: BigInt::ZERO,
partial_derivative: BigInt::ZERO,
parents: vec![],
ntype: And {
children: vec![1, 5, 10]
}
}
);
assert_eq!(
Node::new_or(42, BigInt::from(42), vec![1, 5, 10]),
Node {
marker: false,
count: BigInt::from(42),
temp: BigInt::ZERO,
partial_derivative: BigInt::ZERO,
parents: vec![],
ntype: Or {
children: vec![1, 5, 10]
}
}
);
assert_eq!(
Node::new_literal(42),
Node {
marker: false,
count: BigInt::from(1),
temp: BigInt::ZERO,
partial_derivative: BigInt::ZERO,
parents: vec![],
ntype: Literal { literal: 42 }
}
);
assert_eq!(
Node::new_bool(true),
Node {
marker: false,
count: BigInt::from(1),
temp: BigInt::ZERO,
partial_derivative: BigInt::ZERO,
parents: vec![],
ntype: True
}
);
assert_eq!(
Node::new_bool(false),
Node {
marker: false,
count: BigInt::ZERO,
temp: BigInt::ZERO,
partial_derivative: BigInt::ZERO,
parents: vec![],
ntype: False
}
);
}
}