/** * Parses the expression given in the code string. * Names in the code string are resolved using the given namespace. * * @param code the code string, for the syntax of valid expressions refer * to the class description * @param namespace the environment which is used to resolve names * @throws ParseException if a parse error occurs */ public final Term parse(final String code, final Namespace namespace) throws ParseException { if (code == null) { throw new IllegalArgumentException("code is null"); } Namespace defaultNamespace = this.defaultNamespace; if (namespace != null && namespace != defaultNamespace) { this.defaultNamespace = new NamespaceImpl(namespace); } tokenizer = new Tokenizer(code); Term term = parseImpl(); tokenizer = null; this.defaultNamespace = defaultNamespace; return term; }
private String[] getFunctionTemplates() { final Namespace defaultNamespace = parser.getDefaultNamespace(); // collect names String[] functionNames; if (defaultNamespace instanceof NamespaceImpl) { final NamespaceImpl namespace = (NamespaceImpl) defaultNamespace; final Function[] functions = namespace.getAllFunctions(); functionNames = new String[functions.length]; for (int i = 0; i < functions.length; i++) { functionNames[i] = createFunctionTemplate(functions[i]); } } else { functionNames = FUNCTION_NAMES; } // remove double values Set<String> set = new HashSet<String>(); Collections.addAll(set, functionNames); functionNames = set.toArray(new String[set.size()]); Arrays.sort(functionNames); return functionNames; }
public final void deregisterFunction(final Function function) { Function[] array = getFunctions(function.getName()); if (array != null) { final ArrayList<Function> functionList = new ArrayList<Function>(Arrays.asList(array)); if (!functionList.remove(function)) { return; } array = functionList.toArray(new Function[functionList.size()]); functions.put(function.getName(), array); } }
@Test public void testPow() throws ParseException { NamespaceImpl namespace = new NamespaceImpl(new DefaultNamespace()); namespace.registerSymbol(new AbstractSymbol.I("ix") { @Override public int evalI(EvalEnv env) throws EvalException { return ix; } }); namespace.registerSymbol(new AbstractSymbol.D("dx") { @Override public double evalD(EvalEnv env) throws EvalException { return dx; } }); ParserImpl parser = new ParserImpl(namespace); ix = 1; dx = 1.0; assertEquals(10.0, parser.parse("pow(10, ix)").evalD(null), 1E-10); assertEquals(10.0, parser.parse("pow(10, ix)").evalD(null), 1E-10); assertEquals(10.0, parser.parse("pow(10, dx)").evalD(null), 1E-10); assertEquals(10.0, parser.parse("pow(10, dx)").evalD(null), 1E-10); ix = 2; dx = 2.0; assertEquals(100.0, parser.parse("pow(10.0, ix)").evalD(null), 1E-10); assertEquals(100.0, parser.parse("pow(10.0, ix)").evalD(null), 1E-10); assertEquals(100.0, parser.parse("pow(10.0, dx)").evalD(null), 1E-10); assertEquals(100.0, parser.parse("pow(10.0, dx)").evalD(null), 1E-10); }
/** * Creates a default namespace for the product(s) given in an array. The resulting namespace contains symbols for * all tie-point grids, bands and single flag values. if the array contains more then one product, the symbol's name * will have a prefix according to each product's reference number. * * @param products the array of input products * @param defaultProductIndex the index of the product for which also symbols without the * product prefix <code>$<i>ref-no</i></code> are registered in the namespace * @param prefixProvider a product prefix provider * @return a default namespace, never <code>null</code> */ public static WritableNamespace createDefaultNamespace(Product[] products, int defaultProductIndex, ProductPrefixProvider prefixProvider) { Guardian.assertNotNullOrEmpty("products", products); Guardian.assertWithinRange("defaultProductIndex", defaultProductIndex, 0, products.length); WritableNamespace namespace = new NamespaceImpl(DEFAULT_NAMESPACE); // Register symbols for default product without name prefix registerProductSymbols(namespace, products[defaultProductIndex], ""); // Register symbols for multiple products using a name prefix if (products.length > 1) { for (Product product : products) { registerProductSymbols(namespace, product, prefixProvider.getPrefix(product)); } } namespace.registerSymbol(PIXEL_X_SYMBOL); namespace.registerSymbol(PIXEL_Y_SYMBOL); return namespace; }
public final void registerFunction(final Function function) { Function[] array = getFunctions(function.getName()); if (array != null) { for (Function anArray : array) { if (anArray == function) { return; } } Function[] arrayNew = new Function[array.length + 1]; System.arraycopy(array, 0, arrayNew, 0, array.length); arrayNew[array.length] = function; array = arrayNew; } else { array = new Function[]{function}; } functions.put(function.getName(), array); }
public final Function resolveFunction(final String name, final Term[] args) { final Function[] functions = getFunctions(name); if (functions != null) { Function bestFunction = null;