References:
Project JAYA (for JAVA MAYA) intends to provide a MEL interpreter and a MEL-to-bytecode compiler that may be the starting point to build a fully-featured MAYA-like package, in Java!
At this point, JAYA has its own interpreter and compiler, called JMEL, that fully interprets and compiles
MEL-written files.
JAYA has been build mainly from two packages:
To invoke the MEL script environnement, enter:
java jmel.MainJMEL starts and tries to invoke a welcome command called printBanner, as does BeanShell. If it finds such a command (in fact, a class named PrintBanner and implementing the Script interface, as explained here), JMEL invokes the command (in fact, it calls its exec method) and displays (for example):
java jmel.Main //****************** // JMel 0.1 - by RVA //****************** jmel>From this point, the user can enter any MEL instructions that are immediately interpreted and executed by the program.
jmel> int $i = 1 jmel> $i++; print "$i = " + $i + "\n" // notice: character ';' is optional at the end of the last MEL instruction // $i = 2 jmel>(here print is a reserved keyword that prints its string argument on the standard output).
The JMEL interpreter has no option to save the instructions and is only intended to test simple commands.
To invoke the MEL compiler, enter:
java jmel.Main -O1/-O2 [options] <MEL source files to be compiled>
where possible options include:
-O1 Generate class file -O2 Generate class file (with full opimization for commands)These first three options are specific to compilation and execution of MEL scripts.
-g Generate all debugging info -g:none Generate no debugging info -g:{lines,vars,source} Generate only some debugging info -verbose Output messages about what the compiler is doing -deprecation Output source locations where deprecated APIs are used -classpath <path> Specify where to find user class files -sourcepath <path> Specify where to find input source files -bootclasspath <path> Override location of bootstrap class files -extdirs <dirs> Override location of installed extensions -d <directory> Specify where to place generated class files -target <release> Generate class files for specific VM versionThese options are used by the MEL compiler to search for libraries and generate Java bytecode.
'-O1' generates Java bytecode that invoke JAYA commands called from the MEL file with arguments expressed as string values.
It is useful for testing and debugging.
'-O2' generates Java bytecode that is fully optimized: a JAYA command is invoked by generating all its property accessors and finally by calling its exec method.
The Jaya command is supposed to be fully tested and its .class file available somewhere.
For further information, see below
For example:
java jmel.Main -O2 myScript.melgenerates (if no error is found) a Java class file named myScript.class in the same directory as myScript.mel.
To invoke a compiled MEL script, enter:
java jmel.Main -test <class name> [<args>]The class is loaded and executed if a static method called exec is found. The optional arguments are passed to the exec method and parsed to test the match.
References:
Typically, the MAYA documentation provide for each MAYA command:
The examples are instructions that may be included within a MEL script file or entered in the MEL command shell.
For example:
Command isTrue is documented as described below.
NAMEisTrue SYNOPSISisTrue <condition to be tested> RETURN VALUE0 or 1 DESCRIPTIONThis commmand returns the state of the named condition. FLAGSnone EXAMPLESisTrue SomethingSelected; // Result: 1 // |
The power of Java is be able to analyse a .class file that contain the bytecode (the binary instructions) of a Java program (called a class)
to dynamically know what are the fields of that class, its methods, and how to invoke them.
This principle is typically used by JavaBeans, that are Java classes where the syntax of some methods defines itself what are called 'properties'.
A property, named for example myprop, has generally a getMyprop and/or a setMyprop method, that respectively returns the value of myprop,
and affects its value.
For example:
public class MyClass { private String myprop; // The 'getter' method public String getMyprop() { return myprop; } // The 'setter' method public void setMyprop(String value) { myprop = value; } }The idea in JAYA is to use any Java class as a potential JAYA command, by invoking setter methods in one command call.
jmel> myClass -myprop "hello"and JAYA:
Two interfaces are defined in JAYA: the Script interface and the Commandable interface.
The Script interface
The Script interface is defined as:
package jmel.script; public interface Script { ... }Although it defines no method, a class implementing the Script interface **MUST** provide a static exec method, that is invoked when the command name is entered in JAYA script.
For example we can implement the command isTrue described above like this:
package jmel.script; public class IsTrue implements Script { public static boolean exec(boolean condition) { return condition; } }Note #1: by convention, Java class names are prefixed by an uppercase character.
That's it!
The Commandable interface
The Commandable interface is defined as:
package jmel.script; public interface Commandable { public static final ReturnValue returnValue = new ReturnValue(); public void exec(); } where the ReturnValue class is defined as: public class ReturnValue { private Object value; public Object get() { return value; } public void set(Object value) { this.value = value; } }
The ReturnValue class provides an uniform way for a command to return a value, by invoking its set method.
If a command has its own flags, it must implement the Commandable interface.
For example:
import jmel.script.*; public class MyCmd implements Commandable { private int mycount; public MyCmd(int count) { mycount = count; } private String myprop; public void setMyprop(String value) { myprop = value; } public void exec() { returnValue.set(new Integer(mycount)); } }The myCmd command must be provided a integer value to be instantiated:
jmel> myCmd 1; jmel> int $i=`myCmd 1`; // here $i contains the return value provided in the exec method jmel> myCmd -myprop "me" 1; // an instance is created and its method 'setMyprop' is invoked with "me" as an argumentTo query or edit a value, the MEL flags '-q/-query' and '-e/-edit' must be used.
For example:
jmel> string $myClassInstanceName = `myClass`; jmel> myClass -e -myprop "hello" $myClassInstanceName
The list of MAYA commands is given here.