Skip to content

Commit ad6ed99

Browse files
authored
Merge pull request #8 from vlkong/master
Allowing for datasource to be defined .dat
2 parents 60214fc + 1c70b42 commit ad6ed99

File tree

10 files changed

+304
-7
lines changed

10 files changed

+304
-7
lines changed

examples/embedded_in_dat/README.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# Example using IloOplCallJava
2+
3+
This sample is basically the same as [ilo_opl_call_java](../ilo_opl_call_java).
4+
5+
The difference is that the connection strings and queries are in the .dat as well.
6+
7+
# Setup
8+
9+
Before you run the sample, you will need to edit [oil.dat](oil.dat), and configure
10+
paths. In particular, you want to add the JDBC connector jar, for instance:
11+
12+
```
13+
// Update this to point to your jdbc driver.
14+
// For instance mssql-jdbc-7.2.2.jre8.jar for mssql
15+
IloOplImportJava("../../external_libs/mssql-jdbc-7.2.2.jre8.jar")
16+
```
17+
18+
And you want to specify the configuration file for your database connection:
19+
20+
```
21+
// This is the configuration file for accessing your database.
22+
var jdbcConfigXml = "../../data/db_mssql.xml";
23+
```
24+
25+
Lastly, you want to point to the jdbc custom data source jar.
26+
It is distributed as binaries in the [lib](../../lib) directory:
27+
28+
```
29+
// The jar containing the jdbc custom data source
30+
IloOplImportJava("../../lib/jdbc-custom-data-source.jar");
31+
```
32+
# Running the sample
33+
34+
To run the sample, invoke the `run.bat` script:
35+
36+
37+
```
38+
C:\> run
39+
Buildfile: C:\opl\OPL-jdbc-custom-data-source\examples\ilo_opl_call_java\build.xml
40+
41+
platform:
42+
43+
run:
44+
[exec]
45+
[exec] <<< setup
46+
[exec]
47+
[exec] Reading elements from database
48+
[exec] Reading Gasolines using "SELECT NAME FROM GASDATA"
49+
[exec] Reading OilData using "SELECT * FROM OILDATA"
50+
[exec] Reading Oils using "SELECT NAME FROM OILDATA"
51+
[exec] Reading GasData using "SELECT * FROM GASDATA"
52+
[exec] Done
53+
[exec]
54+
[exec] <<< generate
55+
[exec]
56+
[exec] Tried aggregator 1 time.
57+
[exec] LP Presolve eliminated 1 rows and 0 columns.
58+
[exec] Reduced LP has 12 rows, 12 columns, and 43 nonzeros.
59+
[exec] Presolve time = 0.00 sec. (0.01 ticks)
60+
[exec]
61+
[exec] Iteration log . . .
62+
[exec] Iteration: 1 Scaled dual infeas = 0.000000
63+
[exec] Iteration: 2 Dual objective = 434000.000000
64+
[exec]
65+
[exec] <<< solve
66+
[exec]
67+
[exec]
68+
[exec] OBJECTIVE: 287750
69+
[exec] Result = {<"Crude1" "Diesel" 800 0> <"Crude1" "Regular" 2111.1 750>
70+
71+
[exec] <"Crude1" "Super" 2088.9 0> <"Crude2" "Diesel" 0 0>
72+
[exec] <"Crude2" "Regular" 4222.2 750> <"Crude2" "Super" 777.78 0>
73+
[exec] <"Crude3" "Diesel" 200 0> <"Crude3" "Regular" 3166.7 750>
74+
[exec] <"Crude3" "Super" 133.33 0>}
75+
[exec]
76+
[exec] <<< post process
77+
[exec]
78+
[exec]
79+
[exec] <<< done
80+
[exec]
81+
82+
BUILD SUCCESSFUL
83+
Total time: 1 second
84+
Press any key to continue . . .
85+
```

