Malik H | 15 Aug 22:24
Picon
Favicon

Dot operators...

Konstantin:

Thanks for your email to my colleage Prem. But it appears that We are still experiecing the problem. We are trying to dynamically evaluate java expression like:

"someobject.isTrue()==true" where someObject is created and registered thru Dynamic lib registration.

Similarly we get the following compilitaiion error for String object:

COMPILATION ERROR :The name "String" is unknown.

String.toString()

Here is the entire code....Please help,...

package

test;

import

gnu.jel.*;

import

gov.irs.efile.tfrp.dataModel.PTFResult;

 

public class

JelTest {

public static void main(String[] args) {

// Set up the library

Class[] staticLib=new Class[1];

try {

staticLib[0]=Class.forName(

"java.lang.String");

}

catch(ClassNotFoundException e) {

// Can't be ;)) ...... in java ... ;)

};

Class[] dynamicLib=

new Class[1];

/*

PTFResult ptfResult = new PTFResult(0, true);

Object[] context = new Object[1];

context[0]=ptfResult;

dynamicLib[0]=ptfResult.getClass();

*/

String ptfResult =

new String();

Object[] context =

new Object[1];

context[0]=ptfResult;

dynamicLib[0]=ptfResult.getClass();

Library lib=

new Library(null,dynamicLib,new Class[0],null,null);

try {

lib.markStateDependent(

"random",null);

}

catch (Exception e) {

// Can't be also

};

String testStr =

"abc";

//DVMap resolver = new DVMap();

//PTFResult ptfResult = new PTFResult(0, true);

//

//String expr = "\"abc\".length()==3";

String expr =

"testStr.toString()";///"ptfResult.getIsValid()==true";

//String expr = "PTFResult.getIsValid()==true";

CompiledExpression expr_c=null;

try {

expr_c=Evaluator.compile(expr,lib);

}

catch (CompilationException ce) {

System.err.print(

"COMPILATION ERROR :");

System.err.println(ce.getMessage());

System.err.print(

" ");

System.err.println(expr);

int column=ce.getColumn(); // Column, where error was found

for(int i=0;i<column+23-1;i++) System.err.print(' ');

System.err.println(

'^');

}

if (expr_c != null) {

// Evaluate (Can do it now any number of times FAST !!!)

Boolean result=null;

try {

result=(Boolean)expr_c.evaluate(

null);

}

catch (Throwable e) {

System.err.println(

"Exception emerged from JEL compiled"+

" code (IT'S OK) :");

System.err.print(e);

}

System.err.println(

"result = " + result);

}

}

}

 


Do You Yahoo!?
HotJobs, a Yahoo! service - Search Thousands of New Jobs
Konstantin L. Metlov | 15 Aug 23:17
Picon

Re: Dot operators...

Hello,

Well, probably the word "dynamic" is overloaded too much. Let me define 
several _different_ dynamic things I'll be talking about.

1) "dynamic expressions", "dynamic evaluation of expressions", "dynamic
loading of code" these do not have any special meaning in the further text
sometimes they are used to express the purpose of JEL.

2) "dynamic library" (let's consider it a term and denote by an acronim
"DL") stands for a set of classes whose non-static methods/fields are
exported to JEL's namespace. The DL consists of all classes, passed in the
second argument array to gnu.jel.Library constructor. The fields/methods
of all DL classes become available in the _root_ namespace of the
expressions.

3) "dynamic variables interface" (let's shorten it to "DVI") is a JEL
interface allowing to add an extendable at run-time mappings between a
names (possibly containing dots) in JEL namespace to function calls of
getXXXValue() methods.

Now, back to the point. The code you've sent is basically the same as
before. It registeres the class PTResult as a part of DL. Therefore,
making all its non-static methods available in the root namespace of the
expressions. In partucular it adds the methods 
-- public int getTransID()
-- public boolean isValid()
to the root namespace, allowing the JEL
expressions, containing their names to compile. For example the 
expressions "getTransID()", "getTransID", "getTransID+5", "isValid()", 
"!isValid()" will be successfully compiled by JEL and executed by java 
run-time (provided they are invoked the right way, but more on that 
later **).

Your code also adds the "java.lang.String" class to JEL as a part of
static library (passed in a first argument of the gnu.jel.Library
constructor), which makes all its (j.l.S) _static_ methods/fields
available in _root_ namespace of JEL expressions. These methods/fields are
CASE_INSENSITIVE_ORDER 
-- String copyValueOf(char[] data)
-- String copyValueOf(char[] data, int offset, int count) 
-- String valueOf(boolean b) 
...
-- String valueOf(long l)
-- String valueOf(Object obj)

NO OTHER NAMES BESIDES MARKED "--" ABOVE ARE DEFINED IN THE NAMESPACE YOU 
CONSTRUCTED. In particular it means neither "String", nor "testStr". 
Therefore JEL rightfully tells you that such names are undefined. If you 
want them defined -- define them.

** invocation of expressions. It is stressed in JEL documentation more 
than once that the methods of DL classes require "this" pointer to be 
passed on invocation and, therefore, you need to pass pointers to their 
instances to the expression's "evaluate" methos, and there must be a 
one-to-one correspondence between the elements of DL array (which are 
classes) and elements of "evaluate(Object [] context)" context array on 
invocation. Instead, in your example, even though you have DL defined the 
argument of evaluate is "null". I have corrested this problem in the diff 
file of my previous message to this mailing list (and to you). Did you 
read it ?

Second. You are talking about the dot operator. It is said in the section 
of the dot operators in JEL manual that their behaviour is controlled by 
the last argument of the gnu.jel.Library constructor, and that the way to 
_forbide_ the dot operator is to pass "null" as that argument (just as you 
do it in your code). Did you read the documentation ?

So, my final suggestion on this matter is -- RTFM.

Konstantin.

Gmane