moodymudskipper / flow

View and Browse Code Using Flow Diagrams
https://moodymudskipper.github.io/flow/
Other
398 stars 28 forks source link

plantUML #40

Open moodymudskipper opened 4 years ago

moodymudskipper commented 4 years ago

there's an R package for it, and it looks quite neat : https://plantuml.com/fr/activity-diagram-beta

That'd be a nice alternative to nomnoml

moodymudskipper commented 4 years ago

It works quite nicely :

view_pant_ulm <- function(f) {
  code_str <- view_pant_ulm0(body(f))
  # we could add a header and a last exit point
  plant_uml_object <- plantuml::plantuml(code_str)
  plot(plant_uml_object, vector = FALSE)
}

view_pant_ulm0 <- function(expr) {
  if(is.call(expr) && identical(expr[[1]], quote(`{`))) {
    exprs <- as.list(expr)[-1]
  } else {
    exprs <- list(expr)
  }

  res <- sapply(exprs, function(expr) {
    #browser()
    #### SYMBOL / LITTERAL
    if(!is.call(expr)) return(paste0(":", as.character(expr), ";"))

    #### IF
    if(identical(expr[[1]], quote(`if`))) {
      if_txt   <- sprintf(
        "if (if(%s)) then (yes)",
        paste(deparse(expr[[2]]), collapse= "\\n"))
      yes_txt <- view_pant_ulm0(expr[[3]])
      if (length(exprs) == 4) {
        else_txt <- "else (no)"
        no_txt <-  view_pant_ulm0(expr[[4]])
        txt <- paste(if_txt, yes_txt, else_txt, no_txt, "endif", sep = "\n")
      } else {
        txt <- paste(if_txt, yes_txt, "endif", sep = "\n")
      }
      return(txt)
    }

    #### WHILE
    if(identical(expr[[1]], quote(`while`))) {
      while_txt   <- sprintf(
        "while (while(%s))",
        paste(deparse(expr[[2]]), collapse= "\\n"))
      expr_txt <- view_pant_ulm0(expr[[3]])
      txt <- paste(while_txt, expr_txt, "endwhile", sep = "\n")
      return(txt)
    }

    #### FOR
    if(identical(expr[[1]], quote(`for`))) {
      for_txt   <- sprintf(
        "while (for(%s in %s))",
        paste(deparse(expr[[2]]), collapse= "\\n"),
        paste(deparse(expr[[3]]), collapse= "\\n"))
      expr_txt <- view_pant_ulm0(expr[[4]])
      txt <- paste(for_txt, expr_txt, "endwhile", sep = "\n")
      return(txt)
    }

    #### REPEAT
    if(identical(expr[[1]], quote(`for`))) {
      repeat_txt   <- "while (repeat)"
      expr_txt <- view_pant_ulm0(expr[[2]])
      txt <- paste(repeat_txt, expr_txt, "endwhile", sep = "\n")
      return(txt)
    }

    #### STOP
    if(identical(expr[[1]], quote(`stop`))) {
      stop_txt <- paste(deparse(expr), collapse= "\\n")
      return(paste0(":",stop_txt, ";\nstop"))
    }

    #### RETURN
    if(identical(expr[[1]], quote(`return`))) {
      return_txt   <- paste(deparse(expr), collapse= "\\n")
      return(paste0(":",return_txt, ";\nstop"))
    }

    #### REGULAR CALL
    paste0(":", paste(deparse(expr), collapse = "\\n"), ";")
  })
  paste(res, collapse="\n")
}

view_pant_ulm(rle)

image

#> [1] 0

Created on 2020-08-29 by the reprex package (v0.3.0)

I don't know if I could implement flow_run() using it. but I think pretty much everything from flow_view would be possible (not sure about break and next statements though).

It's very compact and it's more rigorous UML. There are also options to have multiple if else on a line which might be really interesting in some cases.

One problemis that the package is not on CRAN and is hard to install (at least from windows). I couldn't get everything to work, but enough for all that is needed for this package I think : https://github.com/rkrug/plantuml/issues/18

But it has value so I'd like to push the above code further :

moodymudskipper commented 4 years ago

implemented in plantuml branch :

flow_view(rle, engine = "plantuml")

image

Because plantuml supports hyperlinks and looks better it would be awesome to make it work with all features but it would be a big challenge :

On the other hand from where we are already we can decently easily build diagrams for a full package and display them in a markdown report.

moodymudskipper commented 4 years ago

about big diagrams, found this on official website, maybe we can use it somehow or request the feature:

I want to generate huge diagrams!Back to topEdit using Dokuwiki syntaxEdit using Asciidoc syntaxEdit using Markdown syntax PlantUML limits image width and height to 4096. There is a environment variable that you can set to override this limit: PLANTUML_LIMIT_SIZE. You have to define this variable before launching PlantUML, something like:

set PLANTUML_LIMIT_SIZE=8192

or

setenv PLANTUML_LIMIT_SIZE 8192

Another way is an option in the command line:

java -DPLANTUML_LIMIT_SIZE=8192 -jar /path/to/plantuml.jar ...

Note that if you generate very big diagrams, (for example, something like 20 000 x 10 000 pixels), you can have some memory issues. The solution is to add this parameter to the java vm : -Xmx1024m.

moodymudskipper commented 4 years ago

Now that we have some core functionalities working OK this should be split in separate issues, we can keep this thread for the overview.

I see :

moodymudskipper commented 3 years ago

let's leverage more of plantuml's features in V2, then maybe it'll be on CRAN too