一个不错的java的表达式计算的工具

在我们的项目中,如果是做流程或者是一些涉及到计算的模块时,是不是有动态进行计算的需求呀。

比如,我们从配置里读取到一个公式,比如最简单的 sin(A) + cos(B) 你会怎样实现呢,

这个实现上没有多大的困难,从最简单的hard code的解析,到正则表达解析,或者是AST语法树解析,或者用bsh,jython,jruby/groovy等脚本引擎,或者更有甚者直接用sql执行,等等方法多种多样。不过每种方法或多或少都有自己一定的局限。如何找到一个比较好的解决方案呢。

这里给大家推荐一个比较不错灵活的公式模块的java实现equals

equals是exceoon的david.liu在google codes里贡献的一个项目。是实现公式计算模板的一个解决方案。该项目在多个商业的股票行情软件的自编规则和自编指标的模块中得以广泛应用,有使用过股票软件的朋友,应该对选股器和自定义指标这两个功能比较熟悉吧。

也就是客户自己在编辑器一定义出一个或者是一套计算公式,然后程序会根据你指定的公式进行计算,当然作为商业上的应用,这里的计算不仅仅局限于加减乘除,还包含标准偏差,平均线,移动平均线等指标的计算。

比如下面的这段测试代码

String txt = "DIF:EMA(CLOSE,SHORT)-EMA(OPEN,LONG);"
+ "DEA:EMA(DIF,MID);"

+ "MACD:(DIF-DEA)*2::COLORSTICK;";

先定义一个公式 这个公式是计算股票的指标MACD的。

String[] defs = new String[]
{ "SHORT", "LONG", "MID" };

定义公式中的输入参数

Formula f = null;
f = FormulaBuilder.constructFormula(txt, defs);

声明一个Formula的变量,这里就是整个公式计算的entry

FormulaParameters fp = new FormulaParameters();
fp.addParameter("short", 12);
fp.addParameter("long", 26);
fp.addParameter("mid", 9);

声明公式需要的输入变量。

FormulaResult fr = f.eval(fp);
Map<String, Object> results = fr.getResult();

进行计算得到结果

运行最后的结果

{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, 5.102424718335767, 2.3071096698386544, -0.3636299728305956, -1.0764588575814014, -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, 10.630656737970867, 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]}

这个例子是专门针对股票中的一个指标做的公式。最后的计算结果是测试数据的结果,

项目介绍: http://www.inthirties.com/thread-2708-1-1.html

项目地址: http://code.google.com/p/exceoon/source/browse/trunk#trunk/components/equals

你可能感兴趣的:(java,正则表达式,groovy,FP,jython)