if-else control flow

Version 3

    if-else controll flow was introduced in 7.2 builds

     

     

    With if-else control flow you can choose which set of commands and operations to execute based on a condition. The condition is a boolean expression which evaluates the response of the operation (or command) specified in the if statement. Let's look at an example

     

    {code}

    if (outcome != success) of /system-property=test:read-resource

        /system-property=test:add(value=true)

    end-if{code}

     

    The if statement can be read as "if the outcome attribute of /system-property=test:read-resource response is not success (which means the property does not exist) then add the property and initialize it to true".

     

    To understand the condition expression better, the result of /system-property=test:read-resource if the test property doesn't exist will be

     

    {code}[standalone@localhost:9999 /] /system-property=test:read-resource

    {

        "outcome" => "failed",

        "failure-description" => "JBAS014807: Management resource '[(\"system-property\" => \"test\")]' not found",

        "rolled-back" => true

    }{code}

     

    You can see that the outcome attribute equals failed, so the expression in our if statement will evaluate to true and the body of the if will be executed. If we execute read-resource after the if again we'll see

     

    {code}[standalone@localhost:9999 /] /system-property=test:read-resource

    {

        "outcome" => "success",

        "result" => {"value" => "true"}

    }{code}

     

    Here is another example which demonstrates if with else and inverts the true-false value every time it's executed. It also shows how you can navigate the response, i.e. it evaluates attribute value - the child result - using a dot.

     

    {code}

    if (result.value == true) of /system-property=test:read-resource

        /system-property=test:write-attribute(name=value,value=false)

    else

        /system-property=test:write-attribute(name=value,value=true)

    end-if{code}

     

    The of keyword can be followed by any management command or an operation which results in a response from the controller.

     

    And condition expressions can contain

    • boolean && (and), || (or) and parentheses to group and prioritize expressions;

    • >, >=, < and <= in addition to == and !=;

    • the whitespaces are insignificant and ignored.

     

    E.g. something like this could be a valid condition expression: ((a==b || c.c1<=d && e.e1.e2>f) && g!=h || i>=j) && (k<l && m>n || o==p)

     

    If you use a navigation path and the path doesn't exist then the expression (the comparison which involves the path) will evaluate to false.

     

    The examples above are simple and show only one operation in the bodies of the if and else but there is no such limitation. Each if/else body is a batch (which will be translated into a single composite operation) and can contain any number of commands and operations. Although, nested if-else and try-catch-finally are not yet supported.