Decision

The Decision component enables users to specify a condition or a set of conditions, which if satisfied are either considered as true, otherwise false. It enables users to automate the process of decision making.
Screenshot

Supported Functions

Decision component supports the following functions and operators.

Values

A value is a terminal token representing a concrete element. This can be:

  • an integer

  • a floating point number

  • a date time

  • a boolean

  • a string

  • a function

  • a parameter

Integers

They are represented using numbers.

123456

They are evaluated as Int32.

Floating point numbers

Use the dot to define the decimal part.

123.456 .123

They are evaluated as Decimal.

Scientific notation

You can use the e to define power of ten (10^).

1.22e1

1e2

1e+2

1e-2

.1e-2

1e10

They are evaluated as Double.

Dates and Times

Must be enclosed between sharps.

#2008/01/31# // for en-US culture

The are evaluated as DateTime. NCalc uses the current Culture to evaluate them.

Booleans

Booleans can be either true or false.

Strings

Any character between single quotes ' are evaluated as String.

'hello'

You can escape special characters using \, `', \n, \r, \t`.

Parameters

A parameter as a name, and can be optionally contained inside brackets.

2 + x, 2 + [x]

Function

A function is made of a name followed by braces, containing optionally any value as arguments. Abs(1), doSomehting(1, 'dummy')

Operators

Expressions can be combined using operators. Each operator as a precedence priority. Here is the list of those expression's priority.

  1. primary

  2. unary

  3. power

  4. multiplicative

  5. additive

  6. relational

  7. logical

Logical

These operators can do some logical comparison between other expressions:

  • or,||
  • and,&&

true or false

The and operator has more priority than the or, thus in the example above, false and true is evaluated first.

Relational

Relational operators are used to compare values. These operators always result in a boolean value.

  • =, ==, !=, <>
  • <, <=, >, >=

    1 < 2

Additive

The additive operators are used for addition and string concatenation (+) and subtraction (-). The additive operators appear in additive expressions.

  • +, -

    1 + 2 – 3

Multiplicative

The multiplicative are used for multiplication (*), division (/), and the remainder operation (%). The multiplicative operators appear in multiplicative expressions.

  • *, /, %
  • 1 * 2 % 3

Bitwise

Decimal numbers are natural to humans. Binary numbers are native to computers. Binary, octal, decimal, or hexadecimal symbols are only notations of the same number. Bitwise operators work with bits of a binary number.

  • & (bitwise and), | (bitwise or), ^(bitwise xor), << (left shift), >>(right shift)

    2 >> 3

Unary

Unary operator that takes a single operand in an expression or a statement.

  • !, not, -, ~ (bitwise not)

    not true

Primary

Two primary operators let you specify structure and union members. The dot (a period) and arrow (formed by a minus and a greater than symbol) operators are always preceded by a primary expression and followed by an identifier.

  • (, )
  • values

    2 * ( 3 + 2 )

The framework includes a set of already implemented functions.

Name Description Usage Result
Abs Returns the absolute value of a specified number. Abs(-1) 1M
Acos Returns the angle whose cosine is the specified number. Acos(1) 0d
Asin Returns the angle whose sine is the specified number. Asin(0) 0d
Atan Returns the angle whose tangent is the specified number. Atan(0) 0d
Ceiling Returns the smallest integer greater than or equal to the specified number. Ceiling(1.5) 2d
Cos Returns the cosine of the specified angle. Cos(0) 1d
Exp Returns e raised to the specified power. Exp(0) 1d
Floor Returns the largest integer less than or equal to the specified number. Floor(1.5) 1d
IEEERemainder Returns the remainder resulting from the division of a specified number by another specified number. IEEERemainder(3, 2) -1d
Log Returns the logarithm of a specified number. Log(1, 10) 0d
Log10 Returns the base 10 logarithm of a specified number. Log10(1) 0d
Max Returns the larger of two specified numbers. Max(1, 2) 2
Min Returns the smaller of two numbers. Min(1, 2) 1
Pow Returns a specified number raised to the specified power. Pow(3, 2) 9d
Round Rounds a value to the nearest integer or specified number of decimal places. The mid number behavior can be changed by using EvaluateOption.RoundAwayFromZero during construction of the Expression object. Round(3.222, 2) 3.22d
Sign Returns a value indicating the sign of a number. Sign(-10) -1
Sin Returns the sine of the specified angle. Sin(0) 0d
Sqrt Returns the square root of a specified number. Sqrt(4) 2d
Tan Returns the tangent of the specified angle. Tan(0) 0d
Truncate Calculates the integral part of a number. Truncate(1.7) 1

It also includes other general purpose ones.

Name Description Usage Result
in Returns whether an element is in a set of values. in(1 + 1, 1, 2, 3) true
If Returns a value based on a condition. if(3 % 2 = 1, 'value is true', 'value is false') 'value is true'

Ports

The Decision component exposes Control In and Control Out ports by default and dynamically created Data In ports.

PORT Description
Control In Must be connected to the Control Out port of one or more components.
Control Out Must be connected to the Control In port of any other component. The Decision component exposes the following two types of Control Out ports:
Success: is connected to the Control In port of any other component as long as their data types are the same.
Fail: is connected to the Control In port of any other component as long as their data types are the same.
Data In Can be connected to the Data Out port of another component from where the data is to be collected for the variable in the Decision component.
This port is dynamically created with the name of the variable provided in the Decision component.

Properties

To edit the properties of the Decision component, in the lower-right pane, change the required property. This is required to select the action to be performed using the Decision component.

Users can edit the following properties:

Property Description
Search Search for the respective property.
Delay After Execution Specifies the wait time (in seconds) after the action is executed.
Delay Before Execution Specifies the wait time (in seconds) before the action is executed.
Name Specifies the name for the Decision component.
Set the name to match the application field the user is interacting with.

Example

Let us consider an example of deciding if a number is greater than or equal to 40.

Screenshot

To use the Decision component:

  1. From the Toolbox, expand Conditions.

  2. Drag the Decision component and drop it on the Design surface.

  3. Below the title bar, enter a logical condition. For example, the condition, x>=40. A Data In port is dynamically created with the name of the variable provided in the Decision Text Box.

    For this example, lets add a number to define the value of 'X' and see whether the condition is satisfied. To execute the action, the user must add the Variable component.

  4. To add the Variable component:

    • In the Toolbox, expand General.

    • Drag the Variable component and drop it on the Design surface.

    • Double-click the Variable component, and enter a number.

    The user must also add two MessagBox Show components to display a success and a failure notification in case the decision fails to satisfy the condition.

  5. To add the MessageBox Show component:

    • In the Toolbox, expand General.

    • Expand the MessagBox component, drag Show component and drop it on the Design surface.

    • Double-click the Show component text box, and enter the success message.

    • Repeat the same steps by adding another Show component to add a failure message.

  6. Connect:

    • The Control Out port of the Start component to the Control In port of the Variable component.

    • The Control Out port of the Variable component with the Control In port of the Decision component.

    • The Data Out port of the Variable component to the Data In port of the Deision component.

    • The True Control Out port of the Decision component with the pass MessagBox component.

    • The False Control Out port of the Decision component with the fail MessagBox component.

  7. In the toolbar, click Run.

    If the condition is successfully satisfied, the success message is displayed.

    Screenshot

    If the condition is not satisfied, the failure message is displayed.

    Screenshot