1 /**
2  * \file    FormulaGraphvizFormatter.h
3  * \brief   Formats an AST formula tree as a dot directed graph
4  * \author  Sarah Keating
5  *
6  * <!--------------------------------------------------------------------------
7  * This sample program is distributed under a different license than the rest
8  * of libSBML.  This program uses the open-source MIT license, as follows:
9  *
10  * Copyright (c) 2013-2018 by the California Institute of Technology
11  * (California, USA), the European Bioinformatics Institute (EMBL-EBI, UK)
12  * and the University of Heidelberg (Germany), with support from the National
13  * Institutes of Health (USA) under grant R01GM070923.  All rights reserved.
14  *
15  * Permission is hereby granted, free of charge, to any person obtaining a
16  * copy of this software and associated documentation files (the "Software"),
17  * to deal in the Software without restriction, including without limitation
18  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
19  * and/or sell copies of the Software, and to permit persons to whom the
20  * Software is furnished to do so, subject to the following conditions:
21  *
22  * The above copyright notice and this permission notice shall be included in
23  * all copies or substantial portions of the Software.
24  *
25  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
28  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
30  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
31  * DEALINGS IN THE SOFTWARE.
32  *
33  * Neither the name of the California Institute of Technology (Caltech), nor
34  * of the European Bioinformatics Institute (EMBL-EBI), nor of the University
35  * of Heidelberg, nor the names of any contributors, may be used to endorse
36  * or promote products derived from this software without specific prior
37  * written permission.
38  * ------------------------------------------------------------------------ -->
39  */
40 
41 #ifndef FormulaGraphvizFormatter_h
42 #define FormulaGraphvizFormatter_h
43 
44 #include "sbml/common/extern.h"
45 #include "sbml/util/StringBuffer.h"
46 #include "sbml/math/ASTNode.h"
47 
48 
49 
50 BEGIN_C_DECLS
51 
52 
53 /**
54  * @return the given formula AST as a directed graph.  The caller
55  * owns the returned string and is responsible for freeing it.
56  */
57 char *
58 SBML_formulaToDot (const ASTNode_t *tree);
59 
60 
61 #ifndef SWIG
62 
63 
64 /**
65  * @return true (non-zero) if the given ASTNode is to formatted as a
66  * function.
67  */
68 int
69 FormulaGraphvizFormatter_isFunction (const ASTNode_t *node);
70 
71 
72 /**
73  * Formats the given ASTNode as a directed graph token and returns the result as
74  * a string.
75  */
76 char *
77 FormulaGraphvizFormatter_format (const ASTNode_t *node);
78 
79 /**
80  * Since graphviz will interpret identical names as referring to
81  * the same node presentation-wise it is better if each function node
82  * has a unique name.
83  *
84  * Returns the name with the name of the first child
85  * prepended
86  *
87  * THIS COULD BE DONE BETTER
88  */
89 char *
90 FormulaGraphvizFormatter_getUniqueName (const ASTNode_t *node);
91 
92 /**
93  * Formats the given ASTNode as a directed graph function name and returns the
94  * result as a string.
95  */
96 char *
97 FormulaGraphvizFormatter_formatFunction (const ASTNode_t *node);
98 
99 /**
100  * Since graphviz will interpret identical names as referring to
101  * the same node presentation-wise it is better if each function node
102  * has a unique name.
103  *
104  * Returns the name of the function with the name of the first child
105  * prepended
106  *
107  * THIS COULD BE DONE BETTER
108  */
109 char *
110 FormulaGraphvizFormatter_FunctionGetUniqueName (const ASTNode_t *node);
111 
112 /**
113  * Formats the given ASTNode as a directed graph operator and returns the result
114  * as a string.
115  */
116 char *
117 FormulaGraphvizFormatter_formatOperator (const ASTNode_t *node);
118 
119 /**
120  * Since graphviz will interpret identical names as referring to
121  * the same node presentation-wise it is better if each function node
122  * has a unique name.
123  *
124  * Returns the name of the operator with the name of the first child
125  * prepended
126  *
127  * THIS COULD BE DONE BETTER
128  */
129 char *
130 FormulaGraphvizFormatter_OperatorGetUniqueName (const ASTNode_t *node);
131 
132 /**
133  * Formats the given ASTNode as a rational number and returns the result as
134  * a string. This amounts to:
135  *
136  *   "(numerator/denominator)"
137  */
138 char *
139 FormulaGraphvizFormatter_formatRational (const ASTNode_t *node);
140 
141 /**
142  * Formats the given ASTNode as a real number and returns the result as
143  * a string.
144  */
145 char *
146 FormulaGraphvizFormatter_formatReal (const ASTNode_t *node);
147 
148 /**
149  * Visits the given ASTNode node.  This function is really just a
150  * dispatcher to either FormulaGraphvizFormatter_visitFunction() or
151  * FormulaGraphvizFormatter_visitOther().
152  */
153 void
154 FormulaGraphvizFormatter_visit ( const ASTNode_t *parent,
155                          const ASTNode_t *node,
156                          StringBuffer_t  *sb );
157 
158 /**
159  * Visits the given ASTNode as a function.  For this node only the
160  * traversal is preorder.
161  * Writes the function as a directed graph and appends the result
162  * to the StringBuffer.
163  */
164 void
165 FormulaGraphvizFormatter_visitFunction ( const ASTNode_t *parent,
166                                  const ASTNode_t *node,
167                                  StringBuffer_t  *sb );
168 
169 /**
170  * Visits the given ASTNode as the function "log(10, x)" and in doing so,
171  * formats it as "log10(x)" (where x is any subexpression).
172  * Writes the function as a directed graph and appends the result
173  * to the StringBuffer.
174  *
175  * A seperate function may not be strictly speaking necessary for graphs
176  */
177 void
178 FormulaGraphvizFormatter_visitLog10 ( const ASTNode_t *parent,
179                               const ASTNode_t *node,
180                               StringBuffer_t  *sb );
181 
182 /**
183  * Visits the given ASTNode as the function "root(2, x)" and in doing so,
184  * formats it as "sqrt(x)" (where x is any subexpression).
185  * Writes the function as a directed graph and appends the result
186  * to the StringBuffer.
187  *
188  * A seperate function may not be strictly speaking necessary for graphs
189  */
190 void
191 FormulaGraphvizFormatter_visitSqrt ( const ASTNode_t *parent,
192                              const ASTNode_t *node,
193                              StringBuffer_t  *sb );
194 
195 /**
196  * Visits the given ASTNode as a unary minus.  For this node only the
197  * traversal is preorder.
198  * Writes the function as a directed graph and appends the result
199  * to the StringBuffer.
200  *
201  * A seperate function may not be strictly speaking necessary for graphs
202  */
203 void
204 FormulaGraphvizFormatter_visitUMinus ( const ASTNode_t *parent,
205                                const ASTNode_t *node,
206                                StringBuffer_t  *sb );
207 
208 /**
209  * Visits the given ASTNode and continues the inorder traversal.
210  * Writes the function as a directed graph and appends the result
211  * to the StringBuffer.
212  */
213 void
214 FormulaGraphvizFormatter_visitOther ( const ASTNode_t *parent,
215                               const ASTNode_t *node,
216                               StringBuffer_t  *sb );
217 
218 
219 
220 
221 END_C_DECLS
222 
223 
224 #endif  /* !SWIG */
225 #endif  /* FormulaGraphvizFormatter_h */
226 
227