1 package org.djutils.eval;
2
3 import java.util.HashMap;
4 import java.util.Map;
5
6 import org.djunits.value.vdouble.scalar.Position;
7 import org.djunits.value.vdouble.scalar.base.DoubleScalar;
8
9
10
11
12
13
14
15
16
17
18
19 public class Calculator
20 {
21
22
23
24
25 public static void main(final String... args)
26 {
27 doEval("3[kg.ms-2]");
28 doEval("23.45*10");
29 doEval(" 23.45 * 10 ");
30 doEval("PI()*AVOGADRO()");
31 doEval("PI()");
32 doEval("PI()/PI()");
33 doEval("CURRENTTIME()");
34 doEval("PHI()");
35 System.out.println();
36 doEval("NEUTRONMASS()-ELECTRONMASS()");
37 doEval("NEUTRONMASS()-PI()");
38 doEval("12 [ms-1] / 123[/s]");
39 Map<String, DoubleScalar<?,?>> valuePool = new HashMap<>();
40 valuePool.put("position", Position.valueOf("100 m"));
41 doEval("position-10[m]", new RetrieveValue() {
42
43 @Override
44 public DoubleScalar<?, ?> lookup(final String name)
45 {
46 return valuePool.get(name);
47 }});
48 doEval("position+10[m]", new RetrieveValue() {
49
50 @Override
51 public DoubleScalar<?, ?> lookup(final String name)
52 {
53 return valuePool.get(name);
54 }});
55 doEval("position-position", new RetrieveValue() {
56
57 @Override
58 public DoubleScalar<?, ?> lookup(final String name)
59 {
60 return valuePool.get(name);
61 }});
62 doEval("position+position", new RetrieveValue() {
63
64 @Override
65 public DoubleScalar<?, ?> lookup(final String name)
66 {
67 return valuePool.get(name);
68 }});
69 doEval("5^3");
70 doEval("5<5");
71 doEval("!5<5");
72 doEval("12 [m/s] > 7 [m]");
73 doEval("TRUE()");
74 doEval("FALSE()");
75 doEval("TRUE()!=5");
76 doEval("TRUE()==5");
77 doEval("FALSE()==5");
78 doEval("(2>3)?5:1+100");
79 doEval("PHI()");
80 doEval("-0==0");
81 doEval("2^-2");
82 doEval("3e+5");
83 doEval("12>4?TRUE():(9+5)");
84 doEval("3==5!");
85 doEval("3[km/h]");
86 doEval("5*2[s]/5[s]");
87 }
88
89
90
91
92
93 public static void doEval(final String expression)
94 {
95 doEval(expression, null);
96 }
97
98
99
100
101
102
103 public static void doEval(final String expression, final RetrieveValue valuePool)
104 {
105 try
106 {
107 Object result = new Eval().setRetrieveValue(valuePool).evaluate(expression);
108 System.out.println(expression + ": " + result);
109 }
110 catch (RuntimeException e)
111 {
112 System.out.println(expression + ": ERROR: "+ e.getMessage());
113 }
114 }
115 }