出處/樂(lè)字節(jié)
MXReflection,一個(gè)基于mXparser庫(kù)功能的Java復(fù)雜計(jì)算框架。
還記不記得求學(xué)時(shí)代各種復(fù)雜的數(shù)學(xué)公式?sin、log2、tan等等等,是不是看到這就覺(jué)得算起來(lái)麻煩?
通過(guò)MXReflection框架,你可以使用Java計(jì)算這些曾經(jīng)我們覺(jué)得無(wú)比復(fù)雜的數(shù)學(xué)運(yùn)算和函數(shù),只需使用與類相關(guān)的字段。
MXReflection可以從指定字段讀取值,并將結(jié)果注入@Expression注釋字段。
<dependency>
? ? <groupId>com.github.ismail-mekni</groupId>
? ? <artifactId>mxreflection</artifactId>
? ? <version>1.0.1</version>
</dependency>
dependencies {
? ? compile group: 'com.github.ismail-mekni', name: 'mxreflection', version: '1.0.1'
}
?那如何使用這個(gè)框架呢?
很簡(jiǎn)單,只需要兩個(gè)java注釋。
1、通過(guò)@Arg值,我們可以指定要在目標(biāo)函數(shù)中使用的自定義參數(shù)名稱。
2、@Expression注釋值包含帶有參數(shù)的函數(shù)表達(dá)式。
這個(gè)框架的使用其實(shí)不難,下面是兩個(gè)計(jì)算案例,通過(guò)案例其實(shí)很容易理解該怎么使用此框架:
案例1:
package com.ismail.mxreflection.example;
import com.ismail.mxreflection.annotations.Arg;
import com.ismail.mxreflection.annotations.Expression;
import com.ismail.mxreflection.core.Calculator;
import com.ismail.mxreflection.factory.MXFactory;
import org.junit.Test;
public class Example1Test {
? ? class Example1 {
? ? ? ? @Arg("f1")
? ? ? ? String field1;
? ? ? ? @Arg("f2")
? ? ? ? int field2;
? ? ? ? @Expression("f1 * sin(f2) * log2(f1 + f2) + der(cos(f1), f1) * pi + int(tan(f2), f2, 0, e)")
? ? ? ? double field3;
? ? }
? ? @Test
public void example1Test() {
? ? ? ? Example1 example1 = new Example1();
? ? ? ? example1.field1 = "2.2";
? ? ? ? example1.field2 = 5;
? ? ? ? Calculator<Example1> calculator = MXFactory.createCalculator(Example1.class);
? ? ? ? calculator.calculate(example1);
? ? ? ? System.out.println("Field 3 result: " + example1.field3);
? ? }
}
Output:
Field 3 result: 2.8
Field 4 result: -2.2
Field 5 result: -2.8
Field 6 result: 0.6
Field 7 result: 3
Field 8 result: 8
MXReflection框架支持mXparser數(shù)學(xué)庫(kù)中提供的數(shù)學(xué)集合如下:
Operators (+, -, *, /, #, !, ^)
Binary Relations (=, ==, =<, =>, <, >, <>, !=, ~=)
Boolean Operators (&, &&, /, ~&, ~&&, ~/, |, ||…)
Bitwise Operators (@~, @&, @^, @|, @<<, @>>)
Unary Functions (sin, cos, tan, tg, ctan, ctg, cot, sec,…)
Binary Functions (log, mod, C, Bern, Stirl1, Stirl2, …)
3-args Functions (if, chi, CHi, Chi, cHi, pUni, cUni, qUni, pNor, cNor, qNor)
Variadic Functions (iff, min, max, ConFrac, ConPol, gcd, …)
Iterated Operators (sum, prod, avg, vari, stdi, mini, maxi)
Calculus Operators (int, der, der-, der+, dern, diff, difb)
Math Constants (pi, e, [gam], [phi], [PN], [B*], [F’d], [F’a], …)
Physical Constants ([c], [G.], [g], [hP], [h-], [lP], [mP], [tP])
Astronomical Constants ([ly], [au], [pc], [kpc], [Earth-R-eq], …)
Random Variables ([Uni], [Int], [Int1], [Int2], [Int3], [Int4], …)
Metric prefixes ([%], [%%], [Y], [sept], [Z], [sext], [E], …)
Parser Symbols ((, ), ,, ;)
Units
在參數(shù)解析方面,MXReflection支持以數(shù)字內(nèi)容作為參數(shù)的所有字段數(shù)據(jù)類型。您可以將所有Java類型與返回?cái)?shù)值結(jié)果的toString實(shí)現(xiàn)一起使用。支持的結(jié)果字段java類型有:
Double
double
Long
long
String
BigInteger
但是要注意的是注意,對(duì)于long、long和BigInteger,MXReflection使用在注入前解析最終結(jié)果。建議確保表達(dá)式返回整數(shù)類型。
——此文章轉(zhuǎn)載于樂(lè)字節(jié)