|
| 1 | + |
| 2 | +function _validate(dfg::DistributedFactorGraph)::Void |
| 3 | + #TODO: Erm WTH? This fails with error |
| 4 | + # if _api == nothing |
| 5 | + # error("The API is not set, please set API with setAPI() call.") |
| 6 | + # end |
| 7 | + if !dfg.api.validateConfig(dfg.api) |
| 8 | + error("The principal API indicated that it is not configured - please configure your API first.") |
| 9 | + end |
| 10 | + for mirror in dfg.mirrors |
| 11 | + if !mirror.validateConfig(mirror) |
| 12 | + error("The mirror API '$(string(typeof(mirror)))' indicated that it is not configured - please configure your API first.") |
| 13 | + end |
| 14 | + end |
| 15 | +end |
| 16 | + |
| 17 | +function addV!(dfg::DistributedFactorGraph, v::DFGVariable)::DFGVariable |
| 18 | + _validate(dfg) |
| 19 | + # Principal |
| 20 | + v = dfg.api.addV!(dfg.api, v) |
| 21 | + # Mirrors #TODO Consider async call here. |
| 22 | + map(api -> api.addV!(api, v), dfg.mirrors) |
| 23 | + return v |
| 24 | +end |
| 25 | + |
| 26 | +function addV!(dfg::DistributedFactorGraph, label::Symbol, softtype::T)::DFGVariable where {T <: DistributedFactorGraphs.InferenceVariable} |
| 27 | + _validate(dfg) |
| 28 | + |
| 29 | + v = DFGVariable(String(label)) |
| 30 | + v.nodeData.softtype = softtype |
| 31 | + v = addV!(dfg, v) |
| 32 | + return v |
| 33 | +end |
| 34 | + |
| 35 | +""" |
| 36 | +Constructs x0x1f1 from [x0; x1] and check for duplicates. |
| 37 | +""" |
| 38 | +function _constructFactorName(dfg::DistributedFactorGraph, labelVariables::Vector{Symbol})::String |
| 39 | + factorName = join(map(label -> String(label), labelVariables))*"f" |
| 40 | + # Get the maximum prioir number e.g. f5 using efficient call to get all factor summaries. |
| 41 | + duplicates = getFs(dfg, "($factorName[0-9]+)") |
| 42 | + newVal = length(duplicates) > 0 ? 0 : maximum(map(f -> parse(Int, replace(f.label, factorName, "")), duplicates)) + 1 |
| 43 | + return "$factorName$newVal" |
| 44 | +end |
| 45 | + |
| 46 | +function addF!(dfg::DistributedFactorGraph, f::DFGFactor)::DFGFactor |
| 47 | + _validate(dfg) |
| 48 | + # Principal |
| 49 | + f = dfg.api.addF!(dfg.api, f) |
| 50 | + # Mirrors #TODO Consider async call here. |
| 51 | + map(api -> api.addF!(api, f), dfg.mirrors) |
| 52 | + return f |
| 53 | +end |
| 54 | + |
| 55 | +function addF!(dfg::DistributedFactorGraph, labelVariables::Vector{Symbol}, factorFunc::R)::DFGFactor where {R <: Union{FunctorInferenceType, InferenceType}} |
| 56 | + variables = map(label -> gtV(dfg, label), labelVariables) |
| 57 | + factName = _constructFactorName(dfg, labelVariables) |
| 58 | + f = DFGFactor(-1, factName, map(v -> v.id, variables), factorFunc) |
| 59 | + return f |
| 60 | +end |
| 61 | + |
| 62 | +function getV(vId::Int64)::DFGVariable |
| 63 | + return DFGVariable(vId, "x0", VariableNodeData(), Vector{String}(), Dict{String, Any}()) |
| 64 | +end |
| 65 | + |
| 66 | +function getV(d::DFGAPI, vLabel::String)::DFGVariable |
| 67 | + _validate(dfg) |
| 68 | + return DFGVariable(0, vLabel, VariableNodeData(), Vector{String}(), Dict{String, Any}()) |
| 69 | +end |
| 70 | + |
| 71 | +function getF(d::DFGAPI, fId::Int64)::DFGFactor |
| 72 | + _validate(dfg) |
| 73 | + return DFGFactor(fId, "x0f0", [], GenericFunctionNodeData{Int64, Symbol}()) |
| 74 | +end |
| 75 | + |
| 76 | +function getF(d::DFGAPI, fLabel::String)::DFGFactor |
| 77 | + _validate(dfg) |
| 78 | + return DFGFactor(1, fLabel, [0], GenericFunctionNodeData{Int64, Symbol}()) |
| 79 | +end |
| 80 | + |
| 81 | +function updateV!(d::DFGAPI, v::DFGVariable)::DFGVariable |
| 82 | + _validate(dfg) |
| 83 | + return v |
| 84 | +end |
| 85 | + |
| 86 | +function updateF!(d::DFGAPI, f::DFGFactor)::DFGFactor |
| 87 | + _validate(dfg) |
| 88 | + return f |
| 89 | +end |
| 90 | + |
| 91 | +function deleteV!(d::DFGAPI, vId::Int64)::DFGVariable |
| 92 | + _validate(dfg) |
| 93 | + return DFGVariable(vId, "x0", VariableNodeData(), Vector{String}(), Dict{String, Any}()) |
| 94 | +end |
| 95 | +function deleteV!(d::DFGAPI, vLabel::String)::DFGVariable |
| 96 | + _validate(dfg) |
| 97 | + return DFGVariable(0, vLabel, VariableNodeData(), Vector{String}(), Dict{String, Any}()) |
| 98 | +end |
| 99 | +function deleteV!(d::DFGAPI, v::DFGVariable)::DFGVariable |
| 100 | + _validate(dfg) |
| 101 | + return v |
| 102 | +end |
| 103 | + |
| 104 | +function deleteF!(d::DFGAPI, fId::Int64)::DFGFactor |
| 105 | + _validate(dfg) |
| 106 | + return DFGFactor(fId, "x0f0", [0], GenericFunctionNodeData{Int64, Symbol}()) |
| 107 | +end |
| 108 | +function deleteF!(d::DFGAPI, fLabel::String)::DFGFactor |
| 109 | + _validate(dfg) |
| 110 | + return DFGFactor(1, fLabel, [0], GenericFunctionNodeData{Int64, Symbol}()) |
| 111 | +end |
| 112 | +function deleteF!(d::DFGAPI, f::DFGFactor)::DFGFactor |
| 113 | + _validate(dfg) |
| 114 | + return f |
| 115 | +end |
| 116 | + |
| 117 | +# Are we going to return variables related to this variable? TODO: Confirm |
| 118 | +function neighbors(d::DFGAPI, v::DFGVariable)::Dict{String, DFGVariable} |
| 119 | + _validate(dfg) |
| 120 | + return Dict{String, DFGVariable}() |
| 121 | +end |
| 122 | + |
| 123 | +# Returns a flat dictionary of the vertices, keyed by ID. |
| 124 | +# Assuming only variables here for now - think maybe not, should be variables+factors? |
| 125 | +function ls(d::DFGAPI)::Dict{Int64, DFGVariable} |
| 126 | + _validate(dfg) |
| 127 | + return Dict{Int64, DFGVariable}() |
| 128 | +end |
| 129 | + |
| 130 | +# Returns a flat dictionary of the vertices around v, keyed by ID. |
| 131 | +# Assuming only variables here for now - think maybe not, should be variables+factors? |
| 132 | +function ls(d::DFGAPI, v::DFGVariable, variableDistance=1)::Dict{Int64, DFGVariable} |
| 133 | + _validate(dfg) |
| 134 | + return Dict{Int64, DFGVariable}() |
| 135 | +end |
| 136 | + |
| 137 | +# Returns a flat dictionary of the vertices around v, keyed by ID. |
| 138 | +# Assuming only variables here for now - think maybe not, should be variables+factors? |
| 139 | +function ls(d::DFGAPI, vId::Int64, variableDistance=1)::Dict{Int64, DFGVariable} |
| 140 | + _validate(dfg) |
| 141 | + return Dict{Int64, DFGVariable}() |
| 142 | +end |
| 143 | + |
| 144 | +function subGraph(d::DFGAPI, vIds::Vector{Int64})::Dict{Int64, DFGVariable} |
| 145 | + _validate(dfg) |
| 146 | + return Dict{Int64, DFGVariable}() |
| 147 | +end |
| 148 | + |
| 149 | +function adjacencyMatrix(d::DFGAPI)::Matrix{DFGNode} |
| 150 | + _validate(dfg) |
| 151 | + return Matrix{DFGNode}(0,0) |
| 152 | +end |
0 commit comments