Skip to content

Commit

Permalink
Merge branch 'testing'
Browse files Browse the repository at this point in the history
  • Loading branch information
nfbvs committed Jan 27, 2024
2 parents 02b7924 + 1ea95ff commit eff4b24
Show file tree
Hide file tree
Showing 280 changed files with 16,188 additions and 18,377 deletions.
39 changes: 3 additions & 36 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ add_subdirectory(amplp/examples/modhide)
add_subdirectory(amplp/examples/heuristic)
add_subdirectory(amplp/examples/ampljson)

# amplxl
add_subdirectory(amplxl)

# simpbit
add_ampl_library(simpbit tables/simpbit.c)
target_link_libraries(simpbit PUBLIC asl)
Expand Down Expand Up @@ -77,42 +80,6 @@ target_link_libraries(ampltabl asl)
# add_test(NAME sqlite3-test COMMAND ${PYTHON_PROGRAM}
# ${CMAKE_CURRENT_BINARY_DIR}/sqlite3/tests/test.py ${AMPL_PROGRAM} $<TARGET_FILE:sqlite3th>)
# endif (SQLite3_FOUND)

# Build zlib from the included sources, it generates the target
# zlibstatic that we can link statically to
set(SKIP_INSTALL_ALL ON) # skip install targets of zlib
add_subdirectory(amplxl/zlib-1.2.11)
addToTablesFolder(support zlib zlibstatic minigzip)
set(ZLIB_INCLUDE_DIRS amplxl/zlib-1.2.11 ${CMAKE_CURRENT_BINARY_DIR}/amplxl/zlib-1.2.11 amplxl/zlib-1.2.11/contrib/minizip)
set(ZLIB_LIBRARIES zlibstatic)

set(AMPLXL_SOURCES
amplxl/src/ampl_xl.cpp
amplxl/src/ioapi.c
amplxl/src/myunz.cpp
amplxl/src/myzip.cpp
amplxl/src/copyzip.cpp
amplxl/src/pugixml.cpp
amplxl/src/oxmlutils.cpp
amplxl/src/logger.cpp
amplxl/src/utils.cpp
amplxl/src/unzip.c
amplxl/src/zip.c)
if (WIN32)
set(AMPLXL_SOURCES ${AMPLXL_SOURCES} amplxl/src/iowin32.c)
endif()

add_ampl_library(amplxl ${AMPLXL_SOURCES}
COMPONENT ${DISTRO_COMPONENT})

set_target_properties(amplxl PROPERTIES LINKER_LANGUAGE CXX)
target_link_libraries(amplxl PRIVATE asl2 ${ZLIB_LIBRARIES})
target_include_directories(amplxl PRIVATE ${ZLIB_INCLUDE_DIRS})

file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/amplxl/tests DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/amplxl)
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/test_utils.py DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/amplxl/tests)
add_test(NAME amplxl-test COMMAND ${PYTHON_PROGRAM}
${CMAKE_CURRENT_BINARY_DIR}/amplxl/tests/test.py ${AMPL_PROGRAM} $<TARGET_FILE:amplxl>)

