[jboss-dev-forums] [JBoss AS 7 Development] - if-else control flow

Tomaz Cerar do-not-reply at jboss.com
Wed Sep 12 17:03:13 EDT 2012


Tomaz Cerar [https://community.jboss.org/people/ctomc] modified the document:

"if-else control flow "

To view the document, visit: https://community.jboss.org/docs/DOC-18764

--------------------------------------------------------------
h4. 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


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


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

[standalone at localhost:9999 /] /system-property=test:read-resource
{
    "outcome" => "failed",
    "failure-description" => "JBAS014807: Management resource '[(\"system-property\" => \"test\")]' not found",
    "rolled-back" => true
}


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

[standalone at localhost:9999 /] /system-property=test:read-resource
{
    "outcome" => "success",
    "result" => {"value" => "true"}
}


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.


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


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.
--------------------------------------------------------------

Comment by going to Community
[https://community.jboss.org/docs/DOC-18764]

Create a new document in JBoss AS 7 Development at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=102&containerType=14&container=2225]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/jboss-dev-forums/attachments/20120912/98ba02c8/attachment.html 


More information about the jboss-dev-forums mailing list