Skip to content

Commit ce64da5

Browse files
committed
Added execute() functionnality + can configure (autodrop,
createSteatement, insertStatement) on output
1 parent 7a6424f commit ce64da5

File tree

9 files changed

+438
-56
lines changed

9 files changed

+438
-56
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# Advanced queries
2+
3+
This sample shows how advanced SQL queries and statements can be used to
4+
create database. This version offers the most flexibility for outputs to database.
5+
6+
# Setup
7+
8+
Before you run the sample, you will need to edit [oil.dat](oil.dat), and configure
9+
paths. In particular, you want to add the JDBC connector jar, for instance:
10+
11+
```
12+
// Update this to point to your jdbc driver.
13+
// For instance mssql-jdbc-7.2.2.jre8.jar for mssql
14+
IloOplImportJava("../../external_libs/mssql-jdbc-7.2.2.jre8.jar")
15+
```
16+
17+
And you want to specify the configuration file for your database connection:
18+
19+
```
20+
// This is the configuration file for accessing your database.
21+
var jdbcConfigXml = "../../data/db_mssql.xml";
22+
```
23+
24+
Lastly, you want to point to the jdbc custom data source jar.
25+
It is distributed as binaries in the [lib](../../lib) directory:
26+
27+
```
28+
// The jar containing the jdbc custom data source
29+
IloOplImportJava("../../lib/jdbc-custom-data-source.jar");
30+
```
31+
32+
# Customize table creation
33+
34+
Instead of using default table creation, this sample relies on SQL statement executed when the model is run.
35+
The output table is dropped first, then we create a table using a SQL statement:
36+
37+
```
38+
config.execute("DROP TABLE result");
39+
config.execute("CREATE TABLE result(oil VARCHAR(30), gas VARCHAR(30), blend FLOAT, a FLOAT)");
40+
```
41+
42+
Then we configure the JdbcWriter to use an insert statement:
43+
44+
```
45+
config.addInsertStatement("Result", "INSERT INTO result(oil, gas, blend, a) VALUES (?,?,?,?)");
46+
```
47+
48+
# Running the sample
49+
50+
To run the sample, invoke the `run.bat` script:
51+
52+
53+
```
54+
[P36] D:\>
55+
run.bat
56+
Buildfile: D:\opl\OPL-jdbc-custom-data-source\examples\ilo_opl_call_java\build.xml
57+
58+
platform:
59+
60+
run:
61+
[exec]
62+
[exec] <<< setup
63+
[exec]
64+
[exec] Reading elements from database
65+
[exec] Reading Gasolines using "SELECT NAME FROM GASDATA"
66+
[exec] Reading OilData using "SELECT * FROM OILDATA"
67+
[exec] Reading Oils using "SELECT NAME FROM OILDATA"
68+
[exec] Reading GasData using "SELECT * FROM GASDATA"
69+
[exec] Done (0.129 s)
70+
[exec]
71+
[exec] <<< generate
72+
[exec]
73+
[exec] Tried aggregator 1 time.
74+
[exec] LP Presolve eliminated 1 rows and 0 columns.
75+
[exec] Reduced LP has 12 rows, 12 columns, and 43 nonzeros.
76+
[exec] Presolve time = 0.00 sec. (0.01 ticks)
77+
[exec]
78+
[exec] Iteration log . . .
79+
[exec] Iteration: 1 Scaled dual infeas = 0.000000
80+
[exec] Iteration: 2 Dual objective = 434000.000000
81+
[exec]
82+
[exec] <<< solve
83+
[exec]
84+
[exec]
85+
[exec] OBJECTIVE: 287750
86+
[exec] Result = {<"Crude1" "Diesel" 800 0> <"Crude1" "Regular" 2111.1 750>
87+
88+
[exec] <"Crude1" "Super" 2088.9 0> <"Crude2" "Diesel" 0 0>
89+
[exec] <"Crude2" "Regular" 4222.2 750> <"Crude2" "Super" 777.78 0>
90+
[exec] <"Crude3" "Diesel" 200 0> <"Crude3" "Regular" 3166.7 750>
91+
[exec] <"Crude3" "Super" 133.33 0>}
92+
[exec] Writing elements to database
93+
[exec] Writing Result
94+
[exec] Done (0.047 s)
95+
[exec]
96+
[exec] <<< post process
97+
[exec]
98+
[exec]
99+
[exec] <<< done
100+
[exec]
101+
102+
BUILD SUCCESSFUL
103+
```
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/advanced_queries/oil.dat

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
18+
var config = IloOplCallJava("com.ibm.opl.customdatasource.JdbcConfiguration", "<init>", "");
19+
config.setUrl(connUrl);
20+
config.setUser(userName);
21+
config.setPassword(password);
22+
23+
config.execute("DROP TABLE result");
24+
config.execute("CREATE TABLE result(oil VARCHAR(30), gas VARCHAR(30), blend FLOAT, a FLOAT)");
25+
26+
27+
config.addReadQuery("Gasolines", "SELECT NAME FROM GASDATA");
28+
config.addReadQuery("Oils", "SELECT NAME FROM OILDATA");
29+
config.addReadQuery("GasData", "SELECT * FROM GASDATA");
30+
config.addReadQuery("OilData", "SELECT * FROM OILDATA");
31+
32+
// Maps result table "Results" to that insert statement
33+
config.addInsertStatement("Result", "INSERT INTO result(oil, gas, blend, a) VALUES (?,?,?,?)");
34+
35+
// add custom data source
36+
IloOplCallJava("com.ibm.opl.customdatasource.JdbcCustomDataSource",
37+
"addDataSource", "", config, thisOplModel);
38+
}
39+
40+
MaxProduction = 14000;
41+
ProdCost = 4;

examples/advanced_queries/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/advanced_queries/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+

lib/jdbc-custom-data-source.jar

2.23 KB
Binary file not shown.

0 commit comments

Comments
 (0)