Skip to content

Commit

Permalink
feat(spdx-utils): Make simplify() remove redundant choices
Browse files Browse the repository at this point in the history
Extract flattening to a function and reuse it to either flatten an only
choice, or the original expression.

Signed-off-by: Sebastian Schuberth <[email protected]>
  • Loading branch information
sschuberth committed Dec 18, 2024
1 parent c208a15 commit 8965839
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
10 changes: 8 additions & 2 deletions utils/spdx/src/main/kotlin/SpdxExpression.kt
Original file line number Diff line number Diff line change
Expand Up @@ -249,13 +249,13 @@ class SpdxCompoundExpression(
override fun normalize(mapDeprecated: Boolean) =
SpdxCompoundExpression(operator, children.map { it.normalize(mapDeprecated) })

override fun simplify(): SpdxExpression {
private fun flatten(): SpdxExpression {
val flattenedChildren = children.flatMapTo(mutableSetOf()) { child ->
val simplifiedChild = child.simplify()

if (simplifiedChild is SpdxCompoundExpression && simplifiedChild.operator == operator) {
// Inline nested children of the same operator.
simplifiedChild.children.map { it.simplify() }
simplifiedChild.children.map { if (it is SpdxCompoundExpression) it.flatten() else it }
} else {
setOf(simplifiedChild)
}
Expand All @@ -264,6 +264,12 @@ class SpdxCompoundExpression(
return flattenedChildren.singleOrNull() ?: SpdxCompoundExpression(operator, flattenedChildren)
}

override fun simplify(): SpdxExpression =
// Eliminate redundant choices by creating the set of unique choices and using the choice if there is only one.
validChoices().singleOrNull()?.let {
if (it is SpdxCompoundExpression) it.flatten() else it
} ?: flatten()

override fun sorted(): SpdxExpression {
/**
* Get all transitive children of this expression that are concatenated with the same operator as this compound
Expand Down
21 changes: 21 additions & 0 deletions utils/spdx/src/test/kotlin/SpdxCompoundExpressionTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,26 @@ class SpdxCompoundExpressionTest : WordSpec({
// Compare string representations to not rely on semantic equality.
expression.simplify().toString() shouldBe "MIT"
}

"remove redundant choices with a nested expression at the beginning" {
val expression = "(Apache-2.0 OR MIT) AND MIT AND Apache-2.0 AND Apache-2.0 AND MIT".toSpdx()

// Compare string representations to not rely on semantic equality.
expression.simplify().sorted().toString() shouldBe "Apache-2.0 AND MIT"
}

"remove redundant choices with a nested expression in the middle" {
val expression = "Apache-2.0 AND (Apache-2.0 OR MIT) AND MIT AND MIT".toSpdx()

// Compare string representations to not rely on semantic equality.
expression.simplify().sorted().toString() shouldBe "Apache-2.0 AND MIT"
}

"remove redundant choices with a nested expression at the end" {
val expression = "MIT AND Apache-2.0 AND Apache-2.0 AND MIT AND (Apache-2.0 OR MIT)".toSpdx()

// Compare string representations to not rely on semantic equality.
expression.simplify().sorted().toString() shouldBe "Apache-2.0 AND MIT"
}
}
})

0 comments on commit 8965839

Please sign in to comment.