From: <nik...@us...> - 2015-04-30 14:01:40
|
Revision: 2263 http://sourceforge.net/p/jsbml/code/2263 Author: niko-rodrigue Date: 2015-04-30 14:01:37 +0000 (Thu, 30 Apr 2015) Log Message: ----------- working on being able to buid a jar file that would use the old ASTNode class Modified Paths: -------------- branches/astnode2-merging-alternate/build.xml branches/astnode2-merging-alternate/core/build.xml branches/astnode2-merging-alternate/core/src/org/sbml/jsbml/AbstractTreeNode.java Added Paths: ----------- branches/astnode2-merging-alternate/core/src/org/sbml/jsbml/ASTNode.java.oldASTNode Removed Paths: ------------- branches/astnode2-merging-alternate/core/src/org/sbml/jsbml/ASTNode.java.mine branches/astnode2-merging-alternate/core/src/org/sbml/jsbml/ASTNode.java.r2147 branches/astnode2-merging-alternate/core/src/org/sbml/jsbml/ASTNode.java.r2184 branches/astnode2-merging-alternate/test/org/sbml/jsbml/test/AllTests.java.mine branches/astnode2-merging-alternate/test/org/sbml/jsbml/test/AllTests.java.r2147 branches/astnode2-merging-alternate/test/org/sbml/jsbml/test/AllTests.java.r2184 Modified: branches/astnode2-merging-alternate/build.xml =================================================================== --- branches/astnode2-merging-alternate/build.xml 2015-04-25 08:58:06 UTC (rev 2262) +++ branches/astnode2-merging-alternate/build.xml 2015-04-30 14:01:37 UTC (rev 2263) @@ -176,9 +176,12 @@ <property name="prepare.src.target.done" value="true"/> + <echo message="Value of the use-old-astnode property = '${use.old.astnodes}'"/> + <ant dir="${core.base.dir}" inheritAll="false" inheritRefs="false"> - <target name="clean" /> - <target name="jar" /> + <property name="use.old.astnodes" value="${use.old.astnodes}"/> + <target name="clean" /> + <target name="jar" /> </ant> <copy todir="${build.src}"> Modified: branches/astnode2-merging-alternate/core/build.xml =================================================================== --- branches/astnode2-merging-alternate/core/build.xml 2015-04-25 08:58:06 UTC (rev 2262) +++ branches/astnode2-merging-alternate/core/build.xml 2015-04-30 14:01:37 UTC (rev 2263) @@ -186,10 +186,28 @@ excludes="**/.svn*, **/*~, **/*.tgz, **/*.zip"> </fileset> </copy> + + <echo message="Value of the use-old-astnode property in core = '${use.old.astnodes}'"/> + <antcall target="prepare-old-astnode" inheritrefs="true" inheritall="true"/> </target> + <!-- =================================================================== --> + <!-- Prepares the source code to use the old ASTNode class --> + <!-- =================================================================== --> + <target + name="prepare-old-astnode" + description="Prepares the source code to use the old ASTNode class" + if="${use.old.astnodes}" + > + <echo message="Building JSBML core using the old ASTNode class!"/> + <copy file="${build.src}/org/sbml/jsbml/ASTNode.java.oldASTNode" tofile="${build.src}/org/sbml/jsbml/ASTNode.java.newASTNode" overwrite="yes"/> + <copy file="${build.src}/org/sbml/jsbml/ASTNode.java.oldASTNode" tofile="${build.src}/org/sbml/jsbml/ASTNode.java" overwrite="yes"/> + <property name="version" value="${version}-oldASTNode"/> + + </target> + <!-- =================================================================== --> <!-- Modify some files --> <!-- =================================================================== --> @@ -283,6 +301,9 @@ </fileset> </copy> <jar jarfile="${build.dir}/${name}-${version}.jar" basedir="${build.dest}" manifest="${build.dest}/META-INF/MANIFEST.MF"/> + + <echo message="Value of the use-old-astnode property in core = '${use.old.astnodes}'"/> + <antcall target="revert-old-astnode" inheritrefs="true" inheritall="true"/> </target> <!-- =================================================================== --> @@ -351,7 +372,21 @@ </jar> </target> + <!-- =================================================================== --> + <!-- Revert the source code to use the new ASTNode class --> + <!-- =================================================================== --> + <target + name="revert-old-astnode" + description="Prepares the source code to use the new ASTNode class" + if="${use.old.astnodes}" + > + <echo message="Reverting JSBML core using the new ASTNode class!"/> + <copy file="${build.src}/org/sbml/jsbml/ASTNode.java.newASTNode" tofile="${build.src}/org/sbml/jsbml/ASTNode.java" overwrite="yes"/> + <property name="version" value="${version}-oldASTNode"/> + + </target> + <!-- =================================================================== --> <!-- Test the jsbml library --> <!-- =================================================================== --> Deleted: branches/astnode2-merging-alternate/core/src/org/sbml/jsbml/ASTNode.java.mine =================================================================== --- branches/astnode2-merging-alternate/core/src/org/sbml/jsbml/ASTNode.java.mine 2015-04-25 08:58:06 UTC (rev 2262) +++ branches/astnode2-merging-alternate/core/src/org/sbml/jsbml/ASTNode.java.mine 2015-04-30 14:01:37 UTC (rev 2263) @@ -1,4315 +0,0 @@ -/* - * $Id: ASTNode.java 2157 2015-03-25 14:00:27Z niko-rodrigue $ - * $URL: svn://svn.code.sf.net/p/jsbml/code/trunk/core/src/org/sbml/jsbml/ASTNode.java $ - * ---------------------------------------------------------------------------- - * This file is part of JSBML. Please visit <http://sbml.org/Software/JSBML> - * for the latest version of JSBML and more information about SBML. - * - * Copyright (C) 2009-2014 jointly by the following organizations: - * 1. The University of Tuebingen, Germany - * 2. EMBL European Bioinformatics Institute (EBML-EBI), Hinxton, UK - * 3. The California Institute of Technology, Pasadena, CA, USA - * 4. The University of California, San Diego, La Jolla, CA, USA - * 5. The Babraham Institute, Cambridge, UK - * - * This library is free software; you can redistribute it and/or modify it - * under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation. A copy of the license agreement is provided - * in the file named "LICENSE.txt" included with this software distribution - * and also available online as <http://sbml.org/Software/JSBML/License>. - * ---------------------------------------------------------------------------- - */ -package org.sbml.jsbml; - -import java.io.StringReader; -import java.text.MessageFormat; -import java.util.ArrayList; -import java.util.List; -import java.util.Set; - -import javax.swing.tree.TreeNode; -import javax.xml.stream.XMLStreamException; - -import org.apache.log4j.Logger; -import org.sbml.jsbml.Unit.Kind; -import org.sbml.jsbml.math.ASTArithmeticOperatorNode; -import org.sbml.jsbml.math.ASTBinaryFunctionNode; -import org.sbml.jsbml.math.ASTBoolean; -import org.sbml.jsbml.math.ASTCSymbolAvogadroNode; -import org.sbml.jsbml.math.ASTCSymbolBaseNode; -import org.sbml.jsbml.math.ASTCSymbolDelayNode; -import org.sbml.jsbml.math.ASTCSymbolNode; -import org.sbml.jsbml.math.ASTCSymbolTimeNode; -import org.sbml.jsbml.math.ASTCiFunctionNode; -import org.sbml.jsbml.math.ASTCiNumberNode; -import org.sbml.jsbml.math.ASTCnExponentialNode; -import org.sbml.jsbml.math.ASTCnIntegerNode; -import org.sbml.jsbml.math.ASTCnNumberNode; -import org.sbml.jsbml.math.ASTCnRationalNode; -import org.sbml.jsbml.math.ASTCnRealNode; -import org.sbml.jsbml.math.ASTConstantNumber; -import org.sbml.jsbml.math.ASTDivideNode; -import org.sbml.jsbml.math.ASTFactory; -import org.sbml.jsbml.math.ASTFunction; -import org.sbml.jsbml.math.ASTHyperbolicNode; -import org.sbml.jsbml.math.ASTLambdaFunctionNode; -import org.sbml.jsbml.math.ASTLogarithmNode; -import org.sbml.jsbml.math.ASTLogicalOperatorNode; -import org.sbml.jsbml.math.ASTMinusNode; -import org.sbml.jsbml.math.ASTNode2; -import org.sbml.jsbml.math.ASTPiecewiseFunctionNode; -import org.sbml.jsbml.math.ASTPlusNode; -import org.sbml.jsbml.math.ASTPowerNode; -import org.sbml.jsbml.math.ASTQualifierNode; -import org.sbml.jsbml.math.ASTRelationalOperatorNode; -import org.sbml.jsbml.math.ASTRootNode; -import org.sbml.jsbml.math.ASTTimesNode; -import org.sbml.jsbml.math.ASTTrigonometricNode; -import org.sbml.jsbml.math.ASTUnaryFunctionNode; -import org.sbml.jsbml.math.ASTUnknown; -import org.sbml.jsbml.math.compiler.ASTNode2Compiler; -import org.sbml.jsbml.math.compiler.ASTNode2Value; -import org.sbml.jsbml.math.compiler.MathMLXMLStreamCompiler; -import org.sbml.jsbml.text.parser.FormulaParser; -import org.sbml.jsbml.text.parser.FormulaParserLL3; -import org.sbml.jsbml.text.parser.IFormulaParser; -import org.sbml.jsbml.text.parser.ParseException; -import org.sbml.jsbml.util.compilers.ASTNodeCompiler; -import org.sbml.jsbml.util.compilers.ASTNodeValue; -import org.sbml.jsbml.util.compilers.FormulaCompiler; -import org.sbml.jsbml.util.compilers.FormulaCompilerLibSBML; -import org.sbml.jsbml.util.compilers.LaTeXCompiler; -import org.sbml.jsbml.util.filters.Filter; -import org.sbml.jsbml.xml.stax.SBMLReader; - -/** - * A node in the Abstract Syntax Tree (AST) representation of a mathematical - * expression. - * - * @author Andreas Dräger - * @author Nicolas Rodriguez - * @author Alexander Dörr - * @since 0.8 - * @version $Rev: 2157 $ - */ -public class ASTNode extends AbstractTreeNode { - - /** - * An enumeration of all possible types that can be represented by an abstract - * syntax tree node. - * - * @author Andreas Dräger - * - */ - public static enum Type { - /** - * If the {@link ASTNode} represents Euler's constant, it should have this - * {@link Type}. - */ - CONSTANT_E, - /** - * If an {@link ASTNode} represents the {@link Boolean} attribute - * {@link Boolean#FALSE} it should have this {@link Type}. - */ - CONSTANT_FALSE, - /** - * If the {@link ASTNode} represents the constant π, it should have - * this {@link Type}. - */ - CONSTANT_PI, - /** - * - */ - CONSTANT_TRUE, - /** - * - */ - CONSTRUCTOR_PIECE, - /** - * - */ - CONSTRUCTOR_OTHERWISE, - /** - * - */ - DIVIDE, - /** - * The type of an {@link ASTNode} containing a reference to a user-defined - * {@link FunctionDefinition}. - */ - FUNCTION, - /** - * - */ - FUNCTION_ABS, - /** - * - */ - FUNCTION_ARCCOS, - /** - * - */ - FUNCTION_ARCCOSH, - /** - * - */ - FUNCTION_ARCCOT, - /** - * - */ - FUNCTION_ARCCOTH, - /** - * - */ - FUNCTION_ARCCSC, - /** - * - */ - FUNCTION_ARCCSCH, - /** - * - */ - FUNCTION_ARCSEC, - /** - * - */ - FUNCTION_ARCSECH, - /** - * - */ - FUNCTION_ARCSIN, - /** - * - */ - FUNCTION_ARCSINH, - /** - * - */ - FUNCTION_ARCTAN, - /** - * - */ - FUNCTION_ARCTANH, - /** - * - */ - FUNCTION_CEILING, - /** - * - */ - FUNCTION_COS, - /** - * - */ - FUNCTION_COSH, - /** - * - */ - FUNCTION_COT, - /** - * - */ - FUNCTION_COTH, - /** - * - */ - FUNCTION_CSC, - /** - * - */ - FUNCTION_CSCH, - /** - * - */ - FUNCTION_DELAY, - /** - * - */ - FUNCTION_EXP, - /** - * - */ - FUNCTION_FACTORIAL, - /** - * - */ - FUNCTION_FLOOR, - /** - * - */ - FUNCTION_LN, - /** - * - */ - FUNCTION_LOG, - /** - * - */ - FUNCTION_PIECEWISE, - /** - * An {@link ASTNode} of this {@link Type} represents a function call of the - * 'pow' function. This function takes two arguments, the base and the - * exponent. Alternatively, also {@link Type#POWER} can be used, which - * represents the simple text symbol '^' to achieve the same effect. - */ - FUNCTION_POWER, - /** - * - */ - FUNCTION_ROOT, - /** - * - */ - FUNCTION_SEC, - /** - * - */ - FUNCTION_SECH, - /** - * - */ - FUNCTION_SELECTOR, - /** - * - */ - FUNCTION_SIN, - /** - * - */ - FUNCTION_SINH, - /** - * - */ - FUNCTION_TAN, - /** - * - */ - FUNCTION_TANH, - /** - * - */ - INTEGER, - /** - * This type describes function definitions: The first n children of a node - * of this type are the arguments, and the last child is the function body. - */ - LAMBDA, - /** - * - */ - LOGICAL_AND, - /** - * - */ - LOGICAL_NOT, - /** - * - */ - LOGICAL_OR, - /** - * - */ - LOGICAL_XOR, - /** - * - */ - MINUS, - /** - * {@link ASTNode}s of this {@link Type} refer to a {@link CallableSBase}. - */ - NAME, - /** - * A type to express Avogadro's number. - */ - NAME_AVOGADRO, - /** - * - */ - NAME_TIME, - /** - * - */ - PLUS, - /** - * This {@link Type} represents an operation with two children: a base and - * an exponent. In textual form, this type is represented by the symbol '^'. - */ - POWER, - /** - * - */ - PRODUCT, - /** - * - */ - QUALIFIER_BVAR, - /** - * - */ - QUALIFIER_DEGREE, - /** - * - */ - QUALIFIER_LOGBASE, - /** - * An {@link ASTNode} of this {@link Type} contains two integer values: a - * numerator and a denominator. - */ - RATIONAL, - /** - * {@link Type} of an {@link ASTNode} that represents a single real value, - * i.e., a double number. - */ - REAL, - /** - * {@link Type} of an {@link ASTNode} with a real value that is split in a - * double mantissa and an integer exponent. - */ - REAL_E, - /** - * An {@link ASTNode} of this {@link Type} represents the relation symbol - * '=' to compare the values of all of its successors in the tree for - * equality. - */ - RELATIONAL_EQ, - /** - * Greater or equal - */ - RELATIONAL_GEQ, - /** - * Greater than - */ - RELATIONAL_GT, - /** - * Less or equal - */ - RELATIONAL_LEQ, - /** - * Less than - */ - RELATIONAL_LT, - /** - * Not equal - */ - RELATIONAL_NEQ, - /** - * - */ - SUM, - /** - * - */ - TIMES, - /** - * - */ - UNKNOWN, - /** - * - */ - VECTOR; - - /** - * Returns the {@link Type} corresponding to the given {@link String}. - * - * @param type - * e.g., sin, asin, exp, and so on. See the specification of the - * MathML subset used in SBML. - * @return The type corresponding to the given {@link String} or - * {@link #UNKNOWN} if no matching can be found. - */ - public static Type getTypeFor(String type) { - // Arithmetic operators - if (type.equals("plus")) { - return PLUS; - } else if (type.equals("minus")) { - return MINUS; - } else if (type.equals("times")) { - return TIMES; - } else if (type.equals("divide")) { - return DIVIDE; - } else if (type.equals("power")) { - return FUNCTION_POWER; - } else if (type.equals("root")) { - return FUNCTION_ROOT; - } else if (type.equals("abs")) { - return FUNCTION_ABS; - } else if (type.equals("exp")) { - return FUNCTION_EXP; - } else if (type.equals("ln")) { - return FUNCTION_LN; - } else if (type.equals("log")) { - return FUNCTION_LOG; - } else if (type.equals("floor")) { - return FUNCTION_FLOOR; - } else if (type.equals("ceiling")) { - return FUNCTION_CEILING; - } else if (type.equals("factorial")) { - return FUNCTION_FACTORIAL; - } - - // Logical operators - else if (type.equals("and") || type.equals("&&")) { - return LOGICAL_AND; - } else if (type.equals("or") || type.equals("||")) { - return LOGICAL_OR; - } else if (type.equals("xor")) { - return LOGICAL_XOR; - } else if (type.equals("not") || type.equals("!")) { - return LOGICAL_NOT; - } - - // Trigonometric operators - else if (type.equals("cos")) { - return FUNCTION_COS; - } else if (type.equals("sin")) { - return FUNCTION_SIN; - } else if (type.equals("tan")) { - return FUNCTION_TAN; - } else if (type.equals("sec")) { - return FUNCTION_SEC; - } else if (type.equals("csc")) { - return FUNCTION_CSC; - } else if (type.equals("cot")) { - return FUNCTION_COT; - } else if (type.equals("sinh")) { - return FUNCTION_SINH; - } else if (type.equals("cosh")) { - return FUNCTION_COSH; - } else if (type.equals("tanh")) { - return FUNCTION_TANH; - } else if (type.equals("sech")) { - return FUNCTION_SECH; - } else if (type.equals("csch")) { - return FUNCTION_CSCH; - } else if (type.equals("coth")) { - return FUNCTION_COTH; - } else if (type.equals("arcsin")) { - return FUNCTION_ARCSIN; - } else if (type.equals("arccos")) { - return FUNCTION_ARCCOS; - } else if (type.equals("arctan")) { - return FUNCTION_ARCTAN; - } else if (type.equals("arcsec")) { - return FUNCTION_ARCSEC; - } else if (type.equals("arccsc")) { - return FUNCTION_ARCCSC; - } else if (type.equals("arccot")) { - return FUNCTION_ARCCOT; - } else if (type.equals("arcsinh")) { - return FUNCTION_ARCSINH; - } else if (type.equals("arccosh")) { - return FUNCTION_ARCCOSH; - } else if (type.equals("arctanh")) { - return FUNCTION_ARCTANH; - } else if (type.equals("arcsech")) { - return FUNCTION_ARCSECH; - } else if (type.equals("arccsch")) { - return FUNCTION_ARCCSCH; - } else if (type.equals("arccoth")) { - return FUNCTION_ARCCOTH; - } - - // Relational operators - else if (type.equals("eq") || type.equals("=")) { - return RELATIONAL_EQ; - } else if (type.equals("neq") || type.equals("!")) { - return RELATIONAL_NEQ; - } else if (type.equals("gt") || type.equals(">")) { - return RELATIONAL_GT; - } else if (type.equals("lt") || type.equals("<")) { - return RELATIONAL_LT; - } else if (type.equals("geq") || type.equals(">=")) { - return RELATIONAL_GEQ; - } else if (type.equals("leq") || type.equals("<=")) { - return RELATIONAL_LEQ; - } - // token: cn, ci, csymbol, sep - // for ci, we have to check if it is a functionDefinition - // for cn, we pass the type attribute to this function to determine the - // proper astNode type - // for csymbol, we pass the definitionURL - else if (type.equalsIgnoreCase("real") || type.equalsIgnoreCase("cn")) { - // we put the type by default to real in case the type attribute is - // not define on the cn element. - return REAL; - } else if (type.equalsIgnoreCase("integer")) { - return INTEGER; - } else if (type.equalsIgnoreCase("e-notation")) { - return REAL_E; - } else if (type.equalsIgnoreCase("rational")) { - return RATIONAL; - } else if (type.equals("ci")) { - return NAME; - } else if (type.equals("csymbol")) { - return UNKNOWN; - } else if (type.equals("sep")) { - return UNKNOWN; - } else if (type.equals(ASTNode.URI_TIME_DEFINITION)) { - return NAME_TIME; - } else if (type.equals(ASTNode.URI_DELAY_DEFINITION)) { - return FUNCTION_DELAY; - } else if (type.equals(ASTNode.URI_AVOGADRO_DEFINITION)) { - return NAME_AVOGADRO; - } - - // general: apply, piecewise, piece, otherwise, lambda, bvar - else if (type.equals("lambda")) { - return LAMBDA; - } else if (type.equals("bvar")) { - // nothing to do, node ignore when parsing - } else if (type.equals("piecewise")) { - return FUNCTION_PIECEWISE; - } else if (type.equals("piece")) { - // nothing to do, node ignore when parsing - } else if (type.equals("otherwise")) { - // nothing to do, node ignore when parsing - } - - // qualifiers: degree, logbase - else if (type.equals("degree")) { - // nothing to do, node ignore when parsing - } else if (type.equals("logbase")) { - // nothing to do, node ignore when parsing - } - - // constants: true, false, notanumber, pi, infinity, exponentiale - else if (type.equals("true")) { - return CONSTANT_TRUE; - } else if (type.equals("false")) { - return CONSTANT_FALSE; - } else if (type.equals("notanumber")) { - return REAL; - } else if (type.equals("pi")) { - return CONSTANT_PI; - } else if (type.equals("infinity")) { - return REAL; - } else if (type.equals("exponentiale")) { - return CONSTANT_E; - } - - // arrays package additional mathML elements - else if (type.equals("selector")) { - return FUNCTION_SELECTOR; - } else if (type.equals("vector")) { - return VECTOR; - } - - // TODO: possible annotations: semantics, annotation, annotation-xml - - return UNKNOWN; - } - - /** - * Checks whether this type is valid for the given SBML Level/Version - * combination. - * - * @jsbml.warning this method is not implemented - * - * @param level - * @param version - * @return whether this type is valid for the given SBML Level/Version - * combination. - */ - public boolean isDefinedIn(int level, int version) { - // TODO - return false; - } - - } - - /** - * Message to indicate that an {@link ASTNode.Type} type has been chosen which - * cannot be used as an operator. - */ - public static final transient String INVALID_OPERATOR_MSG = "Invalid operator %s. The operator must be one of the following constants: PLUS, MINUS, TIMES, DIVIDE, or POWER."; - - /** - * Generated serial version identifier. - */ - private static final long serialVersionUID = -1391327698196553142L; - - /** - * The URI for the definition of the csymbol for avogadro. - */ - public static final transient String URI_AVOGADRO_DEFINITION = "http://www.sbml.org/sbml/symbols/avogadro"; - - /** - * The URI for the definition of the csymbol for delay. - */ - public static final transient String URI_DELAY_DEFINITION = "http://www.sbml.org/sbml/symbols/delay"; - - /** - * URI for the definition of MathML. - */ - public static final transient String URI_MATHML_DEFINITION = "http://www.w3.org/1998/Math/MathML"; - - // TODO: check how we set the math in level 1 - - /** - * URI prefix for the definition of MathML, it will be used to write the sbml - * file - */ - public static final String URI_MATHML_PREFIX = ""; - - /** - * The URI for the definition of the csymbol for time. - */ - public static final transient String URI_TIME_DEFINITION = "http://www.sbml.org/sbml/symbols/time"; - - /** - * Creates and returns an {@link ASTNode} that computes the absolute value of - * the given double value. - * - * @param d - * a double value - * @param parent - * the parent {@link ASTNode} - * @return an {@link ASTNode} that computes the absolute value of the given - * double value. - */ - public static ASTNode abs(double d, MathContainer parent) { - ASTUnaryFunctionNode node = new ASTUnaryFunctionNode(Type.FUNCTION_ABS); - node.addChild(new ASTCnRealNode(d)); - node.setParentSBMLObject(parent); - return new ASTNode(node); - } - - /** - * Creates and returns an {@link ASTNode} that computes the absolute value of - * the given integer value. - * - * @param integer - * an integer value - * @param parent - * the parent {@link ASTNode} - * @return an {@link ASTNode} that computes the absolute value of the given - * integer value. - */ - public static ASTNode abs(int integer, MathContainer parent) { - ASTUnaryFunctionNode node = new ASTUnaryFunctionNode(Type.FUNCTION_ABS); - node.addChild(new ASTCnIntegerNode(integer)); - node.setParentSBMLObject(parent); - return new ASTNode(node); - } - - /** - * Creates a new {@link ASTNode} of type {@code operator} and adds the given - * nodes as children. - * - * @param operator - * the type of arithmetic operation - * @param ast - * the children of the new ASTNode - * @return a new {@link ASTNode} of type {@code operator} and adds the given - * nodes as children. - */ - private static ASTNode arithmeticOperation(Type operator, ASTNode... ast) { - ASTNode2[] list = new ASTNode2[ast.length]; - for (int i = 0; i < ast.length; i++) { - list[i] = ast[i].toASTNode2(); - } - return new ASTNode(ASTFactory.arithmeticOperation(operator, list)); - } - - /** - * Creates a new {@link ASTNode} of type MINUS and adds the given nodes as - * children - * - * @param ast - * the children of the new ASTNode - * @return a new {@link ASTNode} of type MINUS and adds the given nodes as - * children - */ - public static ASTNode diff(ASTNode... ast) { - ASTNode2[] ast2 = new ASTNode2[ast.length]; - for (int i = 0; i < ast.length; i++) { - ast2[i] = ast[i].toASTNode2(); - } - ASTNode node = null; - if (ast.length == 2) { - node = new ASTNode(ASTFactory.minus(ast2[0], ast2[1])); - } else { - node = new ASTNode(ASTFactory.diff(ast2)); - } - return node; - } - - /** - * Creates a new {@link ASTNode} of type RELATIONAL_EQ. - * - * @param left - * the left child. - * @param right - * the right child. - * @return a new {@link ASTNode} of type RELATIONAL_EQ. - */ - public static ASTNode eq(ASTNode left, ASTNode right) { - return new ASTNode(ASTFactory.eq(left.toASTNode2(), right.toASTNode2())); - } - - /** - * Returns a new {@link ASTNode} that represents Euler's constant raised by - * the power of the given exponent. - * - * @param exponent - * the exponent - * @return a new {@link ASTNode} that represents Euler's constant raised by - * the power of the given exponent. - */ - public static ASTNode exp(ASTNode exponent) { - return new ASTNode(ASTFactory.exp(exponent.toASTNode2())); - } - - /** - * Returns the formula from the given ASTNode as an SBML Level 1 text-string - * mathematical formula. - * - * - * @param tree - * the root of the ASTNode formula expression tree - * @return the formula from the given AST as an SBML Level 1 text-string - * mathematical formula. The caller owns the returned string and is - * responsible for freeing it when it is no longer needed. - * {@code null} is returned if the given argument is {@code null}. - * @throws SBMLException - * @see #toFormula() - * - */ - public static String formulaToString(ASTNode tree) throws SBMLException { - return tree.toFormula(); - } - - /** - * Creates a new {@link ASTNode} of type DIVIDE with the given nodes as - * children. - * - * @param numerator - * the numerator - * @param denominator - * the denominator - * @return a new {@link ASTNode} of type DIVIDE with the given nodes as - * children. - */ - public static ASTNode frac(ASTNode numerator, ASTNode denominator) { - return new ASTNode(ASTFactory.frac(numerator.toASTNode2(), - denominator.toASTNode2())); - } - - /** - * Creates a new {@link ASTNode} that of type DIVIDE with the given numerator - * and denominator. - * - * @param numerator - * the numerator - * @param denominator - * the denominator - * @return a new {@link ASTNode} that of type DIVIDE with the given numerator - * and denominator. - */ - public static ASTNode frac(int numerator, ASTNode denominator) { - return frac(new ASTNode(numerator, denominator.getParentSBMLObject()), - denominator); - } - - /** - * Creates a new {@link ASTNode} that divides two {@link CallableSBase} - * objects. - * - * @param container - * the parent object - * @param numerator - * the numerator - * @param denominator - * the denominator - * @return a new {@link ASTNode} that divides two {@link CallableSBase} - * objects. - */ - public static ASTNode frac(MathContainer container, CallableSBase numerator, - CallableSBase denominator) { - return frac(new ASTNode(numerator, container), new ASTNode(denominator, - container)); - } - - /** - * Returns a new {@link ASTNode} that of type DIVIDE with the two entities as - * numerator and denominator. - * - * @param container - * the parent object - * @param numeratorId - * the numerator - * @param denominatorId - * the numerator - * @return a new {@link ASTNode} that of type DIVIDE with the two entities as - * numerator and denominator. - */ - public static ASTNode frac(MathContainer container, String numeratorId, - String denominatorId) { - return frac(new ASTNode(numeratorId, container), new ASTNode(denominatorId, - container)); - } - - /** - * Creates an {@link ASTNode} representing greater or equal for the two given - * nodes. - * - * @param left - * the left child. - * @param right - * the right child. - * @return an {@link ASTNode} representing greater or equal. - */ - public static ASTNode geq(ASTNode left, ASTNode right) { - return new ASTNode(ASTFactory.geq(left.toASTNode2(), right.toASTNode2())); - } - - /** - * Creates an {@link ASTNode} representing greater than for the two given left - * and right child. - * - * @param left - * the left child. - * @param right - * the right child. - * @return an {@link ASTNode} representing greater than for the two given left - * and right child. - */ - public static ASTNode gt(ASTNode left, ASTNode right) { - return new ASTNode(ASTFactory.gt(left.toASTNode2(), right.toASTNode2())); - } - - /** - * Creates an {@link ASTNode} representing less or equal for the two given - * left and right child. - * - * @param left - * the left child. - * @param right - * the right child. - * @return an {@link ASTNode} representing less or equal for the two given - * left and right child. - */ - public static ASTNode leq(ASTNode left, ASTNode right) { - return new ASTNode(ASTFactory.leq(left.toASTNode2(), right.toASTNode2())); - } - - /** - * Creates an {@link ASTNode} representing a logarithm to base 10 of the given - * value. - * - * @param value - * the value which is the argument of the logarithm. - * @return an {@link ASTNode} representing a logarithm to base 10 of the given - * value. - */ - public static ASTNode log(ASTNode value) { - return new ASTNode(ASTFactory.log(value.toASTNode2())); - } - - /** - * Creates an {@link ASTNode} that represents the logarithm function with the - * given base and value. The parent SBML object will be taken from the - * {@link ASTNode} value. - * - * @param base - * The basis of this logarithm. Can be null; then a base of 10 will - * be assumed. - * @param value - * Must not be {@code null}. - * @return An {@link ASTNode} representing the logarithm of the given value - * with respect to the given base or to the base 10 if base is - * {@code null}. - */ - public static ASTNode log(ASTNode base, ASTNode value) { - return new ASTNode(ASTFactory.log(base.toASTNode2(), value.toASTNode2())); - } - - /** - * Creates an {@link ASTNode} that performs a less than comparison between two - * {@link ASTNode}s. The parent SBML object of the resulting node will be - * taken from the left node. - * - * @param left - * the left child. - * @param right - * the right child. - * @return an {@link ASTNode} that performs a less than comparison between two - * {@link ASTNode}s. - */ - public static ASTNode lt(ASTNode left, ASTNode right) { - return new ASTNode(ASTFactory.lt(left.toASTNode2(), right.toASTNode2())); - } - - /** - * Creates an {@link ASTNode} that performs a less than comparison between a - * variable and another {@link ASTNode}. The parent SBML object will be taken - * from the given {@link ASTNode}. - * - * @param variable - * the left child. - * @param node - * the right child. - * @return an {@link ASTNode} that performs a less than comparison between a - * variable and another {@link ASTNode}. - */ - public static ASTNode lt(String variable, ASTNode node) { - return lt(new ASTNode(variable, node.getParentSBMLObject()), node); - } - - /** - * Creates an {@link ASTNode} that performs a not equal comparison between two - * {@link ASTNode}s. - * - * @param left - * the left child. - * @param right - * the right child. - * @return an {@link ASTNode} that performs a not equal comparison between two - * {@link ASTNode}s. - */ - public static ASTNode neq(ASTNode left, ASTNode right) { - return new ASTNode(ASTFactory.neq(left.toASTNode2(), right.toASTNode2())); - } - - /** - * Parses a text-string mathematical formula and returns a representation as - * an Abstract Syntax Tree. - * - * <p> - * Support the syntax defined in {@link FormulaParserLL3} which is almost the same syntax as defined in - * <a href="http://sbml.org/Software/libSBML/docs/java-api/org/sbml/libsbml/libsbml.html#parseL3Formula(java.lang.String)"> - * the LibSBML L3 parser</a>. The things not supported for now are the units associated with numbers. - * - * <p> - * Parsing of the various MathML functions and constants are all - * case-sensitive by default: names such as - * {@code Cos} and {@code COS} are not parsed as the MathML cosine - * operator, {@code <cos>}. As well, if you have an SBML entities (species, parameter, ...) that - * has an id identical to one of the supported mathML element, the parser will interpret the String as the - * mathML element and not the SBML entity. - * - * <p> You can change this behaviour by using the {@link FormulaParserLL3#setCaseSensitive(boolean)} - * method and using the {@link ASTNode#parseFormula(String, IFormulaParser)} method instead of this one: -<p><pre><blockquote> - FormulaParserLL3 caseSensitiveParser = new FormulaParserLL3(new StringReader("")); - caseInsensitiveParser.setCaseSensitive(false); - ASTNode n = ASTNode.parseFormula("Cos(x)", caseInsensitiveParser); -</pre></blockquote></p> - * - * <p> This method has a different behaviour since JSBML-1.0 compare to JSBML-0.8. There is a different - * operator precedence, the parsing is now case sensitive for mathML elements and boolean operators are - * now differently interpreted: '&&' and '||' are used instead of 'and' and 'or'.<br> - * If you want to use the parser used in JSBML-0.8, you can do that by using the {@link FormulaParser} - * parser class and using the {@link ASTNode#parseFormula(String, IFormulaParser)} method instead of this one: -<p><pre><blockquote> - FormulaParser oldParser = new FormulaParser(new StringReader("")); - - ASTNode n = ASTNode.parseFormula("x and y", oldParser); -</pre></blockquote></p> - * - * <p> If you are not satisfied with the behavior of the existing parsers, you can create - * your own, you just need to implement the {@link IFormulaParser} interface. - * - * @param formula - * a text-string mathematical formula. - * @return an {@link ASTNode} representing the formula. - * @throws ParseException - * If the given formula is not of valid format or cannot be - * parsed for other reasons. - * @see ASTNode#parseFormula(String, IFormulaParser) - * @see FormulaParserLL3 - * @see FormulaParser - */ - public static ASTNode parseFormula(String formula) throws ParseException { - FormulaParserLL3 parser = new FormulaParserLL3(new StringReader(formula)); - ASTNode result = null; - - try { - result = parser.parse(); - } catch (Throwable e) { - // the javacc parser can throw some TokenMgrError at least - throw new ParseException(e); - } - - return result; - } - - /** - * Parses a text-string mathematical formula and returns a representation as - * an Abstract Syntax Tree. - * - * @param formula - * a text-string mathematical formula. - * @param parser - * a formula parser. - * @return an {@link ASTNode} representing the formula. - * @throws ParseException - * If the given formula is not of valid format or cannot be parsed - * for other reasons. - */ - public static ASTNode parseFormula(String formula, IFormulaParser parser) - throws ParseException { - parser.ReInit(new StringReader(formula)); - ASTNode result = null; - try { - result = parser.parse(); - } catch (Throwable e) { - // The JavaCC parser can throw a TokenMgrError at least - throw new ParseException(); - } - - return result; - } - - /** - * Creates a piecewise {@link ASTNode}. - * - * <p> - * At least one {@link ASTNode} must be given as a child. The parent SBML - * object of this first node will be the parent of the resulting - * {@link ASTNode}. - * - * @param node - * the parent SBML object of this node will be the parent of the - * resulting {@link ASTNode}. - * @param nodes - * the children of the new piecewise ASTNode - * @return a piecewise {@link ASTNode}. - */ - public static ASTNode piecewise(ASTNode node, ASTNode... nodes) { - // TODO: ASTFactory.piecewise() only accepts ASTQualifierNode[] - // A few assumptions are being made here: i.e. that every pair - // of ASTNodes in list nodes consists of a node that has a number - // type and another node that is a relational operator. - ASTQualifierNode qualifier = null; - ASTQualifierNode[] qualifiers = null; - if (node != null && nodes.length > 0) { - - // Construct PIECE node - qualifier = new ASTQualifierNode(Type.CONSTRUCTOR_PIECE); - qualifier.addChild(node.toASTNode2()); - qualifier.addChild(nodes[0].toASTNode2()); - - qualifiers = new ASTQualifierNode[(nodes.length - 2) / 2]; - int j, k = 0; - for (int i = 2; i < nodes.length; i++) { - j = i + 1; - if (j != nodes.length) { - qualifiers[k] = new ASTQualifierNode(Type.CONSTRUCTOR_PIECE); - qualifiers[k].addChild(nodes[i].toASTNode2()); - qualifiers[k].addChild(nodes[j].toASTNode2()); - } else { - qualifiers[k] = new ASTQualifierNode(Type.CONSTRUCTOR_OTHERWISE); - qualifiers[k].addChild(nodes[i].toASTNode2()); - } - k++; - } - } else { - throw new IllegalArgumentException(); - } - return new ASTNode(ASTFactory.piecewise(qualifier, qualifiers)); - } - - /** - * Creates a power {@link ASTNode}. - * - * @param basis - * the basis - * @param exponent - * the exponent - * @return a power {@link ASTNode}. - */ - public static ASTNode pow(ASTNode basis, ASTNode exponent) { - return new ASTNode( - ASTFactory.pow(basis.toASTNode2(), exponent.toASTNode2())); - } - - /** - * Creates a power {@link ASTNode}. - * - * @param basis - * the basis - * @param exponent - * the exponent - * @return a power {@link ASTNode}. - */ - public static ASTNode pow(ASTNode basis, double exponent) { - return new ASTNode(ASTFactory.pow(basis.toASTNode2(), new ASTCnRealNode( - exponent))); - } - - /** - * Creates a power {@link ASTNode}. - * - * @param basis - * the basis - * @param exponent - * the exponent - * @return a power {@link ASTNode}. - */ - public static ASTNode pow(ASTNode basis, int exponent) { - return new ASTNode(ASTFactory.pow(basis.toASTNode2(), new ASTCnIntegerNode( - exponent))); - } - - /** - * Raises the given basis by the power of the given exponent. - * - * @param container - * the parent object - * @param basis - * the basis - * @param exponent - * the exponent - * @return a power {@link ASTNode}. - */ - public static ASTNode pow(MathContainer container, CallableSBase basis, - CallableSBase exponent) { - ASTCSymbolBaseNode ciBasis = basis instanceof FunctionDefinition - ? new ASTCiFunctionNode() - : new ASTCiNumberNode(); - ciBasis.setName(basis.getId()); - ASTCSymbolBaseNode ciExponent = basis instanceof FunctionDefinition - ? new ASTCiFunctionNode() - : new ASTCiNumberNode(); - ciExponent.setName(basis.getId()); - ASTPowerNode pow = ASTFactory.pow(ciBasis, ciExponent); - pow.setParentSBMLObject(container); - return new ASTNode(pow); - } - - /** - * Reads the MathML from the given XML string. - * - * <p> - * Constructs a corresponding abstract syntax tree, and returns a pointer to - * the root of the tree. - * - * @param xml - * the MathML XML string. - * @return an {@link ASTNode} - */ - public static ASTNode readMathMLFromString(String xml) { - try { - return new SBMLReader().readMathML(xml); - } catch (XMLStreamException e) { - e.printStackTrace(); - } - - return null; - } - - /** - * Creates a relational {@link ASTNode} of the given type with the two given - * children left and right. - * <p> - * Sets the parent SBML object of all nodes to the one provided by the left - * child. - * - * @param type - * the type of relational node. - * @param left - * the left child. - * @param right - * the right child. - * @return a relational {@link ASTNode} of the given type with the two given - * children left and right. - */ - private static ASTNode relational(ASTNode.Type type, ASTNode left, - ASTNode right) { - if ((left == null) || (right == null)) { - throw new NullPointerException( - "Cannot create a relational node with null arguments."); - } - return new ASTNode(ASTFactory.relational(type, left.toASTNode2(), - right.toASTNode2())); - } - - /** - * Creates a root {@link ASTNode}. - * - * @param radicand - * the radicand - * @param rootExponent - * the exponent of the root element. - * @return a root {@link ASTNode}. - */ - public static ASTNode root(ASTNode rootExponent, ASTNode radicand) { - return new ASTNode(ASTFactory.root(rootExponent.toASTNode2(), - radicand.toASTNode2())); - } - - /** - * Sets the Parent of the node and its children to the given value - * - * @param node - * the orphan node - * @param parent - * the parent - */ - static void setParentSBMLObject(ASTNode node, MathContainer parent) { - node.toASTNode2().setParentSBMLObject(parent); - } - - /** - * Sets the parent of the node and its children to the given value - * - * @param node - * the orphan node - * @param parent - * the parent - * @param depth - * the current depth in the {@link ASTNode} tree. It is just here for - * testing purposes to track the depth in the tree during the - * process. - */ - private static void setParentSBMLObject(ASTNode node, MathContainer parent, - int depth) { - // TODO: Is using depth for testing still required? Can we remove this - // method? - setParentSBMLObject(node, parent); - } - - /** - * Creates a root {@link ASTNode}. - * - * @param radicand - * @return a root {@link ASTNode}. - */ - public static ASTNode sqrt(ASTNode radicand) { - return new ASTNode(ASTFactory.sqrt(radicand.toASTNode2())); - } - - /** - * Creates a new {@link ASTNode} of type Plus with the given nodes as - * children. - * - * @param ast - * the children nodes. - * @return a new {@link ASTNode} of type Plus with the given nodes as - * children. - */ - public static ASTNode sum(ASTNode... ast) { - ASTNode2[] ast2 = new ASTNode2[ast.length]; - for (int i = 0; i < ast.length; i++) { - ast2[i] = ast[i].toASTNode2(); - } - return new ASTNode(ASTFactory.sum(ast2)); - } - - /** - * Sum of several NamedSBase objects. - * - * @param parent - * the parent - * @param sbase - * @return the sum of several NamedSBase objects. - */ - public static ASTNode sum(MathContainer parent, CallableSBase... sbase) { - ASTCSymbolBaseNode[] ref = new ASTCSymbolBaseNode[sbase.length]; - for (int i = 0; i < sbase.length; i++) { - ref[i] = sbase[i] instanceof FunctionDefinition - ? new ASTCiFunctionNode() - : new ASTCiNumberNode(); - ref[i].setName(sbase[i].getId()); - } - ASTArithmeticOperatorNode sum = ASTFactory.sum(ref); - sum.setParentSBMLObject(parent); - return new ASTNode(sum); - } - - /** - * Creates an {@link ASTNode} of type product and adds the given nodes as - * children. - * - * @param ast - * @return an {@link ASTNode} of type product and adds the given nodes as - * children. - */ - public static ASTNode times(ASTNode... ast) { - ASTNode2[] ast2 = new ASTNode2[ast.length]; - for (int i = 0; i < ast.length; i++) { - ast2[i] = ast[i].toASTNode2(); - } - return new ASTNode(ASTFactory.product(ast2)); - } - - /** - * Multiplies several {@link CallableSBase} objects. - * - * @param parent - * @param sbase - * @return the multiplication of several {@link CallableSBase} objects. - */ - public static ASTNode times(MathContainer parent, CallableSBase... sbase) { - ASTCSymbolBaseNode[] ref = new ASTCSymbolBaseNode[sbase.length]; - for (int i = 0; i < sbase.length; i++) { - ref[i] = sbase[i] instanceof FunctionDefinition - ? new ASTCiFunctionNode() - : new ASTCiNumberNode(); - ref[i].setName(sbase[i].getId()); - } - ASTArithmeticOperatorNode times = ASTFactory.product(ref); - times.setParentSBMLObject(parent); - return new ASTNode(times); - } - - /** - * Creates a new {@link ASTNode} that has exactly one child and which is of - * type minus, i.e., this negates what is encoded in ast. - * - * @param ast - * @return a new {@link ASTNode} that has exactly one child and which is of - * type minus, i.e., this negates what is encoded in ast. - */ - public static ASTNode uMinus(ASTNode ast) { - return new ASTNode(ASTFactory.uMinus(ast.toASTNode2())); - } - - /** - * Creates a new {@link ASTNode} that has exactly one child and which is of - * type minus, i.e., this negates what is encoded in ast. - * - * @param container - * @return a new {@link ASTNode} that has exactly one child and which is of - * type minus, i.e., this negates what is encoded in ast. - */ - public static ASTNode uMinus(MathContainer container) { - if (container instanceof CallableSBase) { - ASTCSymbolBaseNode ci = container instanceof FunctionDefinition - ? new ASTCiFunctionNode() - : new ASTCiNumberNode(); - ci.setName(((CallableSBase) container).getId()); - return new ASTNode(ASTFactory.multiplyWith(ci)); - } - return null; - } - - /** - * A pointer to the {@link ASTNode2} corresponding to the current - * {@link ASTNode} - */ - private ASTNode2 astnode2; - - /** - * A {@link Logger} for this class. - */ - private static transient final Logger logger = Logger - .getLogger(ASTNode.class); - - /** - * Creates a new {@link ASTNode} of unspecified type and without a pointer to - * its containing {@link MathContainer}. - */ - public ASTNode() { - super(); - setType(Type.UNKNOWN); - } - - /** - * Copy constructor; Creates a deep copy of the given {@link ASTNode}. - * - * @param astNode - * the {@link ASTNode} to be copied. - */ - public ASTNode(ASTNode astNode) { - this(); - - if (astNode.isSetASTNode2()) { - astnode2 = astNode.toASTNode2().clone(); - } - } - - /** - * Creates a new {@link ASTNode} of the given {@link ASTNode.Type} but without - * a pointer to its {@link MathContainer}. - * - * @param type - */ - public ASTNode(ASTNode.Type type) { - setType(type); - } - - /** - * Creates and returns a new {@link ASTNode}. - * - * @param type - * the type of the ASTNode to create. - * @param parent - * the parent SBML object. - */ - public ASTNode(ASTNode.Type type, MathContainer parent) { - setType(type); - setParentSBMLObject(this, parent); - } - - /** - * Create a new node of type {@link ASTNode} from the given {@link ASTNode2} - * - * @param node - * {@link ASTNode2} - */ - public ASTNode(ASTNode2 node) { - astnode2 = node; - } - - /** - * Creates and returns a new {@link ASTNode} referring to the given - * {@link CallableSBase}. - * - * @param nsb - */ - public ASTNode(CallableSBase nsb) { - this(ASTNode.Type.NAME); - setVariable(nsb); - } - - /** - * Creates and returns a new {@link ASTNode} referring to the given - * {@link CallableSBase}. - * - * @param nsb - * @param parent - */ - public ASTNode(CallableSBase nsb, MathContainer parent) { - this(ASTNode.Type.NAME, parent); - setVariable(nsb); - } - - /** - * Creates a new {@link ASTNode} representing an operator, i.e., an internal - * node. - * - * @param operator - */ - public ASTNode(char operator) { - this(); - setCharacter(operator); - } - - /** - * Creates a new {@link ASTNode} representing an operator, i.e., an internal - * node. - * - * @param operator - * @param parent - */ - public ASTNode(char operator, MathContainer parent) { - this(parent); - setCharacter(operator); - } - - /** - * Creates and returns a new {@link ASTNode}. - * - * @param real - */ - public ASTNode(double real) { - switch (Double.compare(real, Math.E)) { - case 0 : - astnode2 = new ASTConstantNumber(Math.E); - break; - default : - astnode2 = new ASTCnRealNode(real); - break; - } - } - - /** - * Creates and returns a new {@link ASTNode}. - * - * @param mantissa - * @param exponent - */ - public ASTNode(double mantissa, int exponent) { - astnode2 = new ASTCnExponentialNode(); - ((ASTCnExponentialNode) astnode2).setMantissa(mantissa); - ((ASTCnExponentialNode) astnode2).setExponent(exponent); - } - - /** - * Creates and returns a new {@link ASTNode}. - * - * @param mantissa - * @param exponent - * @param parent - */ - public ASTNode(double mantissa, int exponent, MathContainer parent) { - this(ASTNode.Type.REAL_E, parent); - setValue(mantissa, exponent); - } - - /** - * Creates and returns a new {@link ASTNode}. - * - * @param real - * @param parent - */ - public ASTNode(double real, MathContainer parent) { - this(ASTNode.Type.REAL, parent); - setValue(real); - } - - /** - * Creates and returns a new {@link ASTNode} with the given integer value. - * - * @param integer - */ - public ASTNode(int integer) { - astnode2 = new ASTCnIntegerNode(integer); - } - - /** - * Creates and returns a new {@link ASTNode} with the given integer value for - * the given {@link MathContainer} as its parent SBML object. - * - * @param integer - * @param parent - */ - public ASTNode(int integer, MathContainer parent) { - this(integer, null, parent); - } - - /** - * Creates and returns a new {@link ASTNode} with the given integer value with - * the given associated {@link #unitId} for the given {@link MathContainer} as - * its parent SBML object. - * - * @param integer - * @param unitsID - * @param parent - */ - public ASTNode(int integer, String unitsID, MathContainer parent) { - this(ASTNode.Type.INTEGER, parent); - setValue(integer); - if (unitsID != null) { - setUnits(unitsID); - } - } - - /** - * Creates and returns a new {@link ASTNode}. - * - * By default, the returned node will have a type of {@link Type#UNKNOWN}. The - * calling code should set the node type to something else as soon as possible - * using setType(int) - * - * @param parent - * the parent SBML object - */ - public ASTNode(MathContainer parent) { - this(); - setParentSBMLObject(this, parent); - } - - /** - * Creates and returns a new {@link ASTNode} with the given name. - * - * @param name - * the name of this ASTNode - */ - public ASTNode(String name) { - this(ASTNode.Type.NAME); - setName(name); - } - - /** - * Creates and returns a new {@link ASTNode} with the given name. - * - * @param name - * the name of this ASTNode - * @param parent - * the parent SBML object. - */ - public ASTNode(String name, MathContainer parent) { - this(ASTNode.Type.NAME, parent); - setName(name); - } - - /** - * Adds a child to this node. - * - * @param child - * the node to add as child. - */ - public void addChild(ASTNode child) { - if (isFunction()) { - ((ASTFunction) astnode2).addChild(child.toASTNode2()); - } - } - - /** - * Creates a new node with the type of this node, moves all children of this - * node to this new node, sets the type of this node to the given operator, - * adds the new node as left child of this node and the given {@link ASTNode} - * as the right child of this node. The parentSBMLObject of the whole - * resulting {@link ASTNode} is then set to the parent of this node. - * - * @param operator - * The new type of this node. This has to be one of the following: - * {@link Type#PLUS}, {@link Type#MINUS}, {@link Type#TIMES}, - * {@link Type#DIVIDE}, {@link Type#POWER}, - * {@link Type#FUNCTION_ROOT}. Otherwise an - * {@link IllegalArgumentException} is thrown. - * @param astnode - * The new right child of this node - * @throws IllegalArgumentException - * if - * <ul> - * <li>this {@link ASTNode} is zero ({@link #isZero()}) and the - * given operator is {@link Type#DIVIDE}</li> - * <li>the operator is not one of the following: {@link Type#PLUS}, - * {@link Type#MINUS}, {@link Type#TIMES}, {@link Type#DIVIDE}, - * {@link Type#POWER}, {@link Type#FUNCTION_ROOT}</li> - * </ul> - */ - private void arithmeticOperation(Type operator, ASTNode astnode) { - astnode2 = ASTFactory.arithmeticOperation(operator, astnode.toASTNode2()); - } - - /* - * (non-Javadoc) - * - * @see java.lang.Object#clone() - */ - @Override - public ASTNode clone() { - return new ASTNode(this); - } - - /** - * Compiles this {@link ASTNode} and returns the result. - * - * @param compiler - * An instance of an {@link ASTNodeCompiler} that provides methods to - * translate this {@link ASTNode} into something different. - * @return Some value wrapped in an {@link ASTNodeValue}. The content of the - * wrapper depends on the {@link ASTNodeCompiler} used to create it. - * However, this {@link ASTNode} will ensure that level and version - * are set appropriately according to this node's parent SBML object. - * @throws SBMLException - * Thrown if an error occurs during the compilation process. - * - */ - public ASTNodeValue compile(ASTNodeCompiler compiler) throws SBMLException { - ASTNodeValue value; - switch (getType()) { - /* - * Numbers - */ - case REAL : - double real = getReal(); - if (Double.isInfinite(real)) { - value = (real > 0d) ? compiler.getPositiveInfinity() : compiler - .getNegativeInfinity(); - } else { - value = compiler.compile(real, getUnits()); - } - break; - case INTEGER : - value = compiler.compile(getInteger(), getUnits()); - break; - /* - * Operators - */ - case POWER : - value = compiler.pow(getLeftChild(), getRightChild()); - break; - case PLUS : - value = compiler.plus(getChildren()); - value.setUIFlag(getChildCount() <= 1); - break; - case MINUS : - if (getChildCount() < 2) { - value = compiler.uMinus(getLeftChild()); - value.setUIFlag(true); - } else { - value = compiler.minus(getChildren()); - value.setUIFlag(false); - } - break; - case TIMES : - value = compiler.times(getChildren()); - value.setUIFlag(getChildCount() <= 1); - break; - case DIVIDE : - int childCount = getChildCount(); - if (childCount != 2) { - throw new SBMLException( - MessageFormat - .format( - "Fractions must have one numerator and one denominator, here {0,number,integer} elements are given.", - childCount)); - } - value = compiler.frac(getLeftChild(), getRightChild()); - break; - case RATIONAL : - value = compiler.frac(getNumerator(), getDenominator()); - break; - case NAME_TIME : - value = compiler.symbolTime(getName()); - break; - case FUNCTION_DELAY : - value = compiler.delay(getName(), getLeftChild(), getRightChild(), - getUnits()); - break; - /* - * Names of identifiers: parameters, functions, species etc. - */ - case NAME : - CallableSBase variable = getVariable(); - - if (variable != null) { - if (variable instanceof FunctionDefinition) { - value = compiler.function((FunctionDefinition) variable, - getChildren()); - } else { - value = compiler.compile(variable); - } - } else { - value = compiler.compile(getName()); - } - break; - /* - * Type: pi, e, true, false, Avogadro - */ - case CONSTANT_PI : - value = compiler.getConstantPi(); - break; - case CONSTANT_E : - value = compiler.getConstantE(); - break; - case CONSTANT_TRUE : - value = compiler.getConstantTrue(); - break; - case CONSTANT_FALSE : - value = compiler.getConstantFalse(); - break; - case NAME_AVOGADRO : - value = compiler.getConstantAvogadro(getName()); - break; - case REAL_E : - value = compiler.compile(getMantissa(), getExponent(), isSetUnits() - ? getUnits() - : null); - break; - /* - * Basic Functions - */ - case FUNCTION_LOG : - if (getChildCount() == 2) { - value = compiler.log(getLeftChild(), getRightChild()); - } else { - value = compiler.log(getRightChild()); - } - break; - case FUNCTION_ABS : - value = compiler.abs(getRightChild()); - break; - case FUNCTION_ARCCOS : - value = compiler.arccos(getLeftChild()); - break; - case FUNCTION_ARCCOSH : - value = compiler.arccosh(getLeftChild()); - break; - case FUNCTION_ARCCOT : - value = compiler.arccot(getLeftChild()); - break; - case FUNCTION_ARCCOTH : - value = compiler.arccoth(getLeftChild()); - break; - case FUNCTION_ARCCSC : - value = compiler.arccsc(getLeftChild()); - break; - case FUNCTION_ARCCSCH : - value = compiler.arccsch(getLeftChild()); - break; - case FUNCTION_ARCSEC : - value = compiler.arcsec(getLeftChild()); - break; - case FUNCTION_ARCSECH : - value = compiler.arcsech(getLeftChild()); - break; - case FUNCTION_ARCSIN : - value = compiler.arcsin(getLeftChild()); - break; -... [truncated message content] |