Reading slices of data via ExternalBinding mapping element
Data is sometimes provided in several, similarly structured, .json
files; for instance as a result of multiple DEX Client requests.
Each of these .json
files provides a slice of the data to be worked on.
In this how-to, two ways are presented for handling the slices:
Doing it yourself, via additional AIMMS parameters.
Using the
ExternalBinding
element
Story
We have three .json
, each describing some properties of a stream.
These properties are named prp1
, prp2
, and prp3
in the .json
file.
Their data is to be read in, into the AIMMS identifiers p_prop1(i_stream)
, p_prop2(i_stream)
, and sp_prop3(i_stream)
respectively.
Solution by adding additional parameters
Doing it yourself, three additional parameters are added: p_prop1_scalar
, p_prop2_scalar
, and sp_prop3_scalar
Then we can use the mapping file:
1<AimmsJSONMapping>
2 <ObjectMapping>
3 <ValueMapping name="prp1" maps-to="p_prop1_scalar"/>
4 <ValueMapping name="prp2" maps-to="p_prop2_scalar"/>
5 <ValueMapping name="prp3" maps-to="sp_prop3_scalar"/>
6 </ObjectMapping>
7</AimmsJSONMapping>
and the procedure:
1Procedure pr_getDataViaScalarReads {
2 Body: {
3 dex::AddMapping(
4 mappingName : "streamScalarRead",
5 mappingFile : "Mappings/streamPropertiesScalarMappingJSON.xml");
6
7 for i_dataFileNo do
8 sp_elemName := SubString(sp_dataFileNames(i_dataFileNo),1,StringLength(sp_dataFileNames(i_dataFileNo))-5);
9 SetElementAdd(
10 Setname : s_streams,
11 Elempar : ep_anotherStream,
12 Newname : sp_elemName);
13
14 dex::ReadFromFile(
15 dataFile : "data/" + sp_dataFileNames(i_dataFileNo),
16 mappingName : "streamScalarRead");
17 p_prop1(ep_anotherStream) := p_prop1_scalar ;
18 p_prop2(ep_anotherStream) := p_prop2_scalar ;
19 sp_prop3(ep_anotherStream) := sp_prop3_scalar ;
20
21 endfor ;
22 }
23 StringParameter sp_elemName;
24}
Solution via the ExternalBinding element
By establishing an external binding, we can directly store the data of the slice read in:
1<AimmsJSONMapping>
2 <ExternalBinding binds-to="i_stream" binding="ep_anotherStream"/>
3 <ObjectMapping>
4 <ValueMapping name="prp1" maps-to="p_prop1(i_stream)"/>
5 <ValueMapping name="prp2" maps-to="p_prop2(i_stream)"/>
6 <ValueMapping name="prp3" maps-to="sp_prop3(i_stream)"/>
7 </ObjectMapping>
8</AimmsJSONMapping>
by using the procedure:
1Procedure pr_getDataViaExternalBinding {
2 Body: {
3 dex::AddMapping(
4 mappingName : "streamExternalRead",
5 mappingFile : "Mappings/streamPropertiesExternalMappingJSON.xml");
6
7 for i_dataFileNo do
8 sp_elemName := SubString(sp_dataFileNames(i_dataFileNo),1,StringLength(sp_dataFileNames(i_dataFileNo))-5);
9 SetElementAdd(
10 Setname : s_streams,
11 Elempar : ep_anotherStream,
12 Newname : sp_elemName);
13
14 dex::ReadFromFile(
15 dataFile : "data/" + sp_dataFileNames(i_dataFileNo),
16 mappingName : "streamExternalRead");
17
18 endfor ;
19 }
20 StringParameter sp_elemName;
21}
As you can see, there is no need anymore for the additional parameters: p_prop1_scalar
, p_prop2_scalar
, and sp_prop3_scalar
.