From 2ae350021a562c2a2ae24d3d906ef0e123b843bb Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Mon, 5 Sep 2022 11:27:26 +0300 Subject: [PATCH] Migrated DistributiveLaw to jbool implementation (#1513) --- .../rules/chapter3/BooleanExpressionsRule.kt | 77 +------------------ 1 file changed, 1 insertion(+), 76 deletions(-) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/BooleanExpressionsRule.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/BooleanExpressionsRule.kt index 6b662dd7e3..10b4e0394e 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/BooleanExpressionsRule.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/BooleanExpressionsRule.kt @@ -6,14 +6,12 @@ import org.cqfn.diktat.ruleset.rules.DiktatRule import org.cqfn.diktat.ruleset.utils.KotlinParser import org.cqfn.diktat.ruleset.utils.findAllNodesWithCondition import org.cqfn.diktat.ruleset.utils.logicalInfixMethods -import com.bpodgursky.jbool_expressions.And import com.bpodgursky.jbool_expressions.Expression -import com.bpodgursky.jbool_expressions.NExpression -import com.bpodgursky.jbool_expressions.Or import com.bpodgursky.jbool_expressions.options.ExprOptions import com.bpodgursky.jbool_expressions.parsers.ExprParser import com.bpodgursky.jbool_expressions.parsers.TokenMapper import com.bpodgursky.jbool_expressions.rules.DeMorgan +import com.bpodgursky.jbool_expressions.rules.DistributiveLaw import com.bpodgursky.jbool_expressions.rules.Rule import com.bpodgursky.jbool_expressions.rules.RuleList import com.bpodgursky.jbool_expressions.rules.RulesHelper @@ -29,8 +27,6 @@ import org.jetbrains.kotlin.psi.KtParenthesizedExpression import org.jetbrains.kotlin.psi.KtPrefixExpression import org.jetbrains.kotlin.psi.psiUtil.parents -typealias ExpressionCreator = (List?>) -> Expression - /** * Rule that checks if the boolean expression can be simplified. */ @@ -258,77 +254,6 @@ class BooleanExpressionsRule(configRules: List) : DiktatRule( } } - /** - * Rule that checks that the expression can be simplified by distributive law. - * Distributive law - A && B || A && C -> A && (B || C) or (A || B) && (A || C) -> A || (B && C) - */ - @Suppress("UnsafeCallOnNullableType") - private class DistributiveLaw : Rule, K>() { - override fun applyInternal(input: NExpression, options: ExprOptions): Expression { - val exprFactory = options.exprFactory!! - val orExpressionCreator: ExpressionCreator = { expressions -> exprFactory.or(expressions.toTypedArray()) } - val andExpressionCreator: ExpressionCreator = { expressions -> exprFactory.and(expressions.toTypedArray()) } - return when (input) { - is And -> applyInternal(input, orExpressionCreator, andExpressionCreator) - is Or -> applyInternal(input, andExpressionCreator, orExpressionCreator) - else -> throw UnsupportedOperationException("Not supported input expression: ${input.exprType}") - } - } - - private fun applyInternal( - input: NExpression, - upperExpressionCreator: ExpressionCreator, - innerExpressionCreator: ExpressionCreator - ): Expression { - // we can be here only after `isApply` -- common exists - val commonExpression = findCommonExpression(input.children)!! - return upperExpressionCreator( - listOf(commonExpression, - innerExpressionCreator( - input.expressions.map { excludeChild(it, upperExpressionCreator, commonExpression) } - ))) - } - - private fun excludeChild( - expression: Expression, - expressionCreator: ExpressionCreator, - childToExclude: Expression - ): Expression { - val leftChildren = expression.children.filterNot { it.equals(childToExclude) } - return if (leftChildren.size == 1) { - leftChildren.first() - } else { - expressionCreator(leftChildren) - } - } - - /** - * Checks the input expression - */ - override fun isApply(inputNullable: Expression?): Boolean = inputNullable?.let { input -> - when (input) { - is And -> isApplicable, Or>(input) - is Or -> isApplicable, And>(input) - else -> false - } - } ?: false - - private inline fun , reified C : NExpression> isApplicable(input: E): Boolean { - val children = input.children ?: return false - if (children.size < 2 || children.any { it !is C }) { - return false - } - return findCommonExpression(children) != null - } - - private fun findCommonExpression(children: List>): Expression? = children.drop(1) - .fold(children[0].children) { commons, child -> - commons.filter { childResult -> - child.children.any { it.equals(childResult) } - } - }.firstOrNull() - } - companion object { const val NAME_ID = "boolean-expressions-rule"