## Check logical expressions in If blocks

Check ID: `mathworks.maab.na_0003`

Guideline: na_0003: Usage of If blocks

### Description

Check If blocks for inappropriate construct of primary expressions in a logical expression.

Identifies instances in an If block where primary expressions are complex.

Primary expressions are defined as:

• An input

• A constant

• A constant parameter

• A parenthesized expression containing no operators other than zero or `<`, `>`, `>=`, `<=`, `==`, `~=`, `|`, `&`, and `~`

Examples of primary expressions include:

• `u1`

• `5`

• `K`

• (`u1` > `0`)

• (`u1` <= `G`)

• (`u1` > `U2`)

• (~`u1`)

Examples of acceptable logical expressions exceptions include:

• `u1 | u2`

• (`(u1 > 0) & (u1 < 20)`

• `(u1 > 0) & (u2 < u3)`

• `(u1 > 0) & (~u2)`

This table provides examples of unacceptable logical expressions.

Primary ExpressionReasoning

`u1 & u2 | u3`

Too many primary expressions.

`u1 & (u2 | u3)`

Unacceptable operator within primary expression.

```(u1 > 0) & (u1 < 20) & (u2 > 5)```

Too many primary expressions that are not inputs.

```(u1 > 0) & ((2* u2) > 6)```

Unacceptable operator within primary expression.

Exception

A logical expression can contain more than two primary expressions when both these conditions are met:

• The primary expressions are all inputs.

• Only one type of logical operator is present.

Examples of acceptable exceptions include:

• `u1 | u2 | u3 | u4 | u5`

• `u1 & u2 & u3 & u4`

Simple "If" Expressions

In the literal interpretation of guideline na_0003, expression `u1 < u2` is a violation. However, the expression follows the commonly used "If" expression template (<Primary Expression><Operator><Primary Expression>). So, when logical operators are not used and only one relational operator is present, the expression satisfies guideline na_0003 and `u1` < `u2` is NOT a violation.

### Check Parameterization

This check does not include sub-checks because the MAB modeling guideline only provides one sub ID.

For reference, the MAB guideline sub ID(s) that are recommended for use by the NA-MAAB and JMAAB modeling standards organizations are:

• NA-MAAB — No recommendations

• JMAAB — a

### Results and Recommended Actions

ConditionRecommended Action
Logical expression contains more than two primary expressions that consist of a constant, constant parameter, and input.

Consider one of the following:

• Make primary expressions an input and either:

• Use parenthesized expressions with one relational operator type

• Construct a simple "If" express using template
<Primary Expression><Logical Operator><Primary Expression>

• Reduce the number of primary expressions to two or less.

• Construct the logical expression using logical blocks other than the If block.

Logical expression contains more than two parenthesized expressions that use multiple relational operators

Consider one of the following:

• Use only one type of relational operator. Acceptable logical operators include `<`, `>`, `>=`, `<=`, `==`, `~=`, `|`, `&`, and `~`. The primary expression must consist of inputs only.

• Reduce the number of parenthesized expressions to two or less.

• Construct the logical expression using logical blocks other than the If block.

Parenthesized expression includes a relational operator other than zero or `<`, `>`, `>=`, `<=`, `==`, `~=`, `|`, `&`, or `~`.

Consider one of the following:

• Use relational operator `<`, `>`, `>=`, `<=`, `==`, `~=`, `|`, `&`, or `~` within the parenthesized expression.

• Construct the logical expression using logical blocks other than the If block.

### Capabilities and Limitations

• Does not flag logical expressions that use only one of these relative operators `<`, `>`, `>=`, `<=`, `==`, `~=`, `|`, `&`, and `~`

• Runs on library models.

• Analyzes content of library-linked blocks. By default, the input parameter Follow links is set to `on`.

• Analyzes content in masked subsystems. By default, the input parameter Look under masks is set to `graphical`.