examples/embedded_in_dat/build.xml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<project name="externaldataread" default="run">
2+
<property environment="env"/>
3+
4+
<condition property="example.home" value="${env.CPLEX_STUDIO_DIR128}/opl">
5+
<isset property="env.CPLEX_STUDIO_DIR128"/>
6+
</condition>
7+
<property name="example.home" value="${opl.home}">
8+
</property>
9+
10+
11+
<import file="${example.home}/examples/opl_interfaces/java/build_common.xml"/>
12+
<condition property="oplrun.binary" value="oplrunjava">
13+
<os family="unix"/>
14+
</condition>
15+
<property name="oplrun.binary" value="oplrun"/>
16+
17+
<target name="run" depends="platform">
18+
<exec executable="${example.home}/bin/${example.platform}/${oplrun.binary}">
19+
<arg value="oil.mod"/>
20+
<arg value="oil.dat"/>
21+
<env key="ODMS_JAVA_ARGS" value='-classpath "${example.home}/lib/oplall.jar"' />
22+
<env key="PATH" path="${example.home}/bin/${example.platform}${platform.separator}${env.PATH}"/>
23+
<env key="Path" path="${example.home}/bin/${example.platform}${platform.separator}${env.Path}"/>
24+
<env key="LD_LIBRARY_PATH" path="${example.home}/bin/${example.platform}${platform.separator}${env.JAVA_HOME}/jre/bin${platform.separator}${env.JAVA_HOME}/jre/bin/j9vm${platform.separator}${env.JAVA_HOME}/jre/bin/classic"/>
25+
<env key="LIBPATH" path="${example.home}/bin/${example.platform}${platform.separator}${env.JAVA_HOME}/jre/bin${platform.separator}${env.JAVA_HOME}/jre/bin/j9vm${platform.separator}${env.JAVA_HOME}/jre/bin/classic"/>
26+
<env key="DYLD_LIBRARY_PATH" path="${example.home}/bin/${example.platform}${platform.separator}${env.DYLD_LIBRARY_PATH}"/>
27+
<env key="LD_PRELOAD_64" path="${preload_64}"/>
28+
</exec>
29+
</target>
30+
31+
</project>

examples/embedded_in_dat/oil.dat

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
prepare {
2+
// Update this to point to your jdbc driver.
3+
// For instance mssql-jdbc-7.2.2.jre8.jar for mssql
4+
IloOplImportJava("../../external_libs/mssql-jdbc-7.2.2.jre8.jar")
5+
6+
// The jar containing the jdbc custom data source
7+
IloOplImportJava("../../lib/jdbc-custom-data-source.jar");
8+
9+
var connUrl = "jdbc:sqlserver://localhost;instanceName=SQLEXPRESS;databaseName=custom_data_source;integratedSecurity=true"
10+
11+
// The default connection string uses mssql in integratedSecurity mode, so you can leave those as empty.
12+
// Otherwise, you must set your user password here
13+
var userName = "";
14+
var password = "";
15+
16+
// Now create JdbcConfiguration
17+
var config = IloOplCallJava("com.ibm.opl.customdatasource.JdbcConfiguration", "<init>", "");
18+
config.setUrl(connUrl);
19+
20+
config.addReadQuery("Gasolines", "SELECT NAME FROM GASDATA");
21+
config.addReadQuery("Oils", "SELECT NAME FROM OILDATA");
22+
config.addReadQuery("GasData", "SELECT * FROM GASDATA");
23+
config.addReadQuery("OilData", "SELECT * FROM OILDATA");
24+
25+
// Maps result table "Results" to database table "result"
26+
config.addWriteMapping("Result", "result");
27+
28+
29+
// add custom data source
30+
IloOplCallJava("com.ibm.opl.customdatasource.JdbcCustomDataSource",
31+
"addDataSource", "", config, thisOplModel);
32+
}
33+
34+
MaxProduction = 14000;
35+
ProdCost = 4;

