-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCommonAccessors.jl
More file actions
230 lines (185 loc) · 6.25 KB
/
CommonAccessors.jl
File metadata and controls
230 lines (185 loc) · 6.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
##==============================================================================
## Common Accessors
##==============================================================================
# Common get and set methods
# NOTE this could be reduced with macros and function generation to even less code.
# Data levels
const DataLevel0 = Union{VariableDataLevel0, FactorDataLevel0}
const DataLevel1 = Union{VariableDataLevel1, FactorDataLevel1}
const DataLevel2 = Union{VariableDataLevel2, FactorDataLevel2}
##------------------------------------------------------------------------------
## label
##------------------------------------------------------------------------------
"""
$SIGNATURES
Return the label for a DFGNode.
"""
getLabel(v::DataLevel0) = v.label
##------------------------------------------------------------------------------
## tags
##------------------------------------------------------------------------------
"""
$SIGNATURES
Return the tags for a DFGNode.
"""
getTags(v::DataLevel0) = v.tags
"""
$SIGNATURES
Set the tags for a DFGNode.
"""
function setTags!(f::DataLevel0, tags::Union{Vector{Symbol}, Set{Symbol}})
f.tags !== tags && empty!(f.tags)
return union!(f.tags, tags)
end
##------------------------------------------------------------------------------
## timestamp
##------------------------------------------------------------------------------
"""
$SIGNATURES
Get the timestamp of a DFGNode.
"""
getTimestamp(v::DataLevel1) = v.timestamp
"""
$SIGNATURES
Set the timestamp of a Variable/Factor object in a factor graph.
Note:
Since `timestamp` is not mutable `setTimestamp!` calls `mergeVariable!` internally.
See also [`setTimestamp`](@ref)
"""
function setTimestamp!(dfg::AbstractDFG, lbl::Symbol, ts::ZonedDateTime)
if isVariable(dfg, lbl)
return mergeVariable!(dfg, setTimestamp(getVariable(dfg, lbl), ts; verbose = false))
else
return mergeFactor!(dfg, setTimestamp(getFactor(dfg, lbl), ts))
end
end
function setTimestamp!(dfg::AbstractDFG, lbl::Symbol, ts::DateTime, timezone = localzone())
return setTimestamp!(dfg, lbl, ZonedDateTime(ts, timezone))
end
##------------------------------------------------------------------------------
## solvable
##------------------------------------------------------------------------------
"""
$SIGNATURES
Variables or factors may or may not be 'solvable', depending on a user definition. Useful for ensuring atomic transactions.
Related:
- isSolveInProgress
"""
getSolvable(var::Union{VariableCompute, FactorCompute}) = var.solvable
#TODO DataLevel2
"""
$SIGNATURES
Get 'solvable' parameter for either a variable or factor.
"""
function getSolvable(dfg::AbstractDFG, sym::Symbol)
if isVariable(dfg, sym)
return getVariable(dfg, sym).solvable
elseif isFactor(dfg, sym)
return getFactor(dfg, sym).solvable
end
end
#TODO data level2 for N
"""
$SIGNATURES
Set the `solvable` parameter for either a variable or factor.
"""
function setSolvable!(node::N, solvable::Int) where {N <: DFGNode}
node.solvable = solvable
return solvable
end
"""
$SIGNATURES
Set the `solvable` parameter for either a variable or factor.
"""
function setSolvable!(dfg::AbstractDFG, sym::Symbol, solvable::Int)
if isVariable(dfg, sym)
getVariable(dfg, sym).solvable = solvable
elseif isFactor(dfg, sym)
getFactor(dfg, sym).solvable = solvable
end
return solvable
end
"""
$SIGNATURES
Variables or factors may or may not be 'solvable', depending on a user definition.
returns true if `getSolvable` > 0
Related:
- `getSolvable`(@ref)
"""
isSolvable(node::Union{VariableCompute, FactorCompute}) = getSolvable(node) > 0
##------------------------------------------------------------------------------
## solveInProgress
##------------------------------------------------------------------------------
"""
$SIGNATURES
Which variables or factors are currently being used by an active solver. Useful for ensuring atomic transactions.
DevNotes:
- Will be renamed to `data.solveinprogress` which will be in VND, not DFGNode -- see DFG #201
Related
isSolvable
"""
function getSolveInProgress(
var::Union{VariableCompute, FactorCompute},
solveKey::Symbol = :default,
)
# Variable
if var isa VariableCompute
if haskey(getSolverDataDict(var), solveKey)
return getSolverDataDict(var)[solveKey].solveInProgress
else
return 0
end
end
# Factor
return getFactorState(var).solveInProgress
end
#TODO missing set solveInProgress and graph level accessor
function isSolveInProgress(
node::Union{VariableCompute, FactorCompute},
solvekey::Symbol = :default,
)
return getSolveInProgress(node, solvekey) > 0
end
##==============================================================================
## Common Layer 2 CRUD and SET
##==============================================================================
##==============================================================================
## TAGS as a set, list, merge, remove, empty
##==============================================================================
"""
$SIGNATURES
Return the tags for a variable or factor.
"""
function listTags(dfg::AbstractDFG, sym::Symbol)
getFnc = isVariable(dfg, sym) ? getVariable : getFactor
return getTags(getFnc(dfg, sym))
end
#alias for completeness
listTags(f::DataLevel0) = getTags(f)
"""
$SIGNATURES
Merge add tags to a variable or factor (union)
"""
function mergeTags!(dfg::InMemoryDFGTypes, sym::Symbol, tags::Vector{Symbol})
getFnc = isVariable(dfg, sym) ? getVariable : getFactor
return union!(getTags(getFnc(dfg, sym)), tags)
end
mergeTags!(f::DataLevel0, tags::Vector{Symbol}) = union!(f.tags, tags)
"""
$SIGNATURES
Remove the tags from the node (setdiff)
"""
function removeTags!(dfg::InMemoryDFGTypes, sym::Symbol, tags::Vector{Symbol})
getFnc = isVariable(dfg, sym) ? getVariable : getFactor
return setdiff!(getTags(getFnc(dfg, sym)), tags)
end
removeTags!(f::DataLevel0, tags::Vector{Symbol}) = setdiff!(f.tags, tags)
"""
$SIGNATURES
Empty all tags from the node (empty)
"""
function emptyTags!(dfg::InMemoryDFGTypes, sym::Symbol)
getFnc = isVariable(dfg, sym) ? getVariable : getFactor
return empty!(getTags(getFnc(dfg, sym)))
end
emptyTags!(f::DataLevel0) = empty!(f.tags)