Skip to content

Commit

Permalink
Merge pull request #40 from COBREXA/mk-sting
Browse files Browse the repository at this point in the history
support autoconversion of string indexes to symbols
  • Loading branch information
exaexa authored Jun 6, 2024
2 parents cb155e7 + fdd5dba commit a566062
Show file tree
Hide file tree
Showing 15 changed files with 29 additions and 15 deletions.
6 changes: 5 additions & 1 deletion docs/src/0-quickstart.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

# Copyright (c) 2023, University of Luxembourg #src
# Copyright (c) 2023-2024, University of Luxembourg #src
# Copyright (c) 2023, Heinrich-Heine University Duesseldorf #src
# #src
# Licensed under the Apache License, Version 2.0 (the "License"); #src
Expand Down Expand Up @@ -73,6 +73,10 @@ s.area

s[:area].barley

# (For convenience in some cases, string indexes are also supported:)

s["area"]["barley"]

# Now let's start rewriting the problem into the constraint-tree-ish
# description. First, we only have 100 square kilometers of area:

Expand Down
2 changes: 1 addition & 1 deletion docs/src/1-metabolic-modeling.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

# Copyright (c) 2023, University of Luxembourg #src
# Copyright (c) 2023-2024, University of Luxembourg #src
# #src
# Licensed under the Apache License, Version 2.0 (the "License"); #src
# you may not use this file except in compliance with the License. #src
Expand Down
2 changes: 1 addition & 1 deletion docs/src/2-quadratic-optimization.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

# Copyright (c) 2023, University of Luxembourg #src
# Copyright (c) 2023-2024, University of Luxembourg #src
# #src
# Licensed under the Apache License, Version 2.0 (the "License"); #src
# you may not use this file except in compliance with the License. #src
Expand Down
2 changes: 1 addition & 1 deletion docs/src/3-mixed-integer-optimization.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

# Copyright (c) 2023, University of Luxembourg #src
# Copyright (c) 2023-2024, University of Luxembourg #src
# Copyright (c) 2023, Heinrich-Heine University Duesseldorf #src
# #src
# Licensed under the Apache License, Version 2.0 (the "License"); #src
Expand Down
2 changes: 1 addition & 1 deletion docs/src/4-functional-tree-processing.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

# Copyright (c) 2023, University of Luxembourg #src
# Copyright (c) 2023-2024, University of Luxembourg #src
# Copyright (c) 2023, Heinrich-Heine University Duesseldorf #src
# #src
# Licensed under the Apache License, Version 2.0 (the "License"); #src
Expand Down
2 changes: 1 addition & 1 deletion src/ConstraintTrees.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

# Copyright (c) 2023, University of Luxembourg
# Copyright (c) 2023-2024, University of Luxembourg
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion src/bound.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

# Copyright (c) 2023, University of Luxembourg
# Copyright (c) 2023-2024, University of Luxembourg
# Copyright (c) 2023, Heinrich-Heine University Duesseldorf
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand Down
2 changes: 1 addition & 1 deletion src/constraint.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

# Copyright (c) 2023, University of Luxembourg
# Copyright (c) 2023-2024, University of Luxembourg
# Copyright (c) 2023, Heinrich-Heine University Duesseldorf
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand Down
2 changes: 1 addition & 1 deletion src/constraint_tree.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

# Copyright (c) 2023, University of Luxembourg
# Copyright (c) 2023-2024, University of Luxembourg
# Copyright (c) 2023, Heinrich-Heine University Duesseldorf
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand Down
2 changes: 1 addition & 1 deletion src/linear_value.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

# Copyright (c) 2023, University of Luxembourg
# Copyright (c) 2023-2024, University of Luxembourg
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion src/quadratic_value.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

# Copyright (c) 2023, University of Luxembourg
# Copyright (c) 2023-2024, University of Luxembourg
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
6 changes: 5 additions & 1 deletion src/tree.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

# Copyright (c) 2023, University of Luxembourg
# Copyright (c) 2023-2024, University of Luxembourg
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -66,17 +66,21 @@ Base.keytype(x::Tree) = keytype(elems(x))
Base.keys(x::Tree) = keys(elems(x))

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

Base.valtype(x::Tree) = valtype(elems(x))

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} =
setindex!(elems(x), val, sym)
Base.setindex!(x::Tree, val, str::String) = 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))

Base.propertynames(x::Tree) = keys(x)

Expand Down
2 changes: 1 addition & 1 deletion src/value.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

# Copyright (c) 2023, University of Luxembourg
# Copyright (c) 2023-2024, University of Luxembourg
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
8 changes: 7 additions & 1 deletion test/misc.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

# Copyright (c) 2023, University of Luxembourg
# Copyright (c) 2023-2024, University of Luxembourg
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -80,6 +80,7 @@ end
@test hasproperty(ct1, :a)
@test !haskey(ct1, :c)
@test !hasproperty(ct1, :c)
@test haskey(ct1, "a")

@test collect(propertynames(ct1)) == [:a, :b]
@test [k for (k, _) in ct2] == [:c, :d]
Expand All @@ -89,6 +90,11 @@ end
@test_throws ErrorException :a^ct1 * ct1
@test_throws ErrorException ct1 * :a^ct1
@test C.var_count(C.variables_for(_ -> C.EqualTo(0.0), ct1 + ct2)) == 4

delete!(ct1, "a")
ct2["a"] = ct1["b"]
@test !haskey(ct1, :a)
@test haskey(ct2, :a)
end

@testset "Solution tree operations" begin
Expand Down
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

# Copyright (c) 2023, University of Luxembourg
# Copyright (c) 2023-2024, University of Luxembourg
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down

0 comments on commit a566062

Please sign in to comment.