@@ -6,14 +6,12 @@ import org.cqfn.diktat.ruleset.rules.DiktatRule
6
6
import org.cqfn.diktat.ruleset.utils.KotlinParser
7
7
import org.cqfn.diktat.ruleset.utils.findAllNodesWithCondition
8
8
import org.cqfn.diktat.ruleset.utils.logicalInfixMethods
9
- import com.bpodgursky.jbool_expressions.And
10
9
import com.bpodgursky.jbool_expressions.Expression
11
- import com.bpodgursky.jbool_expressions.NExpression
12
- import com.bpodgursky.jbool_expressions.Or
13
10
import com.bpodgursky.jbool_expressions.options.ExprOptions
14
11
import com.bpodgursky.jbool_expressions.parsers.ExprParser
15
12
import com.bpodgursky.jbool_expressions.parsers.TokenMapper
16
13
import com.bpodgursky.jbool_expressions.rules.DeMorgan
14
+ import com.bpodgursky.jbool_expressions.rules.DistributiveLaw
17
15
import com.bpodgursky.jbool_expressions.rules.Rule
18
16
import com.bpodgursky.jbool_expressions.rules.RuleList
19
17
import com.bpodgursky.jbool_expressions.rules.RulesHelper
@@ -29,8 +27,6 @@ import org.jetbrains.kotlin.psi.KtParenthesizedExpression
29
27
import org.jetbrains.kotlin.psi.KtPrefixExpression
30
28
import org.jetbrains.kotlin.psi.psiUtil.parents
31
29
32
- typealias ExpressionCreator <K > = (List <Expression <K >? > ) -> Expression <K >
33
-
34
30
/* *
35
31
* Rule that checks if the boolean expression can be simplified.
36
32
*/
@@ -258,77 +254,6 @@ class BooleanExpressionsRule(configRules: List<RulesConfig>) : DiktatRule(
258
254
}
259
255
}
260
256
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
-
332
257
companion object {
333
258
const val NAME_ID = " boolean-expressions-rule"
334
259
0 commit comments