/** * @param value */ private void notRecursive(ASTNode value) { for (int i = 0; i < value.getChildCount(); i++) { value.getChild(i).compile(this); ASTNode child = getNode(); if (child.isVector()) { notRecursive(value.getChild(i)); } else if (useId){ ASTNode notValue = new ASTNode(ASTNode.Type.LOGICAL_NOT, child.getParentSBMLObject()); notValue.addChild(child.clone()); value.replaceChild(i, notValue); } else if (child.isBoolean()) { value.getChild(i).setValue(child.getInteger() == 1 ? 0 : 1); } else { throw new SBMLException(); } } }
@Override public boolean accepts(Object o) { if (o instanceof ASTNode) { ASTNode current = (ASTNode) o; if (current.equals(bvar)) { ASTNode parent = (ASTNode) current.getParent(); int index = parent.getIndex(current); if (index != -1) { parent.replaceChild(index, expandedBVar); } } } return false; } });
/** * 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); }
/** * Adds an SBML element to this node. * * @param nsb * an SBML element that can be represented by a value. * @return the current node for convenience. */ public ASTNode plus(CallableSBase nsb) { plus(new ASTNode(nsb, getParentSBMLObject())); return this; }
/** * Subtracts the given number from this node. * * @param real * a double number. * @return the current node for convenience. */ public ASTNode minus(double real) { minus(new ASTNode(real, getParentSBMLObject())); return this; }
/** * 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) { ASTNode e = new ASTNode(Type.CONSTANT_E, exponent.getParentSBMLObject()); return e.raiseByThePowerOf(exponent); }
/** * @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(); } } }
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); 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();
@Override public ASTNodeValue uMinus(ASTNode value) throws SBMLException { value.compile(this); ASTNode compiled = getNode(); if (compiled.isVector()) { try { uMinusRecursive(compiled); else if (compiled.equals(new ASTNode("unknown"))) { unknownValue(); return dummy; else if (compiled.isNumber()) { if (compiled.isInteger()) { compiled.setValue(-value.getInteger()); } else if (compiled.isRational()) { compiled.setValue(-value.getNumerator(), value.getDenominator()); } else if (compiled.getType().equals(Type.REAL_E)) { compiled.setValue(-value.getMantissa(), value.getExponent()); } else { compiled.setValue(-value.getReal()); if (compiled.toString().equals("unknown")) { compiled.setName("unknown"); } else { ASTNode nodeValue = new ASTNode(ASTNode.Type.MINUS, compiled.getParentSBMLObject()); nodeValue.addChild(compiled); setNode(nodeValue); compiled.setName("unknown");
if (node.getChildCount() == 0) { double result = 1; node.getChildren().clear(); node.setType(ASTNode.Type.PLUS); if (values.size() > 0) { for (int i = 0; i < values.size(); ++i) { ASTNode value = values.get(i); if (value.isNumber()) { result *= value.getReal(); } else if (useId) { node.addChild(values.get(i).clone()); } else { throw new SBMLException(); node.addChild(new ASTNode(result)); 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)); timesRecursive(nodes, node.getChild(i));
/** * @param vector * @param scalar */ private void vectorScalarFrac(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()) { vectorScalarGt(child, scalar); } else if (useId) { vector.replaceChild(i, ASTNode.frac(result, scalar)); } else if (result.isNumber() && scalar.isNumber()) { vector.getChild(i).setValue(result.getReal() / scalar.getReal()); } else { throw new SBMLException(); } } }
@Override public ASTNodeValue not(ASTNode value) throws SBMLException { value.compile(this); ASTNode compiled = getNode(); if (compiled.isVector()) { try { notRecursive(compiled); if (compiled.toString().equals("unknown")) { compiled.setName("unknown"); } else { ASTNode nodeValue = new ASTNode(ASTNode.Type.LOGICAL_NOT, compiled.getParentSBMLObject()); nodeValue.addChild(compiled); setNode(nodeValue); else if (compiled.isBoolean()) { compiled.setValue(compiled.getInteger() == 1 ? 0 : 1); compiled.setName("unknown");
if (node.isBoolean()) { return DT_BOOLEAN; if (node.isNumber() || node.isOperator() || node.isConstant()) { return DT_NUMBER; if (node.isName()) { if (node.isVector()) { return DT_VECTOR; if (node.getType() == Type.FUNCTION_PIECEWISE) { if (node.getNumChildren() > 0) { byte dt = getDataType(node.getChild(0)); for (int i = 0; i < node.getNumChildren(); i += 2) { if (getDataType(node.getChild(i)) != dt) { return DT_UNKNOWN; if (node.getType() == Type.FUNCTION) { SBase parent = node.getParentSBMLObject(); FunctionDefinition fd = m.getFunctionDefinition(node.getName()); if (node.isFunction()) { return DT_NUMBER;
if (body.isName() && body.getNumChildren() == 0) if (arg != null && arg.isString() && arg.getName() != null && body.getName() != null) if (arg.getName().equals(body.getName())) if (body.getType() == Type.NAME_TIME) return body.isBoolean() || body.isNumber() || body.isFunction() || body.isOperator() || body.getType() == Type.CONSTANT_PI || body.getType() == Type.CONSTANT_E;
if (node.getParentSBMLObject() == null || node.getParentSBMLObject() instanceof FunctionDefinition || node.getParentSBMLObject().getModel() == null) { return true; Type t = node.getType(); // TODO - check section 3.4.11 in the L2V5 specs UnitsCompiler unitsCompiler = new UnitsCompiler(node.getParentSBMLObject().getModel(), ctx); if (node.getNumChildren() > 0) { UnitDefinition ud = node.getChild(0).compile(unitsCompiler).getUnits(); for (int n = 1; n < node.getNumChildren(); n++) { UnitDefinition ud2 = node.getChild(n).compile(unitsCompiler).getUnits(); if (node.getNumChildren() == 2) { ASTNode right = node.getRightChild(); UnitDefinition ud = right.getUnitsInstance(); if (ud == null && right.isNumber()) { if (node.getNumChildren() == 0) { return true; if (node.getChild(0) != null) { try { ud = node.getChild(0).deriveUnit(); } catch (Exception e) { for (int n = 1; n < node.getNumChildren(); n++) { ASTNode child = node.getChild(n);
ASTNode current = (ASTNode) o; if (current.getType() == ASTNode.Type.FUNCTION) { String sid = current.getName(); FunctionDefinition fd = m.getFunctionDefinition(sid); if (current.getChildCount() != fd.getArgumentCount()) { System.out.println("expandFunctionDefinition - number of arguments differ, aborting for " + sid); ASTNode newMath = fd.getBody().clone(); for (int i = 0; i < current.getChildCount(); i++) { ASTNode bvar = fd.getArgument(i); ASTNode expandedBVar = current.getChild(i); if (newMath.equals(bvar)) { TreeNode parent = current.getParent(); ((ASTNode) parent).replaceChild(index, newMath);
/** * @param values * @param vector * @throws SBMLException */ private void piecewiseRecursive(List<ASTNode> values, ASTNode vector) throws SBMLException{ if (!vector.isVector()) { vector.setType(ASTNode.Type.FUNCTION_PIECEWISE); vector.getChildren().clear(); for (ASTNode value : values) { vector.addChild(value); } return; } for (int i = 0; i < vector.getChildCount(); ++i) { List<ASTNode> children = new ArrayList<ASTNode>(); for (ASTNode value : values) { if (value.getChildCount() == 0) { children.add(value.clone()); } else if (value.isVector()) { if (value.getChildCount() != vector.getChildCount()) { throw new SBMLException(); } children.add(value.getChild(i).clone()); } } piecewiseRecursive(children, vector.getChild(i)); } } /* (non-Javadoc)
/** * @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"); } }
/** * @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); } }
@Override public ASTNodeValue vector(List<ASTNode> nodes) throws SBMLException { ASTNode vector = new ASTNode(); vector.setType(ASTNode.Type.VECTOR); for (int i = 0; i < nodes.size(); ++i) { nodes.get(i).compile(this); ASTNode node = getNode(); if (node.equals(new ASTNode("unknown"))) { unknownValue(); return dummy; } vector.addChild(getNode()); } setNode(vector); return dummy; }