A good Java expression computing tool

Source: Internet
Author: User

In our project, if it is a process or a number of modules involved in computing, is there a need for dynamic computing.

 

For example, we read a formula from the configuration, for example, the simplest sin (A) + cos (B), how do you implement it,

 

This implementation is not much difficult, from the simplest hard code parsing, to regular expression parsing, or ast syntax tree parsing, or using bsh, Jython, jruby, groovy, and other script engines, or even more directly run SQL statements, and so on. However, each method has its own limitations. How can we find a better solution.

 

Here we recommend you to use the Java implementation equals of a flexible formula module.

 

Equals is a project contributed by David. Liu of exceoon in Google codes. Is a solution for implementing the formula calculation template. This project has been widely used in the self-developed rules and indicators modules of multiple commercial stock market software, and has friends who have used stock software, you should be familiar with the stock selector and custom indicator functions.

 

That is to say, the customer must define one or a set of computing formulas in the editor, and then the program will calculate according to the formula you specify, of course, as a commercial application, the calculation here is not limited to addition, subtraction, multiplication, division, but also to calculation of indicators such as standard deviation, average line, and moving average line.

 

For example, the following test code

 

String TXT = "DIF: EMA (close, short)-EMA (open, long );"
+ "DEA: EMA (DIF, mid );"

+ "Macd :( DIF-DEA) * 2: colorstick ;";

 

First define a formula. This formula calculates the stock index macd.

 

String [] defs = new string []
{"Short", "long", "mid "};

 

Define input parameters in the formula

 

Formula F = NULL;
F = formulabuilder. constructformula (txt, defs );

Declare a formula variable. Here is the entry calculated by the entire formula.

 

Formulaparameters fp = new formulaparameters ();
FP. addparameter ("short", 12 );
FP. addparameter ("long", 26 );
FP. addparameter ("mid", 9 );

Declare the input variables required by the formula.

 

Formularesult Fr = f. eval (FP );
Map <string, Object> Results = Fr. getresult ();

 

Calculation Result

 

 

Run the final result

 