find_package(ODBC)
if (ODBC_FOUND)
Expand Down
6 changes: 3 additions & 3 deletions src/amplp/examples/amplcsv/src/handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ public TableConnector{
// character used to quote strings
std::string quotechar;

// weather or not to read/write the header
// whether or not to read/write the header
bool has_header;

// Whether or not to quote/unquote strings when writing/reading, defaults to false
// whether or not to quote/unquote strings when writing/reading, defaults to false
bool quotestrings;

// Whether or not to use the existing csv header (OUT only)
// whether or not to use the existing csv header (OUT only)
bool use_header;

// override functions
Expand Down
40 changes: 40 additions & 0 deletions src/amplxl/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Build zlib from the included sources, it generates the target
# zlibstatic that we can link statically to
set(SKIP_INSTALL_ALL ON) # skip install targets of zlib

set(Z_DIR zlib-1.3)
set(MZ_DIR ${Z_DIR}/contrib/minizip)

add_subdirectory(${Z_DIR} EXCLUDE_FROM_ALL)
set_property(TARGET zlibstatic PROPERTY POSITION_INDEPENDENT_CODE ON)

set(ZLIB_INCLUDE_DIRS ./${Z_DIR} ${CMAKE_CURRENT_BINARY_DIR}/${Z_DIR} ./${MZ_DIR})

set(AMPLXL_SOURCES
./src/ampl_xl.cpp
./src/myunz.cpp
./src/myzip.cpp
./src/copyzip.cpp
./src/pugixml.cpp
./src/oxmlutils.cpp
./src/logger.cpp
./src/utils.cpp
./${MZ_DIR}/ioapi.c
./${MZ_DIR}/zip.c
./${MZ_DIR}/unzip.c)

if (WIN32)
set(AMPLXL_SOURCES ${AMPLXL_SOURCES} ./${MZ_DIR}//iowin32.c)
endif()

add_ampl_library(amplxl ${AMPLXL_SOURCES}
COMPONENT ${DISTRO_COMPONENT})

set_target_properties(amplxl PROPERTIES LINKER_LANGUAGE CXX)
target_link_libraries(amplxl PRIVATE asl2 zlibstatic)
target_include_directories(amplxl PRIVATE ${ZLIB_INCLUDE_DIRS})

file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/tests DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/amplxl)
file(COPY ../test_utils.py DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/amplxl/tests)
add_test(NAME amplxl-test COMMAND ${PYTHON_PROGRAM}
${CMAKE_CURRENT_BINARY_DIR}/amplxl/tests/test.py ${AMPL_PROGRAM} $<TARGET_FILE:amplxl>)
69 changes: 69 additions & 0 deletions src/amplxl/doc.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
A table handler for spreadsheet files (.xlsx).

General information on table handlers and data correspondence between AMPL and
an external table is available at chapter 10 of the AMPL book:

https://ampl.com/learn/ampl-book/

The available options for amplxl are:

2D
Keyword to to specify that data indexed over two sets will be represented in
a two-dimensional table, with keys from one set labeling the rows, and keys
from the other set labeling the columns.

Example:
table foo IN "amplxl" "2D": [keycol1, keycol2], valcol;

alias:
Instead of writing the data to a specific .xlsx file it is possible to
define an alias. In the following example the table handler will search for
the file bar.xlsx to write the data. If the file does not exist it will be
created.

Example:
table foo OUT "amplxl" "bar": [A], B;

backup=option
Whether or not to backup the existing .xlsx file before writing to it.
The backup will create a file with the same name as the provided .xlsx file
but with an .amplback extension. To use the backup file rename the .amplback
extension to .xlsx.
Options: true (default), false.

Example:
table foo OUT "amplxl" "backup=false": [keycol1, keycol2], valcol;

external-table-spec:
Specifies the path to the .xlsx file to be read or written with the read
table and write table commands. If no file is specified, amplxl will search
for a file with the table name and the .xlsx file extension in the current
directory. If the table is to be written and the file does not exist it will
be created.

Example:
table foo OUT "amplxl" "bar.xlsx": [keycol], valcol;

verbose:
Display warnings during the execution of the read table and write table
commands.

Example:
table foo OUT "amplxl" "verbose": [keycol], valcol;

verbose=option:
Display information according to the specified option. Available options:
0 (default) - display information only on error,
1 - display warnings,
2 - display general information
3 - display debug information.

Example:
table foo OUT "amplxl" "verbose=2": [keycol], valcol;

write=option
Define how the data is written in OUT mode. Available options:
delete (default) - deletes all the rows of the current table (if it
exists) before writing the data from AMPL.
append - append the rows in AMPL to the external representation of the
table.
39 changes: 15 additions & 24 deletions src/amplxl/src/ampl_xl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,9 @@ Write_ampl_xl(AmplExports *ae, TableInfo *TI){
void
funcadd(AmplExports *ae){

/* description of handlers */

static char info[] = "amplxl\n"
"Table handler for .xlsx and .xlsm files:\n"
"one or two strings (an optional 'amplxl' and the file name,\n"
"ending in \".xlsx\" or \".xlsm\") expected before \":[...]\".";

/* Inform AMPL about the .example handlers */

add_table_handler(Read_ampl_xl, Write_ampl_xl, info, 0, 0);


add_table_handler(Read_ampl_xl, Write_ampl_xl, const_cast<char *>(doc.c_str()), 0, 0);
};


Expand Down Expand Up @@ -86,7 +77,7 @@ ExcelManager::ExcelManager(){
has_range = false;
break_mode = false;
verbose = 0;
write = "drop";
write = "delete";
backup = true;
is2D = false;
isReader = true;
Expand Down Expand Up @@ -330,8 +321,8 @@ ExcelManager::prepare(){

option_string = arg_string.substr(write_op.size());

if (option_string == "drop"){
write = "drop";
if (option_string == "delete"){
write = "delete";
}
else if (option_string == "append"){
write = "append";
Expand Down Expand Up @@ -403,7 +394,7 @@ ExcelManager::prepare(){
// we create the non existing file with the declared name
if (inout == "OUT"){

write = "drop";
write = "delete";
msg = "Declared file does not exist. Creating file ";
msg += excel_path;
msg += " with sheet ";
Expand Down Expand Up @@ -471,7 +462,7 @@ ExcelManager::manage_workbook(){
result = myunzip(excel_path, excel_iner_file, temp_folder);

if (result){
msg = "cannot extract workbook";
msg = "Cannot extract workbook. Is the file open in another application?";
logger.log(msg, LOG_ERROR);
return 1;
}
Expand All @@ -482,7 +473,7 @@ ExcelManager::manage_workbook(){
result = parse_workbook();

if (result){
msg = "cannot parse workbook";
msg = "Cannot parse workbook";
logger.log(msg, LOG_ERROR);
return 1;
}
Expand All @@ -501,13 +492,13 @@ ExcelManager::manage_workbook(){
}

if (result){
msg = "cannot parse range";
msg = "Cannot parse range";
logger.log(msg, LOG_ERROR);
return 1;
}
}

msg = "Table type: " + tableType;
msg = "Table type: " + numeric_to_string(tableType);
logger.log(msg, LOG_DEBUG);

//~ sheet_rel = sheet_rel_map[range_sheet];
Expand Down Expand Up @@ -1463,7 +1454,7 @@ ExcelWriteManager::manage_data(){

if (inout == "OUT"){

if (write == "drop"){
if (write == "delete"){

// At this point we have an estimate of the table dimensions.
// However, due to the dynamic nature of data, this estimate may not be correct.
Expand Down Expand Up @@ -2567,7 +2558,7 @@ int
ExcelWriteManager::delete_data(pugi::xml_node parent){

int include_header = 0;
if (write == std::string("drop")){
if (write == std::string("delete")){
include_header = 1;
}

Expand Down Expand Up @@ -4138,7 +4129,7 @@ ExcelWriteManager::manage_data2D(){

if (inout == "OUT"){

if (write == "drop"){
if (write == "delete"){

result = write_data_out_2D(node, first_row, last_row, first_col, last_col);
}
Expand All @@ -4149,7 +4140,7 @@ ExcelWriteManager::manage_data2D(){
}
}
else if (inout == "INOUT"){
if (write == "drop"){
if (write == "delete"){

result = write_data_inout_2D(node, first_row, last_row, first_col, last_col);
}
Expand Down Expand Up @@ -4420,7 +4411,7 @@ ExcelManager::parse_header_2D_reader(
xl_col_map[xl_col_name] = iter_col;
header.push_back(xl_col_name);

msg = "Found column header " + xl_col_name;
msg = "Found column header '" + xl_col_name + "'";
logger.log(msg, LOG_DEBUG);
}
else if (tableType != TABLE_SHEET){
Expand Down Expand Up @@ -4504,7 +4495,7 @@ ExcelManager::parse_data2D(
if (found){
// more than 1 column not mapped
msg = "Found more than one candidate for key row in 2D table. At least "
+ h_set + " and " + ampl_col_name + " are not found.";
+ h_set + " and " + ampl_col_name + " are not key columns.";
logger.log(msg, LOG_ERROR);
return 1;
}
Expand Down
Loading

0 comments on commit eff4b24

Please sign in to comment.