Interpreter Pattern: Calculator : Interpretor Pattern « Design Pattern « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Design Pattern » Interpretor Pattern 




Interpreter Pattern: Calculator
Interpreter Pattern: Calculator

/*

Software Architecture Design Patterns in Java
by Partha Kuchana 

Auerbach Publications

*/



import java.util.HashMap;
import java.util.Stack;

public class Calculator {
  private String expression;

  private HashMap operators;

  private Context ctx;

  public static void main(String[] args) {
    Calculator calc = new Calculator();
    //instantiate the context
    Context ctx = new Context();

    //set the expression to evaluate
    calc.setExpression("(a+b)*(c-d)");

    //configure the calculator with the
    // Context
    calc.setContext(ctx);

    //Display the result
    System.out.println(" Variable Values: " "a=" + ctx.getValue("a")
        ", b=" + ctx.getValue("b"", c=" + ctx.getValue("c")
        ", d=" + ctx.getValue("d"));
    System.out.println(" Expression = (a+b)*(c-d)");
    System.out.println(" Result = " + calc.evaluate());
  }

  public Calculator() {
    operators = new HashMap();
    operators.put("+""1");
    operators.put("-""1");
    operators.put("/""2");
    operators.put("*""2");
    operators.put("(""0");
  }

  public void setContext(Context c) {
    ctx = c;
  }

  public void setExpression(String expr) {
    expression = expr;
  }

  public int evaluate() {
    //infix to Postfix
    String pfExpr = infixToPostFix(expression);

    //build the Binary Tree
    Expression rootNode = buildTree(pfExpr);

    //Evaluate the tree
    return rootNode.evaluate(ctx);
  }

  private NonTerminalExpression getNonTerminalExpression(String operation,
      Expression l, Expression r) {
    if (operation.trim().equals("+")) {
      return new AddExpression(l, r);
    }
    if (operation.trim().equals("-")) {
      return new SubtractExpression(l, r);
    }
    if (operation.trim().equals("*")) {
      return new MultiplyExpression(l, r);
    }

    return null;
  }

  private Expression buildTree(String expr) {
    Stack s = new Stack();

    for (int i = 0; i < expr.length(); i++) {
      String currChar = expr.substring(i, i + 1);

      if (isOperator(currChar== false) {
        Expression e = new TerminalExpression(currChar);
        s.push(e);
      else {
        Expression r = (Expressions.pop();
        Expression l = (Expressions.pop();
        Expression n = getNonTerminalExpression(currChar, l, r);
        s.push(n);
      }
    }//for
    return (Expressions.pop();
  }

  private String infixToPostFix(String str) {
    Stack s = new Stack();
    String pfExpr = "";
    String tempStr = "";

    String expr = str.trim();
    for (int i = 0; i < str.length(); i++) {

      String currChar = str.substring(i, i + 1);

      if ((isOperator(currChar== false&& (!currChar.equals("("))
          && (!currChar.equals(")"))) {
        pfExpr = pfExpr + currChar;
      }
      if (currChar.equals("(")) {
        s.push(currChar);
      }
      //for ')' pop all stack contents until '('
      if (currChar.equals(")")) {
        tempStr = (Strings.pop();
        while (!tempStr.equals("(")) {
          pfExpr = pfExpr + tempStr;
          tempStr = (Strings.pop();
        }
        tempStr = "";
      }
      //if the current character is an
      // operator
      if (isOperator(currChar)) {
        if (s.isEmpty() == false) {
          tempStr = (Strings.pop();
          String strVal1 = (Stringoperators.get(tempStr);
          int val1 = new Integer(strVal1).intValue();
          String strVal2 = (Stringoperators.get(currChar);
          int val2 = new Integer(strVal2).intValue();

          while ((val1 >= val2)) {
            pfExpr = pfExpr + tempStr;
            val1 = -100;
            if (s.isEmpty() == false) {
              tempStr = (Strings.pop();
              strVal1 = (Stringoperators.get(tempStr);
              val1 = new Integer(strVal1).intValue();

            }
          }
          if ((val1 < val2&& (val1 != -100))
            s.push(tempStr);
        }
        s.push(currChar);
      }//if

    }// for
    while (s.isEmpty() == false) {
      tempStr = (Strings.pop();
      pfExpr = pfExpr + tempStr;
    }
    return pfExpr;
  }

  private boolean isOperator(String str) {
    if ((str.equals("+")) || (str.equals("-")) || (str.equals("*"))
        || (str.equals("/")))
      return true;
    return false;
  }
// End of class

class Context {
  private HashMap varList = new HashMap();

  public void assign(String var, int value) {
    varList.put(var, new Integer(value));
  }

  public int getValue(String var) {
    Integer objInt = (IntegervarList.get(var);
    return objInt.intValue();
  }

  public Context() {
    initialize();
  }

  //Values are hardcoded to keep the example simple
  private void initialize() {
    assign("a"20);
    assign("b"40);
    assign("c"30);
    assign("d"10);
  }

}

class TerminalExpression implements Expression {
  private String var;

  public TerminalExpression(String v) {
    var = v;
  }

  public int evaluate(Context c) {
    return c.getValue(var);
  }
}

interface Expression {
  public int evaluate(Context c);
}

abstract class NonTerminalExpression implements Expression {
  private Expression leftNode;

  private Expression rightNode;

  public NonTerminalExpression(Expression l, Expression r) {
    setLeftNode(l);
    setRightNode(r);
  }

  public void setLeftNode(Expression node) {
    leftNode = node;
  }

  public void setRightNode(Expression node) {
    rightNode = node;
  }

  public Expression getLeftNode() {
    return leftNode;
  }

  public Expression getRightNode() {
    return rightNode;
  }
}// NonTerminalExpression

class AddExpression extends NonTerminalExpression {
  public int evaluate(Context c) {
    return getLeftNode().evaluate(c+ getRightNode().evaluate(c);
  }

  public AddExpression(Expression l, Expression r) {
    super(l, r);
  }
}// AddExpression

class SubtractExpression extends NonTerminalExpression {
  public int evaluate(Context c) {
    return getLeftNode().evaluate(c- getRightNode().evaluate(c);
  }

  public SubtractExpression(Expression l, Expression r) {
    super(l, r);
  }
}// SubtractExpression

class MultiplyExpression extends NonTerminalExpression {
  public int evaluate(Context c) {
    return getLeftNode().evaluate(c* getRightNode().evaluate(c);
  }

  public MultiplyExpression(Expression l, Expression r) {
    super(l, r);
  }

}// MultiplyExpression


//File: Interprepter.properties
/*
a=10
b=20
c=30
d=40

*/


           
       














Related examples in the same category
1.Interpreter pattern in JavaInterpreter pattern in Java
2.Interpreter Pattern 2
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.