Design Question on managing access to mutable Java resources

I’m working on a personal learning project that involves interacting with mathematical programming solvers like the open sourced CLP or commercial solvers like Gurobi and CPLEX.

I’d like to create an abstraction layer for the solver, but I’m a bit confused about how to proceed. Main source of confusion is that the solver (java) object is mutable, and needs to be called repeatedly to add variables and constraints to build the mathematical model incrementally. Here is some pseudo-code to make this more concrete.

(def model (clp-solver/get-new-solver)) ;; creates and returns a java solver object which is mutable
(def vars (create-variables model 3)) ;; create 3 variables in model
(def expr1 (create-expression vars ...)) ;; create an expression like 3 * x1 - 4 * x2 + 0.8 * x3
(add-expression model expr1)

I’d also like to allow concurrent access to the model object for both reading and writing.

Does this mean that I define clp-solver as a record with a atom which holds the concrete java class, and implements a protocol for the solver interface?

In OO paradigm, this would be done by an adapter pattern. I’m trying to think through the most idiomatic clojure design.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.