{DEA = [0.9358484784928137, 5.223248740604646, 9.484484720782131, 12.812405231923686, 17.625797032274512, 21.48934699188941, 24.338687429831023, 24.967767843873517, 23.713504323762475, 23.419514164728735, 22.735086398910138, 22.495512066265064, 22.42045061047972, 21.686444772033873, 20.699838512275377, 19.800386512274844, 20.637560544699593, 22.159859719130218, 24.403776610862046, 24.227408662528966, 22.137340048573154, 21.27741048836811, 20.240579387890197, 19.05695339586254, 16.70384447055847, 16.076128111445378, 14.878188371277464, 13.761807666795649, 13.446553319790361, 14.507684011227127, 13.374907832743064,-10.633227879860222,-8.01815943098602, -2.007847505375248,-3.3771308924710546,-2.992205267376599,-3.409601523132302,-3.152630485981012,-1.652301728989015, 0.06031436069506646, 2.1743828361502264, 3.225697255197028, 5.4291566819545904, 5.2381674589246146, 6.318190562739442, 6.539097031605232, 8.047663761906332, 6.987127060299267, 6.393116866583883, 5.5090016976278795, 5.697663742732732, 4.697814328207814, 2.9086831988864628, 2.2716142389282123, 1.5414277242134833, 0.511061516567303, 0.5807628095128398, 2.356397569537216, 4.278134723604381, 4.052172273526611, 5.2945745230142816, 4.856523216525936, 4.856215977440695, 4.448840846212395, 4.620369162264453, 4.711465166220274, 5.204702439818421, 6.139324792095153, 6.07435288804274, 5.050472337328575, 2.760808950677174, 1.0762871740060749,-0.3525313760925096,-0.4576312199107083,-0.3576855874336494,-0.047701505433911356,-0.45477110003830445,-1.5058191379602623,-3.437010627869526,-4.072244650056223,-2.541007163949839,-2.0103388844296686,-2.50312479232222,-2.2894491447695273, -0.5914388323214326,-0.4169389134949677,-0.012264436855367211, 0.687085671118843, 2.5344611936514294, 4.747483969091154, 5.063547335745879, 6.866522716801898, 6.918328460171757, 7.541830872993569, 8.271715110463383, 10.144861208894433], macd = [7.48678782794251, 34.29920209689466, 34.08988784141987, 26.623364089132433, 38.5071344028066, 30.90839967691918, 22.794723503532886, 5.032643312339985,-10.03410816088833,-2.3519212722699123,-5.4754221265487715,-1.9165946611605875,-0.6004916462827623,-5.872046707566781,-7.892850078067966,-7.1956160000042715, 6.6973922593979935, 12.178393395444985, 17.951335133854613, -1.4109435866646578,-16.720548911646496,-6.879436481640376,-8.2946488038233,-9.469007936221267,-18.82487140243257,-5.021730872904712,-9.583517921343322,-8.931045635854506,-2.522034776042304, 8.489045531494128,-9.062209427872492,-21.93343962306274, -20.92054759099361,-23.325877701202014,-22.362520387976904,-21.365917141354,-5.702631078006446,-7.4511091823507725,-10.95426709676645, 3.079405000755645,-3.3391700460456244, 2.0557682972103173, 12.002630055935978, 13.70092871747265, 16.91254780364128, 8.410515352374414, 17.627675414060498, -1.5279137842398, 8.640184830518628, 1.7672517509263166, 12.06853384240879,-8.484293612856522,-4.752081549723076,-7.0729213516480325, 1.5092963608388121,-7.998795316199338,-14.313049034570806,-5.096551679666001,-5.8414921177178325,-8.242929661169443, 0.5576103435642941, 14.205078080195007, 15.373897232537331,-1.807699600622163, 9.939217995901362,-3.5044104519067645,-0.002457912681927965,-3.259001049826402, 1.372226528416462, 0.7287680316465632, 3.9458981887851756,-7.476978818213864,-0.5197752324193079,-8.19104440571332,-18.317307093211213,-13.476174213368791,-11.430548400788675, -0.8407987505455898, 0.7995650598164714, 2.4798726559979047,-3.2565567568351446,-8.408384303375662,-15.449531919274108,-5.081872177493574, 12.249899888851068, 4.245346236161361,-3.942287263140411, 1.709405180421541, 13.584082499584758, 1.395999350611719, 3.237395813116804, 5.594800863793681, 14.779004180260689, 17.704182203517796, 2.5285069332377965, 14.423803048448146, 0.41444594695887815, 4.988019302574491, 5.839073899758503, 14.985168787448416], DIF = [4.679242392464069, 22.372849789051976, 26.529428641492068, 26.124087276489902, 36.87936423367781, 36.943546830349, 35.736049181597465, 27.48408950004351, 18.69645024331831, 22.24355352859378, 19.997375335635752, 21.53721473568477, 22.120204787338338, 18.750421418250482, 16.753413473241395, 16.20257851227271, 23.98625667439859, 28.24905641685271, 33.37944417778935, 23.521936869196637, 13.777065592749906, 17.83769224754792, 16.093254985978547, 14.322449427751906, 7.291408769342183, 13.565262674993022, 10.086429410605803, 9.296284848868396, 12.18553593176921, 18.75220677697419, 8.843803118806818, -0.33349193167114777,-2.442114364510786,-6.56051413226524,-8.874150524149798,-11.046588543507596,-3.927774396584624,-5.733402096550634,-8.85426444085428,-1.4525027669987765,-5.079186546155114,-2.1247463373758535, 4.349013298978974, 6.910778719431391, 7.430954931384235, 14.24299438898484, 4.4742105668047145, 10.638282977998756, 7.42272290706839, 14.081930683110727, 2.744980253871006, 4.017076091722345, 1.9725410218038633, 6.452311923152138, 0.6984166701081449,-4.24784131839894,-0.276661600904788,-1.3793183346454327, 3.610403314017418, 0.8595679812949868, 9.45893660963472, 11.965083339873047, 3.1483224732155293, 10.264183520964963, 3.1043179905725538, 4.854987021099731, 2.819340321299194, 5.306482426472684, 5.075849182043555, 7.177651534211009, 9.877814201202085, 5.814465271833086, 0.9549501344719147,-6.397844595928433,-5.661799932678321,-6.0678055764868475, 0.8780305951835032, 0.042096942474586285, 1.192234822565041, -2.0830494784558766,-5.710011289648094,-11.16177658750658,-6.61318073880301, 3.583942780475695, 0.11233423365101203,-4.4742684238924255,-1.4347465545587568, 6.200602417470947, 0.2810607618108918, 1.6064334697030347, 3.4844861030156835, 9.923963283781774, 13.599575070850051, 6.327800802364777, 14.07842424102597, 7.1255514336511965, 10.035840524280815, 11.191252060342634, 17.63744560261864]}

 

This example is a formula specific to an index in a stock. The final calculation result is the result of the test data,

 

Project Introduction: http://www.inthirties.com/thread-2708-1-1.html

Project address: http://code.google.com/p/exceoon/source/browse/trunk#trunk/components/equals

 

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.