Skip to content

Commit

Permalink
add printCSMHistoryLogical
Browse files Browse the repository at this point in the history
  • Loading branch information
dehann committed Sep 11, 2020
1 parent 4c0867f commit 426e98f
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 2 deletions.
142 changes: 142 additions & 0 deletions src/CliqStateMachineUtils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,23 @@ function dbgSaveDFG(dfg::AbstractDFG,
folder*filename
end

clampStringLength(st::AbstractString, len::Int=5) = st[1:minimum([len; length(st)])]

function clampBufferString(st::AbstractString, max::Int, len::Int=minimum([max,length(st)]))
@assert 0 <= max "max must be greater or equal to zero"
st = clampStringLength(st, len)
for i in len:max-1 st *= " "; end
return st
end

"""
$SIGNATURES
Print one specific line of a clique state machine history.
DevNotes
- TODO refactor to use `clampBufferString` -- see e.g. `printHistoryLane`
Related:
printCliqHistorySummary, printCliqHistorySequential
Expand Down Expand Up @@ -176,6 +188,136 @@ function printCliqHistorySequential(hists::Dict{Int,Vector{Tuple{DateTime, Int,
nothing
end


"""
$SIGNATURES
Print one line of lanes summarizing all clique state machine histories.
Notes
- hiVec is vector of all cliques (i.e. lanes) to print as one LINE into `fid`
- contains `::Tuple{Int,..}` with global counter (not the default CSM counter)
- Vector of `Tuple{DateTime, Int, Function, CliqStateMachineContainer}`
Related:
printCliqHistoryLogical, printCliqHistoryLine
"""
function printHistoryLane(fid,
linecounter::Union{Int,String},
hiVec::Vector{<:Tuple},
hackCounter::NothingUnion{Base.RefValue{Int}}=nothing )
#

## build a string
line = clampBufferString("$linecounter",4)
for counter in 1:length(hiVec)
# lane marker
line *= "| "
if !isassigned(hiVec, counter)
line *= clampBufferString("", 17)
continue
end
hi = hiVec[counter]
# global counter
useCount = if hackCounter !== nothing
hackCounter[] += 1
hackCounter[]
else
hi[2]
end
line *= clampBufferString("$(useCount)",4)
# next function
nextfn = split(string(hi[3]),'.')[end]
line *= clampBufferString(nextfn, 8, 7)
# clique status
st = hi[4] isa String ? hi[4] : string(getCliqueStatus(hi[4].cliq))
line *= clampBufferString(st, 5, 4)
end
## print the string
println(fid, line)
end



"""
$SIGNATURES
Print history in swimming lanes, side by side with global sequence counter.
DevNotes
- `order` should be flexible like `Sequential` and `<:CSMRanges`.
"""
function printCSMHistoryLogical(hists::Dict{Int,Vector{Tuple{DateTime, Int, Function, CliqStateMachineContainer}}},
order::AbstractVector{Int}=sort(collect(keys(hists))),
fid=stdout )
#

# vectorize all histories in single Array
allhists = Vector{Tuple{DateTime, Int, Function, CliqStateMachineContainer}}()
alltimes = Vector{DateTime}()
allcliqids = Vector{Int}()
# "lanes" (i.e. individual cliques)
numLanes = length(order)
# "lines" (i.e. CSM steps)
maxLines = [0;0]
for (cid,hist) in hists
# find max number of lines to print later
maxLines[2] = length(hist)
maxLines[1] = maximum(maxLines)
for hi in hist
push!(allhists, hi)
push!(alltimes, hi[1])
push!(allcliqids, cid)
end
end

# sort array by timestamp element
pm = sortperm(alltimes)
allhists_ = allhists[pm]
alltimes_ = alltimes[pm]
allcliqids_ = allcliqids[pm]

# print the column titles
titles = Vector{Tuple{String, Int, String, String}}()
for ord in order
csym = 0 < length(hists[ord]) ? getFrontals(hists[ord][1][4].cliq)[1] |> string : ""
csym = clampBufferString(csym, 9)
push!(titles, ("",ord,csym,clampBufferString("", 5)) )
end
printHistoryLane(fid, "", titles)
print(fid,"----")
for i in 1:numLanes
print(fid,"+------------------")
end
println(fid,"")

globalCounter = Ref{Int}(0)
## repeat for the maximum number of "lines" (i.e. CSM steps)
for idx in 1:maxLines[1]
## build each line as vector of "lanes" (i.e. individual cliques)
allLanes = Vector{Tuple{DateTime, Int, Function, CliqStateMachineContainer}}(undef, numLanes)

laIdx = 0
for laId in order
laIdx += 1
# if history data exists for this line (idx) and lane (laId), then build a new lane Tuple
if idx <= length(hists[laId])
# FIXME, swat first element with global counter (not local as stored in hists)
# @show hists[laId][idx][3]
allLanes[laIdx] = hists[laId][idx]
end
end

# show only one line if whichstep is not nothing
# if whichstep === nothing || (whichstep[1] == allcliqids_[idx] && whichstep[2] == hiln[2])
printHistoryLane(fid, idx, allLanes, globalCounter)
end
end
# print each line of the sorted array with correct cliqid marker



"""
$SIGNATURES
Expand Down
4 changes: 2 additions & 2 deletions src/IncrementalInference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ export *,
filterHistAllToArray,
cliqHistFilterTransitions,
printCliqSummary,
printHistoryLine,
printHistoryLine, printHistoryLane,
printCliqHistorySummary,
printCliqHistorySequential,
printCliqHistorySequential, printCSMHistoryLogical,
printGraphSummary,
printSummary,
print,
Expand Down

0 comments on commit 426e98f

Please sign in to comment.