From: <nik...@us...> - 2015-10-05 10:00:45
|
Revision: 2364 http://sourceforge.net/p/jsbml/code/2364 Author: niko-rodrigue Date: 2015-10-05 10:00:41 +0000 (Mon, 05 Oct 2015) Log Message: ----------- merged rev 2361 Revision Links: -------------- http://sourceforge.net/p/jsbml/code/2361 Modified Paths: -------------- branches/astnode2-merging-alternate/extensions/arrays/src/org/sbml/jsbml/ext/arrays/compiler/VectorCompiler.java branches/astnode2-merging-alternate/extensions/arrays/src/org/sbml/jsbml/ext/arrays/flattening/ArraysFlattening.java Property Changed: ---------------- branches/astnode2-merging-alternate/ Index: branches/astnode2-merging-alternate =================================================================== --- branches/astnode2-merging-alternate 2015-10-05 09:08:27 UTC (rev 2363) +++ branches/astnode2-merging-alternate 2015-10-05 10:00:41 UTC (rev 2364) Property changes on: branches/astnode2-merging-alternate ___________________________________________________________________ Modified: svn:mergeinfo ## -1 +1 ## -/trunk:2191,2194-2195,2197-2200,2202-2206,2209-2213,2216-2220,2222,2224-2244,2246-2248,2251-2262,2280,2293,2296-2297,2301-2305,2307-2310,2313,2318,2320-2321,2325-2326,2332-2333,2343,2348-2352,2354-2359 +/trunk:2191,2194-2195,2197-2200,2202-2206,2209-2213,2216-2220,2222,2224-2244,2246-2248,2251-2262,2280,2293,2296-2297,2301-2305,2307-2310,2313,2318,2320-2321,2325-2326,2332-2333,2343,2348-2352,2354-2359,2361 \ No newline at end of property Modified: branches/astnode2-merging-alternate/extensions/arrays/src/org/sbml/jsbml/ext/arrays/compiler/VectorCompiler.java =================================================================== --- branches/astnode2-merging-alternate/extensions/arrays/src/org/sbml/jsbml/ext/arrays/compiler/VectorCompiler.java 2015-10-05 09:08:27 UTC (rev 2363) +++ branches/astnode2-merging-alternate/extensions/arrays/src/org/sbml/jsbml/ext/arrays/compiler/VectorCompiler.java 2015-10-05 10:00:41 UTC (rev 2364) @@ -1,4922 +1,4925 @@ -/* - * $Id$ - * $URL$ - * ---------------------------------------------------------------------------- - * 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-2015 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 - * 6. The University of Utah, Salt Lake City, UT, USA - * - * 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.ext.arrays.compiler; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - -import org.sbml.jsbml.ASTNode; -import org.sbml.jsbml.CallableSBase; -import org.sbml.jsbml.Compartment; -import org.sbml.jsbml.FunctionDefinition; -import org.sbml.jsbml.InitialAssignment; -import org.sbml.jsbml.Model; -import org.sbml.jsbml.NamedSBase; -import org.sbml.jsbml.Parameter; -import org.sbml.jsbml.Quantity; -import org.sbml.jsbml.SBMLException; -import org.sbml.jsbml.SBase; -import org.sbml.jsbml.SpeciesReference; -import org.sbml.jsbml.ext.arrays.ArraysConstants; -import org.sbml.jsbml.ext.arrays.ArraysSBasePlugin; -import org.sbml.jsbml.ext.arrays.Dimension; -import org.sbml.jsbml.ext.arrays.util.ArraysMath; -import org.sbml.jsbml.util.Maths; -import org.sbml.jsbml.util.compilers.ASTNodeCompiler; -import org.sbml.jsbml.util.compilers.ASTNodeValue; - -/** - * @author Leandro Watanabe - * @version $Rev$ - * @since 1.0 - * @date Jun 30, 2014 - */ -//TODO: need to check for errors -//TODO: need to check if its name -//TODO: SBMLException error id -//TODO: need to test functions. might have warnings -public class VectorCompiler implements ASTNodeCompiler { - - /** - * - */ - private final ASTNodeValue dummy = new ASTNodeValue("dummy", null); - /** - * - */ - private final Model model; - /** - * - */ - private ASTNode node; - /** - * - */ - private Map<String, ASTNode> idToVector; - /** - * - */ - private boolean isSetIdToVector; - /** - * - */ - private final boolean useId; - /** - * - */ - private NamedSBase sbase; - - /** - * @param model - */ - public VectorCompiler(Model model) { - this.model = model; - node = new ASTNode(); - useId = false; - isSetIdToVector = false; - } - - /** - * @param model - * @param useId - */ - public VectorCompiler(Model model, boolean useId) { - this.model = model; - node = new ASTNode(); - this.useId = useId; - isSetIdToVector = false; - - } - - /** - * @param model - * @param sbase - * @param useId - */ - public VectorCompiler(Model model, NamedSBase sbase, boolean useId) { - this.model = model; - node = new ASTNode(); - this.useId = useId; - this.sbase = sbase; - isSetIdToVector = false; - } - - - - /** - * @param model - * @param useId - * @param idToVector - */ - public VectorCompiler(Model model, boolean useId, Map<String, ASTNode> idToVector) { - this.model = model; - node = new ASTNode(); - this.useId = useId; - this.idToVector = idToVector; - if (idToVector == null) { - isSetIdToVector = false; - } else { - isSetIdToVector = true; - } - } - - /** - * @return - */ - public ASTNode getNode() { - return node; - } - - /** - * @param node - */ - public void setNode(ASTNode node) { - this.node = node; - } - - /** - * @param idToVector - */ - public void setIdToVector(Map<String, ASTNode> idToVector) { - this.idToVector = idToVector; - if (idToVector == null) { - isSetIdToVector = false; - } else { - isSetIdToVector = true; - } - } - - /** - * - */ - public void clearIdToVector() { - idToVector.clear(); - isSetIdToVector = false; - } - - /** - * @return - */ - public boolean isSetIdToVector() { - if (idToVector != null && isSetIdToVector) { - return true; - } else { - return false; - } - } - - /* (non-Javadoc) - * @see org.sbml.jsbml.util.compilers.ASTNodeCompiler#abs(org.sbml.jsbml.ASTNode) - */ - @Override - public ASTNodeValue abs(ASTNode value) throws SBMLException { - value.compile(this); - ASTNode compiled = getNode(); - if (compiled.isVector()) { - try{ - absRecursive(compiled); - } - catch(SBMLException e) { - unknownValue(); - return dummy; - } - } - else if (useId) { - if (compiled.toString().equals("unknown")) { - compiled.setName("unknown"); - } else { - ASTNode nodeValue = new ASTNode(ASTNode.Type.FUNCTION_ABS, compiled.getParentSBMLObject()); - nodeValue.addChild(compiled); - setNode(nodeValue); - } - return dummy; - } - else if (compiled.isNumber()) { - compiled.setValue(Math.abs(compiled.getReal())); - } - else { - compiled.setName("unknown"); - } - setNode(compiled); - - return dummy; - } - - /** - * This function calculates the abs value of a vector element-wise. - * - * @param value - * @throws SBMLException - */ - private void absRecursive(ASTNode value) throws SBMLException{ - for (int i = 0; i < value.getChildCount(); i++) { - value.getChild(i).compile(this); - ASTNode child = getNode(); - if (child.isVector()) { - absRecursive(value.getChild(i)); - } else if (useId){ - ASTNode absValue = new ASTNode(ASTNode.Type.FUNCTION_ABS, child.getParentSBMLObject()); - absValue.addChild(child.clone()); - value.replaceChild(i, absValue); - } else if (child.isNumber()) { - value.getChild(i).setValue(Math.abs(child.getReal())); - } else { - throw new SBMLException(); - } - } - } - - /* (non-Javadoc) - * @see org.sbml.jsbml.util.compilers.ASTNodeCompiler#and(java.util.List) - */ - @Override - public ASTNodeValue and(List<ASTNode> values) throws SBMLException { - - List<ASTNode> vectors = new ArrayList<ASTNode>(); - List<ASTNode> scalars = new ArrayList<ASTNode>(); - List<ASTNode> ids = new ArrayList<ASTNode>(); - boolean success = getScalarsAndVectors(values, vectors, scalars, ids); - - if (!success) { - unknownValue(); - return dummy; - } - double sumScalar; - boolean isSetSumScalar = false; - boolean hasIds = false; - - if (scalars.size() > 0) { - isSetSumScalar = true; - } - if (ids.size() > 0) { - hasIds = true; - } - - boolean scalarResult = true; - for (ASTNode node : scalars) { - scalarResult &= node.getReal() == 1 ? true : false; - } - sumScalar = scalarResult ? 1 : 0; - ASTNode out = new ASTNode(sumScalar); - if (!vectors.isEmpty()) { - out = vectors.get(0).clone(); - try { - andRecursive(vectors, out); - } - catch(IndexOutOfBoundsException e) { - unknownValue(); - return dummy; - } - if (isSetSumScalar) { - scalarVectorAnd(out, new ASTNode(sumScalar)); - } - if (hasIds) { - for (ASTNode id : ids) { - scalarVectorAnd(out, id); - } - } - } - else { - if (hasIds) { - ASTNode result = new ASTNode(ASTNode.Type.LOGICAL_AND, out.getParentSBMLObject()); - if (!ids.isEmpty()) { - for (int i = 0; i < ids.size(); ++i) { - result.addChild(ids.get(i).clone()); - } - } - out = result; - } - if (isSetSumScalar) { - out.addChild(new ASTNode(sumScalar)); - } - } - - setNode(out); - return dummy; - } - - /** - * - * @param values - * @param node - * @throws IndexOutOfBoundsException - */ - private void andRecursive(List<ASTNode> values, ASTNode node) throws IndexOutOfBoundsException{ - if (!node.isVector()) { - boolean result = true; - boolean isResultSet = false; - node.setType(ASTNode.Type.LOGICAL_AND); - node.getChildren().clear(); - if (values.size() > 0) { - for (int i = 0; i < values.size(); ++i) { - ASTNode value = values.get(i); - if (value.isNumber()) { - if (!isResultSet) { - result = value.getInteger() == 1; - isResultSet = true; - } else { - result &= value.getInteger() == 1 ? true : false; - } - } else if (useId) { - node.addChild(values.get(i).clone()); - } else { - throw new SBMLException(); - } - } - if (isResultSet) { - node.addChild(new ASTNode(result ? 1 : 0)); - } - return; - } - } - for (int i = 0; i < node.getChildCount(); ++i) { - List<ASTNode> nodes = new ArrayList<ASTNode>(); - for (ASTNode value : values) { - value.compile(this); - ASTNode result = getNode(); - if (result.isVector()) { - nodes.add(result.getChild(i)); - } - } - andRecursive(nodes, node.getChild(i)); - } - - } - - /** - * - * @param vector - * @param scalar - */ - private void scalarVectorAnd(ASTNode vector, ASTNode scalar) { - for (int i = 0; i < vector.getChildCount(); ++i) { - ASTNode child = vector.getChild(i); - child.compile(this); - ASTNode result = getNode(); - if (result.isVector()) { - scalarVectorAnd(child, scalar); - } - else if (useId) { - ASTNode nodeValue = new ASTNode(ASTNode.Type.LOGICAL_AND, vector.getChild(i).getParentSBMLObject()); - nodeValue.addChild(result.clone()); - nodeValue.addChild(scalar.clone()); - vector.replaceChild(i, nodeValue); - } - else if (result.isNumber()) { - boolean resBool = result.getReal() == 1 ? true : false; - boolean scalBool = scalar.getReal() == 1 ? true : false; - vector.getChild(i).setValue(resBool & scalBool ? 1 : 0); - } - else { - throw new SBMLException(); - } - } - } - - /* (non-Javadoc) - * @see org.sbml.jsbml.util.compilers.ASTNodeCompiler#arccos(org.sbml.jsbml.ASTNode) - */ - @Override - public ASTNodeValue arccos(ASTNode value) throws SBMLException { - value.compile(this); - ASTNode compiled = getNode(); - if (compiled.isVector()) { - try { - arccosRecursive(compiled); - } - catch(SBMLException e) { - unknownValue(); - return dummy; - } - } - else if (useId) { - if (compiled.toString().equals("unknown")) { - compiled.setName("unknown"); - } else { - ASTNode nodeValue = new ASTNode(ASTNode.Type.FUNCTION_ARCCOS, compiled.getParentSBMLObject()); - nodeValue.addChild(compiled); - setNode(nodeValue); - } - return dummy; - } - else if (compiled.isNumber()) { - compiled.setValue(Math.acos(compiled.getReal())); - } - else { - compiled.setName("unknown"); - } - setNode(compiled); - - return dummy; - } - - /** - * This function calculates the arcccos value of a vector element-wise. - * - * @param value - * @throws SBMLException - */ - private void arccosRecursive(ASTNode value) { - for (int i = 0; i < value.getChildCount(); i++) { - value.getChild(i).compile(this); - ASTNode child = getNode(); - if (child.isVector()) { - arccosRecursive(value.getChild(i)); - } else if (useId){ - ASTNode arccosValue = new ASTNode(ASTNode.Type.FUNCTION_ARCCOS, child.getParentSBMLObject()); - arccosValue.addChild(child.clone()); - value.replaceChild(i, arccosValue); - } else if (child.isNumber()) { - value.getChild(i).setValue(Math.acos(child.getReal())); - } else { - throw new SBMLException(); - } - } - } - - /* (non-Javadoc) - * @see org.sbml.jsbml.util.compilers.ASTNodeCompiler#arccosh(org.sbml.jsbml.ASTNode) - */ - @Override - public ASTNodeValue arccosh(ASTNode value) throws SBMLException { - value.compile(this); - ASTNode compiled = getNode(); - if (compiled.isVector()) { - try { - arccoshRecursive(compiled); - } - catch(SBMLException e) { - unknownValue(); - return dummy; - } - } - else if (useId) { - if (compiled.toString().equals("unknown")) { - compiled.setName("unknown"); - } else { - compiled.setName("arccosh(" + compiled.getName() + ")"); - } - } - else if (compiled.isNumber()) { - compiled.setValue(Maths.arccosh(compiled.getReal())); - } - else { - compiled.setName("unknown"); - } - setNode(compiled); - - return dummy; - } - - /** - * @param value - * @throws SBMLException - */ - private void arccoshRecursive(ASTNode value) throws SBMLException { - for (int i = 0; i < value.getChildCount(); i++) { - value.getChild(i).compile(this); - ASTNode child = getNode(); - if (child.isVector()) { - arccoshRecursive(value.getChild(i)); - } else if (useId){ - ASTNode arccoshValue = new ASTNode(ASTNode.Type.FUNCTION_ARCCOSH, child.getParentSBMLObject()); - arccoshValue.addChild(child.clone()); - value.replaceChild(i, arccoshValue); - } else if (child.isNumber()) { - value.getChild(i).setValue(Maths.arccosh(child.getReal())); - } else { - throw new SBMLException(); - } - } - } - - - /* (non-Javadoc) - * @see org.sbml.jsbml.util.compilers.ASTNodeCompiler#arccot(org.sbml.jsbml.ASTNode) - */ - @Override - public ASTNodeValue arccot(ASTNode value) throws SBMLException { - value.compile(this); - ASTNode compiled = getNode(); - if (compiled.isVector()) { - try { - arccotRecursive(compiled); - } - catch(SBMLException e) { - unknownValue(); - return dummy; - } - } - else if (useId) { - if (compiled.toString().equals("unknown")) { - compiled.setName("unknown"); - } else { - compiled.setName("arccot(" + compiled.getName() + ")"); - } - } - else if (compiled.isNumber()) { - compiled.setValue(Maths.arccot(compiled.getReal())); - } - else { - compiled.setName("unknown"); - } - setNode(compiled); - - return dummy; - } - - /** - * @param value - */ - private void arccotRecursive(ASTNode value) { - for (int i = 0; i < value.getChildCount(); i++) { - value.getChild(i).compile(this); - ASTNode child = getNode(); - if (child.isVector()) { - arccotRecursive(value.getChild(i)); - } else if (useId){ - ASTNode arccotValue = new ASTNode(ASTNode.Type.FUNCTION_ARCCOT, child.getParentSBMLObject()); - arccotValue.addChild(child.clone()); - value.replaceChild(i, arccotValue); - } else if (child.isNumber()) { - value.getChild(i).setValue(Maths.arccot(child.getReal())); - } else { - throw new SBMLException(); - } - } - } - - /* (non-Javadoc) - * @see org.sbml.jsbml.util.compilers.ASTNodeCompiler#arccoth(org.sbml.jsbml.ASTNode) - */ - @Override - public ASTNodeValue arccoth(ASTNode value) throws SBMLException { - value.compile(this); - ASTNode compiled = getNode(); - if (compiled.isVector()) { - try { - arccothRecursive(compiled); - } - catch(SBMLException e) { - unknownValue(); - return dummy; - } - } - else if (useId) { - if (compiled.toString().equals("unknown")) { - compiled.setName("unknown"); - } else { - ASTNode nodeValue = new ASTNode(ASTNode.Type.FUNCTION_ARCCOTH, compiled.getParentSBMLObject()); - nodeValue.addChild(compiled); - setNode(nodeValue); - } - return dummy; - } - else if (compiled.isNumber()) { - compiled.setValue(Maths.arccoth(compiled.getReal())); - } - else { - compiled.setName("unknown"); - } - setNode(compiled); - - return dummy; - } - - /** - * @param value - */ - private void arccothRecursive(ASTNode value) { - for (int i = 0; i < value.getChildCount(); i++) { - value.getChild(i).compile(this); - ASTNode child = getNode(); - if (child.isVector()) { - arccothRecursive(value.getChild(i)); - } else if (useId){ - ASTNode arccothValue = new ASTNode(ASTNode.Type.FUNCTION_ARCCOTH, child.getParentSBMLObject()); - arccothValue.addChild(child.clone()); - value.replaceChild(i, arccothValue); - } else if (child.isNumber()) { - value.getChild(i).setValue(Maths.arccoth(child.getReal())); - } else { - throw new SBMLException(); - } - } - } - - /* (non-Javadoc) - * @see org.sbml.jsbml.util.compilers.ASTNodeCompiler#arccsc(org.sbml.jsbml.ASTNode) - */ - @Override - public ASTNodeValue arccsc(ASTNode value) throws SBMLException { - value.compile(this); - ASTNode compiled = getNode(); - if (compiled.isVector()) { - try { - arccscRecursive(compiled); - } - catch(SBMLException e) { - unknownValue(); - return dummy; - } - } - else if (useId) { - if (compiled.toString().equals("unknown")) { - compiled.setName("unknown"); - } else { - ASTNode nodeValue = new ASTNode(ASTNode.Type.FUNCTION_ARCCSC, compiled.getParentSBMLObject()); - nodeValue.addChild(compiled); - setNode(nodeValue); - } - return dummy; - } - else if (compiled.isNumber()) { - compiled.setValue(Maths.arccsc(compiled.getReal())); - } - else { - compiled.setName("unknown"); - } - setNode(compiled); - - return dummy; - } - - /** - * @param value - */ - private void arccscRecursive(ASTNode value) { - for (int i = 0; i < value.getChildCount(); i++) { - value.getChild(i).compile(this); - ASTNode child = getNode(); - if (child.isVector()) { - arccscRecursive(value.getChild(i)); - } else if (useId){ - ASTNode arccscValue = new ASTNode(ASTNode.Type.FUNCTION_ARCCSC, child.getParentSBMLObject()); - arccscValue.addChild(child.clone()); - value.replaceChild(i, arccscValue); - } else if (child.isNumber()) { - value.getChild(i).setValue(Maths.arccsc(child.getReal())); - } else { - throw new SBMLException(); - } - } - } - - /* (non-Javadoc) - * @see org.sbml.jsbml.util.compilers.ASTNodeCompiler#arccsch(org.sbml.jsbml.ASTNode) - */ - @Override - public ASTNodeValue arccsch(ASTNode value) throws SBMLException { - value.compile(this); - ASTNode compiled = getNode(); - if (compiled.isVector()) { - try { - arccschRecursive(compiled); - } - catch(SBMLException e) { - unknownValue(); - return dummy; - } - } - else if (useId) { - if (compiled.toString().equals("unknown")) { - compiled.setName("unknown"); - } else { - ASTNode nodeValue = new ASTNode(ASTNode.Type.FUNCTION_ARCCSCH, compiled.getParentSBMLObject()); - nodeValue.addChild(compiled); - setNode(nodeValue); - } - return dummy; - } - else if (compiled.isNumber()) { - compiled.setValue(Maths.arccsch(compiled.getReal())); - } - else { - compiled.setName("unknown"); - } - setNode(compiled); - - return dummy; - } - - /** - * @param value - */ - private void arccschRecursive(ASTNode value) { - for (int i = 0; i < value.getChildCount(); i++) { - value.getChild(i).compile(this); - ASTNode child = getNode(); - if (child.isVector()) { - arccschRecursive(value.getChild(i)); - } else if (useId){ - ASTNode arccschValue = new ASTNode(ASTNode.Type.FUNCTION_ARCCSCH, child.getParentSBMLObject()); - arccschValue.addChild(child.clone()); - value.replaceChild(i, arccschValue); - } else if (child.isNumber()) { - value.getChild(i).setValue(Maths.arccsch(child.getReal())); - } else{ - throw new SBMLException(); - } - } - } - - /* (non-Javadoc) - * @see org.sbml.jsbml.util.compilers.ASTNodeCompiler#arcsec(org.sbml.jsbml.ASTNode) - */ - @Override - public ASTNodeValue arcsec(ASTNode value) throws SBMLException { - value.compile(this); - ASTNode compiled = getNode(); - if (compiled.isVector()) { - try { - arcsecRecursive(compiled); - } - catch(SBMLException e) { - unknownValue(); - return dummy; - } - } else if (useId) { - if (compiled.toString().equals("unknown")) { - compiled.setName("unknown"); - } else { - ASTNode nodeValue = new ASTNode(ASTNode.Type.FUNCTION_ARCSEC, compiled.getParentSBMLObject()); - nodeValue.addChild(compiled); - setNode(nodeValue); - } - return dummy; - } else if (compiled.isNumber()) { - compiled.setValue(Maths.arcsec(compiled.getReal())); - } else { - compiled.setName("unknown"); - } - setNode(compiled); - - return dummy; - } - - /** - * @param value - */ - private void arcsecRecursive(ASTNode value) { - for (int i = 0; i < value.getChildCount(); i++) { - value.getChild(i).compile(this); - ASTNode child = getNode(); - if (child.isVector()) { - arcsecRecursive(value.getChild(i)); - } else if (useId){ - ASTNode arcsecValue = new ASTNode(ASTNode.Type.FUNCTION_ARCSEC, child.getParentSBMLObject()); - arcsecValue.addChild(child.clone()); - value.replaceChild(i, arcsecValue); - } else if (child.isNumber()) { - value.getChild(i).setValue(Maths.arcsec(child.getReal())); - } else { - throw new SBMLException(); - } - } - } - - /* (non-Javadoc) - * @see org.sbml.jsbml.util.compilers.ASTNodeCompiler#arcsech(org.sbml.jsbml.ASTNode) - */ - @Override - public ASTNodeValue arcsech(ASTNode value) throws SBMLException { - value.compile(this); - ASTNode compiled = getNode(); - if (compiled.isVector()) { - try { - arcsechRecursive(compiled); - } - catch(SBMLException e) { - unknownValue(); - return dummy; - } - } - else if (useId) { - if (compiled.toString().equals("unknown")) { - compiled.setName("unknown"); - } else { - ASTNode nodeValue = new ASTNode(ASTNode.Type.FUNCTION_ARCSECH, compiled.getParentSBMLObject()); - nodeValue.addChild(compiled); - setNode(nodeValue); - } - return dummy; - } - else if (compiled.isNumber()) { - compiled.setValue(Maths.arcsech(compiled.getReal())); - } - - else { - compiled.setName("unknown"); - } - setNode(compiled); - - return dummy; - } - - /** - * @param value - */ - private void arcsechRecursive(ASTNode value) { - for (int i = 0; i < value.getChildCount(); i++) { - value.getChild(i).compile(this); - ASTNode child = getNode(); - if (child.isVector()) { - arcsechRecursive(value.getChild(i)); - } else if (useId){ - ASTNode arcsechValue = new ASTNode(ASTNode.Type.FUNCTION_ARCSECH, child.getParentSBMLObject()); - arcsechValue.addChild(child.clone()); - value.replaceChild(i, arcsechValue); - } else if (child.isNumber()) { - value.getChild(i).setValue(Maths.arcsech(child.getReal())); - } else { - throw new SBMLException(); - } - } - } - - /* (non-Javadoc) - * @see org.sbml.jsbml.util.compilers.ASTNodeCompiler#arcsin(org.sbml.jsbml.ASTNode) - */ - @Override - public ASTNodeValue arcsin(ASTNode value) throws SBMLException { - value.compile(this); - ASTNode compiled = getNode(); - if (compiled.isVector()) { - try { - arcsinRecursive(compiled); - } - catch(SBMLException e) { - unknownValue(); - return dummy; - } - } - else if (useId) { - if (compiled.toString().equals("unknown")) { - compiled.setName("unknown"); - } else { - ASTNode nodeValue = new ASTNode(ASTNode.Type.FUNCTION_ARCSIN, compiled.getParentSBMLObject()); - nodeValue.addChild(compiled); - setNode(nodeValue); - } - return dummy; - } - else if (compiled.isNumber()) { - compiled.setValue(Math.asin(compiled.getReal())); - } - - else { - compiled.setName("unknown"); - } - setNode(compiled); - - return dummy; - } - - /** - * @param value - */ - private void arcsinRecursive(ASTNode value) { - for (int i = 0; i < value.getChildCount(); i++) { - value.getChild(i).compile(this); - ASTNode child = getNode(); - if (child.isVector()) { - arcsinRecursive(value.getChild(i)); - } else if (useId){ - ASTNode arcsinValue = new ASTNode(ASTNode.Type.FUNCTION_ARCSIN, child.getParentSBMLObject()); - arcsinValue.addChild(child.clone()); - value.replaceChild(i, arcsinValue); - } else if (child.isNumber()) { - value.getChild(i).setValue(Math.asin(child.getReal())); - } else { - throw new SBMLException(); - } - } - } - - /* (non-Javadoc) - * @see org.sbml.jsbml.util.compilers.ASTNodeCompiler#arcsinh(org.sbml.jsbml.ASTNode) - */ - @Override - public ASTNodeValue arcsinh(ASTNode value) throws SBMLException { - value.compile(this); - ASTNode compiled = getNode(); - if (compiled.isVector()) { - arcsinhRecursive(compiled); - } - else if (useId) { - if (compiled.toString().equals("unknown")) { - compiled.setName("unknown"); - } else { - ASTNode nodeValue = new ASTNode(ASTNode.Type.FUNCTION_ARCSINH, compiled.getParentSBMLObject()); - nodeValue.addChild(compiled); - setNode(nodeValue); - } - return dummy; - } - else if (compiled.isNumber()) { - compiled.setValue(Maths.arcsinh(compiled.getReal())); - } - else { - compiled.setName("unknown"); - } - setNode(compiled); - - return dummy; - } - - /** - * @param value - */ - private void arcsinhRecursive(ASTNode value) { - for (int i = 0; i < value.getChildCount(); i++) { - value.getChild(i).compile(this); - ASTNode child = getNode(); - if (child.isVector()) { - arcsinhRecursive(value.getChild(i)); - } else if (useId){ - ASTNode arcsinhValue = new ASTNode(ASTNode.Type.FUNCTION_ARCSINH, child.getParentSBMLObject()); - arcsinhValue.addChild(child.clone()); - value.replaceChild(i, arcsinhValue); - } else if (child.isNumber()) { - value.getChild(i).setValue(Maths.arcsinh(child.getReal())); - } else { - throw new SBMLException(); - } - } - } - - /* (non-Javadoc) - * @see org.sbml.jsbml.util.compilers.ASTNodeCompiler#arctan(org.sbml.jsbml.ASTNode) - */ - @Override - public ASTNodeValue arctan(ASTNode value) throws SBMLException { - value.compile(this); - ASTNode compiled = getNode(); - if (compiled.isVector()) { - try { - arctanRecursive(compiled); - } - catch(SBMLException e) { - unknownValue(); - return dummy; - } - } - else if (useId) { - if (compiled.toString().equals("unknown")) { - compiled.setName("unknown"); - } else { - ASTNode nodeValue = new ASTNode(ASTNode.Type.FUNCTION_ARCTAN, compiled.getParentSBMLObject()); - nodeValue.addChild(compiled); - setNode(nodeValue); - } - return dummy; - } - else if (compiled.isNumber()) { - compiled.setValue(Math.atan(compiled.getReal())); - } - else { - compiled.setName("unknown"); - } - setNode(compiled); - - return dummy; - } - - /** - * @param value - */ - private void arctanRecursive(ASTNode value) { - for (int i = 0; i < value.getChildCount(); i++) { - value.getChild(i).compile(this); - ASTNode child = getNode(); - if (child.isVector()) { - arctanRecursive(value.getChild(i)); - } else if (useId){ - ASTNode arctanValue = new ASTNode(ASTNode.Type.FUNCTION_ARCTAN, child.getParentSBMLObject()); - arctanValue.addChild(child.clone()); - value.replaceChild(i, arctanValue); - } else if (child.isNumber()) { - value.getChild(i).setValue(Math.atan(child.getReal())); - } else { - throw new SBMLException(); - } - } - } - - /* (non-Javadoc) - * @see org.sbml.jsbml.util.compilers.ASTNodeCompiler#arctanh(org.sbml.jsbml.ASTNode) - */ - @Override - public ASTNodeValue arctanh(ASTNode value) throws SBMLException { - value.compile(this); - ASTNode compiled = getNode(); - if (compiled.isVector()) { - try { - arctanhRecursive(compiled); - } - catch(SBMLException e) { - unknownValue(); - return dummy; - } - } - else if (useId) { - if (compiled.toString().equals("unknown")) { - compiled.setName("unknown"); - } else { - ASTNode nodeValue = new ASTNode(ASTNode.Type.FUNCTION_ARCTANH, compiled.getParentSBMLObject()); - nodeValue.addChild(compiled); - setNode(nodeValue); - } - return dummy; - } - else if (compiled.isNumber()) { - compiled.setValue(Maths.arctanh(compiled.getReal())); - } - else { - compiled.setName("unknown"); - } - setNode(compiled); - - return dummy; - } - - /** - * @param value - */ - private void arctanhRecursive(ASTNode value) { - for (int i = 0; i < value.getChildCount(); i++) { - value.getChild(i).compile(this); - ASTNode child = getNode(); - if (child.isVector()) { - arctanhRecursive(value.getChild(i)); - } else if (useId){ - ASTNode arctanhValue = new ASTNode(ASTNode.Type.FUNCTION_ARCTANH, child.getParentSBMLObject()); - arctanhValue.addChild(child.clone()); - value.replaceChild(i, arctanhValue); - } else if (child.isNumber()) { - value.getChild(i).setValue(Maths.arctanh(child.getReal())); - } else { - throw new SBMLException(); - } - } - } - - /* (non-Javadoc) - * @see org.sbml.jsbml.util.compilers.ASTNodeCompiler#ceiling(org.sbml.jsbml.ASTNode) - */ - @Override - public ASTNodeValue ceiling(ASTNode value) throws SBMLException { - value.compile(this); - ASTNode compiled = getNode(); - if (compiled.isVector()) { - try { - ceilingRecursive(compiled); - } - catch(SBMLException e) { - unknownValue(); - return dummy; - } - } - else if (useId) { - if (compiled.toString().equals("unknown")) { - compiled.setName("unknown"); - } else { - ASTNode nodeValue = new ASTNode(ASTNode.Type.FUNCTION_CEILING, compiled.getParentSBMLObject()); - nodeValue.addChild(compiled); - setNode(nodeValue); - } - return dummy; - } - else if (compiled.isNumber()) { - compiled.setValue(Math.ceil(compiled.getReal())); - } - else { - compiled.setName("unknown"); - } - setNode(compiled); - - return dummy; - } - - /** - * @param value - */ - private void ceilingRecursive(ASTNode value) { - for (int i = 0; i < value.getChildCount(); i++) { - value.getChild(i).compile(this); - ASTNode child = getNode(); - if (child.isVector()) { - ceilingRecursive(value.getChild(i)); - } else if (useId){ - ASTNode ceilValue = new ASTNode(ASTNode.Type.FUNCTION_CEILING, child.getParentSBMLObject()); - ceilValue.addChild(child.clone()); - value.replaceChild(i, ceilValue); - } else if (child.isNumber()) { - value.getChild(i).setValue(Math.ceil(child.getReal())); - } else { - throw new SBMLException(); - } - } - } - - /* (non-Javadoc) - * @see org.sbml.jsbml.util.compilers.ASTNodeCompiler#compile(org.sbml.jsbml.Compartment) - */ - @Override - public ASTNodeValue compile(Compartment c) { - if (useId) { - transformNamedSBase(c); - } else { - transformSBase(c); - } - return dummy; - } - - /* (non-Javadoc) - * @see org.sbml.jsbml.util.compilers.ASTNodeCompiler#compile(double, int, java.lang.String) - */ - @Override - public ASTNodeValue compile(double mantissa, int exponent, String units) { - ASTNode node = new ASTNode(); - node.setValue(mantissa, exponent); - setNode(node); - return dummy; - } - - /* (non-Javadoc) - * @see org.sbml.jsbml.util.compilers.ASTNodeCompiler#compile(double, java.lang.String) - */ - @Override - public ASTNodeValue compile(double real, String units) { - setNode(new ASTNode(real)); - return dummy; - } - - /* (non-Javadoc) - * @see org.sbml.jsbml.util.compilers.ASTNodeCompiler#compile(int, java.lang.String) - */ - @Override - public ASTNodeValue compile(int integer, String units) { - setNode(new ASTNode(integer)); - return dummy; - } - - /* (non-Javadoc) - * @see org.sbml.jsbml.util.compilers.ASTNodeCompiler#compile(org.sbml.jsbml.CallableSBase) - */ - @Override - public ASTNodeValue compile(CallableSBase variable) throws SBMLException { - if (useId) { - transformNamedSBase(variable); - } - else { - transformSBase(variable); - } - return dummy; - } - - - /* (non-Javadoc) - * @see org.sbml.jsbml.util.compilers.ASTNodeCompiler#compile(java.lang.String) - */ - @Override - public ASTNodeValue compile(String name) { - - if(isSetIdToVector() && idToVector.containsKey(name)) - { - setNode(idToVector.get(name)); - } - else - { - SBase sbase = model.findNamedSBase(name); - if (sbase != null) { - if (useId) { - if (isSetIdToVector() && idToVector.containsKey(name)) { - setNode(idToVector.get(name)); - } else { - transformNamedSBase(sbase); - } - } else { - transformSBase(sbase); - } - } - else if (this.sbase != null && this.sbase.getId().equals(name)) { - if (useId) { - if (isSetIdToVector() && idToVector.containsKey(name)) { - setNode(idToVector.get(name)); - } else { - transformNamedSBase(this.sbase); - } - } else { - transformSBase(this.sbase); - } - } - else - { - if (useId) { - setNode(new ASTNode(name)); - } - else { - unknownValue(); - } - } - } - return dummy; - } - - /** - * @param sbase - */ - private void transformNamedSBase(SBase sbase) { - ArraysSBasePlugin arraysPlugin = (ArraysSBasePlugin) sbase.getExtension(ArraysConstants.shortLabel); - if (arraysPlugin == null || arraysPlugin.getDimensionCount() == 0) { - if (sbase instanceof FunctionDefinition) { - FunctionDefinition namedSBase = (FunctionDefinition) sbase; - ASTNode func = new ASTNode(ASTNode.Type.FUNCTION); - func.setName(namedSBase.getId()); - setNode(func); - } - else if (sbase instanceof NamedSBase) { - NamedSBase namedSBase = (NamedSBase) sbase; - setNode(new ASTNode(namedSBase.getId())); - } - else { - unknownValue(); - } - } - else { - if (sbase instanceof SpeciesReference) { - SpeciesReference ref = (SpeciesReference) sbase; - setNode(constructVector(arraysPlugin, ref)); - - } - else if (sbase instanceof NamedSBase) { - NamedSBase namedSBase = (NamedSBase) sbase; - setNode(constructVector(arraysPlugin, namedSBase)); - } - else { - unknownValue(); - } - } - } - - /** - * @param arraysPlugin - * @param sbase - * @return - */ - private ASTNode constructVector(ArraysSBasePlugin arraysPlugin, NamedSBase sbase) { - String id = sbase.getId(); - Dimension dim = arraysPlugin.getDimensionByArrayDimension(0); - double size = ArraysMath.getSize(model, dim); - List<ASTNode> vector = new ArrayList<ASTNode>((int) size); - for (int i = 0; i < size; ++i) { - vector.add(new ASTNode("_" + i)); - } - vector(vector); - ASTNode vectorNode = getNode(); - for (int i = 1; i < arraysPlugin.getDimensionCount(); ++i) { - dim = arraysPlugin.getDimensionByArrayDimension(i); - size = ArraysMath.getSize(model, dim); - vector = new ArrayList<ASTNode>((int) size); - for (int j = 0; j < size; ++j) { - ASTNode clone = vectorNode.clone(); - updateASTNodeName(clone, j); - //TODO check if clone is unique - vector.add(clone); - } - vector(vector); - vectorNode = getNode(); - } - updateASTNodeName(vectorNode, id); - return vectorNode; - } - - /** - * @param arraysPlugin - * @param quantity - * @return - */ - private ASTNode constructVector(ArraysSBasePlugin arraysPlugin, Quantity quantity) { - double value = quantity.getValue(); - Dimension dim = arraysPlugin.getDimensionByArrayDimension(0); - Parameter p = model.getParameter(dim.getSize()); - double size = p.getValue(); - List<ASTNode> vector = new ArrayList<ASTNode>((int) size); - for (int j = 0; j < size; ++j) { - vector.add(new ASTNode(value)); - } - vector(vector); - ASTNode vectorNode = getNode(); - for (int i = 1; i < arraysPlugin.getDimensionCount(); ++i) { - dim = arraysPlugin.getDimensionByArrayDimension(i); - p = model.getParameter(dim.getSize()); - size = p.getValue(); - vector = new ArrayList<ASTNode>((int) size); - for (int j = 0; j < size; ++j) { - vector.add(vectorNode); - } - vector(vector); - vectorNode = getNode(); - } - - return vectorNode; - } - - /** - * @param arraysPlugin - * @param node - * @return - */ - private ASTNode constructVector(ArraysSBasePlugin arraysPlugin, ASTNode node) { - - Dimension dim = arraysPlugin.getDimensionByArrayDimension(0); - Parameter p = model.getParameter(dim.getSize()); - double size = p.getValue(); - List<ASTNode> vector = new ArrayList<ASTNode>((int) size); - for (int j = 0; j < size; ++j) { - vector.add(node.clone()); - } - vector(vector); - ASTNode vectorNode = getNode(); - for (int i = 1; i < arraysPlugin.getDimensionCount(); ++i) { - dim = arraysPlugin.getDimensionByArrayDimension(i); - p = model.getParameter(dim.getSize()); - size = p.getValue(); - vector = new ArrayList<ASTNode>((int) size); - for (int j = 0; j < size; ++j) { - vector.add(vectorNode); - } - vector(vector); - vectorNode = getNode(); - } - - return vectorNode; - } - - /* (non-Javadoc) - * @see org.sbml.jsbml.util.compilers.ASTNodeCompiler#cos(org.sbml.jsbml.ASTNode) - */ - @Override - public ASTNodeValue cos(ASTNode value) throws SBMLException { - value.compile(this); - ASTNode compiled = getNode(); - if (compiled.isVector()) { - try{ - cosRecursive(compiled); - } - catch(SBMLException e) { - unknownValue(); - return dummy; - } - } - else if (useId) { - if (compiled.toString().equals("unknown")) { - compiled.setName("unknown"); - } else { - ASTNode nodeValue = new ASTNode(ASTNode.Type.FUNCTION_COS, compiled.getParentSBMLObject()); - nodeValue.addChild(compiled); - setNode(nodeValue); - } - return dummy; - } - else if (compiled.isNumber()) { - compiled.setValue(Math.cos(compiled.getReal())); - } - - else { - compiled.setName("unknown"); - } - setNode(compiled); - - return dummy; - } - - /** - * @param value - */ - private void cosRecursive(ASTNode value) { - for (int i = 0; i < value.getChildCount(); i++) { - value.getChild(i).compile(this); - ASTNode child = getNode(); - if (child.isNumber()) { - value.getChild(i).setValue(Math.cos(child.getReal())); - } else if (child.isVector()) { - cosRecursive(value.getChild(i)); - } else if (useId){ - ASTNode cosValue = new ASTNode(ASTNode.Type.FUNCTION_COS, child.getParentSBMLObject()); - cosValue.addChild(child.clone()); - value.replaceChild(i, cosValue); - } else { - throw new SBMLException(); - } - } - } - - /* (non-Javadoc) - * @see org.sbml.jsbml.util.compilers.ASTNodeCompiler#cosh(org.sbml.jsbml.ASTNode) - */ - @Override - public ASTNodeValue cosh(ASTNode value) throws SBMLException { - value.compile(this); - ASTNode compiled = getNode(); - if (compiled.isVector()) { - try { - coshRecursive(compiled); - } - catch(SBMLException e) { - unknownValue(); - return dummy; - } - } - else if (useId) { - if (compiled.toString().equals("unknown")) { - compiled.setName("unknown"); - } else { - ASTNode nodeValue = new ASTNode(ASTNode.Type.FUNCTION_COSH, compiled.getParentSBMLObject()); - nodeValue.addChild(compiled); - setNode(nodeValue); - } - return dummy; - } - else if (compiled.isNumber()) { - compiled.setValue(Math.cosh(compiled.getReal())); - } - else { - compiled.setName("unknown"); - } - setNode(compiled); - - return dummy; - } - - /** - * @param value - */ - private void coshRecursive(ASTNode value) { - for (int i = 0; i < value.getChildCount(); i++) { - value.getChild(i).compile(this); - ASTNode child = getNode(); - if (child.isNumber()) { - value.getChild(i).setValue(Math.cosh(child.getReal())); - } else if (child.isVector()) { - coshRecursive(value.getChild(i)); - } else if (useId){ - ASTNode coshValue = new ASTNode(ASTNode.Type.FUNCTION_COSH, child.getParentSBMLObject()); - coshValue.addChild(child.clone()); - value.replaceChild(i, coshValue); - } else { - throw new SBMLException(); - } - } - } - - /* (non-Javadoc) - * @see org.sbml.jsbml.util.compilers.ASTNodeCompiler#cot(org.sbml.jsbml.ASTNode) - */ - @Override - public ASTNodeValue cot(ASTNode value) throws SBMLException { - value.compile(this); - ASTNode compiled = getNode(); - if (compiled.isVector()) { - try { - cotRecursive(compiled); - } - catch(SBMLException e) { - unknownValue(); - return dummy; - } - } - else if (useId) { - if (compiled.toString().equals("unknown")) { - compiled.setName("unknown"); - } else { - ASTNode nodeValue = new ASTNode(ASTNode.Type.FUNCTION_COT, compiled.getParentSBMLObject()); - nodeValue.addChild(compiled); - setNode(nodeValue); - } - return dummy; - } - else if (compiled.isNumber()) { - compiled.setValue(Maths.cot(compiled.getReal())); - } - else { - compiled.setName("unknown"); - } - setNode(compiled); - - return dummy; - } - - /** - * @param value - */ - private void cotRecursive(ASTNode value) { - for (int i = 0; i < value.getChildCount(); i++) { - value.getChild(i).compile(this); - ASTNode child = getNode(); - if (child.isNumber()) { - value.getChild(i).setValue(Maths.cot(child.getReal())); - } else if (child.isVector()) { - cotRecursive(value.getChild(i)); - } else if (useId){ - ASTNode cotValue = new ASTNode(ASTNode.Type.FUNCTION_COT, child.getParentSBMLObject()); - cotValue.addChild(child.clone()); - value.replaceChild(i, cotValue); - } else { - throw new SBMLException(); - } - } - } - - /* (non-Javadoc) - * @see org.sbml.jsbml.util.compilers.ASTNodeCompiler#coth(org.sbml.jsbml.ASTNode) - */ - @Override - public ASTNodeValue coth(ASTNode value) throws SBMLException { - value.compile(this); - ASTNode compiled = getNode(); - if (compiled.isVector()) { - try { - cothRecursive(compiled); - } - catch(SBMLException e) { - unknownValue(); - return dummy; - } - } - else if (useId) { - if (compiled.toString().equals("unknown")) { - compiled.setName("unknown"); - } else { - ASTNode nodeValue = new ASTNode(ASTNode.Type.FUNCTION_COTH, compiled.getParentSBMLObject()); - nodeValue.addChild(compiled); - setNode(nodeValue); - } - return dummy; - } - else if (compiled.isNumber()) { - compiled.setValue(Maths.coth(compiled.getReal())); - } - else { - compiled.setName("unknown"); - } - setNode(compiled); - - return dummy; - } - - /** - * @param value - */ - private void cothRecursive(ASTNode value) { - for (int i = 0; i < value.getChildCount(); i++) { - value.getChild(i).compile(this); - ASTNode child = getNode(); - if (child.isVector()) { - cothRecursive(value.getChild(i)); - } else if (useId){ - ASTNode cothValue = new ASTNode(ASTNode.Type.FUNCTION_COTH, child.getParentSBMLObject()); - cothValue.addChild(child.clone()); - value.replaceChild(i, cothValue); - } else if (child.isNumber()) { - value.getChild(i).setValue(Maths.coth(child.getReal())); - } else { - throw new SBMLException(); - } - } - } - - /* (non-Javadoc) - * @see org.sbml.jsbml.util.compilers.ASTNodeCompiler#csc(org.sbml.jsbml.ASTNode) - */ - @Override - public ASTNodeValue csc(ASTNode value) throws SBMLException { - value.compile(this); - ASTNode compiled = getNode(); - if (compiled.isVector()) { - try { - cscRecursive(compiled); - } - catch(SBMLException e) { - unknownValue(); - return dummy; - } - } - else if (useId) { - if (compiled.toString().equals("unknown")) { - compiled.setName("unknown"); - } else { - ASTNode nodeValue = new ASTNode(ASTNode.Type.FUNCTION_CSC, compiled.getParentSBMLObject()); - nodeValue.addChild(compiled); - setNode(nodeValue); - } - return dummy; - } - else if (compiled.isNumber()) { - compiled.setValue(Maths.csc(compiled.getReal())); - } - else { - compiled.setName("unknown"); - } - setNode(compiled); - - return dummy; - } - - /** - * @param value - */ - private void cscRecursive(ASTNode value) { - for (int i = 0; i < value.getChildCount(); i++) { - value.getChild(i).compile(this); - ASTNode child = getNode(); - if (child.isVector()) { - cscRecursive(value.getChild(i)); - } else if (useId){ - ASTNode cscValue = new ASTNode(ASTNode.Type.FUNCTION_CSC, child.getParentSBMLObject()); - cscValue.addChild(child.clone()); - value.replaceChild(i, cscValue); - } else if (child.isNumber()) { - value.getChild(i).setValue(Maths.csc(child.getReal())); - } else { - throw new SBMLException(); - } - } - } - - /* (non-Javadoc) - * @see org.sbml.jsbml.util.compilers.ASTNodeCompiler#csch(org.sbml.jsbml.ASTNode) - */ - @Override - public ASTNodeValue csch(ASTNode value) throws SBMLException { - value.compile(this); - ASTNode compiled = getNode(); - if (compiled.isVector()) { - try { - cschRecursive(compiled); - } - catch(SBMLException e) { - unknownValue(); - return dummy; - } - } - else if (useId) { - if (compiled.toString().equals("unknown")) { - compiled.setName("unknown"); - } else { - ASTNode nodeValue = new ASTNode(ASTNode.Type.FUNCTION_CSCH, compiled.getParentSBMLObject()); - nodeValue.addChild(compiled); - setNode(nodeValue); - } - return dummy; - } - else if (compiled.isNumber()) { - compiled.setValue(Maths.csch(compiled.getReal())); - } - - else { - compiled.setName("unknown"); - } - setNode(compiled); - - return dummy; - } - - /** - * @param value - */ - private void cschRecursive(ASTNode value) { - for (int i = 0; i < value.getChildCount(); i++) { - value.getChild(i).compile(this); - ASTNode child = getNode(); - if (child.isVector()) { - cschRecursive(value.getChild(i)); - } else if (useId){ - ASTNode cschValue = new ASTNode(ASTNode.Type.FUNCTION_CSCH, child.getParentSBMLObject()); - cschValue.addChild(child.clone()); - value.replaceChild(i, cschValue); - } else if (child.isNumber()) { - value.getChild(i).setValue(Maths.csch(child.getReal())); - } else { - throw new SBMLException(); - } - } - } - - /* (non-Javadoc) - * @see org.sbml.jsbml.util.compilers.ASTNodeCompiler#delay(java.lang.String, org.sbml.jsbml.ASTNode, org.sbml.jsbml.ASTNode, java.lang.String) - */ - @Override - public ASTNodeValue delay(String delayName, ASTNode x, ASTNode delay, - String timeUnits) throws SBMLException { - // TODO Auto-generated method stub - unknownValue(); - return dummy; - } - - /* (non-Javadoc) - * @see org.sbml.jsbml.util.compilers.ASTNodeCompiler#eq(org.sbml.jsbml.ASTNode, org.sbml.jsbml.ASTNode) - */ - @Override - public ASTNodeValue eq(ASTNode left, ASTNode right) throws SBMLException { - left.compile(this); - ASTNode leftCompiled = getNode(); - right.compile(this); - ASTNode rightCompiled = getNode(); - - if (leftCompiled.isVector()) { - if (rightCompiled.isVector()) { - ASTNode result = leftCompiled.clone(); - try { - eqRecursive(rightCompiled,leftCompiled, result); - } - catch(SBMLException e) { - unknownValue(); - return dummy; - } - setNode(result); - - } else if (rightCompiled.isNumber() || useId) { - ASTNode result = leftCompiled.clone(); - try { - scalarVectorEq(result, rightCompiled); - } - catch(SBMLException e) { - unknownValue(); - return dummy; - } - setNode(result); - - } else { - unknownValue(); - } - } - else if (leftCompiled.isNumber() || useId) { - if (rightCompiled.isVector()) { - ASTNode result = rightCompiled.clone(); - try { - scalarVectorEq(result, leftCompiled); - } - catch(SBMLException e) { - unknownValue(); - return dummy; - } - setNode(result); - } else if (useId) { - if (leftCompiled.toString().equals("unknown") || rightCompiled.toString().equals("unknown")) { - unknownValue(); - } else { - ASTNode result = new ASTNode(ASTNode.Type.RELATIONAL_EQ); - result.addChild(leftCompiled); - result.addChild(rightCompiled); - setNode(result); - } - } else if (leftCompiled.isNumber() && rightCompiled.isNumber()) { - double leftValue = leftCompiled.getReal(); - double rightValue = rightCompiled.getReal(); - ASTNode result = new ASTNode(leftValue == rightValue ? 1 : 0, left.getParentSBMLObject()); - setNode(result); - } else { - unknownValue(); - } - } else { - unknownValue(); - } - return dummy; - } - - /** - * - * @param right - * @param left - * @param node - * @throws IndexOutOfBoundsException - * @throws SBMLException - */ - private void eqRecursive(ASTNode right, ASTNode left, ASTNode node) throws IndexOutOfBoundsException, SBMLException{ - if (node.getChildCount() == 0) { - if (useId) { - node.setType(ASTNode.Type.RELATIONAL_EQ); - node.getChildren().clear(); - node.addChild(left); - node.addChild(right); - } - else if (left.isNumber() && right.isNumber()) { - if (left.getReal() == right.getReal()) { - node.setValue(1); - } else { - node.setValue(0); - } - } else { - throw new SBMLException(); - } - return; - } - for (int i = 0; i < node.getChildCount(); ++i) { - right.compile(this); - ASTNode rightResult = getNode(); - left.compile(this); - ASTNode leftResult = getNode(); - if (rightResult.isVector() && leftResult.isVector()) { - eqRecursive(right.getChild(i),left.getChild(i), node.getChild(i)); - } else { - throw new SBMLException(); - } - } - } - - /** - * - * @param vector - * @param scalar - */ - private void scalarVectorEq(ASTNode vector, ASTNode scalar) { - for (int i = 0; i < vector.getChildCount(); ++i) { - ASTNode child = vector.getChild(i); - child.compile(this); - ASTNode result = getNode(); - if (result.isVector()) { - scalarVectorEq(child, scalar); - } else if (useId) { - vector.replaceChild(i, ASTNode.eq(result, scalar)); - } else if (result.isNumber() && scalar.isNumber()) { - vector.getChild(i).setValue(result.getReal() == scalar.getReal() ? 1 : 0); - } else { - throw new SBMLException(); - } - } - } - - /* (non-Javadoc) - * @see org.sbml.jsbml.util.compilers.ASTNodeCompiler#exp(org.sbml.jsbml.ASTNode) - */ - @Override - public ASTNodeValue exp(ASTNode value) throws SBMLException { - value.compile(this); - ASTNode compiled = getNode(); - if (compiled.isVector()) { - try { - expRecursive(compiled); - } - catch(SBMLException e) { - unknownValue(); - return dummy; - } - } - else if (useId) { - if (compiled.toString().equals("unknown")) { - compiled.setName("unknown"); - } else { - ASTNode nodeValue = new ASTNode(ASTNode.Type.FUNCTION_EXP, compiled.getParentSBMLObject()); - nodeValue.addChild(compiled); - setNode(nodeValue); - } - return dummy; - } - else if (compiled.isNumber()) { - compiled.setValue(Math.exp(compiled.getReal())); - } - else { - compiled.setName("unknown"); - } - setNode(compiled); - - return dummy; - } - - /** - * @param value - */ - private void expRecursive(ASTNode value) { - for (int i = 0; i < value.getChildCount(); i++) { - value.getChild(i).compile(this); - ASTNode child = getNode(); - if (child.isVector()) { - expRecursive(value.getChild(i)); - } - else if (useId){ - ASTNode expValue = new ASTNode(ASTNode.Type.FUNCTION_EXP, child.getParentSBMLObject()); - expValue.addChild(child.clone()); - value.replaceChild(i, expValue); - } - else if (child.isNumber()) { - value.getChild(i).setValue(Math.exp(child.getReal())); - } - else { - throw new SBMLException(); - } - } - } - - /* (non-Javadoc) - * @see org.sbml.jsbml.util.compilers.ASTNodeCompiler#factorial(org.sbml.jsbml.ASTNode) - */ - @Override - public ASTNodeValue factorial(ASTNode value) throws SBMLException { - value.compile(this); - ASTNode compiled = getNode(); - if (compiled.isVector()) { - try { - factorialRecursive(compiled); - } - catch(SBMLException e) { - unknownValue(); - return dummy; - } - } - else if (useId) { - if (compiled.toString().equals("unknown")) { - compiled.setName("unknown"); - } else { - ASTNode nodeValue = new ASTNode(ASTNode.Type.FUNCTION_FACTORIAL, compiled.getParentSBMLObject()); - nodeValue.addChild(compiled); - setNode(nodeValue); - } - return dummy; - } - else if (compiled.isNumber()) { - double result = Maths.factorial(compiled.getInteger()); - compiled.setValue(result); - } - else { - compiled = new ASTNode("unknown"); - } - - setNode(compiled); - return dummy; - } - - /** - * @param value - */ - private void factorialRecursive(ASTNode value) { - for (int i = 0; i < value.getChildCount(); i++) { - value.getChild(i).compile(this); - ASTNode child = getNode(); - if (child.isVector()) { - factorialRecursive(value.getChild(i)); - } else if (useId){ - ASTNode factValue = new ASTNode(ASTNode.Type.FUNCTION_FACTORIAL, child.getParentSBMLObject()); - factValue.addChild(child.clone()); - value.replaceChild(i, factValue); - } else if (child.isNumber()) { - value.getChild(i).setValue(Maths.factorial(child.getInteger())); - } else { - throw new SBMLException(); - } - } - } - /* (non-Javadoc) - * @see org.sbml.jsbml.util.compilers.ASTNodeCompiler#floor(org.sbml.jsbml.ASTNode) - */ - @Override - public ASTNodeValue floor(ASTNode value) throws SBMLException { - value.compile(this); - ASTNode compiled = getNode(); - if (compiled.isVector()) { - try { - floorRecursive(compiled); - } - catch(SBMLException e) { - unknownValue(); - return dummy; - } - } - else if (useId) { - if (compiled.toString().equals("unknown")) { - compiled.setName("unknown"); - } else { - ASTNode nodeValue = new ASTNode(ASTNode.Type.FUNCTION_FLOOR, compiled.getParentSBMLObject()); - nodeValue.addChild(compiled); - setNode(nodeValue); - } - return dummy; - } - else if (compiled.isNumber()) { - compiled.setValue(Math.floor(compiled.getReal())); - } - else { - compiled.setName("unknown"); - } - setNode(compiled); - - return dummy; - } - - /** - * @param value - */ - private void floorRecursive(ASTNode value) { - for (int i = 0; i < value.getChildCount(); i++) { - value.getChild(i).compile(this); - ASTNode child = getNode(); - if (child.isVector()) { - floorRecursive(value.getChild(i)); - } else if (useId){ - ASTNode floorValue = new ASTNode(ASTNode.Type.FUNCTION_FLOOR, child.getParentSBMLObject()); - floorValue.addChild(child.clone()); - value.replaceChild(i, floorValue); - } else if (child.isNumber()) { - value.getChild(i).setValue(Math.floor(child.getReal())); - } else { - throw new SBMLException(); - } - } - } - - /* (non-Javadoc) - * @see org.sbml.jsbml.util.compilers.ASTNodeCompiler#frac(org.sbml.jsbml.ASTNode, org.sbml.jsbml.ASTNode) - */ - @Override - public ASTNodeValue frac(ASTNode numerator, ASTNode denominator) - throws SBMLException ... [truncated message content] |