Moql-operands (operand) (3)

Source: Internet
Author: User
Function operand

The operand format of the function is as follows: function name (parameter 1, parameter 2 ...). The name of the function is the same as that of the identifier in the Java syntax. The name is described in the variable operand. Each of its parameters is an operand. operand can be a constant, variable, function, or expression. For example, sum (A), _ formattime ('yyyy-mm-dd', getcurrenttime (), and random (23 + 15.

The example code of the function operand is as follows:

Entitymap =NewEntitymapimpl ();

Entitymap. putentity ("num", 123 );

Try{

Operand function = moqlutils.Createoperand("Max (Num )");

System.Out. Println (function. tostring () + "" + function. getoperandtype ());

System.Out. Println (function. Operate (entitymap ));

Entitymap. putentity ("num", 345 );

System.Out. Println (function. Operate (entitymap ));

// Reset the function status

Function. Reset ();

Entitymap. putentity ("num", 12 );

System.Out. Println (function. Operate (entitymap ));

Function = moqlutils.Createoperand("Test (1, num, 'A ')");

System.Out. Println (function. tostring () + "" + function. getoperandtype ());

}Catch(Moqlexception e ){

E. printstacktrace ();

}

The execution result is as follows:

Max (Num) Function

123

345

12

Test (1, num, 'A') Function

Moql currently supports some operand functions, such as the aggregate functions operand: Count, sum, AVG, Min, and Max. After the operand is generated, the operate method can be called to directly calculate the data. However, if you want to create a function not supported by moql, moql will create a default operand for it, which only parses the function string, the function "test (1, num, 'A')" in the code snippet above ')". This function string is parsed as a default function operand, but when we call its operate method, It will throw a java. Lang. unsupportedoperationexception, indicating that the operand does not support this method. To enable moql to support the test function, we need to implement operand for test. The related implementation code is as follows:

PublicclassTestregistfunction {

PublicstaticclassTestExtendsAbstractfunction {

PublicTest (list <operand> parameters ){

/* "Test" is the operand name of the function corresponding to the object, and 3 is the number of parameters acceptable to the test function. If the parameters size is different from this value, illegalargumentexception will be thrown, indicating that the input function string is invalid */

Super("Test", 3, parameters );

Functiontype = functiontype.Common;

}

@ Override

ProtectedObject inneroperate (entitymap ){

Object obj1 = parameters. Get (0). Operate (entitymap );

Object obj2 = parameters. Get (1). Operate (entitymap );

Object obj3 = parameters. Get (2). Operate (entitymap );

Stringbuffer sbuf =NewStringbuffer ();

Sbuf. append (obj1.tostring ());

Sbuf. append ("| ");

Sbuf. append (obj2.tostring ());

Sbuf. append ("| ");

Sbuf. append (obj3.tostring ());

ReturnSbuf. tostring ();

}

}

PublicstaticvoidMain (string [] ARGs ){

Entitymap =NewEntitymapimpl ();

Entitymap. putentity ("num", 123 );

Try{

Operand function = moqlutils.Createoperand("Test (1, num, 'A ')");

System.Out. Println (function. tostring () + "" + function. getoperandtype ());

System.Out. Println (function. Operate (entitymap ));

}Catch(Exception e ){

E. printstacktrace ();

}

Try{

// Register the implementation of the test function

Moqlutils.Registfunction("Test", test.Class. Getname ());

Operand function = moqlutils.Createoperand("Test (1, num, 'A ')");

System.Out. Println (function. tostring () + "" + function. getoperandtype ());

System.Out. Println (function. Operate (entitymap ));

}Catch(Moqlexception e ){

E. printstacktrace ();

}

}

}

 

The execution result is as follows:

Test (1, num, 'A') Function

Java. Lang. unsupportedoperationexception

At org. moql. operand. function. memberfunction. inneroperate (memberfunction. Java: 49)

At org. moql. operand. function. abstractfunction. Operate (abstractfunction. Java: 120)

At org. moql. Core. Test. testregistfunction. Main (testregistfunction. Java: 44)

Test (1, num, 'A') Function

1 | 123 |

 

The test class inherits the abstractfunction abstract class, which is the operand Implementation of the test function. The test function can receive three parameters. When the number of parameters is different from 3, the constructor throws an exception, indicating that the call format of the test function is incorrect, for example, test (1, num) will throw an exception. When we define a function whose parameter is variable length, we can replace 3 with-1 to indicate variable length. Or in org. moql. operand. function. functionVariant_parametersConstant. The value is-1. The subject logic of the test class is implemented in inneroperate. The test class overwrites the method and Concatenates the three parameters into a string and returns the result.

From the execution of the preceding code, we can see that after the operand method of the test function is created for the first time, an exception is thrown when the operate method is called. Therefore, before the operand of the test function is created for the second time, we call the moql function registration method and register the operand implementation class test for the test function. Then we created the operand of the test function and called its operate method. This call achieves the expected output results. The root cause is that the operand function created for the first time, because the registered operand implementation is not found, a default function operand is generated, which does not support the operate method; the second time we created the operand function, we found the registered implementation class test, so we bound the implementation and generated the operand corresponding to the test function.

Project address: http://sourceforge.net/projects/moql/

Code path: SVN: // svn.code.sf.net/p/moql/code/trunk

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.