Control Statements in DIAL
Control statements, such as if, while, and walk, direct the flow of other, usually compound, statements in a DIAL script.
The dive statement has the following form:
dive(<model>, <dimension_expression>) <statement>;
The dive statement creates a new Tabular window and runs a series of statements that reference the window. When used against a cBase, Model, or DivePlan with model.open, the dive statement creates new windows starting at the top level of the Model. When used against a Marker with marker.open, it creates new windows below the bottom window of the Marker.
If there are already open windows in the Model from using a Marker or previous dive or walk statements, a row must be selected in the bottom window. This row can be selected by using a walk statement, which successively selects rows, or by using the window.select_row() and window.select_row_index() functions. The dive statement will dive off that row in the window to create the new window.
Example:
diveline.connect("localhost:2130", "dial_user");
diveline.set_project("myProject");
model.open(my_model, "/models/demo-sales.mdl");
dive(my_model, "Sales Region")
window.select_row(model, "Far West");
diveline.disconnect();
The if statement has the following form:
if(<expression>)
<statement1>;
else
<statement2>;
The expression is evaluated. If it is true (non-zero or non-empty), then statement1 runs. If it is false (zero or empty), then statement2 runs. Statement1 and statement2 can be compound statements. Statement2 is optional.
Example:
if(marker.find_succeeded(model))
console.writeln("Ready to use Your Report");
else
console.writeln("Your Report Marker is not found");
To perform a series of statements inside an if statement, use a compound statement that encloses the series in braces ({}).
if (<expression>)
{
<statement1a>;
<statement1b>;
}
else
<statement2>;
Opens the auto_test_class_type.mrk marker file and walks the Class Type dimension. If auto_test_class_type.mrk contains a dive on Class Type, the script generates a report. In some cases, the Find might result in an empty report. In order to avoid this, the script checks to see if the Find succeeds. If it does, the script saves a report.
diveline.connect("localhost", "dial_user");
diveline.set_project("myProject");
marker.open(my_model,"/models/auto_test_class_type.mrk");
walk(my_model, "Class Type") {
if(marker.find_succeeded(my_model)) {
console.writeln ("report generated for: "+Dimension[my_model.Class Type]);
filename="../temp/"+Dimension[my_model.Class Type]+".pdf";
marker.save_window(my_model,filename,"pdf");
}
else {
console.writeln ("no report generated for: "+Dimension[my_model.Class Type]);
}
console.writeln(" ");
}
diveline.disconnect();
NOTE: The filename here points to a temp folder in the DiveLine dataroot, which is dl-dataroot/temp.
This example script connects to the DiveLine server, opens the auto_test_group.mrk marker and walks the Group Dimension. If the Cost100Miles is over $20,000, the script states that the group is over budget. If not, the script states that the group is under budget and generates and saves a report.
diveline.connect("localhost", "dial_user");
diveline.set_project("myProject");
marker.open(my_model,"/models/auto_test_group.mrk");
walk(my_model, "Group") {
if(calc[my_model.Cost100Miles] > 20000) {
console.writeln (Dimension[my_model.Group]+" is OVER budget");
}
else {
console.writeln (Dimension[my_model.Group]+" is UNDER budget");
filename = "../temp/" + Dimension[my_model.Group] + ".pdf";
marker.save_window(my_model, filename, "pdf");
console.writeln ("report generated");
}
console.writeln(" ");
}
diveline.disconnect();
NOTE: The filename variable points to a temp folder in the DiveLine dataroot, which is dl-dataroot/temp.
The while statement has the following form:
while(<expression>)
<statement>;
The while statement performs a loop by repeatedly testing an expression and running a statement. The while statement evaluates the expression. If the expression is true, the script runs the statement. The script repeats this loop until the expression is false.
Example:
The following DIAL script uses a while statement to get the arguments that are passed to the script. The script evaluates the expression argloop < arg.count() in a while loop. As long as the expression evaluates to true, that is, as long as there are additional arguments, the if statements are processed. When the while expression is false, that is, argloop is equal to or greater than arg.count(), the while statement stops processing.
while(argloop < arg.count()) {
if(arg.get(argloop) == "-server") {
server = arg.get(argloop+1);
}
if(arg.get(argloop) == "-mode") {
mode = arg.get(argloop+1);
}
argloop = argloop+1;
}
NOTE: This script is designed to run from the command line. With each loop through the while statement, the if statements compare the argument with the strings -server and -mode. When a match is found, the script retrieves the next argument and assigns it to the variable server or mode.
For example, the following command runs the DIAL script, ch7_args_loop.dial. The script sets the server variable to the value of the argument that follows it, mail.company.com, and sets the mode variable to the argument that follows it, test:
java -jar dial.jar ch7_args_loop.dial -server mail.company.com -mode test
To run this script in a Production DIAL node, specify the string -server as an argument and specify the value that you want to set it to as the next argument. Likewise, specify -mode as an argument and the value that you want to set it to as the next argument.
The walk statement has the following form:
walk(<model_name>, <dimension_expression>)
<statement>;
The walk statement is a major control construct in DIAL. It is used to walk through all entries in a Tabular window or a QuickView for a Model and apply a set of DIAL statements on that entry and its child windows. The walk statement controls how and when DIAL accesses tables in the Model.
NOTES:
- QuickViews provide access to multiple Dimensions and values on one screen. They act as Parent windows, cascading any windows below them to reflect data only for the Dimension selected. There are multiple types of QuickViews, but DIAL can only process Dimension QuickViews. For more information, see ProDiver Help.
- In the walk statement, model_name is a Model Object Variable created by using the marker.open() or model.open() function.
- The dimension_expression is a string whose value identifies a Dimension name in the Model. If model_name is opened as a Marker, this Dimension must appear in a simple Tabular window or a QuickView in the Marker. If model_name is opened as a Model or DivePlan, a simple Tabular window for this Dimension is created by diving or by selecting a QuickView value.
- If the Tabular window is the bottom-most window of the Model, the walk statement walks through the rows of this window and repeatedly runs the statement while logically assigning the Dimension value to the specified Dimension. Any Summary Model References, such as Total[< column name >], that do not contain a parent attribute refer to the value in that row; any Dimension or Info Model References based on the Dimension refer to that Dimension value.
- If the Tabular window is an intermediate window of the Model, the walk statement walks through the different rows of this window and repeatedly runs the statement while logically assigning the Dimension value to the specified Dimension. Any Dimension and Info references in the statement for that Dimension use that Dimension value. Summary Model References in a walk statement depend on walk statement nesting.
- Certain functions, such as marker.save or marker.save_report, operate on the bottom-most window of a Model. The actual window used is affected by all walk statements that enclose the function call.
Walk statements on Markers can be nested in the order that they appear in the Marker. In this situation, the values of Summary Model References default to the row value for the immediate enclosing walk statement. For example, if we have a two-level Marker containing a Tabular of Product and a tabular of Salesperson, and we have nested walk statements, Summary values inside both walk statements refer to row values in the Salesperson tabular, while Summary values inside the Product walk statement but outside the Salesperson walk statement refer to row values in the Product Tabular.
Example 1 and Example 2 demonstrate the usage of walk statements. Example 3 and Example 4 demonstrate the usage of nesting walk statements. Example 5 shows use of walk, if, and loop statements.
This script opens a Marker and walks the Group Tabular. For each row in this Tabular, the script outputs the Group name. The console then prints each value in the Group Dimension.
diveline.connect("localhost", "dial_user");
diveline.set_project("myProject");
marker.open(mymarker,"/models/auto_test_group.mrk");
walk(mymarker, "Group"){
console.writeln("Group\t" +Dimension[mymarker."Group"]);
}
diveline.disconnect();
This script opens a DivePlan, dives on Group, and then walks the Group Dimension. Note that because the model is a DivePlan, an_walkex1.dvp, you must include a dive statement before the walk statement.
diveline.connect("localhost", "dial_user");
diveline.set_project("myProject");
model.open(mymodel,"/models/an_walkex1.dvp");
dive(mymodel,"Group") {
walk(mymodel, "Group") {
console.writeln("Group\t" +Dimension[mymodel."Group"]);
}
}
diveline.disconnect();
The following script opens a DivePlan, dives on the Group Dimension, walks the Group Dimension, and does a nested dive and walk of the Class Type Dimension.
diveline.connect("localhost", "dial_user");
diveline.set_project("myProject");
model.open(mymodel,"/models/an_walkex1.dvp");
dive (mymodel, "Group") {
walk (mymodel, "Group") {
console.writeln("Group\t" +Dimension[mymodel."Group"]);
dive (mymodel, "Class Type") {
walk (mymodel, "Class Type") {
console.writeln("Class Type\t" +Dimension[mymodel."Class Type"]);
}
}
}
}
diveline.disconnect();
The following script opens the auto marker with a dive on class and make, does a walk through the Class Type Dimension, and a nested walk through the Make Dimension.
diveline.connect("localhost", "dial_user");
diveline.set_project("myProject");
marker.open(mymarker,"/models/auto_test_classtype_make.mrk");
walk (mymarker, "Class Type") {
console.writeln(Dimension[mymarker.Class Type]);
walk (mymarker, "Make") {
console.writeln("\t" + Dimension[mymarker.Make]);
}
}
diveline.disconnect();
This example combines the walk statement and the if statement. It opens the auto_test.mrk marker with a dive on Make and a sort down on Vehicles sold. The script walks the Make Dimension and lists the top six.
diveline.connect("localhost", "dial_user");
diveline.set_project("myProject");
marker.open(my_model,"/models/auto_test.mrk");
loop=0;
walk(my_model, "Make") {
if(loop==0) rank="first";
else
if(loop==1) rank="second";
else
if(loop==2) rank="third";
else
if(loop==3) rank="fourth";
else
if(loop==4) rank="fifth";
else
if(loop==5) rank="sixth";
else
rank="";
if(loop>5)console.writeln(" ");
else console.writeln(Dimension[my_model.Make] + " ranks "+rank +".");
loop=loop+1;
console.writeln(" ");
}
diveline.disconnect();