View Javadoc
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   * Calculator.java. Demonstrates use of the Eval class.
11   * <p>
12   * Copyright (c) 2023-2024 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
13   * for project information <a href="https://djutils.org" target="_blank"> https://djutils.org</a>. The DJUTILS project is
14   * distributed under a three-clause BSD-style license, which can be found at
15   * <a href="https://djutils.org/docs/license.html" target="_blank"> https://djutils.org/docs/license.html</a>.
16   * </p>
17   * @author <a href="https://www.tudelft.nl/pknoppers">Peter Knoppers</a>
18   */
19  public class Calculator
20  {
21      /**
22       * Demonstrate the Eval class. Program entry point.
23       * @param args String ...; the command line arguments
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")); // Absolute
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");//FIXME
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       * Print expression followed by the result.
91       * @param expression String; expression to evaluate
92       */
93      public static void doEval(final String expression)
94      {
95          doEval(expression, null);
96      }
97      
98      /**
99       * Print expression followed by the result.
100      * @param expression String; the expression to evaluate
101      * @param valuePool RetrieveValue; variable pool
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 }