Step 6 - How to Embed into Other Software

In this page, as an advanced extra step, Let's embed the processing engine of RINPn into your original app!

- Index -

You can embed the scripting/calculation engine used in the RINPn, the "Vnano Engine", into your applications written in Java®. Let's try!

Example Code

In the package of the RINPn, an example code calculating a value of the expression "1.2 + 3.4" under almost the same engine-settings with the RINPn is attached as "EmbedUseExample.java":

import javax.script.ScriptEngine;
import javax.script.ScriptException;
import java.util.Map;
import java.util.HashMap;

public class EmbedUseExample {
    public static void main(String[] args) {

        ////////// Preparation of the Vnano Engine: Begin //////////

        // Get the script engine (Vnano Engine) used in the RINPn for calculations.
        ScriptEngine engine = new ScriptEngineManager.getEngineByName("vnano");
        if (engine == null) {
            System.err.println("Script engine loading error");
            return;
        }

        // Set ptions of the script engine (for details, see Settings.txt)
        Map optionMap = new HashMap();
        optionMap.put("EVAL_INT_LITERAL_AS_FLOAT", true);
        optionMap.put("EVAL_ONLY_FLOAT", true);
        optionMap.put("EVAL_ONLY_EXPRESSION", true);
        optionMap.put("UI_MODE", "CUI");      // For CUI applications
        // optionMap.put("UI_MODE", "GUI"); // For GUI applications
        optionMap.put("ACCELERATOR_ENABLED", true);
        optionMap.put("ACCELERATOR_OPTIMIZATION_LEVEL", 0);
        engine.put("___VNANO_OPTION_MAP", optionMap);

        // Set permissions referred from plug-ins supporting permission features.
        Map permissionMap = new HashMap();
        permissionMap.put("DEFAULT", "ASK");   // For asking to users by default
        // permissionMap.put("DEFAULT", "DENY"); // For denying by default
        // (Here add permission items you want to allow, as follows)
        // permissionMap.put("FILE_READ", "ALLOW");
        engine.put("___VNANO_PERMISSION_MAP", permissionMap);

        // Settings for loading libraries/plugins
        engine.put("___VNANO_LIBRARY_LIST_FILE", "lib/VnanoLibraryList.txt");
        engine.put("___VNANO_PLUGIN_LIST_FILE", "plugin/VnanoPluginList.txt");

        ////////// Preparation of the Vnano Engine: End //////////

        // Calculate the value of "1.2 + 3.4" by the script engine, and output
        try{
            String input = "1.2 + 3.4";
            double x = (double) engine.eval(input + ";");
            System.out.println("Output of the Vnano Engine: " + x);
        } catch (ScriptException e) {
            e.printStackTrace();
        }

        // Unload libraries/plugins
        engine.put("___VNANO_COMMAND", "REMOVE_LIBRARY");
        engine.put("___VNANO_COMMAND", "REMOVE_PLUGIN");
    }
}

How to Compile & Execute

How to compile:

javac -encoding UTF-8 EmbedUseExample.java

When the compilation is succeeded, a class file "EmbedUseExample.class" will be generated. Execute it with adding the Jar file of the Vnano Engine "Vnano.jar" (bundled in the package of the RINPn) to the class path:

java -cp ".;Vnano.jar" EmbedUseExample     (For Microsoft® Windows®)
java -cp ".:Vnano.jar" EmbedUseExample     (For Linux®, etc.)

(Result) Output of the Vnano Engine: 4.6

As the above, we get the correct value "4.6" of the expression "1.2 + 3.4". In the expression, you can use functions of libraries/plug-ins. In addition, to the eval(...) method of the script engine, you can pass the content(code) of a Vnano script instead of an expression.

License & More Detailed Information about the Vnano Engine

The Vnano Engine is an open source software released under the MIT License, so you can use it for free in both of commercial/non-commercial purposes, the same as the RINPn.

More detailed information (guide/specification documents and so on) are provided on the official website of the Vnano:

Vnano Official Website
https://www.vcssl.org/en-us/vnano/
- Trademarks/Credits -