114 lines
3.6 KiB
Plaintext
114 lines
3.6 KiB
Plaintext
<snippet>
|
|
<subclass>afryca.ase.Snippet</subclass>
|
|
<file></file>
|
|
<category>ASE-API</category>
|
|
<name>Eval snippet examples</name>
|
|
<description></description>
|
|
<code>
|
|
var snippets = Array();
|
|
|
|
function createSnippet(id, code) {
|
|
var prefix = 'ASE-API eval snippet example';
|
|
var SnippetClass = Java.type('afryca.ase.Snippet');
|
|
var snippet = new SnippetClass();
|
|
var name = prefix + ' ' + id;
|
|
var file = name + '.script';
|
|
snippet.setFile(file);
|
|
snippet.setName(name);
|
|
snippet.setCode(code);
|
|
snippet.setCategory('ASE-API');
|
|
ase.getSnippets().put(snippet.getFile(), snippet);
|
|
snippets[id] = {name: name, file: file};
|
|
}
|
|
|
|
function createSnippets() {
|
|
var groovy = 'def product(Double x, Double y) {\n'
|
|
groovy += ' return x * y\n';
|
|
groovy += '}';
|
|
createSnippet('groovy', groovy);
|
|
|
|
var r = 'product <- function(x ,y) {\n';
|
|
r += ' return (x * y)\n';
|
|
r += '}';
|
|
createSnippet('r', r);
|
|
|
|
var python = 'def product(x , y):\n';
|
|
python += ' return x * y';
|
|
createSnippet('python', python);
|
|
|
|
var ruby = 'def product(x, y)\n';
|
|
ruby += ' return x * y\n';
|
|
ruby += 'end';
|
|
createSnippet('ruby', ruby);
|
|
|
|
var lua = 'function product(x, y)\n';
|
|
lua += ' return x * y\n';
|
|
lua += 'end';
|
|
createSnippet('lua', lua);
|
|
|
|
var scala = 'def product(x:Double, y:Double) : Double = {\n';
|
|
scala += ' return x * y\n';
|
|
scala += '}';
|
|
createSnippet('scala', scala);
|
|
}
|
|
|
|
function deleteSnippets() {
|
|
for (var key in snippets) {
|
|
ase.getSnippets().remove(snippets[key].file);
|
|
}
|
|
}
|
|
|
|
createSnippets();
|
|
|
|
if (typeof Groovy !== 'undefined') ase.evalSnippet(Groovy, snippets['groovy'].name);
|
|
if (typeof R !== 'undefined') ase.evalSnippet(R, snippets['r'].name);
|
|
if (typeof Jython !== 'undefined') ase.evalSnippet(Jython, snippets['python'].name);
|
|
ase.evalSnippet('JRuby', snippets['ruby'].name);
|
|
ase.evalSnippet('LuaJ', snippets['lua'].name);
|
|
ase.evalSnippet('Scala', snippets['scala'].name);
|
|
|
|
var x = 3;
|
|
var y = 4;
|
|
var code = 'product(' + x + ',' + y + ')';
|
|
|
|
var notLoaded = 'is not loaded';
|
|
groovyResult = (typeof Groovy !== 'undefined') ? ase.evalCode(Groovy, code) : notLoaded;
|
|
rengine = (typeof R !== 'undefined') ? ase.evalCode(R, code) : notLoaded;
|
|
jython = (typeof Jython !== 'undefined') ? ase.evalCode(Jython, code) : notLoaded;
|
|
jruby = ase.evalCode('JRuby', code);
|
|
luaj = ase.evalCode('LuaJ', 'result = ' + code + ' -- ase get result'); // ASE shortcut
|
|
scalaResult = ase.evalCode('Scala', code);
|
|
|
|
var e1 = 'Groovy: ' + groovyResult + '\n' +
|
|
'R: ' + rengine + '\n' +
|
|
'Jython: ' + jython + '\n' +
|
|
'JRuby: ' + jruby + '\n' +
|
|
'LuaJ: ' +luaj + '\n'+
|
|
'Scala: ' + scalaResult
|
|
|
|
createSnippet('code', code);
|
|
createSnippet('luaj code', 'result = ' + code + ' -- ase get result'); // ASE shortcut
|
|
|
|
groovyResult = (typeof Groovy !== 'undefined') ? ase.evalSnippet(Groovy, snippets['code'].name) : notLoaded;
|
|
rengine = (typeof R !== 'undefined') ? ase.evalSnippet(R, snippets['code'].name) : notLoaded;
|
|
jython = (typeof Jython !== 'undefined') ? ase.evalSnippet(Jython, snippets['code'].name) : notLoaded;
|
|
jruby = ase.evalSnippet('JRuby', snippets['code'].name);
|
|
luaj = ase.evalSnippet('LuaJ', snippets['luaj code'].name);
|
|
scalaResult = ase.evalSnippet('Scala', snippets['code'].name);
|
|
|
|
var e2 = 'Groovy: ' + groovyResult + '\n' +
|
|
'R: ' + rengine + '\n' +
|
|
'Jython: ' + jython + '\n' +
|
|
'JRuby: ' + jruby + '\n' +
|
|
'LuaJ: ' +luaj + '\n'+
|
|
'Scala: ' + scalaResult
|
|
|
|
deleteSnippets();
|
|
|
|
'Example 1\n' + e1 + '\n\nExample 2\n' + e2
|
|
|
|
//
|
|
// Note: See 'Scripting examples' for more information
|
|
//
|
|
</code>
|
|
</snippet> |