Try OpenEdge Now
skip to main content
BPM Events User's Guide
The rule language : Predefined rule actions : println(): Printing values to a file : Printing values in a file
 
Printing values in a file
In order to redirect the output to a text file, use the following sequence of statements:
*Open a file. Give the full path of the file. If the file does not exist, then it will be created. The openFile function can take a second argument: if true, then the file is opened in append mode. If false or absent, then the file is opened in rewrite mode (all previous content is lost). The syntax is:
val <file_identifier> = openFile(<file_path>);
val <file_identifier> = openFile(<file_path>, <append_flag>);
*Print one or more lines in the file, using:
<file_identifier>.println(<primitive_value>);
*Close the file, using:
<file_identifier>.close().
You may also flush the file: <file_identifier>.flush(). This flushing has the same semantics as in Java and guarantees that the file contents are fully printed from any memory buffer. For example:
rule abc
activated by EVT_A of BP Server::W_COMPLETED{WORKSTEPNAME : "DoTask",
PROCESSTEMPLATENAME : "Assign_A_Task_V2"}
then {
val myfile = openFile("C:\\temp\\test.log");
myfile.println("hello from abc rule");
myfile.println(2001);
myfile.println(true);
myfile.close();
}
In case the output of several rules (or of several activations of the same rule) requires writing to the same file, then a global file variable should be used. Indeed, opening and closing the file should only be done once, otherwise the file would be rewritten from the beginning each time. A variable that is global to a module must be declared and used as follows:
*declare a global file variable immediately after the module declaration statement,
*use println() on this variable in any rule that needs to write on the file, and
*close the file in a final rule or in the finalize{} section.
For example:
application BP_3_test_suites
module InfopadTest
val outputFile = openFile("e:\\BP_test_suites\\rules\\InfopadTest.out");
group Group1
{  rule Rule1
  activated by event1 of Infopad::remove
  then{
      outputFile.println(" ");
      outputFile.println("Rule1 activated.");
      schedule("InfopadCheckRemoval1", NOW + 10*SEC, type: "Message", value: "Scheduling");
      outputFile.flush();
  }
}
initialize {
    outputFile.println(" ");
    outputFile.println("*** Start of InfopadTest");
    //--------------
    outputFile.println("*** Defining Static Infopads.");
    infoPad1 :=
        new infopad<cell{strSlot:string, intSlot:int,
        floatSlot:float}>[5][5]("infoPad1");
}finalize {
    discard(infoPad1);
    outputFile.println("*** InfopadTest done");
    outputFile.println(" ");
    outputFile.flush();
    outputFile.close();
}
Note: Global values like the outputFile value above, are not recoverable because they are transient objects. Only infopads are recoverable. In order to avoid recovery problems, it is preferable to store any global value in an infopad. This is described in Recovery of global variables and file handling.