Skip to content

Commit 2ae3500

Browse files
authored
Migrated DistributiveLaw to jbool implementation (#1513)
1 parent b5ef51d commit 2ae3500

File tree

1 file changed

+1
-76
lines changed

1 file changed

+1
-76
lines changed

diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/BooleanExpressionsRule.kt

+1-76
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,12 @@ import org.cqfn.diktat.ruleset.rules.DiktatRule
66
import org.cqfn.diktat.ruleset.utils.KotlinParser
77
import org.cqfn.diktat.ruleset.utils.findAllNodesWithCondition
88
import org.cqfn.diktat.ruleset.utils.logicalInfixMethods
9-
import com.bpodgursky.jbool_expressions.And
109
import com.bpodgursky.jbool_expressions.Expression
11-
import com.bpodgursky.jbool_expressions.NExpression
12-
import com.bpodgursky.jbool_expressions.Or
1310
import com.bpodgursky.jbool_expressions.options.ExprOptions
1411
import com.bpodgursky.jbool_expressions.parsers.ExprParser
1512
import com.bpodgursky.jbool_expressions.parsers.TokenMapper
1613
import com.bpodgursky.jbool_expressions.rules.DeMorgan
14+
import com.bpodgursky.jbool_expressions.rules.DistributiveLaw
1715
import com.bpodgursky.jbool_expressions.rules.Rule
1816
import com.bpodgursky.jbool_expressions.rules.RuleList
1917
import com.bpodgursky.jbool_expressions.rules.RulesHelper
@@ -29,8 +27,6 @@ import org.jetbrains.kotlin.psi.KtParenthesizedExpression
2927
import org.jetbrains.kotlin.psi.KtPrefixExpression
3028
import org.jetbrains.kotlin.psi.psiUtil.parents
3129

32-
typealias ExpressionCreator<K> = (List<Expression<K>?>) -> Expression<K>
33-
3430
/**
3531
* Rule that checks if the boolean expression can be simplified.
3632
*/
@@ -258,77 +254,6 @@ class BooleanExpressionsRule(configRules: List<RulesConfig>) : DiktatRule(
258254
}
259255
}
260256

261-
/**
262-
* Rule that checks that the expression can be simplified by distributive law.
263-
* Distributive law - A && B || A && C -> A && (B || C) or (A || B) && (A || C) -> A || (B && C)
264-
*/
265-
@Suppress("UnsafeCallOnNullableType")
266-
private class DistributiveLaw<K> : Rule<NExpression<K>, K>() {
267-
override fun applyInternal(input: NExpression<K>, options: ExprOptions<K>): Expression<K> {
268-
val exprFactory = options.exprFactory!!
269-
val orExpressionCreator: ExpressionCreator<K> = { expressions -> exprFactory.or(expressions.toTypedArray()) }
270-
val andExpressionCreator: ExpressionCreator<K> = { expressions -> exprFactory.and(expressions.toTypedArray()) }
271-
return when (input) {
272-
is And -> applyInternal(input, orExpressionCreator, andExpressionCreator)
273-
is Or -> applyInternal(input, andExpressionCreator, orExpressionCreator)
274-
else -> throw UnsupportedOperationException("Not supported input expression: ${input.exprType}")
275-
}
276-
}
277-
278-
private fun applyInternal(
279-
input: NExpression<K>,
280-
upperExpressionCreator: ExpressionCreator<K>,
281-
innerExpressionCreator: ExpressionCreator<K>
282-
): Expression<K> {
283-
// we can be here only after `isApply` -- common exists
284-
val commonExpression = findCommonExpression(input.children)!!
285-
return upperExpressionCreator(
286-
listOf(commonExpression,
287-
innerExpressionCreator(
288-
input.expressions.map { excludeChild(it, upperExpressionCreator, commonExpression) }
289-
)))
290-
}
291-
292-
private fun excludeChild(
293-
expression: Expression<K>,
294-
expressionCreator: ExpressionCreator<K>,
295-
childToExclude: Expression<K>
296-
): Expression<K> {
297-
val leftChildren = expression.children.filterNot { it.equals(childToExclude) }
298-
return if (leftChildren.size == 1) {
299-
leftChildren.first()
300-
} else {
301-
expressionCreator(leftChildren)
302-
}
303-
}
304-
305-
/**
306-
* Checks the input expression
307-
*/
308-
override fun isApply(inputNullable: Expression<K>?): Boolean = inputNullable?.let { input ->
309-
when (input) {
310-
is And -> isApplicable<And<K>, Or<K>>(input)
311-
is Or -> isApplicable<Or<K>, And<K>>(input)
312-
else -> false
313-
}
314-
} ?: false
315-
316-
private inline fun <E : NExpression<K>, reified C : NExpression<K>> isApplicable(input: E): Boolean {
317-
val children = input.children ?: return false
318-
if (children.size < 2 || children.any { it !is C }) {
319-
return false
320-
}
321-
return findCommonExpression(children) != null
322-
}
323-
324-
private fun findCommonExpression(children: List<Expression<K>>): Expression<K>? = children.drop(1)
325-
.fold(children[0].children) { commons, child ->
326-
commons.filter { childResult ->
327-
child.children.any { it.equals(childResult) }
328-
}
329-
}.firstOrNull()
330-
}
331-
332257
companion object {
333258
const val NAME_ID = "boolean-expressions-rule"
334259

0 commit comments

Comments
 (0)