Code Monkey home page Code Monkey logo

tweakflow's Introduction

Tweakflow - safe embeddable scripting for the JVM

Java 8+ License Maven Central Travis Build Status AppVeyor Build status

Applications can use tweakflow to expose runtime information to an expression-based scripting runtime, allowing users to safely interact with the provided data and collect scripted results.

Tweakflow keeps the application in control of the data exchange. Users cannot arbitrarily call into application internals.

Language features

A simple computation model

Tweakflow has values and functions acting on them. All language constructs like variables, libraries, and modules merely serve to name and organize values and functions into sensible groups. Application users do not have to learn any programming paradigms to start using tweakflow expressions. If they can use formulas in a spreadsheet application, they can use formulas in your application too.

Dynamically typed

Tweakflow is a dynamically typed language. Data types include booleans, strings, longs, doubles, datetimes and functions, as well as nestable lists and dictionaries. All data types have literal notations.

All data is immutable

All values in tweakflow are immutable. It is always safe to pass values between user expressions and the host application without worrying about mutable state or object identity.

All functions are pure

All functions in tweakflow are pure and free of observable side-effects. A tweakflow function, given the same arguments, will always return the same result. The host application must take care of all non-pure operations like file I/O.

Batteries included

Tweakflow comes with a standard library that allows users to perform common tasks when working with data. Your application can limit or extend the standard library to suit its needs.

Automatic dependency tracking

When the application changes an input variable, tweakflow efficiently recalculates the values of any user variables that depend on it. Much like a spreadsheet application updates dependent formula cells when a cell changes.

Inline documentation and meta-data

Tweakflow supports documentation annotations as well as arbitrary meta-data on variables, libraries and modules. This feature supports interactive help as well as automated generation of project documentation.

Requirements

Java 8 or later is required. Builds are tested against JDK 8 and JDK 11.

Getting the jar

Get the latest release jar from github, or from maven central.

Getting started

Start the REPL using:

$ java -jar tweakflow-0.11.0.jar repl

Start typing expressions for the REPL to evaluate:

tweakflow interactive shell    \? for help, \q to quit
std.tf> 1+2
3

std.tf> "Hello " .. "World"
"Hello World"

std.tf> data.map([1, 2, 3], (x) -> x*x)
[1, 4, 9]

See the getting started guide for a short guided tour of language features.

Embedding

Evaluating simple expressions is as easy as:

TweakFlow.evaluate("1+2"); // returns the Value 3

Or it can be more sophisticated, providing users with application variables they can reference. Your app is in control of functions and variables available to user expressions. A corresponding tweakflow module might look like this:

# my_module.tf
# allow users to use core, data, math, and strings libraries from the standard library
import core, data, math, strings from 'std'

# place customer.first_name and customer.last_name into scope, provided dynamically during runtime
library customer {
  provided first_name;
  provided last_name;
}

# generated from user input, re-evaluated automatically when customer changes
library user {
  greeting: if customer.first_name && customer.last_name
              "Hello #{customer.first_name} #{customer.last_name}"
            else
              "Dear anonymous"
}

The embedding code for above module:

Runtime runtime = TweakFlow.compile(loadPath, "my_module.tf");
// get the module out of the runtime
Runtime.Module module = runtime.getModules().get(runtime.unitKey("user_module.tf"));

// get a handle to customer.first_name, and customer.last_name provided vars
Runtime.Var firstName = module.getLibrary("customer").getVar("first_name");
Runtime.Var lastName = module.getLibrary("customer").getVar("last_name");

// get a handle to greeting expression
Runtime.Var greeting = module.getLibrary("user").getVar("greeting");

// loop over customer collection and calculate the user-defined greeting
for (Customer c : myCustomerCollection){
  runtime.updateVars(
    firstName, Values.make(c.getFirstName()),
    lastName, Values.make(c.getLastName())
  );
  String userGreeting = greeting.getValue().string();
}

Your application can allow users to define variables, group them into libraries and even separate modules for reuse across their projects. How much sophistication is available to users depends on how much your application wants to expose.

See the embedding guide for more information and examples.

Using tweakflow standalone

Tweakflow is designed to be an expression language embedded in a bigger application. Much like formula languages are embedded in spreadsheet applications. However, for prototyping, development and testing, it can be handy to invoke tweakflow directly.

See the tools guide, for more information on the tweakflow REPL, runner, and documentation tool.

License

Tweakflow uses the business friendly MIT license.

Support

Open source does not mean you're on your own. Tweakflow is developed by Twineworks GmbH. Twineworks offers commercial support and consulting services. Contact us if you'd like us to help with a project.

tweakflow's People

Contributors

slawo-ch avatar

Forkers

jp-yao

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.