Skip to content

Commit e2622c1

Browse files
committed
Added studio integration example
1 parent 854164b commit e2622c1

File tree

6 files changed

+250
-0
lines changed

6 files changed

+250
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="ASCII"?><project>
2+
<version>1.0</version>
3+
<configs>
4+
<config default="true" name="demo" category="opl">
5+
<ref name="demo.mod" type="model">
6+
</ref>
7+
<ref name="demo.dat" type="data">
8+
</ref>
9+
</config>
10+
</configs>
11+
12+
</project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>studio_integration</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
</buildSpec>
9+
<natures>
10+
<nature>ilog.odms.ide.core.opl.project.nature</nature>
11+
</natures>
12+
</projectDescription>
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Example with integration in CPLEX Optimization Studio IDE
2+
3+
4+
## Setup
5+
6+
Before you run the sample, please make sure you created an example database.
7+
See the *Setup the sample database* section for [DB2](../../README.DB2.md),
8+
[MySQL](../../README.MySQL.md), [Microsoft SQL Server](../../README.SQLServer.md).
9+
10+
Then, you will need to edit [jdbc.js](jdbc.js), and configure paths.
11+
In particular, you want to add the JDBC connector jar. For convenience you can
12+
also just set environment variables OPL_JDBC_DRIVER to point to your driver location
13+
and OPL_JDC_LIBS to the directory containing `jdbc-custom-data-source.jar`
14+
15+
```
16+
// OPL_JDBC_DRIVER points to the jar for the jdbc driver you want to use.
17+
var jdbc_driver = IloOplGetEnv("OPL_JDBC_DRIVER");
18+
if (! jdbc_driver ) {
19+
jdbc_driver = "../../external_libs/mssql-jdbc-7.2.2.jre8.jar"; // default for this project
20+
}
21+
22+
// OPL_JDBC_LIBS points to the directory containing the library needed for this sample.
23+
// You want to put jdbc-custom-data-source.jar there.
24+
var libs = IloOplGetEnv("OPL_JDBC_LIBS");
25+
if (! libs ) {
26+
libs = "../../lib"; // default value use the lib at the root of this project
27+
}
28+
```
29+
30+
## Data definition
31+
32+
The data input definition relies on support functions defined in `jdbc.js`.
33+
34+
You include that script using:
35+
36+
```
37+
includeScript("jdbc.js");
38+
```
39+
40+
Then use the defined functions to create a connector and define inputs:
41+
42+
```
43+
// Create the jdbc custom data source
44+
var db = JDBCConnector("jdbc:sqlserver://localhost;instanceName=SQLEXPRESS;databaseName=custom_data_source;integratedSecurity=true");
45+
46+
// input data
47+
db.read("Gasolines", "SELECT NAME FROM GASDATA");
48+
db.read("Oils", "SELECT NAME FROM OILDATA");
49+
db.read("GasData", "SELECT * FROM GASDATA");
50+
db.read("OilData", "SELECT * FROM OILDATA");
51+
52+
// create result table
53+
db.execute("DROP TABLE result");
54+
db.execute("CREATE TABLE result(oil VARCHAR(30), gas VARCHAR(30), blend FLOAT, a FLOAT)");
55+
56+
// Formerly: items to DBUpdate(db, )
57+
db.update("items", "INSERT INTO result(oil, gas, blend, a) VALUES (?,?,?,?)");
58+
```
59+
60+
61+
## Running the sample
62+
63+
Import the project in your studio. Create a new *Run Configuration*.
64+
In the Projects view, drag and drop the `integration.mod` and `integration.dat` to the configuration.
65+
You are now ready to run the sample in the studio.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// ----------------------------------------------------------------------------
2+
// Sample material distributed under Apache 2.0 license.
3+
//
4+
// Copyright IBM Corporation 2019. All Rights Reserved.
5+
// ----------------------------------------------------------------------------
6+
7+
prepare {
8+
includeScript("jdbc.js");
9+
10+
// Create the jdbc custom data source
11+
var db = JDBCConnector("jdbc:sqlserver://localhost;instanceName=SQLEXPRESS;databaseName=custom_data_source;integratedSecurity=true");
12+
13+
// input data
14+
db.read("Gasolines", "SELECT NAME FROM GASDATA");
15+
db.read("Oils", "SELECT NAME FROM OILDATA");
16+
db.read("GasData", "SELECT * FROM GASDATA");
17+
db.read("OilData", "SELECT * FROM OILDATA");
18+
19+
// create result table
20+
db.execute("DROP TABLE result");
21+
db.execute("CREATE TABLE result(oil VARCHAR(30), gas VARCHAR(30), blend FLOAT, a FLOAT)");
22+
23+
// Formerly: items to DBUpdate(db, )
24+
db.update("items", "INSERT INTO result(oil, gas, blend, a) VALUES (?,?,?,?)");
25+
}
26+
27+
MaxProduction = 14000;
28+
ProdCost = 4;
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// ----------------------------------------------------------------------------
2+
// Sample material distributed under Apache 2.0 license.
3+
//
4+
// Copyright IBM Corporation 2019. All Rights Reserved.
5+
// ----------------------------------------------------------------------------
6+
7+
8+
{string} Gasolines = ...;
9+
{string} Oils = ...;
10+
tuple gasType {
11+
string name;
12+
float demand;
13+
float price;
14+
float octane;
15+
float lead;
16+
}
17+
18+
tuple oilType {
19+
string name;
20+
float capacity;
21+
float price;
22+
float octane;
23+
float lead;
24+
}
25+
{gasType} GasData = ...;
26+
{oilType} OilData = ...;
27+
gasType Gas[Gasolines] = [ g.name : g | g in GasData ];
28+
oilType Oil[Oils] = [ o.name : o | o in OilData ];
29+
30+
float MaxProduction = ...;
31+
float ProdCost = ...;
32+
33+
34+
dvar float+ a[Gasolines];
35+
dvar float+ Blend[Oils][Gasolines];
36+
37+
38+
maximize
39+
sum( g in Gasolines , o in Oils )
40+
(Gas[g].price - Oil[o].price - ProdCost) * Blend[o][g]
41+
- sum( g in Gasolines ) a[g];
42+
43+
subject to {
44+
45+
ctDemand: forall( g in Gasolines )
46+
sum( o in Oils )
47+
Blend[o][g] == Gas[g].demand + 10 * a[g];
48+
49+
ctCapacity: forall( o in Oils )
50+
sum( g in Gasolines )
51+
Blend[o][g] <= Oil[o].capacity;
52+
53+
ctMaxProd: sum( o in Oils , g in Gasolines )
54+
Blend[o][g] <= MaxProduction;
55+
56+
ctOctane: forall( g in Gasolines )
57+
sum( o in Oils )
58+
(Oil[o].octane - Gas[g].octane) * Blend[o][g] >= 0;
59+
ctLead: forall( g in Gasolines )
60+
sum( o in Oils )
61+
(Oil[o].lead - Gas[g].lead) * Blend[o][g] <= 0;
62+
}
63+
tuple result {
64+
string oil;
65+
string gas;
66+
float blend;
67+
float a;
68+
}
69+
70+
{result} Result =
71+
{ <o,g,Blend[o][g],a[g]> | o in Oils, g in Gasolines };
72+
73+
execute DISPLAY_RESULT{
74+
writeln("Result = ",Result)
75+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// ----------------------------------------------------------------------------
2+
// Sample material distributed under Apache 2.0 license.
3+
//
4+
// Copyright IBM Corporation 2019. All Rights Reserved.
5+
// ----------------------------------------------------------------------------
6+
7+
//
8+
// Before running this sample, please review and setup the following
9+
//
10+
11+
// OPL_JDBC_DRIVER points to the jar for the jdbc driver you want to use.
12+
var jdbc_driver = IloOplGetEnv("OPL_JDBC_DRIVER");
13+
if (! jdbc_driver ) {
14+
jdbc_driver = "../../external_libs/mssql-jdbc-7.2.2.jre8.jar"; // default for this project
15+
}
16+
17+
// OPL_JDBC_LIBS points to the directory containing the library needed for this sample.
18+
// You want to put jdbc-custom-data-source.jar there.
19+
var libs = IloOplGetEnv("OPL_JDBC_LIBS");
20+
if (! libs ) {
21+
libs = "../../lib"; // default value use the lib at the root of this project
22+
}
23+
24+
25+
//
26+
// From this point, nothing is to be edited.
27+
//
28+
29+
// Update this to point to your jdbc driver.
30+
IloOplImportJava(jdbc_driver)
31+
32+
33+
34+
// The jar containing the jdbc custom data source
35+
IloOplImportJava(libs + "/jdbc-custom-data-source.jar");
36+
37+
function JDBCConnector(url) {
38+
// Now create JdbcConfiguration
39+
this.db = IloOplCallJava("com.ibm.opl.customdatasource.JdbcConfiguration", "<init>", "");
40+
this.db.setUrl(url);
41+
// add custom data source
42+
IloOplCallJava("com.ibm.opl.customdatasource.JdbcCustomDataSource",
43+
"addDataSource", "", this.db, thisOplModel);
44+
this.read = __JDBCConnector_read;
45+
this.execute = __JDBCConnector_execute;
46+
this.update = __JDBCConnector_update;
47+
return this;
48+
}
49+
50+
function __JDBCConnector_read(name, query) {
51+
this.db.addReadQuery(name, query);
52+
}
53+
function __JDBCConnector_execute(statement) {
54+
this.db.execute(statement);
55+
}
56+
function __JDBCConnector_update(name, statement) {
57+
this.db.addInsertStatement(name, statement);
58+
}

0 commit comments

Comments
 (0)