Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(SpdxExpression): Fix the DNF algorithm #6721

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 18 additions & 13 deletions utils/spdx/src/main/kotlin/SpdxExpression.kt
Original file line number Diff line number Diff line change
Expand Up @@ -253,22 +253,25 @@ class SpdxCompoundExpression(
val rightDnf = right.disjunctiveNormalForm()

return when (operator) {
SpdxOperator.OR -> SpdxCompoundExpression(leftDnf, SpdxOperator.OR, rightDnf)

SpdxOperator.AND -> when {
leftDnf is SpdxCompoundExpression && leftDnf.operator == SpdxOperator.OR &&
rightDnf is SpdxCompoundExpression && rightDnf.operator == SpdxOperator.OR ->
((leftDnf.left and rightDnf.left) or (leftDnf.left and rightDnf.right)) or
((leftDnf.right and rightDnf.left) or (leftDnf.right and rightDnf.right))

leftDnf is SpdxCompoundExpression && leftDnf.operator == SpdxOperator.OR ->
(leftDnf.left and rightDnf) or (leftDnf.right and rightDnf)
SpdxOperator.OR -> leftDnf or rightDnf
SpdxOperator.AND -> distributeAndOverOr(leftDnf, rightDnf)
}
}

rightDnf is SpdxCompoundExpression && rightDnf.operator == SpdxOperator.OR ->
(leftDnf and rightDnf.left) or (leftDnf and rightDnf.right)
/**
* Distribute the AND operator over the OR operator e.g. a AND (b OR c) to (a AND b) OR (a AND c).
*/
private fun distributeAndOverOr(left: SpdxExpression, right: SpdxExpression): SpdxExpression {
return when {
left is SpdxCompoundExpression && left.operator == SpdxOperator.OR -> {
distributeAndOverOr(left.left, right) or distributeAndOverOr(left.right, right)
}

else -> SpdxCompoundExpression(leftDnf, operator, rightDnf)
right is SpdxCompoundExpression && right.operator == SpdxOperator.OR -> {
distributeAndOverOr(left, right.left) or distributeAndOverOr(left, right.right)
}

else -> left and right
}
}

Expand Down Expand Up @@ -674,3 +677,5 @@ enum class SpdxOperator(
*/
OR(0)
}

enum class Test { AND, OR }
29 changes: 25 additions & 4 deletions utils/spdx/src/test/kotlin/SpdxExpressionTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -378,16 +378,27 @@ class SpdxExpressionTest : WordSpec() {
}

"correctly convert an OR on the left side of an AND expression" {
"(a OR b) AND c".toSpdx().disjunctiveNormalForm() should beString("(a AND c) OR (b AND c)")
"(a OR b OR c) AND d".toSpdx().disjunctiveNormalForm() should
beString("(a AND d) OR (b AND d) OR (c AND d)")
}

"correctly convert an OR on the right side of an AND expression" {
"a AND (b OR c)".toSpdx().disjunctiveNormalForm() should beString("(a AND b) OR (a AND c)")
"a AND (b OR c OR d)".toSpdx().disjunctiveNormalForm() should
beString("(a AND b) OR (a AND c) OR (a AND d)")
}

"correctly convert ORs on both sides of an AND expression" {
"(a OR b) AND (c OR d)".toSpdx().disjunctiveNormalForm() should
beString("(a AND c) OR (a AND d) OR (b AND c) OR (b AND d)")
"(a OR b OR c) AND (d OR e OR f)".toSpdx().disjunctiveNormalForm() should beString(
"(a AND d) OR " +
"(a AND e) OR " +
"(a AND f) OR " +
"(b AND d) OR " +
"(b AND e) OR " +
"(b AND f) OR " +
"(c AND d) OR " +
"(c AND e) OR " +
"(c AND f)"
)
}

"correctly convert a complex expression" {
Expand Down Expand Up @@ -424,6 +435,16 @@ class SpdxExpressionTest : WordSpec() {
)
}

"return the correct choices for a mixed expression" {
val spdxExpression = "a AND (a OR b OR c)".toSpdx()

spdxExpression.validChoices() should containExactlyInAnyOrder(
"a".toSpdx(),
"a AND b".toSpdx(),
"a AND c".toSpdx()
)
}

"not contain a duplicate valid choice for a simple expression" {
"a AND a".toSpdx().validChoices() should containExactly("a".toSpdx())
}
Expand Down