Using IBM ILOG Script in data files
Discusses the use of script statements in data files as a way to load data.
Another custom way to load data is to use script statements in data files.
It is possible to declare some script functions in a .dat file using a prepare{} block.
If you do so, at each initialization of an element, you can invoke
one of these functions using the invoke keyword.
In the function, two properties are being defined:
name: the name of the element being initialized.element: the element being initialized.
You can then use these constructs along with some Java external functions to do some custom
reading of data. The example used in this tutorial reads in a file called
externaldataread.txt that uses a format where all the elements of a sets are
separated with commas (','). For this case, a simple parser has been written
(SimpleTextReader.java). It has mainly two public methods:
public SimpleTextReader (String fileName, String token)
public void fillOplElement(IloOplElement element) throws IOException
The parser is used as follows:
prepare {
function read(element, name) {
var customDataSource =
IloOplCallJava("externaldatasource.SimpleTextReader",
"<init>", "(Ljava.lang.String;Ljava.lang.String;)V",
"[install_dir]/opl/examples/opl_interfaces/java/externaldataread/externaldataread.txt", ",");
customDataSource.fillOplElement(element);
return true;
}
}
strings = {"val1"} invoke read;
Results
Running the example, you can see that:
the OPL element
atakes the value1the string set
stringscontains not onlyval1as defined in the .dat file, but also two more values added from the text file.