/** * Checks whether the number of child nodes is exactly one. * * @return {@code true} if the number of child nodes is exactly one. */ public boolean isUnary() { return getChildCount() == 1; }
/** * Returns {@code true} if this node is a unary minus operator, {@code false} otherwise. A * node is defined as a unary minus node if it is of type MINUS and has * exactly one child. * * For numbers, unary minus nodes can be 'collapsed' by negating the number. * In fact, SBML_parseFormula() does this during its parse. However, unary * minus nodes for symbols (NAMES) cannot be 'collapsed', so this predicate * function is necessary. * * @return {@code true} if this ASTNode is a unary minus, {@code false} otherwise. */ public boolean isUMinus() { return (type == Type.MINUS) && (getChildCount() == 1); }
/** * Get the number of arguments (bound variables) taken by this * {@link FunctionDefinition}. * * @return the number of arguments (bound variables) that must be passed to * this {@link FunctionDefinition}. */ public int getArgumentCount() { return isSetMath() && (getMath().getChildCount() > 1) ? getMath().getChildCount() - 1 : 0; }
/** * @param math * @return */ public static boolean isVectorOperation(ASTNode math) { boolean hasVector = false; for (int i = 0; i < math.getChildCount(); ++i) { if (math.getChild(i).isVector()) { hasVector = true; break; } } return hasVector; }
@Override public ASTNodeValue pow(ASTNode base, ASTNode exponent) throws SBMLException { StringBuilder value = new StringBuilder(); value.append(base.compile(this)); String exp = exponent.compile(this).toString(); if (!exp.equals("1")) { if (!(base.getChildCount() < 2)) { value = brackets(value); } value.append('^'); value.append('{'); value.append(exp); value.append('}'); } return new ASTNodeValue(value.toString(), this); }
/** * Recursively checks if a node is of type FUNCTION_SELECTOR. If so, * add to the list of selector nodes. * * @param math * @param listOfNodes */ private void getSelectorNodes(ASTNode math, List<ASTNode> listOfNodes) { if (math.getType() == ASTNode.Type.FUNCTION_SELECTOR) { listOfNodes.add(math); } for (int i = 0; i < math.getChildCount(); ++i) { getSelectorNodes(math.getChild(i), listOfNodes); } }
/** * Replaces dimension id with the appropriate integer value. * * @param math * @param id * @param index */ private static void recursiveReplaceDimensionId(ASTNode math, String id, int index) { if (math.getChildCount() == 0) { if (math.isString() && math.getName().equals(id)) { math.setValue(index); } return; } for (int i = 0; i < math.getChildCount(); ++i) { recursiveReplaceDimensionId(math.getChild(i), id, index); } }
@Override public ASTNodeValue not(ASTNode node) throws SBMLException { return new ASTNodeValue(concat( "\\neg ", (node.getChildCount() == 0) ? node.compile(this) : brackets(node.compile(this))).toString(), this); }
@Override public boolean check(ValidationContext ctx, ASTNode node) { if (node.getType() == ASTNode.Type.FUNCTION_RATE_OF) { // check that the child of a rateOf is a ci element. if (node.getChildCount() > 0 && !(node.getChild(0).getType().equals(ASTNode.Type.NAME))) { return false; } } return true; }
/** * @param math * @param bvar * @param value */ private void recursiveReplaceDimensionId(ASTNode math, String bvar, ASTNode value) { for (int i = 0; i < math.getChildCount(); ++i) { if (math.getChild(i).isString() && math.getChild(i).getName().equals(bvar)) { math.replaceChild(i, value); } recursiveReplaceDimensionId(math.getChild(i), bvar, value); } }
/** * @param node * @param value */ private void updateASTNodeName(ASTNode node, int value) { if (node.isVector()) { for (int i = 0; i < node.getChildCount(); ++i) { ASTNode child = node.getChild(i); updateASTNodeName(child, value); } } else if (node.isName()) { node.setName("_" + String.valueOf(value) + node.getName()); } else { node.setName("unknown"); } }
@Override public boolean check(ValidationContext ctx, ASTNode node) { // In comparator... if (node.isRelational() && node.getChildCount() > 0) { byte dt = ValidationTools.getDataType(node.getChild(0)); // all children must have same Type for (int i = 1; i < node.getNumChildren(); i++) { if (dt != ValidationTools.getDataType(node.getChild(i))) { return false; } } } return true; } };
/** * Creates brackets if needed. * * @param node * @return * @throws SBMLException */ protected String checkDenominatorBrackets(ASTNode node) throws SBMLException { if ((node.getType() == Type.POWER) && (node.getChildCount() > 1) && node.getRightChild().toString().equals("1")) { return checkDenominatorBrackets(node.getLeftChild()); } String term = node.compile(this).toString(); if (node.isSum() || node.isDifference() || node.isUMinus() || (node.getType() == Type.TIMES) || node.getType() == Type.DIVIDE || node.isRelational() || node.isLogical()) { term = brackets(term).toString(); } return term; }
/** * @param value */ private void sinRecursive(ASTNode value) { for (int i = 0; i < value.getChildCount(); i++) { value.getChild(i).compile(this); ASTNode child = getNode(); if (child.isVector()) { sinRecursive(value.getChild(i)); } else if (useId){ ASTNode sinValue = new ASTNode(ASTNode.Type.FUNCTION_SIN, child.getParentSBMLObject()); sinValue.addChild(child.clone()); value.replaceChild(i, sinValue); } else if (child.isNumber()) { value.getChild(i).setValue(Math.sin(child.getReal())); } else { throw new SBMLException(); } } }
/** * @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(); } } }
/** * @param value */ private void lnRecursive(ASTNode value) { for (int i = 0; i < value.getChildCount(); i++) { value.getChild(i).compile(this); ASTNode child = getNode(); if (child.isVector()) { lnRecursive(value.getChild(i)); } else if (useId){ ASTNode lnValue = new ASTNode(ASTNode.Type.FUNCTION_LN, child.getParentSBMLObject()); lnValue.addChild(child.clone()); value.replaceChild(i, lnValue); } else if (child.isNumber()) { value.getChild(i).setValue(Math.log(child.getReal())); } else { throw new SBMLException(); } } }
/** * @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(); } } }
/** * @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(); } } }
/** * @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(); } } }
/** * @param value */ private void sechRecursive(ASTNode value) { for (int i = 0; i < value.getChildCount(); i++) { value.getChild(i).compile(this); ASTNode child = getNode(); if (child.isVector()) { sechRecursive(value.getChild(i)); } else if (useId){ ASTNode sechValue = new ASTNode(ASTNode.Type.FUNCTION_SECH, child.getParentSBMLObject()); sechValue.addChild(child.clone()); value.replaceChild(i, sechValue); } else if (child.isNumber()) { value.getChild(i).setValue(Maths.sech(child.getReal())); } else { throw new SBMLException(); } } }