Coder Social home page Coder Social logo

nsdlib's Introduction

nsdlib

Build Status Test Coverage Maintainability

This is a Java library for working with Nassi-Shneiderman diagrams (structograms).

It offers the following functionality:

  • Construct elements through code
    • All standard block types supported
  • Read structograms from files
    • Currently only support for the Structorizer format, but easy to add your own
  • Render structograms as images
    • Basic layouting, dynamic size calculation
    • High amount of abstraction through RenderAdapter (AWT adapter included; support for custom adapters for other targets, e.g. Android)

Reading a Structogram

The following code reads a structogram stored in Structorizer's XML format.

NSDReader reader = new StructorizerReader();

NSDRoot root;

File file = new File("/path/to/file.nsd");
try (FileInputStream in = new FileInputStream(file)) {
    root = reader.read(in);
} catch (NSDReaderException | IOException e) {
    e.printStackTrace();
}

// do something with `root`

Feel free to submit a PR or open an issue if you require support for other formats. The custom reader should implement the NSDReader interface.

Rendering a Structogram

To render a structogram (or part of one), the following steps need to be done:

  1. Convert the element into a RenderPart (do this for the root element).
  2. Layout the render part.
  3. Render the actual image.

The following example uses the AwtRenderer for rendering to a BufferedImage, but you can easily substitute a custom renderer instead.

AwtRenderer renderer = new AwtRenderer();

// 1. convert (`diagram` is an instance of `NSDRoot`)
RenderPart part = diagram.toRenderPart();

// 2. layout
part.layout(renderer.createContext());
// optional: get the resulting size
// Size s = part.getSize();

// 3. render
BufferedImage img = renderer.render(part);

Manual Structogram Construction

Constructing a structogram with code is as simple as invoking a few constructors.

NSDRoot diagram = new NSDRoot("When start clicked");

NSDDecision dec = new NSDDecision("(pick random 1 to 10) = 1");
{
    dec.getThen().addChild(new NSDInstruction("say \"You're lucky!\""));
    dec.getElse().addChild(new NSDInstruction("say \"Not so lucky.\""));
}
diagram.addChild(dec);

NSDForever forever = new NSDForever(Arrays.asList(
    new NSDInstruction("wait 1 secs")
));
diagram.addChild(forever);

// etc

Element Types

The base class for every element is nsdlib.elements.NSDElement. Elements that contain other elements extend its subclass nsdlib.elements.NSDContainer.

Type Package Class
Root element nsdlib.elements NSDRoot
Instruction nsdlib.elements NSDInstruction
If/Then/Else nsdlib.elements.alternatives NSDDecision
Switch-Case nsdlib.elements.alternatives NSDCase
Test-first loop nsdlib.elements.loops NSDTestFirstLoop
Test-last loop nsdlib.elements.loops NSDTestLastLoop
Unconditional loop nsdlib.elements.loops NSDForever
Concurrent execution nsdlib.elements.parallel NSDParallel

nsdlib's People

Contributors

meyfa avatar

Stargazers

 avatar

Watchers

 avatar  avatar  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.