Skip to content

Commit c0710c8

Browse files
committed
Adding pom.xml for mvn build + unittests using sqlite
1 parent 41a4b30 commit c0710c8

File tree

7 files changed

+448
-0
lines changed

7 files changed

+448
-0
lines changed

pom.xml

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<name>OPL jdbc database access</name>
8+
9+
<groupId>com.ibm.optim.opl</groupId>
10+
<artifactId>jdbc-custom-data-source</artifactId>
11+
<version>1.0-SNAPSHOT</version>
12+
<packaging>jar</packaging>
13+
14+
<properties>
15+
<jdk.version>1.7</jdk.version>
16+
<cplex_home>${env.CPLEX_STUDIO_DIR128}</cplex_home>
17+
<cplex_version>12.8.0.0</cplex_version>
18+
</properties>
19+
20+
<dependencies>
21+
<dependency>
22+
<!-- To install oplall.jar on your machine, do something like:
23+
mvn install:install-file "-Dfile=%CPLEX_STUDIO_DIR128%\opl\lib\oplall.jar" -DgroupId=com.ibm.ilog.optim -DartifactId=oplall -Dversion=12.8.0.0 -Dpackaging=jar
24+
-->
25+
<groupId>com.ibm.ilog.optim</groupId>
26+
<artifactId>oplall</artifactId>
27+
<version>${cplex_version}</version>
28+
</dependency>
29+
<dependency>
30+
<groupId>org.xerial</groupId>
31+
<artifactId>sqlite-jdbc</artifactId>
32+
<version>3.27.2.1</version>
33+
<scope>test</scope>
34+
</dependency>
35+
<dependency>
36+
<groupId>junit</groupId>
37+
<artifactId>junit</artifactId>
38+
<version>4.11</version>
39+
<scope>test</scope>
40+
</dependency>
41+
</dependencies>
42+
43+
<build>
44+
<plugins>
45+
<plugin>
46+
<groupId>org.apache.maven.plugins</groupId>
47+
<artifactId>maven-compiler-plugin</artifactId>
48+
<version>2.3.2</version>
49+
<configuration>
50+
<source>${jdk.version}</source>
51+
<target>${jdk.version}</target>
52+
</configuration>
53+
</plugin>
54+
55+
<plugin>
56+
<groupId>org.apache.maven.plugins</groupId>
57+
<artifactId>maven-jar-plugin</artifactId>
58+
<version>3.1.2</version>
59+
<configuration>
60+
<excludes>
61+
<exclude>**/log4j.properties</exclude>
62+
</excludes>
63+
</configuration>
64+
</plugin>
65+
</plugins>
66+
</build>
67+
68+
<profiles>
69+
<profile>
70+
<id>binary.windows</id>
71+
<activation>
72+
<os>
73+
<family>windows</family>
74+
</os>
75+
</activation>
76+
<properties>
77+
<binary.group>x64_windows</binary.group>
78+
</properties>
79+
</profile>
80+
<profile>
81+
<id>binary.linux</id>
82+
<activation>
83+
<os>
84+
<name>Linux</name>
85+
<arch>amd64</arch>
86+
</os>
87+
</activation>
88+
<properties>
89+
<binary.group>x86-64_linux</binary.group>
90+
</properties>
91+
</profile>
92+
<profile>
93+
<id>binary.plinux</id>
94+
<activation>
95+
<os>
96+
<name>Linux</name>
97+
<arch>ppc64le</arch>
98+
</os>
99+
</activation>
100+
<properties>
101+
<binary.group>ppc64le_linux</binary.group>
102+
</properties>
103+
</profile>
104+
<profile>
105+
<id>binary.osx</id>
106+
<activation>
107+
<os>
108+
<name>Mac</name>
109+
</os>
110+
</activation>
111+
<properties>
112+
<binary.group>x86-64_osx</binary.group>
113+
</properties>
114+
</profile>
115+
</profiles>
116+
</project>
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
package com.ibm.opl.customdatasource;
2+
3+
import java.io.File;
4+
import java.io.FileOutputStream;
5+
import java.io.IOException;
6+
import java.io.InputStream;
7+
import java.sql.Connection;
8+
import java.sql.DriverManager;
9+
import java.sql.Statement;
10+
11+
import static org.junit.Assert.*;
12+
13+
import org.junit.Test;
14+
15+
import ilog.concert.IloException;
16+
import ilog.opl.IloOplDataSource;
17+
import ilog.opl.IloOplErrorHandler;
18+
import ilog.opl.IloOplException;
19+
import ilog.opl.IloOplFactory;
20+
import ilog.opl.IloOplModel;
21+
import ilog.opl.IloOplModelDefinition;
22+
import ilog.opl.IloOplRunConfiguration;
23+
24+
public class JavaApiTest {
25+
public static String OIL_DAT="models/oil.dat";
26+
public static String OIL_MOD="models/oil.mod";
27+
public static String CONFIG_RESOURCE = "models/oil_sqlite.xml";
28+
29+
public static String CREATE_OIL_MOD="models/oil_create_db.mod";
30+
public static String CREATE_CONFIG_RESOURCE = "models/oil_create_db.xml";
31+
32+
33+
/**
34+
* Runs a model as a .mod file, using a set of .dat files, and a given jdbc configuration.
35+
*
36+
* If connectionString is specified, it will be used instead of the url in the jdbc configuration,
37+
* allowing for tests with database which url is not static (ex: temporary test databases).
38+
* @param modFilename The .mod file
39+
* @param datFilenames An array of .dat files
40+
* @param jdbcConfigurationFile The jdbc configuration file
41+
* @param connectionString An override url
42+
* @throws IOException
43+
* @throws IloException
44+
*/
45+
public final void runMod(String modFilename, String[] datFilenames, String jdbcConfigurationFile,
46+
String connectionString) throws IOException, IloException {
47+
// create OPL
48+
IloOplFactory.setDebugMode(true);
49+
IloOplFactory oplF = new IloOplFactory();
50+
IloOplErrorHandler errHandler = oplF.createOplErrorHandler(System.out);
51+
52+
IloOplRunConfiguration rc = null;
53+
if (datFilenames == null || datFilenames.length == 0) {
54+
rc = oplF.createOplRunConfiguration(modFilename);
55+
}
56+
else {
57+
rc = oplF.createOplRunConfiguration(modFilename, datFilenames);
58+
}
59+
60+
rc.setErrorHandler(errHandler);
61+
IloOplModel opl = rc.getOplModel();
62+
63+
IloOplModelDefinition def = opl.getModelDefinition();
64+
65+
//
66+
// Reads the JDBC configuration, initialize a JDBC custom data source
67+
// and sets the source in OPL.
68+
//
69+
JdbcConfiguration jdbcProperties = null;
70+
if (jdbcConfigurationFile != null) {
71+
jdbcProperties = new JdbcConfiguration();
72+
jdbcProperties.read(jdbcConfigurationFile);
73+
// we want to override connection string with conn string that has the actual temp db path
74+
if (connectionString != null)
75+
jdbcProperties.setUrl(connectionString);
76+
// Create the custom JDBC data source
77+
IloOplDataSource jdbcDataSource = new JdbcCustomDataSource(jdbcProperties, oplF, def);
78+
// Pass it to the model.
79+
opl.addDataSource(jdbcDataSource);
80+
}
81+
82+
opl.generate();
83+
84+
boolean success = false;
85+
if (opl.hasCplex()) {
86+
if (opl.getCplex().solve()) {
87+
success = true;
88+
}
89+
} else {
90+
if (opl.getCP().solve()) {
91+
success = true;
92+
}
93+
}
94+
if (success == true) {
95+
opl.postProcess();
96+
// write results
97+
if (jdbcProperties != null) {
98+
JdbcWriter writer = new JdbcWriter(jdbcProperties, def, opl);
99+
writer.customWrite();
100+
}
101+
}
102+
}
103+
104+
/**
105+
* Tests the JdbcCustomReader/Writer by API.
106+
*/
107+
@Test
108+
public final void testApiCall() {
109+
File tempdb = null;
110+
try {
111+
// create temp db
112+
tempdb = File.createTempFile("testApiCall", ".db");
113+
String connectionString = "jdbc:sqlite:" + tempdb.getAbsolutePath();
114+
Connection conn = null;
115+
try {
116+
// creates the db
117+
conn = DriverManager.getConnection(connectionString);
118+
} finally {
119+
if (conn != null)
120+
conn.close();
121+
}
122+
123+
// use a .mod to create tmp database
124+
String createModFilename = new File(getClass().getResource(CREATE_OIL_MOD).getFile()).getAbsolutePath();
125+
String createJdbcConfigurationFile = new File(getClass().getResource(CREATE_CONFIG_RESOURCE).getFile()).getAbsolutePath();
126+
runMod(createModFilename, null, createJdbcConfigurationFile, connectionString);
127+
128+
// now solve the oil model
129+
String modFilename = new File(getClass().getResource(OIL_MOD).getFile()).getAbsolutePath();
130+
String[] datFilenames = {new File(getClass().getResource(OIL_DAT).getFile()).getAbsolutePath()};
131+
String jdbcConfigurationFile = new File(getClass().getResource(CONFIG_RESOURCE).getFile()).getAbsolutePath();
132+
runMod(modFilename, datFilenames, jdbcConfigurationFile, connectionString);
133+
134+
} catch (IloOplException ex) {
135+
ex.printStackTrace();
136+
fail("### OPL exception: " + ex.getMessage());
137+
} catch (IloException ex) {
138+
ex.printStackTrace();
139+
fail("### CONCERT exception: " + ex.getMessage());
140+
} catch (Exception ex) {
141+
ex.printStackTrace();
142+
fail("### UNEXPECTED UNKNOWN ERROR ...");
143+
} finally {
144+
if (tempdb != null) {
145+
tempdb.delete();
146+
}
147+
}
148+
}
149+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
MaxProduction = 14000;
2+
ProdCost = 4;
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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+
{string} Gasolines = ...;
13+
{string} Oils = ...;
14+
tuple gasType {
15+
string name;
16+
float demand;
17+
float price;
18+
float octane;
19+
float lead;
20+
}
21+
22+
tuple oilType {
23+
string name;
24+
float capacity;
25+
float price;
26+
float octane;
27+
float lead;
28+
}
29+
{gasType} GasData = ...;
30+
{oilType} OilData = ...;
31+
gasType Gas[Gasolines] = [ g.name : g | g in GasData ];
32+
oilType Oil[Oils] = [ o.name : o | o in OilData ];
33+
/* Alternate way to initialize the indexing arrays 'Gas', 'Oil':
34+
gasType Gas[Gasolines];
35+
execute {
36+
for(var g in GasData) {
37+
Gas[g.name] = g
38+
}
39+
}
40+
oilType Oil[Oils];
41+
execute {
42+
for(var o in OilData) {
43+
Oil[o.name] = o
44+
}
45+
}
46+
*/
47+
float MaxProduction = ...;
48+
float ProdCost = ...;
49+
50+
dvar float+ a[Gasolines];
51+
dvar float+ Blend[Oils][Gasolines];
52+
53+
54+
maximize
55+
sum( g in Gasolines , o in Oils )
56+
(Gas[g].price - Oil[o].price - ProdCost) * Blend[o][g]
57+
- sum( g in Gasolines ) a[g];
58+
59+
subject to {
60+
61+
ctDemand: forall( g in Gasolines )
62+
sum( o in Oils )
63+
Blend[o][g] == Gas[g].demand + 10 * a[g];
64+
65+
ctCapacity: forall( o in Oils )
66+
sum( g in Gasolines )
67+
Blend[o][g] <= Oil[o].capacity;
68+
69+
ctMaxProd: sum( o in Oils , g in Gasolines )
70+
Blend[o][g] <= MaxProduction;
71+
72+
ctOctane: forall( g in Gasolines )
73+
sum( o in Oils )
74+
(Oil[o].octane - Gas[g].octane) * Blend[o][g] >= 0;
75+
ctLead: forall( g in Gasolines )
76+
sum( o in Oils )
77+
(Oil[o].lead - Gas[g].lead) * Blend[o][g] <= 0;
78+
}
79+
tuple result {
80+
string oil;
81+
string gas;
82+
float blend;
83+
float a;
84+
}
85+
86+
{result} Result =
87+
{ <o,g,Blend[o][g],a[g]> | o in Oils, g in Gasolines };
88+
89+
execute DISPLAY_RESULT{
90+
writeln("Result = ",Result)
91+
}

0 commit comments

Comments
 (0)