// Vnanoのスクリプトエンジンを搭載して使用する、ホストアプリケーションのコード例 import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; import javax.script.ScriptException; public class Example { // A class which provides a field/method accessed from the script as external functions/variables. // スクリプト内から外部変数・外部関数としてアクセスされるフィールドとメソッドを提供するクラス public class ExamplePlugin { public int LOOP_MAX = 100; public void output(int value) { System.out.println("Output from script: " + value); } } public static void main(String[] args) { // Get ScriptEngine of Vnano from ScriptEngineManager. // ScriptEngineManagerでVnanoのスクリプトエンジンを検索して取得 ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("vnano"); if (engine == null) { System.err.println("Script engine not found."); return; } // Connect methods/fields of ExamplePlugin to the script engine as external functions/variables. // ExamplePluginクラスのメソッド・フィールドを外部関数・変数としてスクリプトエンジンに接続 ExamplePlugin examplePlugin = new Example().new ExamplePlugin(); engine.put("ExamplePlugin", examplePlugin); // Create a script code (calculates the value of summation from 1 to 100). // スクリプトコードを用意(1から100までの和を求める) String scriptCode = " int sum = 0; " + " int n = LOOP_MAX; " + " for (int i=1; i<=n; i++) { " + " sum += i; " + " } " + " output(sum); " ; // Run the script code by the script engine of Vnano. // Vnanoのスクリプトエンジンにスクリプトコードを渡して実行 try{ engine.eval(scriptCode); } catch (ScriptException e) { System.err.println("Scripting error occurred."); e.printStackTrace(); return; } } }