Coder Social home page Coder Social logo

codeflow-core's Introduction

CodeFlowicon

Let's convert Java code to flowchart graph!

Features

Java code to flowchart graph:

*: reconsider or will not be supported.

  • Control flow in Java: sequence, loop(for, while, do while) and condition(if)
  • Support switch statement
  • Support break, contuine with labels
  • throw, return keywords
  • Top-level function and statement
  • Multi-function supported, which represented in subgraphs
  • Function overload(different arguments size only)
  • Semantic check and warning
  • *Support try...catch

Quick start

CodeFlow codeFlow = CodeFlow.builder()
              .outDir("tests")
              .format(Format.PNG)
              .build();
codeFlow.parse("if(ok){doSome();}else{doSomeElse();}").toFile("file.png");

Usage

  1. Add dependency

Maven:

<repositories>
    <repository>
        <id>jitpack.io</id>
		<url>https://jitpack.io</url>
    </repository>
</repositories>
<dependency>
    <groupId>com.github.LeeReindeer</groupId>
    <artifactId>codeflow-core</artifactId>
    <version>${latest-version}</version>
</dependency>

Gradle:

allprojects {
    repositories {
        ...
	    maven { url 'https://jitpack.io' }
	}
}
implementation 'com.github.LeeReindeer:codeflow-core:${latest-version}'

For snapshot, use master-SNAPSHOT as the version tag.

  1. Get a builder
CodeFlow codeFlow = CodeFlow.builder()
        .useNative(true)            // Linux x86_64 only.Use native bind of graphviz, faster.
        .supportClass(false)        // whether support class declare
        .failFast(true)		    // not recovery from syntax error
   			            // the graph will not created when syntax error occurred.
        .workDir("examples")        // input file dir
        .outDir("tests")            // output file dir
        .format(Format.PNG)         // output file format
        .build();
  1. Configuration Flowchart style(optional)

All flowchart configuration is in FlowchartConfig as static fields.

  public static boolean virtualStartNode = true; // add a start node in graph 
  public static boolean virtualEndNode = true;   // add a end node
  public static boolean mergeSequences = false;  // merge non-logical relationship sequence statements
  public static String functionColor = "lightblue"; //function call node color
  // You can set decision nodes' compass,
  // but the best you can do is not interfering the layout engine.
  // It will avoid most line intersections in graphviz.
  // Nevertheless, if you sill want to customize, the recommend preference is commented following.
  public static String decisionTrueCompass; // s
  public static String decisionFalseCompass; // w
  public static String doWhileDecisionTrueCompass; // e
  public static String doWhileDecisionFalseCompass; // s
  1. Do Convert
  • parse(String code)

  • parse(Supplier<String> supplier)

  • parse(File file)

  • parseFile(String path)

Examples:

// convert code string to java.awt.image.BufferedImage
codeFlow.parse("if(ok){doSome();}else{doSomeElse();}").toImage();
// convert code in file simple.cf to simple.png
codeFlow.parseFile("simple.cf").toFile("simple.png");

Examples

int binarySearch(int[] a, int k) {
    int len = a.length;
    int l = 0, r = len - 1;
    while (l < r) {
      int mid = (l + r) / 2;
      if (a[mid] < k) {
        l = mid + 1;
      }
      else if (a[mid] > k) {
        r = mid - 1;
      } else {
        return mid;
      }
    }
    return -1;
}

Above code will generate a flowchart like:

binarySearch

See more examples at examples folder and test folder.

How it works

  • It use ANTLR4 to generate parser to parse Java code.

  • And then, visit the AST to build flowchart fragment:

    • The single flowchart fragment is built directly
    • The complex flowchart fragment(like nested if statements and loops) is built recursively
  • Use graphviz-java library convert node data structure to dot file.

  • Finally, use Graphviz to convert dot file to graph image.

codeflow-core's People

Contributors

leereindeer avatar dependabot[bot] avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.