[vassalengine-svn] SF.net SVN: vassalengine: [3761] VASSAL-src/branches/swampwallaby-32/src
Brought to you by:
rodneykinney,
uckelman
From: <swa...@us...> - 2008-06-26 20:47:53
|
Revision: 3761 http://vassalengine.svn.sourceforge.net/vassalengine/?rev=3761&view=rev Author: swampwallaby Date: 2008-06-26 13:47:41 -0700 (Thu, 26 Jun 2008) Log Message: ----------- Calc Property improved version Modified Paths: -------------- VASSAL-src/branches/swampwallaby-32/src/VASSAL/script/CalculatedProperty.java VASSAL-src/branches/swampwallaby-32/src/VASSAL/script/ExpressionConfigurer.java VASSAL-src/branches/swampwallaby-32/src/VASSAL/script/ExpressionInterpreter.java VASSAL-src/branches/swampwallaby-32/src/bsh/ExpressionValidator.java Added Paths: ----------- VASSAL-src/branches/swampwallaby-32/src/VASSAL/script/BeanShell.java VASSAL-src/branches/swampwallaby-32/src/images/init_expression.bsh Removed Paths: ------------- VASSAL-src/branches/swampwallaby-32/src/VASSAL/script/BeanShell.java VASSAL-src/branches/swampwallaby-32/src/VASSAL/script/ExpressionNameSpace.java VASSAL-src/branches/swampwallaby-32/src/images/init.bsh Deleted: VASSAL-src/branches/swampwallaby-32/src/VASSAL/script/BeanShell.java =================================================================== --- VASSAL-src/branches/swampwallaby-32/src/VASSAL/script/BeanShell.java 2008-06-26 06:08:58 UTC (rev 3760) +++ VASSAL-src/branches/swampwallaby-32/src/VASSAL/script/BeanShell.java 2008-06-26 20:47:41 UTC (rev 3761) @@ -1,145 +0,0 @@ -/* - * $Id: BeanShell.java,v 1.1 2006/09/28 04:59:19 swampwallaby Exp $ - * - * Copyright (c) 2008 by Brent Easton - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License (LGPL) as published by the Free Software Foundation. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Library General Public License for more details. - * - * You should have received a copy of the GNU Library General Public - * License along with this library; if not, copies are available - * at http://www.opensource.org. - */ -package VASSAL.script; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.net.URL; -import java.util.HashMap; - -import VASSAL.build.module.properties.PropertySource; -import VASSAL.tools.ErrorLog; -import bsh.EvalError; -import bsh.ExpressionValidator; -import bsh.Interpreter; -import bsh.NameSpace; - -/** - * - * Class encapsulating BeanShell support in Vassal - * - */ -public class BeanShell { - - public static BeanShell instance; - - public static BeanShell getInstance() { - if (instance == null) { - instance = new BeanShell(); - } - return instance; - } - - /* - * Global Vassal NameSpace. Contains utility variables and - * methods available to all expressions. - */ - protected NameSpace globalNameSpace; - - /* - * Collection of Interpreters for evaluating Expressions (Calculated Property - * and Property Match Expressions). All traits with the same expression - * will share the same Interpreter - */ - protected HashMap<String,ExpressionInterpreter> expressionInterpreters; - - /* - * An interpreter for adding script methods to the global NameSpace - */ - protected Interpreter globalInterpreter; - - public BeanShell() { - expressionInterpreters = new HashMap<String,ExpressionInterpreter>(); - - globalNameSpace = new NameSpace((NameSpace) null, "global"); - - globalInterpreter = new Interpreter(); - globalInterpreter.setNameSpace(globalNameSpace); - globalInterpreter.getNameSpace().importClass("VASSAL.build.module.properties.PropertySource"); - globalInterpreter.getNameSpace().importClass("VASSAL.script.ExpressionInterpreter"); - - // Read in the Vassal init script - URL ini = getClass().getResource("/images/init.bsh"); - BufferedReader in = null; - try { - in = new BufferedReader( - new InputStreamReader( - ini.openStream())); - - try { - globalInterpreter.eval(in); - } - catch (EvalError e) { - e.printStackTrace(); - } - - in.close(); - } - catch (IOException e) { - ErrorLog.warn(e); - } - finally { - if (in != null) { - try { - in.close(); - } - catch (IOException e) { - // - } - } - } - } - - /** - * Return the Vassal shared namespace - * - * @return Global NameSpace - */ - public NameSpace getGlobalNameSpace() { - return globalNameSpace; - } - - /** - * Parse and validate a single expression or script. No evaluation or checking - * for undefined variables - * - * @param expression Expression to validate - */ - public static boolean validateExpression(String expression) { - return new ExpressionValidator(expression).isValid(); - } - - /** - * Evaluate an Expression. All identical expressions share the - * same Interpreter. - * - * @param expression Expression to evaluate - * @param piece GamePiece to use to find property values - * @return evaluated expression - */ - public String evaluateExpression(String expression, PropertySource piece) { - ExpressionInterpreter interpreter = expressionInterpreters.get(expression); - if (interpreter == null) { - interpreter = new ExpressionInterpreter(expression, piece); - expressionInterpreters.put(expression, interpreter); - } - return interpreter.evaluate(piece); - } -} \ No newline at end of file Added: VASSAL-src/branches/swampwallaby-32/src/VASSAL/script/BeanShell.java =================================================================== --- VASSAL-src/branches/swampwallaby-32/src/VASSAL/script/BeanShell.java (rev 0) +++ VASSAL-src/branches/swampwallaby-32/src/VASSAL/script/BeanShell.java 2008-06-26 20:47:41 UTC (rev 3761) @@ -0,0 +1,145 @@ +/* + * $Id: BeanShell.java,v 1.1 2006/09/28 04:59:19 swampwallaby Exp $ + * + * Copyright (c) 2008 by Brent Easton + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License (LGPL) as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, copies are available + * at http://www.opensource.org. + */ +package VASSAL.script; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.URL; +import java.util.HashMap; + +import VASSAL.build.module.properties.PropertySource; +import VASSAL.tools.ErrorLog; +import bsh.EvalError; +import bsh.ExpressionValidator; +import bsh.Interpreter; +import bsh.NameSpace; + +/** + * + * Class encapsulating BeanShell support in Vassal + * + */ +public class BeanShell { + + public static BeanShell instance; + + public static BeanShell getInstance() { + if (instance == null) { + instance = new BeanShell(); + } + return instance; + } + + /* + * Global Vassal NameSpace. Contains utility variables and + * methods available to all expressions. + */ + protected NameSpace globalNameSpace; + + /* + * Collection of Interpreters for evaluating Expressions (Calculated Property + * and Property Match Expressions). All traits with the same expression + * will share the same Interpreter + */ + protected HashMap<String,ExpressionInterpreter> expressionInterpreters; + + /* + * An interpreter for adding script methods to the global NameSpace + */ + protected Interpreter globalInterpreter; + + public BeanShell() { + expressionInterpreters = new HashMap<String,ExpressionInterpreter>(); + + globalNameSpace = new NameSpace((NameSpace) null, "global"); + + globalInterpreter = new Interpreter(); + globalInterpreter.setNameSpace(globalNameSpace); + globalInterpreter.getNameSpace().importClass("VASSAL.build.module.properties.PropertySource"); + globalInterpreter.getNameSpace().importClass("VASSAL.script.ExpressionInterpreter"); + + // Read in the Vassal init script + URL ini = getClass().getResource("/images/init.bsh"); + BufferedReader in = null; + try { + in = new BufferedReader( + new InputStreamReader( + ini.openStream())); + + try { + globalInterpreter.eval(in); + } + catch (EvalError e) { + e.printStackTrace(); + } + + in.close(); + } + catch (IOException e) { + ErrorLog.warn(e); + } + finally { + if (in != null) { + try { + in.close(); + } + catch (IOException e) { + // + } + } + } + } + + /** + * Return the Vassal shared namespace + * + * @return Global NameSpace + */ + public NameSpace getGlobalNameSpace() { + return globalNameSpace; + } + + /** + * Parse and validate a single expression or script. No evaluation or checking + * for undefined variables + * + * @param expression Expression to validate + */ + public static boolean validateExpression(String expression) { + return new ExpressionValidator(expression).isValid(); + } + + /** + * Evaluate an Expression. All identical expressions share the + * same Interpreter. + * + * @param expression Expression to evaluate + * @param piece GamePiece to use to find property values + * @return evaluated expression + */ + public String evaluateExpression(String expression, PropertySource piece) { + ExpressionInterpreter interpreter = expressionInterpreters.get(expression); + if (interpreter == null) { + interpreter = new ExpressionInterpreter(expression, piece); + expressionInterpreters.put(expression, interpreter); + } + return interpreter.evaluate(piece); + } +} \ No newline at end of file Modified: VASSAL-src/branches/swampwallaby-32/src/VASSAL/script/CalculatedProperty.java =================================================================== --- VASSAL-src/branches/swampwallaby-32/src/VASSAL/script/CalculatedProperty.java 2008-06-26 06:08:58 UTC (rev 3760) +++ VASSAL-src/branches/swampwallaby-32/src/VASSAL/script/CalculatedProperty.java 2008-06-26 20:47:41 UTC (rev 3761) @@ -51,7 +51,7 @@ protected static int counter = 0; protected String name = ""; - protected String expression = ""; + protected ExpressionInterpreter interpreter; public CalculatedProperty() { this(ID, null); @@ -85,7 +85,7 @@ public String myGetType() { SequenceEncoder se = new SequenceEncoder(';'); se.append(name) - .append(expression); + .append(getExpression()); return ID + se.getValue(); } @@ -116,8 +116,12 @@ SequenceEncoder.Decoder st = new SequenceEncoder.Decoder(type, ';'); st.nextToken(); name = st.nextToken(""); - expression = st.nextToken(""); + interpreter = new ExpressionInterpreter(st.nextToken("")); } + + protected String getExpression() { + return interpreter.getExpression(); + } /** * Return the value of this trait's property. @@ -158,8 +162,7 @@ } protected String evaluate() { - String result = BeanShell.getInstance().evaluateExpression(expression, Decorator.getOutermost(this)); - return result; + return interpreter.evaluate(Decorator.getOutermost(this)); } public PieceEditor getEditor() { @@ -197,7 +200,7 @@ nameConfig = new StringConfigurer(null, "Property Name: ", piece.name); box.add(nameConfig.getControls()); - expressionConfig = new ExpressionConfigurer(null, "Expression: ", piece.expression); + expressionConfig = new ExpressionConfigurer(null, "Expression: ", piece.getExpression()); box.add(expressionConfig.getControls()); } Modified: VASSAL-src/branches/swampwallaby-32/src/VASSAL/script/ExpressionConfigurer.java =================================================================== --- VASSAL-src/branches/swampwallaby-32/src/VASSAL/script/ExpressionConfigurer.java 2008-06-26 06:08:58 UTC (rev 3760) +++ VASSAL-src/branches/swampwallaby-32/src/VASSAL/script/ExpressionConfigurer.java 2008-06-26 20:47:41 UTC (rev 3761) @@ -27,6 +27,8 @@ import javax.swing.JTextField; import javax.swing.SwingUtilities; +import bsh.ExpressionValidator; + import VASSAL.configure.Configurer; /** @@ -151,7 +153,7 @@ if (getValueString().length() == 0) { validator.setStatus(UNKNOWN); } - else if (BeanShell.validateExpression(getValueString())) { + else if ( new ExpressionValidator(getValueString()).isValid()) { validator.setStatus(VALID); } else { Modified: VASSAL-src/branches/swampwallaby-32/src/VASSAL/script/ExpressionInterpreter.java =================================================================== --- VASSAL-src/branches/swampwallaby-32/src/VASSAL/script/ExpressionInterpreter.java 2008-06-26 06:08:58 UTC (rev 3760) +++ VASSAL-src/branches/swampwallaby-32/src/VASSAL/script/ExpressionInterpreter.java 2008-06-26 20:47:41 UTC (rev 3761) @@ -18,6 +18,10 @@ */ package VASSAL.script; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.URL; import java.util.ArrayList; import java.util.List; @@ -38,74 +42,130 @@ * A BeanShell Interpreter customised to evaluate a single Vassal * expression containing Vassal property references. * All traits with the same expression will share the same Interpreter + * + * Each ExpressionInterpreter has 3 levels of NameSpace: + * 1. Top level is a single global NameSpace that contains utility methods + * available to all ExpressionInterpreters. It is the parent of all + * level 2 NameSpaces. + * 2. Level 2 is a NameSpace for each unique expression that contains the + * parsed expression. All expressions in all traits that are the same + * will use the one Expression NameSpace. Is is the parent of all + * Level 3 NameSpaces that use that expression. + * 3. Level 3 is a seperate NameSpace for each ExpressionInterpeter that + * contains Vassal property values and link data unique to the instance + * (i.e. one for each CalculatedProperty) * */ public class ExpressionInterpreter extends Interpreter { private static final long serialVersionUID = 1L; + + protected static final String INIT_SCRIPT = "/images/init_expression.bsh"; protected static final String THIS = "_interp"; protected static final String SOURCE = "_source"; + + // Top-level static NameSpace shared between all ExpressionInterpreters + // Loaded with utility methods available to all interpreters + protected static NameSpace topLevelNameSpace; - // A local NameSpace specific to this Interpreter. Its parent is the - // BeanShell.globalNameSpace. - protected NameSpace nameSpace; + protected NameSpace expressionNameSpace; - // The list of Vassal properties references by this Expression - protected List<String> undefinedVariables = new ArrayList<String>(); - - // The Expression itself + protected NameSpace localNameSpace; + protected String expression; + protected PropertySource source; + protected List<String> variables = new ArrayList<String>(); - // A PropertySource used to evaluate property values - protected PropertySource source; - - public ExpressionInterpreter(String expression, PropertySource piece) { + public ExpressionInterpreter(String expr) { super(); - // Use a custom NameSpace that handles undefined variables. These will - // be bound to Vassal property values. Ignore the temporary variables we - // use to evaluate expressions and the reference to this Interpreter - nameSpace = new NameSpace(BeanShell.getInstance().getGlobalNameSpace(), "expression"); - setNameSpace(nameSpace); - // Install the Vassal Class loader so that bsh can find Vassal classes this.setClassLoader(this.getClass().getClassLoader()); - // Insert a link to this class into the new shell - setVar(THIS, this); - setVar(SOURCE, piece); + if (topLevelNameSpace == null) { + initialiseStatic(); + } - // Initialise the expression to be evaluated - setExpression(expression); - } - - /** - * Convert the expression to a method so that it only needs to be - * parsed once. Record any undefined variables. These will need to - * be bound to Vassal property values when evaluating. - * - * @param s Expression - */ - public void setExpression(String s) { - expression = s; + // Find an existing expression reference for this expression, or + // create one if needed. + expression = (expr == null || expr.length() == 0) ? "\"\"" : expr; - // Validate the expression and get a list of undefined variables that - // will need to be bound to Vassal property values - ExpressionValidator v = new ExpressionValidator(expression); - undefinedVariables = v.getVariables(); + expressionNameSpace = new NameSpace(topLevelNameSpace, "expression"); + variables = new ExpressionValidator(expression).getVariables(); // Build a method enclosing the expression. This saves the results // of the expression parsing, improving performance. Force return // value to a String as this is what Vassal is expecting. + setNameSpace(expressionNameSpace); try { eval("String _plugh() { _plover=" + expression + "; return _plover.toString();}"); } catch (EvalError e) { ErrorLog.warn(e); } + + // Add a link to this Interpreter into the new NameSpace for callbacks from + // BeanShell back to us + setVar(THIS, this); + + // Temporary NameSpace + localNameSpace = new NameSpace(expressionNameSpace, "local"); + } /** + * Initialise the static elements of this class. Create a Top Level + * NameSpace using the Vassal class loader, load useful classes and + * read and process the init_expression.bsh file to load scripted + * methods available to expressions. + */ + protected void initialiseStatic() { + topLevelNameSpace = new NameSpace((NameSpace) null, getClassManager(), "topLevel"); + setNameSpace(topLevelNameSpace); + getNameSpace().importClass("VASSAL.build.module.properties.PropertySource"); + getNameSpace().importClass("VASSAL.script.ExpressionInterpreter"); + + // Read the Expression initialisation script into the top level namespace + URL ini = getClass().getResource(INIT_SCRIPT); + BufferedReader in = null; + try { + in = new BufferedReader( + new InputStreamReader( + ini.openStream())); + + try { + eval(in); + } + catch (EvalError e) { + ErrorLog.warn(e); + } + + in.close(); + } + catch (IOException e) { + ErrorLog.warn(e); + } + finally { + if (in != null) { + try { + in.close(); + } + catch (IOException e) { + // + } + } + } + } + + /** + * Return the current expression + * @return expression + */ + public String getExpression() { + return expression; + } + + /** * Evaluate the expression, setting the value of any undefined * values to the matching Vassal property value. Primitives must * be wrapped. @@ -118,9 +178,11 @@ // GamePiece supplied. source = ps == null ? GameModule.getGameModule() : ps; + setNameSpace(expressionNameSpace); + // Bind each undeclared variable with the value of the // corresponding Vassal property. Allow for old-style $variable$ references - for (String var : undefinedVariables) { + for (String var : variables) { String name = var; if (name.length() > 2 && name.startsWith("$") && name.endsWith("$")) { name = name.substring(1, name.length()-1); @@ -148,10 +210,11 @@ // Re-evaluate the pre-parsed expression now that the undefined variables have // been bound to their Vassal property values. + + setVar(SOURCE, source); + setNameSpace(localNameSpace); + String result = ""; - setVar(SOURCE, source); - source = ps; - try { eval("_xyzzy=_plugh()"); result = get("_xyzzy").toString(); @@ -163,6 +226,10 @@ return result; } + public String evaluate() { + return evaluate(GameModule.getGameModule()); + } + protected Object wrap (String value) { if (value == null) { return ""; @@ -226,7 +293,7 @@ /***************************************************************** * Callbacks from BeanShell to Vassal - *****************************************************************/ + **/ public Object getProperty(String name) { return wrap(source.getProperty(name).toString()); Deleted: VASSAL-src/branches/swampwallaby-32/src/VASSAL/script/ExpressionNameSpace.java =================================================================== --- VASSAL-src/branches/swampwallaby-32/src/VASSAL/script/ExpressionNameSpace.java 2008-06-26 06:08:58 UTC (rev 3760) +++ VASSAL-src/branches/swampwallaby-32/src/VASSAL/script/ExpressionNameSpace.java 2008-06-26 20:47:41 UTC (rev 3761) @@ -1,104 +0,0 @@ -/* - * $Id: ExpressionNameSpace.java,v 1.3 2006/09/29 06:48:21 swampwallaby Exp $ - * - * Copyright (c) 2008 by Brent Easton - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License (LGPL) as published by the Free Software Foundation. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Library General Public License for more details. - * - * You should have received a copy of the GNU Library General Public - * License along with this library; if not, copies are available - * at http://www.opensource.org. - */ -package VASSAL.script; - -import java.util.ArrayList; -import java.util.List; - -import VASSAL.tools.ErrorLog; -import bsh.EvalError; -import bsh.NameSpace; -import bsh.UtilEvalError; -import bsh.Variable; - -/** - * Custom BeanShell NameSpace class that records and returns the names - * of undefined variables - * - */ -@Deprecated -public class ExpressionNameSpace extends NameSpace { - - private static final long serialVersionUID = 1L; - private List<String> undefinedVariables = new ArrayList<String>(); - private ExpressionInterpreter interpreter; - - /** - * Create a new NameSpace for this expression with the Vassal - * Global NameSpace as the parent - */ - public ExpressionNameSpace(ExpressionInterpreter i) { - super(BeanShell.getInstance().getGlobalNameSpace(), "expression"); - interpreter = i; - } - - /** - * Return list of undefined variables. Remove from list any variables - * that may have been bound since the list was originally generated. - * - * @return undefined variable list - */ - public List<String> getUndefinedVariables() { - return undefinedVariables; - } - - /** - * Custom implementation of getVariableImpl. - * Build a list of undefined variables. These will be Vassal property - * names and will be bound to real values prior to final evaluation - */ - protected static String updating = ""; - protected Variable getVariableImpl(java.lang.String name, boolean recurse) { - - Variable o = null; - try { - o = super.getVariableImpl(name, recurse); - } - catch (UtilEvalError e) { - ErrorLog.warn(e); - } - - if (updating.equals(name)) { - return o; - } - - if (o == null) { - if (!name.startsWith("_")) { - try { - // Variable is undefined, but we need to create the variable - // or the evaluation will fail. Interpreter.set() causes - // another call to this routine, so set updating to prevent - // an infinite loop. There has to be a better way to do this! - updating = name; - interpreter.set(name, 1); - undefinedVariables.add(name); - } - catch (EvalError e) { - e.printStackTrace(); - } - finally { - updating = ""; - } - } - } - - return o; - } - -} \ No newline at end of file Modified: VASSAL-src/branches/swampwallaby-32/src/bsh/ExpressionValidator.java =================================================================== --- VASSAL-src/branches/swampwallaby-32/src/bsh/ExpressionValidator.java 2008-06-26 06:08:58 UTC (rev 3760) +++ VASSAL-src/branches/swampwallaby-32/src/bsh/ExpressionValidator.java 2008-06-26 20:47:41 UTC (rev 3761) @@ -94,6 +94,10 @@ * @param node Parser Node */ public void processNode (SimpleNode node) { + if (node == null) { + return; + } + if (node instanceof BSHAmbiguousName) { String name = ((BSHAmbiguousName) node).getText().trim(); if (! (node.parent instanceof BSHMethodInvocation) && ! variables.contains(name)) { Deleted: VASSAL-src/branches/swampwallaby-32/src/images/init.bsh =================================================================== --- VASSAL-src/branches/swampwallaby-32/src/images/init.bsh 2008-06-26 06:08:58 UTC (rev 3760) +++ VASSAL-src/branches/swampwallaby-32/src/images/init.bsh 2008-06-26 20:47:41 UTC (rev 3761) @@ -1,100 +0,0 @@ -/* - * $Id: init.bsh,v 1.1 2006/09/28 04:59:19 swampwallaby Exp $ - * - * Copyright (c) 2008 by Brent Easton - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License (LGPL) as published by the Free Software Foundation. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Library General Public License for more details. - * - * You should have received a copy of the GNU Library General Public - * License along with this library; if not, copies are available - * at http://www.opensource.org. - */ - -/** - * Initialisation file for Vassal BeanShell Expression interpreters. - * Contains utility variables and methods available to all - * bsh interpreters - * - * To access variables inside a method that have been set in the calling - * NameSpace, (i.e. using ExpressionInterpreter.set(name, value), use: - * - * variable = this.caller.namespace.getVariable("variableName"); - * - * Currently, the following variables are set in the ExpressionInterpreter - * prior to evaluation: - * - * _interp The ExpressionInterpreter initiating the evaluation - * _source A PropertySource - */ - -/** - * Additional useful imports. The following are already imported - * by default: - * - * javax.swing.event - * javax.swing - * java.awt.event - * java.awt - * java.net - * java.util - * java.io - * java.lang - */ - -/** - * If function for Expressions. Note the capitalization to distinguish - * it from the if java keyword. - * - * Example: If(range < 5, 5, range+2) - */ -If(boolean exp, Object o1, Object o2) { - if (exp) - return o1; - else - return o2; -}; - -/** - * Callback to Vassal to sum the value of a property in all - * counters in the same stack as the target property - * - * Example: SumStack("Strength") - */ -SumStack(String property) { - _interp = this.caller.namespace.getVariable("_interp"); - _source = this.caller.namespace.getVariable("_source"); - return unwrap(_interp.sumStack(property, _source)); -} - -/** - * Callback to Vassal to sum the value of a property in all - * counters in the same location as the target property - * WARNING - This could be slow on maps with many counters - * - * Example: SumLocation("Strength") - */ -SumLocation(String property) { - _interp = this.caller.namespace.getVariable("_interp"); - _source = this.caller.namespace.getVariable("_source"); - return unwrap(_interp.sumLocation(property, _source)); -} - -/** - * Unwrap Integer and Boolean values - */ -unwrap(Object value) { - if (value instanceof Integer) { - return ((Integer) value).intValue(); - } - else if (value instanceof Boolean) { - return ((Boolean) value).booleanValue(); - } - return value; -} \ No newline at end of file Copied: VASSAL-src/branches/swampwallaby-32/src/images/init_expression.bsh (from rev 3760, VASSAL-src/branches/swampwallaby-32/src/images/init.bsh) =================================================================== --- VASSAL-src/branches/swampwallaby-32/src/images/init_expression.bsh (rev 0) +++ VASSAL-src/branches/swampwallaby-32/src/images/init_expression.bsh 2008-06-26 20:47:41 UTC (rev 3761) @@ -0,0 +1,107 @@ +/* + * $Id: init_expression.bsh,v 1.1 2006/09/28 04:59:19 swampwallaby Exp $ + * + * Copyright (c) 2008 by Brent Easton + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License (LGPL) as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, copies are available + * at http://www.opensource.org. + */ + +/** + * Initialisation file for Vassal BeanShell Expression interpreters + * top level NameSpace. + * + * To access variables inside a method that have been set in the calling + * NameSpace, (i.e. using ExpressionInterpreter.set(name, value), use: + * + * variable = this.caller.namespace.getVariable("variableName"); + * + * Currently, the following variables are set in the ExpressionInterpreter + * prior to evaluation: + * + * _interp The ExpressionInterpreter initiating the evaluation + * _source A PropertySource + */ + +/** + * Additional useful imports. The following are already imported + * by default: + * + * javax.swing.event + * javax.swing + * java.awt.event + * java.awt + * java.net + * java.util + * java.io + * java.lang + */ + +/** + * If function for Expressions. Note the capitalization to distinguish + * it from the if java keyword. + * + * Example: If(range < 5, 5, range+2) + */ +If(boolean exp, Object o1, Object o2) { + if (exp) + return o1; + else + return o2; +}; + +/** + * Callback to Vassal to sum the value of a property in all + * counters in the same stack as the target property + * + * Example: SumStack("Strength") + */ +SumStack(String property) { + _interp = this.caller.namespace.getVariable("_interp"); + _source = this.caller.namespace.getVariable("_source"); + return unwrap(_interp.sumStack(property, _source)); +} + +/** + * Callback to Vassal to sum the value of a property in all + * counters in the same location as the target property + * WARNING - This could be slow on maps with many counters + * + * Example: SumLocation("Strength") + */ +SumLocation(String property) { + _interp = this.caller.namespace.getVariable("_interp"); + _source = this.caller.namespace.getVariable("_source"); + return unwrap(_interp.sumLocation(property, _source)); +} + +/** + * Callback to Vassal to display text in a dialog box + */ +Alert(String text) { + interp = this.caller.namespace.getVariable("_interp"); + interp.alert(text); +} + +/** + * Unwrap Integer and Boolean values + */ +unwrap(Object value) { + if (value instanceof Integer) { + return ((Integer) value).intValue(); + } + else if (value instanceof Boolean) { + return ((Boolean) value).booleanValue(); + } + return value; +} \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |