Coder Social home page Coder Social logo

java_on_script's Introduction

Java on Script License: MIT Maven Central Sonatype Nexus (Snapshots)

Simple scripting library for Java programs


Features

  • Simple scripting syntax with support for nested blocks, for loops, if conditions
  • Support for adding custom function implementations

Maven configuration

JoS is available on Maven Central. You just have to add the following dependency in your pom.xml file.

<dependency>
  <groupId>com.adtsw.jos</groupId>
  <artifactId>dsl</artifactId>
  <version>1.0.12</version>
</dependency>

For snapshots, add the following repository to your pom.xml file.

<repository>
    <id>sonatype snapshots</id>
    <url>https://oss.sonatype.org/content/repositories/snapshots</url>
</repository>

The snapshot version has not been released yet.

<dependency>
  <groupId>com.adtsw.jos</groupId>
  <artifactId>dsl</artifactId>
  <version>TBD</version>
</dependency>

Sample Script

    // init variables
    test_variable = 14;
    test_variable_expression = test_variable * 2;
    test_diff = 0.0;
    
    // conditions
    if(test_variable != 0) {
        
        test_diff = ((test_variable * 1.0) / (test_variable_expression * 1.0)) * 100.0;
        
        // logging
        log("test diff");
        log(test_diff);

        // nested conditions
        if(test_diff == 50.0) {
            log("value matches. updating original value");
            test_diff = test_diff + 50.0;
        }
    }

    // for loops
    for_loop_value = 0;
    
    for(i=0, i<test_variable_expression, i=i+1) {
        if(i % 2 == 0) {
            for_loop_value = for_loop_value + 1;
        }
    }

    // support for custom UDFs
    udf_value = custom_udf(var_1: test_variable, var_2: test_variable_expression);

    log("udf value");
    log(udf_value);

Usage

    public static void main(String[] args) {
        
        ScriptCompiler compiler = new ScriptCompiler("basic_udf", "examples/src/main/resources", new HashMap<>());
        ScriptContext scriptContext = compiler.compile();
        ScriptRunner scriptRunner = new ScriptRunner(scriptContext, new ScriptInput(new HashMap<>()), new HashMap<>(){{
            put("custom_udf", new AbstractFunctionDefinition() {
                @Override
                public void execute(ScriptLineContext lineContext, ScriptRuntimeContext runtimeContext) {
                    FunctionContext functionContext = lineContext.getFunctionContext();
                    String variableName = lineContext.getVariableContext().getName();
                    Integer var1 = (Integer) getArgValue(functionContext, runtimeContext, 0);
                    Integer var2 = (Integer) getArgValue(functionContext, runtimeContext, 1);
                    runtimeContext.getRunTimeVariables().put(variableName, var1 + var2);
                }
            });
        }});
        scriptRunner.run();
        Object udf_value = scriptRunner.getRuntimeContext().getRunTimeVariables().get("udf_value");
        System.out.println("value accessed from code : " + udf_value);
    }

java_on_script's People

Contributors

adityapawade avatar adityapawadeflipkart avatar

Watchers

James Cloos 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.