In the following sample module, the output file is opened appropriately when the global variables are initialized, for example, either in append mode or in rewrite mode.
application BP_test_suites
module test_util
val file_name = "e:\\obms\\log\\test_util.log";
val out_file: output = openFile(file_name, is_recovery_mode());
fun fprintln(s: string) {
out_file.println(s);
}group abc {
rule myrule1 activated by ... then {fprintln("rule 1");}
rule myrule2 activated by ... then {out_file.println("rule 2");}
}initialize {
...
}finalize {
if (out_file != nil) out_file.close();
}
The function openFile() was overloaded to accept a second argument (boolean). If it is equal to true, then the function opens the file in append mode. If it is equal to false or absent, then the function opens the file in rewrite mode. Here, the predefined function is_recovery_mode() decides whether the file should be appended (in the case of recovery) or rewritten (in the case of normal loading).
The following example shows some of a file-writing function, either from inside the module or from outside. It is usually good practice to manage such resources as files, in a single module that is imported by other modules, so that this resource management does not require replicating across modules. Note that in the module below, the global variable start_time was not made recovery proof.
application BP_test_suites
module test_util
val file_name = "e:\\obms\\log\\test_util.log";
val out_file: output= openFile(file_name, is_recovery_mode());
var start_time: date;
fun fprintln(s: string) {
out_file.println(s);
}fun set_start_time(msg: string) {
fprintln(">> started: " + msg);
start_time := NOW;
}fun display_consumed_time(msg: string) {
fprintln(">> "+msg+": consumed time: "+duration(NOW, start_time)/1000);
}initialize {
}finalize {
if (out_file != nil) out_file.close();
}-----
//from other modules, fprint can be used as well as other functions:
// (Note that the test_util module needs to be imported by the using modules)
{ test_util::fprintln(">> create_instances<3>");
test_util::display_consumed_time("Test_CrossCorrel_A<1>");
test_util::set_start_time("Test_CrossCorrel_A");
}