Skip to content

Model JSON to Mermaid #609

@rburghol

Description

@rburghol

@COBrogan @BrendanBrogan
Known Problems:

  • Parentheses in equations breaks everything.
  • Creates multiple copies of the mermaid diagram, resulting in a huge file, but if we can eliminate that, most models should fit in a github limit of 65,536 (I think fixed)
  • The ignore array is a bit unwieldy, we could easily replace this by individual behavior at the plugin level
  • matrix elements with quotes around them break rendering. I've tried to strip the quotes, but my meager R skills have not been up to the task. So, I just concatenated them into a string.

model_mm_diagram <- function(
    model_json, root_name = 'root', parent_name = FALSE, mm_diagram='', indent="   ", 
    ignore=c(
      'id', 'name', 'plugin', 'object_class', 'defaultval', 'value', 'code', 
      'equation','minvalue', 'keycol1', 'keycol2', 'lutype1', 'lutype2',
      'valuetype', 'autosetvars', 'nonnegative', 'engine', 
      'nonnegative', 'engine', 'linked_property', 'exec_hierarch',
      'value_dbcolumntype'
      )
  ) {
  if (parent_name == FALSE) {
    parent_name = 'root'
    mm_diagram <- paste(mm_diagram, 'flowchart TD\n')
  }
  for (n in names(model_json)) {
    m <- model_json[[n]]
    if (!is.list(m)) {
      m <- list(value=m)
      follow = FALSE
    } else {
      follow = TRUE
    }
    if ( !(n %in% ignore)) {
      v <- m$value
      if (!is.null(m[['name']])) {
        l <- m[['name']]
      } else {
        l = n
      }
      if ( (l == 'matrix') || (l == 'broadcast_params') ) {
        #v = knitr::kable(as.matrix(v, format="markdown") )
        v=paste(as.matrix(v), collapse=",")
      }
      mm_diagram <- paste0(mm_diagram, indent, n, "(\"", l, " = ", v, "\")")
      if (parent_name != root_name) {
        mm_diagram <- paste(mm_diagram, "-->", parent_name)
      }
      mm_diagram <- paste(mm_diagram, "\n")
      if (follow) {
        mm_diagram <- model_mm_diagram(
          m, root_name = root_name, parent_name = n, 
          mm_diagram = mm_diagram, indent=paste0(indent, '   '), ignore = ignore
        )
      }
    }
  }
  return(mm_diagram)
}

dubsheds <- lseg_json[['dubsheds']]
mmd <- model_mm_diagram(dubsheds)

cat(mmd)  

jfj = model_mm_diagram(lunga_fac_json)
cat(jfj)
write(jfj,"C:/Workspace/tmp/lunga.mermaid")

furl = "http://deq1.bse.vt.edu:81/p6/out/land/subsheds2/pwater/forN51069_pwater.csv"
fdata <- read.csv(furl)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions