Skip to content

Commit

Permalink
fixed code style issues + checking on expression size
Browse files Browse the repository at this point in the history
  • Loading branch information
nulls committed Jun 16, 2022
1 parent 7b68c23 commit 16066ef
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import org.cqfn.diktat.ruleset.constants.Warnings.COMPLEX_BOOLEAN_EXPRESSION
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.findLeafWithSpecificType
import org.cqfn.diktat.ruleset.utils.logicalInfixMethods
import com.bpodgursky.jbool_expressions.And
import com.bpodgursky.jbool_expressions.Expression
Expand All @@ -20,7 +19,6 @@ import com.bpodgursky.jbool_expressions.rules.RuleList
import com.bpodgursky.jbool_expressions.rules.RulesHelper
import com.pinterest.ktlint.core.ast.ElementType.BINARY_EXPRESSION
import com.pinterest.ktlint.core.ast.ElementType.CONDITION
import com.pinterest.ktlint.core.ast.ElementType.OPERATION_REFERENCE
import com.pinterest.ktlint.core.ast.ElementType.PARENTHESIZED
import com.pinterest.ktlint.core.ast.ElementType.PREFIX_EXPRESSION
import com.pinterest.ktlint.core.ast.isLeaf
Expand Down Expand Up @@ -261,32 +259,27 @@ class BooleanExpressionsRule(configRules: List<RulesConfig>) : 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)
*
*/
private class DistributiveLaw<K> : Rule<NExpression<K>, K>() {

private val andExpressionCreator: ExpressionCreator<K> = { exprList ->
And.of(exprList.toList())
}

private val orExpressionCreator: ExpressionCreator<K> = { exprList ->
Or.of(exprList.toList())
}

override fun applyInternal(input: NExpression<K>?, options: ExprOptions<K>?): Expression<K> {
requireNotNull(input) {
"input expression is null"
}
val exprFactory = requireNotNull(options?.exprFactory) {
"jbool issue: Failed to get exprFactory"
}
val orExpressionCreator: ExpressionCreator<K> = { expressions -> exprFactory.or(expressions.toTypedArray()) }
val andExpressionCreator: ExpressionCreator<K> = { 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}")
}
else -> throw UnsupportedOperationException("Not supported input expression: ${input.exprType}")
}
}

private fun applyInternal(input: NExpression<K>, upperExpressionCreator: ExpressionCreator<K>, innerExpressionCreator: ExpressionCreator<K>): Expression<K> {
private fun applyInternal(input: NExpression<K>,
upperExpressionCreator: ExpressionCreator<K>,
innerExpressionCreator: ExpressionCreator<K>): Expression<K> {
val commonExpression = requireNotNull(findCommonExpression(input.children)) {
"Common expression is not found for $input"
}
Expand All @@ -297,7 +290,9 @@ class BooleanExpressionsRule(configRules: List<RulesConfig>) : DiktatRule(
)))
}

private fun excludeChild(expression: Expression<K>, expressionCreator: ExpressionCreator<K>, childToExclude: Expression<K>): Expression<K> {
private fun excludeChild(expression: Expression<K>,
expressionCreator: ExpressionCreator<K>,
childToExclude: Expression<K>): Expression<K> {
val leftChildren = expression.children.filterNot { it.equals(childToExclude) }
return if (leftChildren.size == 1) {
leftChildren.first()
Expand All @@ -307,35 +302,30 @@ class BooleanExpressionsRule(configRules: List<RulesConfig>) : DiktatRule(
}

/**
*
* Checks the input expression
*/
override fun isApply(inputNullable: Expression<K>?): Boolean {
return inputNullable?.let { input ->
when (input) {
is And -> isApplicable<And<K>, Or<K>>(input)
is Or -> isApplicable<Or<K>, And<K>>(input)
else -> false
}
} ?: false
}
override fun isApply(inputNullable: Expression<K>?): Boolean = inputNullable?.let { input ->
when (input) {
is And -> isApplicable<And<K>, Or<K>>(input)
is Or -> isApplicable<Or<K>, And<K>>(input)
else -> false
}
} ?: false

private inline fun <E: NExpression<K>, reified C: NExpression<K>> isApplicable(input: E): Boolean {
private inline fun <E : NExpression<K>, reified C : NExpression<K>> isApplicable(input: E): Boolean {
val children = input.children ?: return false

if (children.any { it !is C }) {
if (children.size < 2 || children.any { it !is C }) {
return false
}
return findCommonExpression(children) != null
}

private fun findCommonExpression(children: List<Expression<K>>): Expression<K>? {
return children.drop(1)
.fold(children[0].children) { commons, child ->
commons.filter { childResult ->
child.children.any { it.equals(childResult) }
}
}.firstOrNull()
}
private fun findCommonExpression(children: List<Expression<K>>): Expression<K>? = children.drop(1)
.fold(children[0].children) { commons, child ->
commons.filter { childResult ->
child.children.any { it.equals(childResult) }
}
}.firstOrNull()
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,14 @@ fun some() {
if (a > 5 && ((b > 6 && z > 3) || (c > 7 && y > 4) || (d > 8 && w > 5))) {

}

// long case #2.1
if (b > 6 && a > 5 && (z > 3 || c > 7 || w > 5)) {

}

// long case #2.2
if (b > 6 || a > 5 || (z > 3 && c > 7 && w > 5)) {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,14 @@ fun some() {
if ((b > 6 && a > 5 && z > 3) || (c > 7 && a > 5 && y > 4) || (a > 5 && d > 8 && w > 5)) {

}

// long case #2.1
if ((b > 6 && a > 5 && z > 3) || (c > 7 && a > 5 && b > 6) || (a > 5 && b > 6 && w > 5)) {

}

// long case #2.2
if ((b > 6 || a > 5 || z > 3) && (c > 7 || a > 5 || b > 6) && (a > 5 || b > 6 || w > 5)) {

}
}

0 comments on commit 16066ef

Please sign in to comment.