examples/embedded_in_dat/oil.mod

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// --------------------------------------------------------------------------
2+
// Licensed Materials - Property of IBM
3+
//
4+
// 5725-A06 5725-A29 5724-Y48 5724-Y49 5724-Y54 5724-Y55
5+
// Copyright IBM Corporation 1998, 2013. All Rights Reserved.
6+
//
7+
// Note to U.S. Government Users Restricted Rights:
8+
// Use, duplication or disclosure restricted by GSA ADP Schedule
9+
// Contract with IBM Corp.
10+
// --------------------------------------------------------------------------
11+
12+
13+
{string} Gasolines = ...;
14+
{string} Oils = ...;
15+
tuple gasType {
16+
string name;
17+
float demand;
18+
float price;
19+
float octane;
20+
float lead;
21+
}
22+
23+
tuple oilType {
24+
string name;
25+
float capacity;
26+
float price;
27+
float octane;
28+
float lead;
29+
}
30+
{gasType} GasData = ...;
31+
{oilType} OilData = ...;
32+
gasType Gas[Gasolines] = [ g.name : g | g in GasData ];
33+
oilType Oil[Oils] = [ o.name : o | o in OilData ];
34+
35+
float MaxProduction = ...;
36+
float ProdCost = ...;
37+
38+
39+
dvar float+ a[Gasolines];
40+
dvar float+ Blend[Oils][Gasolines];
41+
42+
43+
maximize
44+
sum( g in Gasolines , o in Oils )
45+
(Gas[g].price - Oil[o].price - ProdCost) * Blend[o][g]
46+
- sum( g in Gasolines ) a[g];
47+
48+
subject to {
49+
50+
ctDemand: forall( g in Gasolines )
51+
sum( o in Oils )
52+
Blend[o][g] == Gas[g].demand + 10 * a[g];
53+
54+
ctCapacity: forall( o in Oils )
55+
sum( g in Gasolines )
56+
Blend[o][g] <= Oil[o].capacity;
57+
58+
ctMaxProd: sum( o in Oils , g in Gasolines )
59+
Blend[o][g] <= MaxProduction;
60+
61+
ctOctane: forall( g in Gasolines )
62+
sum( o in Oils )
63+
(Oil[o].octane - Gas[g].octane) * Blend[o][g] >= 0;
64+
ctLead: forall( g in Gasolines )
65+
sum( o in Oils )
66+
(Oil[o].lead - Gas[g].lead) * Blend[o][g] <= 0;
67+
}
68+
tuple result {
69+
string oil;
70+
string gas;
71+
float blend;
72+
float a;
73+
}
74+
75+
{result} Result =
76+
{ <o,g,Blend[o][g],a[g]> | o in Oils, g in Gasolines };
77+
78+
execute DISPLAY_RESULT{
79+
writeln("Result = ",Result)
80+
}

examples/embedded_in_dat/run.bat

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
@echo off
2+
rem Run the example
3+
4+
rem If CPLEX_STUDIO_DIR128 is not already defined, add the following line :
5+
rem CPLEX_STUDIO_DIR128=%~dp0\..\..\..\..\..
6+
7+
cd %~dp0
8+
9+
set "arg="
10+
:Loop
11+
IF "%1"=="" GOTO Continue
12+
set "arg=%arg% %1"
13+
SHIFT
14+
GOTO Loop
15+
:Continue
16+
17+
call "%CPLEX_STUDIO_DIR128%\opl\ant\bin\ant" -Dexample.arg.line="%arg%" run
18+
19+

examples/ilo_opl_call_java/oil.dat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ prepare {
1010
IloOplImportJava("../../lib/jdbc-custom-data-source.jar");
1111

1212
IloOplCallJava("com.ibm.opl.customdatasource.JdbcCustomDataSource",
13-
"addDataSource", "", jdbcConfigXml, thisOplModel);
13+
"addDataSourceXMLConfig", "", jdbcConfigXml, thisOplModel);
1414
}
1515

1616
MaxProduction = 14000;

lib/jdbc-custom-data-source.jar

294 Bytes
Binary file not shown.

