22
33import org .antlr .v4 .runtime .CharStreams ;
44import org .antlr .v4 .runtime .CommonTokenStream ;
5+
6+ import toolgood .algorithm .enums .AreaUnitType ;
7+ import toolgood .algorithm .enums .DistanceUnitType ;
8+ import toolgood .algorithm .enums .MassUnitType ;
9+ import toolgood .algorithm .enums .VolumeUnitType ;
510import toolgood .algorithm .internals .AntlrCharStream ;
611import toolgood .algorithm .internals .AntlrErrorListener ;
712import toolgood .algorithm .internals .CharUtil ;
813import toolgood .algorithm .internals .DiyNameVisitor ;
14+ import toolgood .algorithm .internals .MathVisitor ;
15+ import toolgood .algorithm .internals .MyFunction ;
916import toolgood .algorithm .math .mathLexer ;
1017import toolgood .algorithm .math .mathParser ;
1118import toolgood .algorithm .math .mathParser .ProgContext ;
1724import java .math .BigDecimal ;
1825import java .util .HashSet ;
1926import java .util .Set ;
27+ import java .util .function .Function ;
2028
2129/**
2230 * 算法引擎助手
@@ -327,7 +335,8 @@ public static BigDecimal UnitConversion(BigDecimal src, String oldSrcUnit, Strin
327335 * @return
328336 * @throws Exception
329337 */
330- public static BigDecimal UnitConversion (BigDecimal src , String oldSrcUnit , String oldTarUnit , String name ) throws Exception {
338+ public static BigDecimal UnitConversion (BigDecimal src , String oldSrcUnit , String oldTarUnit , String name )
339+ throws Exception {
331340 if (oldSrcUnit == null || oldSrcUnit .equals ("" ) || oldTarUnit == null || oldTarUnit .equals ("" )) {
332341 return src ;
333342 }
@@ -352,8 +361,207 @@ public static BigDecimal UnitConversion(BigDecimal src, String oldSrcUnit, Strin
352361 return c .LeftToRight (src );
353362 }
354363 if (name == null || name .equals ("" )) {
355- throw new Exception ("The input item has different units and cannot be converted from [" + oldSrcUnit + "] to [" + oldTarUnit + "]" );
364+ throw new Exception ("The input item has different units and cannot be converted from [" + oldSrcUnit
365+ + "] to [" + oldTarUnit + "]" );
366+ }
367+ throw new Exception ("The input item [" + name + "] has different units and cannot be converted from ["
368+ + oldSrcUnit + "] to [" + oldTarUnit + "]" );
369+ }
370+
371+ /**
372+ * 解析
373+ *
374+ * @param exp
375+ * @return
376+ * @throws Exception
377+ */
378+ public static mathParser .ProgContext Parse (String exp ) throws Exception {
379+ if (null == exp || exp .equals ("" )) {
380+ throw new Exception ("Parameter exp invalid !" );
381+ }
382+ final AntlrCharStream stream = new AntlrCharStream (CharStreams .fromString (exp ));
383+ final mathLexer lexer = new mathLexer (stream );
384+ final CommonTokenStream tokens = new CommonTokenStream (lexer );
385+ final mathParser parser = new mathParser (tokens );
386+ final AntlrErrorListener antlrErrorListener = new AntlrErrorListener ();
387+ parser .removeErrorListeners ();
388+ parser .addErrorListener (antlrErrorListener );
389+ final ProgContext context = parser .prog ();
390+
391+ if (antlrErrorListener .IsError ) {
392+ throw new Exception (antlrErrorListener .ErrorMsg );
393+ }
394+ return context ;
395+ }
396+
397+ /**
398+ * 执行
399+ *
400+ * @param context
401+ * @return
402+ */
403+ public static Operand Evaluate (mathParser .ProgContext context ) {
404+ return Evaluate (context , null , null , true , false , DistanceUnitType .M , AreaUnitType .M2 , VolumeUnitType .M3 ,
405+ MassUnitType .KG );
406+ }
407+
408+ /**
409+ * 执行
410+ *
411+ * @param context
412+ * @param GetParameter
413+ * @return
414+ */
415+ public static Operand Evaluate (mathParser .ProgContext context , Function <String , Operand > GetParameter ) {
416+ return Evaluate (context , GetParameter , null , true , false , DistanceUnitType .M , AreaUnitType .M2 ,
417+ VolumeUnitType .M3 ,
418+ MassUnitType .KG );
419+ }
420+
421+ /**
422+ * 执行
423+ *
424+ * @param context
425+ * @param GetParameter
426+ * @param ExecuteDiyFunction
427+ * @return
428+ */
429+ public static Operand Evaluate (mathParser .ProgContext context , Function <String , Operand > GetParameter ,
430+ Function <MyFunction , Operand > ExecuteDiyFunction ) {
431+ return Evaluate (context , GetParameter , ExecuteDiyFunction , true , false , DistanceUnitType .M , AreaUnitType .M2 ,
432+ VolumeUnitType .M3 ,
433+ MassUnitType .KG );
434+ }
435+
436+ /**
437+ * 执行
438+ *
439+ * @param context
440+ * @param GetParameter
441+ * @param ExecuteDiyFunction
442+ * @param UseExcelIndex
443+ * @return
444+ */
445+ public static Operand Evaluate (mathParser .ProgContext context , Function <String , Operand > GetParameter ,
446+ Function <MyFunction , Operand > ExecuteDiyFunction , boolean UseExcelIndex ) {
447+ return Evaluate (context , GetParameter , ExecuteDiyFunction , UseExcelIndex , false , DistanceUnitType .M ,
448+ AreaUnitType .M2 ,
449+ VolumeUnitType .M3 ,
450+ MassUnitType .KG );
451+ }
452+
453+ /**
454+ * 执行
455+ *
456+ * @param context
457+ * @param GetParameter
458+ * @param ExecuteDiyFunction
459+ * @param UseExcelIndex
460+ * @param UseLocalTime
461+ * @return
462+ */
463+ public static Operand Evaluate (mathParser .ProgContext context , Function <String , Operand > GetParameter ,
464+ Function <MyFunction , Operand > ExecuteDiyFunction , boolean UseExcelIndex , boolean UseLocalTime ) {
465+ return Evaluate (context , GetParameter , ExecuteDiyFunction , UseExcelIndex , UseLocalTime , DistanceUnitType .M ,
466+ AreaUnitType .M2 ,
467+ VolumeUnitType .M3 ,
468+ MassUnitType .KG );
469+ }
470+
471+ /**
472+ * 执行
473+ *
474+ * @param context
475+ * @param GetParameter
476+ * @param ExecuteDiyFunction
477+ * @param UseExcelIndex
478+ * @param UseLocalTime
479+ * @param DistanceUnit
480+ * @return
481+ */
482+ public static Operand Evaluate (mathParser .ProgContext context , Function <String , Operand > GetParameter ,
483+ Function <MyFunction , Operand > ExecuteDiyFunction , boolean UseExcelIndex , boolean UseLocalTime ,
484+ DistanceUnitType DistanceUnit ) {
485+ return Evaluate (context , GetParameter , ExecuteDiyFunction , UseExcelIndex , UseLocalTime , DistanceUnit ,
486+ AreaUnitType .M2 ,
487+ VolumeUnitType .M3 ,
488+ MassUnitType .KG );
489+ }
490+
491+ /**
492+ * 执行
493+ *
494+ * @param context
495+ * @param GetParameter
496+ * @param ExecuteDiyFunction
497+ * @param UseExcelIndex
498+ * @param UseLocalTime
499+ * @param DistanceUnit
500+ * @param AreaUnit
501+ * @return
502+ */
503+ public static Operand Evaluate (mathParser .ProgContext context , Function <String , Operand > GetParameter ,
504+ Function <MyFunction , Operand > ExecuteDiyFunction , boolean UseExcelIndex , boolean UseLocalTime ,
505+ DistanceUnitType DistanceUnit , AreaUnitType AreaUnit ) {
506+ return Evaluate (context , GetParameter , ExecuteDiyFunction , UseExcelIndex , UseLocalTime , DistanceUnit ,
507+ AreaUnit ,
508+ VolumeUnitType .M3 ,
509+ MassUnitType .KG );
510+ }
511+
512+ /**
513+ * 执行
514+ *
515+ * @param context
516+ * @param GetParameter
517+ * @param ExecuteDiyFunction
518+ * @param UseExcelIndex
519+ * @param UseLocalTime
520+ * @param DistanceUnit
521+ * @param AreaUnit
522+ * @param VolumeUnit
523+ * @return
524+ */
525+ public static Operand Evaluate (mathParser .ProgContext context , Function <String , Operand > GetParameter ,
526+ Function <MyFunction , Operand > ExecuteDiyFunction , boolean UseExcelIndex , boolean UseLocalTime ,
527+ DistanceUnitType DistanceUnit , AreaUnitType AreaUnit , VolumeUnitType VolumeUnit ) {
528+ return Evaluate (context , GetParameter , ExecuteDiyFunction , UseExcelIndex , UseLocalTime , DistanceUnit ,
529+ AreaUnit ,
530+ VolumeUnit ,
531+ MassUnitType .KG );
532+ }
533+
534+ /**
535+ * 执行
536+ *
537+ * @param context
538+ * @param GetParameter
539+ * @param ExecuteDiyFunction
540+ * @param UseExcelIndex
541+ * @param UseLocalTime
542+ * @param DistanceUnit
543+ * @param AreaUnit
544+ * @param VolumeUnit
545+ * @param MassUnit
546+ * @return
547+ */
548+ public static Operand Evaluate (mathParser .ProgContext context , Function <String , Operand > GetParameter ,
549+ Function <MyFunction , Operand > ExecuteDiyFunction , boolean UseExcelIndex , boolean UseLocalTime ,
550+ DistanceUnitType DistanceUnit , AreaUnitType AreaUnit , VolumeUnitType VolumeUnit , MassUnitType MassUnit ) {
551+ MathVisitor visitor = new MathVisitor ();
552+ if (GetParameter != null ) {
553+ visitor .GetParameter = GetParameter ;
356554 }
357- throw new Exception ("The input item [" + name + "] has different units and cannot be converted from [" + oldSrcUnit + "] to [" + oldTarUnit + "]" );
555+ if (ExecuteDiyFunction != null ) {
556+ visitor .DiyFunction = ExecuteDiyFunction ;
557+ }
558+ visitor .excelIndex = UseExcelIndex ? 1 : 0 ;
559+ visitor .useLocalTime = UseLocalTime ;
560+ visitor .MassUnit = MassUnit ;
561+ visitor .DistanceUnit = DistanceUnit ;
562+ visitor .AreaUnit = AreaUnit ;
563+ visitor .VolumeUnit = VolumeUnit ;
564+ return visitor .visit (context );
358565 }
566+
359567}
0 commit comments