/** * @return the given formula AST as a directed graph. The caller * owns the returned string and is responsible for freeing it. */ char * SBML_formulaToDot (const ASTNode_t *tree) { StringBuffer_t *sb = StringBuffer_create(128); char *name; char *s; if (FormulaGraphvizFormatter_isFunction(tree) || ASTNode_isOperator(tree)) { FormulaGraphvizFormatter_visit(NULL, tree, sb); } else { name = FormulaGraphvizFormatter_format(tree); StringBuffer_append(sb, name); } StringBuffer_append(sb, "}\n"); s = StringBuffer_getBuffer(sb); free(sb); return s; }
/** * Formats the given ASTNode as an SBML L1 token and appends the result to * the given StringBuffer. */ void FormulaFormatter_format (StringBuffer_t *sb, const ASTNode_t *node) { if (sb == NULL) return; if (ASTNode_isOperator(node)) { FormulaFormatter_formatOperator(sb, node); } else if (ASTNode_isFunction(node)) { FormulaFormatter_formatFunction(sb, node); } else if (ASTNode_isInteger(node)) { StringBuffer_appendInt(sb, ASTNode_getInteger(node)); } else if (ASTNode_isRational(node)) { FormulaFormatter_formatRational(sb, node); } else if (ASTNode_isReal(node)) { FormulaFormatter_formatReal(sb, node); } else if ( !ASTNode_isUnknown(node) ) { StringBuffer_append(sb, ASTNode_getName(node)); } }
/** * Formats the given ASTNode as an SBML L3 token and appends the result to * the given StringBuffer. */ void L3FormulaFormatter_format (StringBuffer_t *sb, const ASTNode_t *node, const L3ParserSettings_t *settings) { if (sb == NULL) return; if (L3FormulaFormatter_isFunction(node, settings)) { L3FormulaFormatter_formatFunction(sb, node, settings); } else if (ASTNode_isOperator(node) || ASTNode_getType(node) == AST_FUNCTION_POWER) { L3FormulaFormatter_formatOperator(sb, node); } else if (ASTNode_isLogical(node) || ASTNode_isRelational(node)) { L3FormulaFormatter_formatLogicalRelational(sb, node); } else if (ASTNode_isRational(node)) { L3FormulaFormatter_formatRational(sb, node, settings); } else if (ASTNode_isInteger(node)) { L3FormulaFormatter_formatReal(sb, node, settings); } else if (ASTNode_isReal(node)) { L3FormulaFormatter_formatReal(sb, node, settings); } else if ( !ASTNode_isUnknown(node) ) { StringBuffer_append(sb, ASTNode_getName(node)); } }
/** * Formats the given ASTNode as a directed graph token and returns the result as * a string. */ char * FormulaGraphvizFormatter_format (const ASTNode_t *node) { StringBuffer_t *p = StringBuffer_create(128); char *s = NULL; if (ASTNode_isOperator(node)) { s = FormulaGraphvizFormatter_formatOperator(node); } else if (ASTNode_isFunction(node)) { s = FormulaGraphvizFormatter_formatFunction(node); } else if (ASTNode_isInteger(node)) { StringBuffer_appendInt(p, ASTNode_getInteger(node)); s = StringBuffer_toString(p); } else if (ASTNode_isRational(node)) { s = FormulaGraphvizFormatter_formatRational(node); } else if (ASTNode_isReal(node)) { s = FormulaGraphvizFormatter_formatReal(node); } else if ( !ASTNode_isUnknown(node) ) { if (ASTNode_getName(node) == NULL) { StringBuffer_append(p, "unknown"); } else { StringBuffer_append(p, ASTNode_getName(node)); } s = StringBuffer_toString(p); } free(p); return s; }
/** * Since graphviz will interpret identical names as referring to * the same node presentation-wise it is better if each function node * has a unique name. * * Returns the name of the operator with the name of the first child * prepended * * THIS COULD BE DONE BETTER */ char * FormulaGraphvizFormatter_OperatorGetUniqueName (const ASTNode_t *node) { char *s; char number[10]; StringBuffer_t *p = StringBuffer_create(128); ASTNodeType_t type = ASTNode_getType(node); if (FormulaGraphvizFormatter_isFunction(ASTNode_getChild(node,0)) || ASTNode_isOperator(ASTNode_getChild(node,0))) { StringBuffer_append(p, "func"); } else { if (ASTNode_isInteger(ASTNode_getChild(node, 0))) { sprintf(number, "%d", (int)ASTNode_getInteger(ASTNode_getChild(node, 0))); StringBuffer_append(p, number); } else if (ASTNode_isReal(ASTNode_getChild(node, 0))) { sprintf(number, "%ld", ASTNode_getNumerator(ASTNode_getChild(node, 0))); StringBuffer_append(p, number); } else { StringBuffer_append(p, ASTNode_getName(ASTNode_getChild(node,0))); } } switch (type) { case AST_TIMES: StringBuffer_append(p, "times"); break; case AST_DIVIDE: StringBuffer_append(p, "divide"); break; case AST_PLUS: StringBuffer_append(p, "plus"); break; case AST_MINUS: StringBuffer_append(p, "minus"); break; case AST_POWER: StringBuffer_append(p, "power"); break; default: StringBuffer_appendChar(p, ASTNode_getCharacter(node)); break; } s = StringBuffer_toString(p); free(p); return s; }