carfield.com.hkAndExp.java2004-03-24T16:00:00Z2004-03-24T16:00:00Z<br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.interpreter.java;
/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* This file is part of the design patterns project at UBC
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is ca.ubc.cs.spl.patterns.
*
* Contributor(s):
*/
/**
* Implements an AND expression for booleans. This is a concrete boolean
* expression
*
* @author Jan Hannemann
* @author Gregor Kiczales
* @version 1.0, 05/13/02
*/
public class AndExp implements BooleanExp {
/**
* stores the first part of the AND expression
*/
protected BooleanExp op1 = null;
/**
* stores the second part of the AND expression
*/
protected BooleanExp op2 = null;
/**
* Creates a new AND expression with the given parts
*
* @param op1 the first expression
* @param op1 the second expression
*/
public AndExp(BooleanExp op1, BooleanExp op2) {
this.op1 = op1;
this.op2 = op2;
}
/**
* Evaluates this expression in the given <i>Context</i>
*
* @param c the context to evaluate the expression in
* @returns the boolean value of the expression
*/
public boolean evaluate(Context c) {
return (op1.evaluate(c) && op2.evaluate(c));
}
/**
* Replaces a variable with an expression
*
* @param name the name of the variable
* @param exp the expression to replace the variable
* @returns a copy of this expression with the variable replaced
*/
public BooleanExp replace(String name, BooleanExp exp) {
return new AndExp(op1.replace(name, exp), op2.replace(name,exp));
}
/**
* Copies this expression
*
* @returns the copied expression
*/
public BooleanExp copy() {
return new AndExp(op1.copy(), op2.copy());
}
}</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>2004-03-24T16:00:00ZBooleanConstant.java2004-03-24T16:00:00Z2004-03-24T16:00:00Z<br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.interpreter.java;
/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* This file is part of the design patterns project at UBC
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is ca.ubc.cs.spl.patterns.
*
* Contributor(s):
*/
/**
* Implements boolean constants, a concrete boolean expression
*
* @author Jan Hannemann
* @author Gregor Kiczales
* @version 1.0, 05/13/02
*/
public class BooleanConstant implements BooleanExp {
/**
* the value of this constant
*/
protected boolean value;
/**
* Creates a new constant with the given value
*
* @param value the value this constant should represent
*/
public BooleanConstant(boolean value) {
this.value = value;
}
/**
* Evaluates this expression in the given <i>Context</i>.
*
* @param c the context to evaluate the expression in
* @returns the boolean value of the expression
*/
public boolean evaluate(Context c) {
return value;
}
/**
* Replaces a variable with an expression. Has no effect.
*
* @param name the name of the variable
* @param exp the expression to replace the variable
* @returns a copy of this expression with the variable replaced
*/
public BooleanExp replace(String name, BooleanExp exp) {
return this;
}
/**
* Copies this expression
*
* @returns the copied expression
*/
public BooleanExp copy() {
return new BooleanConstant(value);
}
}</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>2004-03-24T16:00:00ZBooleanExp.java2004-03-24T16:00:00Z2004-03-24T16:00:00Z<br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.interpreter.java;
/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* This file is part of the design patterns project at UBC
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is ca.ubc.cs.spl.patterns.
*
* Contributor(s):
*/
/**
* Defines the <i>AbstractExpression</i> interface for boolean expressions.
*
* @author Jan Hannemann
* @author Gregor Kiczales
* @version 1.0, 05/13/02
*/
public abstract interface BooleanExp {
/**
* Evaluates this expression in the given <i>Context</i>
*
* @param c the context to evaluate the expression in
* @returns the boolean value of the expression
*/
public boolean evaluate(Context c);
/**
* Replaces a variable with an expression
*
* @param name the name of the variable
* @param exp the expression to replace the variable
* @returns a copy of this expression with the variable replaced
*/
public BooleanExp replace(String name, BooleanExp exp);
/**
* Copies this expression
*
* @returns the copied expression
*/
public BooleanExp copy();
}</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>2004-03-24T16:00:00ZContext.java2004-03-24T16:00:00Z2004-03-24T16:00:00Z<br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.interpreter.java;
/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* This file is part of the design patterns project at UBC
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is ca.ubc.cs.spl.patterns.
*
* Contributor(s):
*/
import java.util.Hashtable;
/**
* Implements a <i>Context</i> for the interpretation of boolean
* expressions<p>
*
* @author Jan Hannemann
* @author Gregor Kiczales
* @version 1.0, 05/13/02
*
* @see BooleanExp
*/
public class Context {
/**
* stores the mapping between variable names and values
*/
protected Hashtable assignments = new Hashtable();
/**
* Returns the current value for a variable
*
* @param name the name of the variable
* @returns the value of the variable
*/
public boolean lookup(String name) {
Boolean value = (Boolean) assignments.get(name);
if (value == null) {
throw new ExpressionException("No variable \""+name+"\" declared.");
}
return value.booleanValue();
}
/**
* Assigns a boolean value to a <code>VariableExp</code>
*
* @param varExp the varaible expression to assign a value to
* @param bool the boolean value to assign
*/
public void assign(VariableExp varExp, boolean bool) {
assignments.put(varExp.getName(), new Boolean(bool));
}
}</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>2004-03-24T16:00:00ZExpressionException.java2004-03-24T16:00:00Z2004-03-24T16:00:00Z<br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.interpreter.java;
/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* This file is part of the design patterns project at UBC
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is ca.ubc.cs.spl.patterns.
*
* Contributor(s):
*/
/**
* Implements a specialized exception that gets raised when the interpreter
* runs into errors.
*
* @author Jan Hannemann
* @author Gregor Kiczales
* @version 1.0, 05/13/02
*/
public class ExpressionException extends RuntimeException {
/**
* Creates a new ExpressionException with the given message
*
* @param s the exception message
*/
public ExpressionException(String s) {
super(s);
}
}</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>2004-03-24T16:00:00ZMain.java2004-03-24T16:00:00Z2004-03-24T16:00:00Z<br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.interpreter.java;
/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* This file is part of the design patterns project at UBC
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is ca.ubc.cs.spl.patterns.
*
* Contributor(s):
*/
/**
* Implements the driver for the Intepreter design pattern example.<p>
*
* Intent: <i>Given a language, defeine a representation fro its grammar along
* with an interpreter that uses the representation to interpret sentences
* in the language.</i><p>
*
* Participating objects are <code>BooleanContant</code>, <code>VariableExp
* </code>, <code>OrExp</code>, <code>AndExp</code>, and <code>NotExp</code>
* as <i>Expressions</i>. The <i>AbstractExpression</i> interface is defined
* in <code>BooelanExp</i>.<p>
*
* This example implements an interpreter for a language of boolean
* expressions. As a sample expression, "((true & x) | (y & !x))" is
* interpreted for all possible boolean values for x and y. After that,
* y is replaced by another expression and the whole expression is
* evaluated again.
*
* <p><i>This is the Java version.</i><p>
*
* @author Jan Hannemann
* @author Gregor Kiczales
* @version 1.0, 05/13/02
*
* @see BooleanExp
*/
public class Main {
/**
* Assigns boolean values to two <code>VariableExp</code>s and evaluates
* an expression in the given context.
*
* @param x a boolean variable expression
* @param xValue the value to assign to x
* @param y another boolean variable expression
* @param yValue the value to assign to y
* @param context the context to evaluate the expression in
* @param exp the expression to evaluate
*/
private static void assignAndEvaluate( VariableExp x, boolean xValue,
VariableExp y, boolean yValue,
Context context, BooleanExp exp) {
context.assign(x, xValue);
context.assign(y, yValue);
boolean result = exp.evaluate(context);
System.out.println("The result for (x="+xValue+", y="+yValue+") is: "+result);
}
/**
* Implements the driver for the Intepreter design pattern example.<p>
*
* This example implements an interpreter for a language of boolean
* expressions. As a sample expression, "((true & x) | (y & !x))" is
* interpreted for all possible boolean values for x and y. After that,
* y is replaced by another expression and the whole expression is
* evaluated again.
*
* @args command-line parameters, unused.
*/
public static void main(String[] args) {
BooleanExp exp = null;
Context context = new Context();
VariableExp x = new VariableExp("X");
VariableExp y = new VariableExp("Y");
exp = new OrExp(new AndExp(new BooleanConstant(true), x),
new AndExp(y, new NotExp(x)));
System.out.println("Testing Expr: ((true & x) | (y & !x))");
assignAndEvaluate(x, false, y, false, context, exp);
assignAndEvaluate(x, false, y, true, context, exp);
assignAndEvaluate(x, true, y, false, context, exp);
assignAndEvaluate(x, true, y, true, context, exp);
VariableExp z = new VariableExp("Z");
NotExp notZ = new NotExp(z);
BooleanExp replacement = exp.replace("Y", notZ);
context.assign(z, false);
boolean result = replacement.evaluate(context);
System.out.println("The result for the replacement is: "+result);
}
}
</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>2004-03-24T16:00:00ZNotExp.java2004-03-24T16:00:00Z2004-03-24T16:00:00Z<br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.interpreter.java;
/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* This file is part of the design patterns project at UBC
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is ca.ubc.cs.spl.patterns.
*
* Contributor(s):
*/
/**
* Implements negation for booleans expressions. This is a concrete boolean
* expression
*
* @author Jan Hannemann
* @author Gregor Kiczales
* @version 1.0, 05/13/02
*/
public class NotExp implements BooleanExp {
/**
* the expression this expression negates
*/
protected BooleanExp exp = null;
/**
* Creates a new NOT expression negating the argument expression
*
* @param exp the expression to negate
*/
public NotExp(BooleanExp exp) {
this.exp = exp;
}
/**
* Evaluates this expression in the given <i>Context</i>
*
* @param c the context to evaluate the expression in
* @returns the boolean value of the expression
*/
public boolean evaluate(Context c) {
return (! exp.evaluate(c));
}
/**
* Replaces a variable with an expression
*
* @param name the name of the variable
* @param exp the expression to replace the variable
* @returns a copy of this expression with the variable replaced
*/
public BooleanExp replace(String name, BooleanExp exp) {
return new NotExp(this.exp.replace(name, exp));
}
/**
* Copies this expression
*
* @returns the copied expression
*/
public BooleanExp copy() {
return new NotExp(exp.copy());
}
}</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>2004-03-24T16:00:00ZOrExp.java2004-03-24T16:00:00Z2004-03-24T16:00:00Z<br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.interpreter.java;
/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* This file is part of the design patterns project at UBC
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is ca.ubc.cs.spl.patterns.
*
* Contributor(s):
*/
/**
* Implements an OR expression for booleans. This is a concrete boolean
* expression
*
* @author Jan Hannemann
* @author Gregor Kiczales
* @version 1.0, 05/13/02
*/
public class OrExp implements BooleanExp {
/**
* stores the first part of the OR expression
*/
protected BooleanExp op1 = null;
/**
* stores the second part of the OR expression
*/
protected BooleanExp op2 = null;
/**
* Creates a new OR expression with the given parts
*
* @param op1 the first expression
* @param op1 the second expression
*/
public OrExp(BooleanExp op1, BooleanExp op2) {
this.op1 = op1;
this.op2 = op2;
}
/**
* Evaluates this expression in the given <i>Context</i>
*
* @param c the context to evaluate the expression in
* @returns the boolean value of the expression
*/
public boolean evaluate(Context c) {
return (op1.evaluate(c) || op2.evaluate(c));
}
/**
* Replaces a variable with an expression
*
* @param name the name of the variable
* @param exp the expression to replace the variable
* @returns a copy of this expression with the variable replaced
*/
public BooleanExp replace(String name, BooleanExp exp) {
return new OrExp(op1.replace(name, exp), op2.replace(name,exp));
}
/**
* Copies this expression
*
* @returns the copied expression
*/
public BooleanExp copy() {
return new OrExp(op1.copy(), op2.copy());
}
}</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>2004-03-24T16:00:00ZVariableExp.java2004-03-24T16:00:00Z2004-03-24T16:00:00Z<br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.interpreter.java;
/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* This file is part of the design patterns project at UBC
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is ca.ubc.cs.spl.patterns.
*
* Contributor(s):
*/
/**
* Implements a variable expression for booleans. This is a concrete boolean
* expression
*
* @author Jan Hannemann
* @author Gregor Kiczales
* @version 1.0, 05/13/02
*/
public class VariableExp implements BooleanExp {
/**
* the name of the variable this object represents
*/
protected String name = null;
/**
* Creates a new variable expression with a given name
*
* @param name the name of the new variable
*/
public VariableExp(String name) {
this.name = name;
}
/**
* Accessor for the variable's name
*
* @returns the name of the variable
*/
public String getName() {
return name;
}
/**
* Evaluates this expression in the given <i>Context</i>
*
* @param c the context to evaluate the expression in
* @returns the boolean value of the expression
*/
public boolean evaluate(Context c) {
return c.lookup(name);
}
/**
* Replaces a variable with an expression
*
* @param name the name of the variable
* @param exp the expression to replace the variable
* @returns a copy of this expression with the variable replaced
*/
public BooleanExp replace(String name, BooleanExp exp) {
if (name.equals(this.name)) {
return exp.copy();
} else {
return new VariableExp(this.name);
}
}
/**
* Copies this expression
*
* @returns the copied expression
*/
public BooleanExp copy() {
return new VariableExp(name);
}
}</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>2004-03-24T16:00:00Zfiles.lst2004-03-24T16:00:00Z2004-03-24T16:00:00Z<br/><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>2004-03-24T16:00:00Z