Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various maintenance fixes and improvements #46

Merged
merged 2 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Pkg.add("NEOSServer")

## The NEOS API

This package contains an interface for the [NEOS XML-RPC API](http://www.neos-server.org/neos/NEOS-API.html).
This package contains an interface for the [NEOS XML-RPC API](https://neos-server.org/neos/xml-rpc.html).

The following example shows how you can interact with the API. Wrapped XML-RPC
functions begin with `neos_` and are exported.
Expand Down
31 changes: 20 additions & 11 deletions src/NEOSServer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,23 @@ function __init__()
end

"""
Server(email)
Server(email::String)

Construct a `Server` object. The `email` argument should take the users
valid email address (required for solvers like CPLEX).

## Example

```julia
julia> server = NEOSServer.Server("[email protected]")
```
"""
struct Server
user_agent::String
host::String
content_type::String
email::String

function Server(email::String)
return new("NEOS.jl", "https://neos-server.org:3333", "text/xml", email)
end
Expand All @@ -54,6 +61,7 @@ end
# ==============================================================================

_add_text(value, arg::String) = LightXML.add_text(value, arg)

function _add_text(value, arg)
return LightXML.add_text(LightXML.new_child(value, "int"), string(arg))
end
Expand Down Expand Up @@ -83,6 +91,7 @@ function _get_values!(values, node)
_get_values!(values, child)
end
end
return
end

function _api_method(s::Server, name::String, args...)
Expand All @@ -108,15 +117,15 @@ function _decode_to_string(s)
return String(Base64.base64decode(replace(s, "\n" => "")))
end

neos_help(s::Server) = _api_method(s, "help")[1]
neos_help(s::Server) = only(_api_method(s, "help"))

neos_welcome(s::Server) = _api_method(s, "welcome")[1]
neos_welcome(s::Server) = only(_api_method(s, "welcome"))

neos_version(s::Server) = _api_method(s, "version")[1]
neos_version(s::Server) = only(_api_method(s, "version"))

neos_ping(s::Server) = _api_method(s, "ping")[1]
neos_ping(s::Server) = only(_api_method(s, "ping"))

neos_printQueue(s::Server) = _api_method(s, "printQueue")[1]
neos_printQueue(s::Server) = only(_api_method(s, "printQueue"))

function neos_getSolverTemplate(
s::Server,
Expand All @@ -125,7 +134,7 @@ function neos_getSolverTemplate(
inputMethod::String,
)
ret = _api_method(s, "getSolverTemplate", category, solvername, inputMethod)
return ret[1]
return only(ret)
end

neos_listAllSolvers(s::Server) = _api_method(s, "listAllSolvers")
Expand All @@ -142,11 +151,11 @@ function neos_submitJob(s::Server, xmlstring::String)
end

function neos_getJobStatus(s::Server, j::Job)
return _api_method(s, "getJobStatus", j.number, j.password)[1]
return only(_api_method(s, "getJobStatus", j.number, j.password))
end

function neos_getCompletionCode(s::Server, j::Job)
return _api_method(s, "getCompletionCode", j.number, j.password)[1]
return only(_api_method(s, "getCompletionCode", j.number, j.password))
end

function neos_getJobInfo(s::Server, j::Job)
Expand All @@ -173,7 +182,7 @@ end

function neos_getFinalResultsNonBlocking(s::Server, j::Job)
ret = _api_method(s, "getFinalResultsNonBlocking", j.number, j.password)
return _decode_to_string(ret[1])
return _decode_to_string(only(ret))
end

function neos_getIntermediateResultsNonBlocking(s::Server, j::Job, offset::Int)
Expand All @@ -189,7 +198,7 @@ end

function neos_getOutputFile(s::Server, j::Job, fileName::String)
ret = _api_method(s, "getOutputFile", j.number, j.password, fileName)
return _decode_to_string(ret[1])
return _decode_to_string(only(ret))
end

# ==============================================================================
Expand Down
17 changes: 17 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ function test_listCategories()
return
end

function test_getSolverTemplate()
template = neos_getSolverTemplate(SERVER, "NCO", "Ipopt", "AMPL")
@test occursin("CDATA", template)
return
end

function test_Optimizer_no_email()
@test_throws UndefVarError Optimizer(solver = "Ipopt")
return
Expand All @@ -87,6 +93,17 @@ function test_Optimizer()
return
end

function test_Optimizer_unsupported_solver()
@test_throws(
ErrorException(
"NEOS.Optimizer only supports the following solvers: " *
join(collect(keys(NEOS._SUPPORTED_SOLVERS)), ", "),
odow marked this conversation as resolved.
Show resolved Hide resolved
),
NEOSServer.Optimizer(email = EMAIL, solver = "foobar"),
)
return
end

function test_Optimizer_options()
model = MOI.Utilities.Model{Float64}()
x = MOI.add_variable(model)
Expand Down
Loading