Skip to content

Commit

Permalink
Merge pull request #41 from COBREXA/mk-accessors
Browse files Browse the repository at this point in the history
add support for Accessors, patch a weird type issue
  • Loading branch information
exaexa authored Jun 19, 2024
2 parents a566062 + 88a996a commit 10e16a6
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 6 deletions.
8 changes: 6 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
name = "ConstraintTrees"
uuid = "5515826b-29c3-47a5-8849-8513ac836620"
authors = ["The developers of ConstraintTrees.jl"]
version = "1.1.0"
version = "1.2.0"

[deps]
ConstructionBase = "187b0558-2788-49d3-abe0-74a17ed4e7c9"
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"

[compat]
Accessors = "0.1"
Clarabel = "0.6"
ConstructionBase = "1.5"
DataStructures = "0.18"
DocStringExtensions = "0.8, 0.9"
GLPK = "1"
Expand All @@ -19,6 +22,7 @@ SCIP = "0.11"
julia = "1"

[extras]
Accessors = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697"
Clarabel = "61c947e1-3e6d-4ee4-985a-eec8c727bd6e"
Downloads = "f43a241f-c20a-4ad4-852c-f6b1247861c6"
GLPK = "60bf3e95-4087-53dc-ae20-288a0d20c6a6"
Expand All @@ -28,4 +32,4 @@ SCIP = "82193955-e24f-5292-bf16-6f2c5261a85f"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Clarabel", "Downloads", "GLPK", "JuMP", "SBML", "SCIP", "Test"]
test = ["Accessors", "Clarabel", "Downloads", "GLPK", "JuMP", "SBML", "SCIP", "Test"]
1 change: 1 addition & 0 deletions docs/Project.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[deps]
Accessors = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697"
Clarabel = "61c947e1-3e6d-4ee4-985a-eec8c727bd6e"
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
Expand Down
38 changes: 36 additions & 2 deletions docs/src/1-metabolic-modeling.jl
Original file line number Diff line number Diff line change
Expand Up @@ -357,14 +357,15 @@ Dict(k => v.fluxes.R_BIOMASS_Ecoli_core_w_GAM for (k, v) in result.community)
# change at a single place of the tree may easily change values also in other
# parts of any trees, including completely different trees
# - the "convenient way" of making sure that the above problem never happens is
# to deep-copy the whole tree structure, which is typically quite detrimental
# to memory use and program efficiency
# to copy-on-write the whole tree structure, which is typically quite
# detrimental to memory use and program efficiency
#
#md # !!! danger "Rules of thumb for safe use of in-place modification"
#md # Only use the in-place modifications if:
#md # - there is code that explicitly makes sure there is no false sharing via references, e.g. using a deep copy
#md # - the in-place modifications are the last thing happening to the constraint tree before it is used by the solver
#md # - the in-place modification code is not a part of a re-usable library
#md # - you are using a suitable wrapper interface such as [Accessors.jl](https://github.com/JuliaObjects/Accessors.jl)
#
# Now, if you are completely sure that ignoring the robustness guidelines will
# help your code, you can do the in-place tree modifications quite easily using
Expand All @@ -391,6 +392,39 @@ result.exchanges

@test result_with_more_oxygen.exchanges.oxygen < -19.0 #src

# ### Alternative: Using Accessors.jl
#
# Accessors.jl implement a "lensy" way to update immutable data structures.
# That comes with a nice outcome of doing the right amount of shallow copyies
# for you automatically, thus avoiding much of the technical danger of in-place
# modifications. (You still lose the equational reasoning on your code, but that
# may not be an issue at all in usual codebases.)
#
# Accessors interface is used simply through macros `@set` (which sets a deeply
# nested field in a structure, returning a modified copy), or with `@reset`
# which automatically "assigns" the result back to the original variable:

using Accessors

c = @set c.exchanges.biomass.bound = C.Between(-50, 50)

# The above code is equivalent to:

@reset c.exchanges.biomass.bound = C.Between(-50, 50)

# ...and it is also possible to use string and symbol indexes to pick the
# individual tree items:

@reset c[:exchanges]["biomass"].bound = C.Between(-50, 50)

# All of these operations give us:

c.exchanges.biomass

@test typeof(c.exchanges.biomass.bound) == C.Between #src
@test c.exchanges.biomass.bound.lower == -50 #src
@test c.exchanges.biomass.bound.upper == 50 #src

# ## Seeing the differences between the results
#
# ConstraintTrees.jl defines its own version of `zip` function that can apply a
Expand Down
13 changes: 11 additions & 2 deletions src/tree.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import ConstructionBase
import DataStructures: SortedDict, SortedSet

"""
Expand Down Expand Up @@ -75,10 +76,15 @@ Base.values(x::Tree) = values(elems(x))
Base.getindex(x::Tree, sym::Symbol) = getindex(elems(x), sym)
Base.getindex(x::Tree, str::String) = getindex(x, Symbol(str))

Base.setindex!(x::Tree{X}, val::E, sym::Symbol) where {X,E<:X} =
Base.setindex!(x::Tree{X}, val::E, sym::Symbol) where {X,E<:Union{X,Tree{X}}} =
setindex!(elems(x), val, sym)
Base.setindex!(x::Tree, val, str::String) = setindex!(x, val, Symbol(str))

Base.setindex(x::Tree{X}, val::E, sym::Symbol) where {X,E<:Union{X,Tree{X}}} =
Tree{X}(elems(x)..., sym => val)
Base.setindex(x::Tree{X}, val::E, str::String) where {X,E<:Union{X,Tree{X}}} =
Base.setindex(x, val, Symbol(str))

Base.delete!(x::Tree, sym::Symbol) = delete!(elems(x), sym)
Base.delete!(x::Tree, str::String) = delete!(x, Symbol(str))

Expand All @@ -88,9 +94,12 @@ Base.hasproperty(x::Tree, sym::Symbol) = haskey(x, sym)

Base.getproperty(x::Tree, sym::Symbol) = elems(x)[sym]

Base.setproperty!(x::Tree{X}, sym::Symbol, val::E) where {X,E<:X} =
Base.setproperty!(x::Tree{X}, sym::Symbol, val::E) where {X,E<:Union{X,Tree{X}}} =
setindex!(elems(x), val, sym)

ConstructionBase.setproperties(x::Tree{T}, props::NamedTuple) where {T} =
Tree{T}(elems(x)..., pairs(props)...)

#
# Algebraic construction
#
Expand Down

2 comments on commit 10e16a6

@exaexa
Copy link
Member Author

@exaexa exaexa commented on 10e16a6 Jun 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/109332

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.2.0 -m "<description of version>" 10e16a676697742b71d1d672536a6a00acae40ef
git push origin v1.2.0

Please sign in to comment.