src/main/java/com/ibm/opl/customdatasource/JdbcConfiguration.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,11 @@ public JdbcConfiguration() {
4040
public String getUrl() {
4141
return _url;
4242
}
43-
43+
44+
public void setUrl(String url) {
45+
_url = url;
46+
}
47+
4448
public String getUser() {
4549
return _user;
4650
}
@@ -52,11 +56,31 @@ public String getPassword() {
5256
public Properties getReadQueries() {
5357
return _readProperties;
5458
}
59+
60+
/**
61+
* Adds a read query to the datasource.
62+
*
63+
* The specified query is used to populate the OPL data which name is specified.
64+
* @param name The OPL data
65+
* @param query The read query
66+
*/
67+
public void addReadQuery(String name, String query) {
68+
_readProperties.setProperty(name, query);
69+
}
5570

5671
public Properties getWriteMapping() {
5772
return _writeProperties;
5873
}
5974

75+
/**
76+
* Adds a write mapping to the datasource.
77+
* @param name The OPL output name.
78+
* @param target The database table to map the output to.
79+
*/
80+
public void addWriteMapping(String name, String target) {
81+
_writeProperties.setProperty(name, target);
82+
}
83+
6084
/**
6185
* Reads the configuration from the specified file.
6286
*

src/main/java/com/ibm/opl/customdatasource/JdbcCustomDataSource.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,31 @@ public class JdbcCustomDataSource extends IloCustomOplDataSource {
3131
/**
3232
* Adds a custom data source to a model.
3333
*
34-
* @param configXml The xml configuration for the data source
34+
* @param xmlFile The xml configuration for the data source
3535
* @param model The OPL Model
3636
*/
37-
public static void addDataSource(String configXml, IloOplModel model) throws IOException {
37+
public static void addDataSourceXMLConfig(String xmlFile, IloOplModel model) throws IOException {
3838
JdbcConfiguration config = new JdbcConfiguration();
39-
config.read(configXml);
39+
config.read(xmlFile);
4040
IloOplFactory factory = IloOplFactory.getOplFactoryFrom(model);
4141
IloOplModelDefinition definition = model.getModelDefinition();
4242
JdbcCustomDataSource source = new JdbcCustomDataSource(config, factory, definition);
4343
model.addDataSource(source);
4444
}
45-
45+
46+
/**
47+
* Adds a custom data source to a model.
48+
*
49+
* @param config The JDBC configuration object
50+
* @param model The OPL Model
51+
*/
52+
public static void addDataSource(JdbcConfiguration config, IloOplModel model) {
53+
IloOplFactory factory = IloOplFactory.getOplFactoryFrom(model);
54+
IloOplModelDefinition definition = model.getModelDefinition();
55+
JdbcCustomDataSource source = new JdbcCustomDataSource(config, factory, definition);
56+
model.addDataSource(source);
57+
}
58+
4659
/**
4760
* Creates a new JDBC custom data source, based on the specified configuration.
4861
*
@@ -71,6 +84,7 @@ void fillNamesAndTypes(IloTupleSchema schema, String[] names, Type[] types) {
7184
* is generated.
7285
*/
7386
public void customRead() {
87+
long startTime = System.currentTimeMillis();
7488
System.out.println("Reading elements from database");
7589
Properties prop = _configuration.getReadQueries();
7690
Enumeration<?> propertyNames = prop.propertyNames();
@@ -80,7 +94,8 @@ public void customRead() {
8094
System.out.println("Reading " + name + " using \"" + query + "\"");
8195
customRead(name, query);
8296
}
83-
System.out.println("Done");
97+
long endTime = System.currentTimeMillis();
98+
System.out.println("Done (" + (endTime - startTime)/1000.0 + " s)");
8499
}
85100

86101
public void customRead(String name, String query) {

src/main/java/com/ibm/opl/customdatasource/JdbcWriter.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import ilog.concert.IloTuple;
44
import ilog.opl.IloOplElement;
55
import ilog.opl.IloOplElementDefinition;
6+
import ilog.opl.IloOplFactory;
67
import ilog.opl.IloOplElementDefinitionType.Type;
78
import ilog.opl.IloOplModel;
89
import ilog.opl.IloOplModelDefinition;
@@ -24,6 +25,13 @@ public class JdbcWriter {
2425
private JdbcConfiguration _configuration;
2526
private IloOplModelDefinition _def;
2627
private IloOplModel _model;
28+
29+
public static void writeOutput(JdbcConfiguration config, IloOplModel model) {
30+
IloOplFactory factory = IloOplFactory.getOplFactoryFrom(model);
31+
IloOplModelDefinition definition = model.getModelDefinition();
32+
JdbcWriter writer = new JdbcWriter(config, definition, model);
33+
writer.customWrite();
34+
}
2735

2836
public JdbcWriter(JdbcConfiguration configuration, IloOplModelDefinition def, IloOplModel model) {
2937
_configuration = configuration;

0 commit comments

Comments
 (0)