Skip to content

Commit

Permalink
Throw error message if negated literal uses derived variable or compl…
Browse files Browse the repository at this point in the history
…ex term (#461)
  • Loading branch information
Alex Ivliev authored Apr 3, 2024
1 parent 6837c95 commit a7766d2
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 12 deletions.
6 changes: 6 additions & 0 deletions nemo/src/io/parser/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,12 @@ pub enum ParseError {
/// The variable must only depend on variables that occur in a positive body literal.
#[error(r#"the variable "{0}" must only depend on variables that occur in a positive body literals"#)]
UnsafeDefinition(Variable),
/// Negated literal uses a complex term.
#[error(r#"negated literal "{0}" uses the complex term "{1}""#)]
NegatedLiteralComplex(String, String),
/// Negated literal uses a derived variable.
#[error(r#"negated literal "{0}" uses a derived variable "{1}""#)]
NegatedLiteralDerived(String, Variable),
/// Complex term uses a derived variable.
#[error(r#"complex term "{0}" uses a derived variable "{1}""#)]
ComplexTermDerived(String, Variable),
Expand Down
57 changes: 45 additions & 12 deletions nemo/src/model/rule_model/rule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,19 +161,52 @@ impl Rule {
for negative_literal in negative {
let mut current_unsafe = HashSet::<Variable>::new();

for primitive_term in negative_literal.primitive_terms() {
if let PrimitiveTerm::Variable(variable) = primitive_term {
if derived_variables.contains(&variable) || safe_variables.contains(variable) {
continue;
for negative_term in negative_literal.terms() {
if let Term::Primitive(primitive_term) = negative_term {
if let PrimitiveTerm::Variable(variable) = primitive_term {
if safe_variables.contains(variable) {
continue;
}

if derived_variables.contains(variable) {
return Err(ParseError::NegatedLiteralDerived(
negative_literal.to_string(),
variable.clone(),
));
}

if unsafe_negative_variables.contains(variable) {
return Err(ParseError::UnsafeVariableInMultipleNegativeLiterals(
variable.clone(),
));
}
if let PrimitiveTerm::Variable(variable) = primitive_term {
if safe_variables.contains(variable) {
continue;
}

if derived_variables.contains(variable) {
return Err(ParseError::NegatedLiteralDerived(
negative_literal.to_string(),
variable.clone(),
));
}

if unsafe_negative_variables.contains(variable) {
return Err(ParseError::UnsafeVariableInMultipleNegativeLiterals(
variable.clone(),
));
}

current_unsafe.insert(variable.clone());
}
current_unsafe.insert(variable.clone());
}

if unsafe_negative_variables.contains(variable) {
return Err(ParseError::UnsafeVariableInMultipleNegativeLiterals(
variable.clone(),
));
}

current_unsafe.insert(variable.clone());
} else {
return Err(ParseError::NegatedLiteralComplex(
negative_literal.to_string(),
negative_term.to_string(),
));
}
}

Expand Down

0 comments on commit a7766d2

Please sign in to comment.