carfield.com.hk AndExp.java 2004-03-24T16:00:00Z 2004-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 &quot;License&quot;); 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 &quot;AS IS&quot; 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 &lt;i&gt;Context&lt;/i&gt; * * @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) &amp;&amp; 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:00Z BooleanConstant.java 2004-03-24T16:00:00Z 2004-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 &quot;License&quot;); 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 &quot;AS IS&quot; 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 &lt;i&gt;Context&lt;/i&gt;. * * @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:00Z BooleanExp.java 2004-03-24T16:00:00Z 2004-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 &quot;License&quot;); 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 &quot;AS IS&quot; 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 &lt;i&gt;AbstractExpression&lt;/i&gt; 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 &lt;i&gt;Context&lt;/i&gt; * * @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:00Z Context.java 2004-03-24T16:00:00Z 2004-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 &quot;License&quot;); 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 &quot;AS IS&quot; 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 &lt;i&gt;Context&lt;/i&gt; for the interpretation of boolean * expressions&lt;p&gt; * * @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(&quot;No variable \&quot;&quot;+name+&quot;\&quot; declared.&quot;); } return value.booleanValue(); } /** * Assigns a boolean value to a &lt;code&gt;VariableExp&lt;/code&gt; * * @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:00Z ExpressionException.java 2004-03-24T16:00:00Z 2004-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 &quot;License&quot;); 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 &quot;AS IS&quot; 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:00Z Main.java 2004-03-24T16:00:00Z 2004-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 &quot;License&quot;); 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 &quot;AS IS&quot; 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.&lt;p&gt; * * Intent: &lt;i&gt;Given a language, defeine a representation fro its grammar along * with an interpreter that uses the representation to interpret sentences * in the language.&lt;/i&gt;&lt;p&gt; * * Participating objects are &lt;code&gt;BooleanContant&lt;/code&gt;, &lt;code&gt;VariableExp * &lt;/code&gt;, &lt;code&gt;OrExp&lt;/code&gt;, &lt;code&gt;AndExp&lt;/code&gt;, and &lt;code&gt;NotExp&lt;/code&gt; * as &lt;i&gt;Expressions&lt;/i&gt;. The &lt;i&gt;AbstractExpression&lt;/i&gt; interface is defined * in &lt;code&gt;BooelanExp&lt;/i&gt;.&lt;p&gt; * * This example implements an interpreter for a language of boolean * expressions. As a sample expression, &quot;((true &amp; x) | (y &amp; !x))&quot; 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. * * &lt;p&gt;&lt;i&gt;This is the Java version.&lt;/i&gt;&lt;p&gt; * * @author Jan Hannemann * @author Gregor Kiczales * @version 1.0, 05/13/02 * * @see BooleanExp */ public class Main { /** * Assigns boolean values to two &lt;code&gt;VariableExp&lt;/code&gt;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(&quot;The result for (x=&quot;+xValue+&quot;, y=&quot;+yValue+&quot;) is: &quot;+result); } /** * Implements the driver for the Intepreter design pattern example.&lt;p&gt; * * This example implements an interpreter for a language of boolean * expressions. As a sample expression, &quot;((true &amp; x) | (y &amp; !x))&quot; 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(&quot;X&quot;); VariableExp y = new VariableExp(&quot;Y&quot;); exp = new OrExp(new AndExp(new BooleanConstant(true), x), new AndExp(y, new NotExp(x))); System.out.println(&quot;Testing Expr: ((true &amp; x) | (y &amp; !x))&quot;); 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(&quot;Z&quot;); NotExp notZ = new NotExp(z); BooleanExp replacement = exp.replace(&quot;Y&quot;, notZ); context.assign(z, false); boolean result = replacement.evaluate(context); System.out.println(&quot;The result for the replacement is: &quot;+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:00Z NotExp.java 2004-03-24T16:00:00Z 2004-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 &quot;License&quot;); 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 &quot;AS IS&quot; 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 &lt;i&gt;Context&lt;/i&gt; * * @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:00Z OrExp.java 2004-03-24T16:00:00Z 2004-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 &quot;License&quot;); 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 &quot;AS IS&quot; 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 &lt;i&gt;Context&lt;/i&gt; * * @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:00Z VariableExp.java 2004-03-24T16:00:00Z 2004-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 &quot;License&quot;); 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 &quot;AS IS&quot; 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 &lt;i&gt;Context&lt;/i&gt; * * @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:00Z files.lst 2004-03-24T16:00:00Z 2004-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