001: package de.odysseus.el.samples.calculator;
002:
003: import java.io.BufferedReader;
004: import java.io.IOException;
005: import java.io.InputStreamReader;
006:
007: import javax.el.ELException;
008: import javax.el.ExpressionFactory;
009: import javax.el.ValueExpression;
010:
011: import de.odysseus.el.ExpressionFactoryImpl;
012: import de.odysseus.el.util.SimpleContext;
013:
014: /*
015: * Copyright 2006, 2007 Odysseus Software GmbH
016: *
017: * Licensed under the Apache License, Version 2.0 (the "License");
018: * you may not use this file except in compliance with the License.
019: * You may obtain a copy of the License at
020: *
021: * http://www.apache.org/licenses/LICENSE-2.0
022: *
023: * Unless required by applicable law or agreed to in writing, software
024: * distributed under the License is distributed on an "AS IS" BASIS,
025: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
026: * See the License for the specific language governing permissions and
027: * limitations under the License.
028: */
029:
030: /**
031: * Simple command line calculator demo.
032: * Read one expression per line from stdin (without "${" and "}").
033: * Expression "m[0-9]*" is used to save the last evaluation result to "memory".
034: * Other expressions are evaluated.
035: * An empty line terminates the calculator.
036: */
037: public class Calculator {
038: public static void main(String[] args)
039: throws NoSuchMethodException, IOException {
040: ExpressionFactory factory = new ExpressionFactoryImpl();
041:
042: SimpleContext context = new SimpleContext();
043:
044: // variables e, pi
045: context.setVariable("e", factory.createValueExpression(Math.E,
046: double.class));
047: context.setVariable("pi", factory.createValueExpression(
048: Math.PI, double.class));
049:
050: // functions sin, cos, tan, exp, log, abs, sqrt, min, max, pow
051: context.setFunction("", "sin", Math.class.getMethod("sin",
052: double.class));
053: context.setFunction("", "cos", Math.class.getMethod("cos",
054: double.class));
055: context.setFunction("", "tan", Math.class.getMethod("tan",
056: double.class));
057: context.setFunction("", "exp", Math.class.getMethod("exp",
058: double.class));
059: context.setFunction("", "log", Math.class.getMethod("log",
060: double.class));
061: context.setFunction("", "abs", Math.class.getMethod("abs",
062: double.class));
063: context.setFunction("", "sqrt", Math.class.getMethod("sqrt",
064: double.class));
065: context.setFunction("", "min", Math.class.getMethod("min",
066: double.class, double.class));
067: context.setFunction("", "max", Math.class.getMethod("max",
068: double.class, double.class));
069: context.setFunction("", "pow", Math.class.getMethod("pow",
070: double.class, double.class));
071:
072: // print out the rules of the game...
073: System.out
074: .println("> Enter one expression per line (without \"${\" and \"}\"). An expressions matching");
075: System.out
076: .println("> \"m[0-9]*\" saves the previous evaluation result to \"memory\". Other expressions");
077: System.out
078: .println("> are simply evaluated. Functions are sin, cos, tan, exp, log, abs, sqrt, min,");
079: System.out
080: .println("> max and pow, variables are e and pi. An empty line terminates the calculator.");
081:
082: // read/evaluate expressions
083: BufferedReader reader = new BufferedReader(
084: new InputStreamReader(System.in));
085: Object display = 0;
086: while (true) {
087: System.out.print("< ");
088: String line = reader.readLine().trim();
089: if (line.length() == 0) {
090: System.out.println("> Good bye.");
091: System.exit(0);
092: }
093: try {
094: ValueExpression expr = factory.createValueExpression(
095: context, "${" + line + "}", Object.class);
096: if (line.matches("m[0-9]*")) { // "save to memory"
097: expr.setValue(context, display);
098: } else {
099: display = expr.getValue(context);
100: }
101: System.out.println("> " + display);
102: } catch (ELException e) {
103: System.out.println("> " + e.getMessage());
104: }
105: }
106: }
107: }
|