Beispiel #1
0
def _get_variables(astnode: libsbml.ASTNode, variables: Set[str] = None) -> Set[str]:
    """Get variables from ASTNode."""
    if variables is None:
        variables: Set[str] = set()  # type: ignore

    num_children = astnode.getNumChildren()
    if num_children == 0:
        if astnode.isName():
            name = astnode.getName()
            variables.add(name)  # type: ignore
    else:
        for k in range(num_children):
            child: libsbml.ASTNode = astnode.getChild(k)
            _get_variables(child, variables=variables)

    return variables  # type: ignore
Beispiel #2
0
def find_names_in_ast(ast: libsbml.ASTNode,
                      names: Optional[List[str]] = None) -> List[str]:
    """Find all names in given astnode.

    Names are the variables in the formula.

    :param ast:
    :param names:
    :return:
    """
    if names is None:
        names = []

    # name for this node
    if ast.getType() == libsbml.AST_NAME:
        names.append(ast.getName())

    for k in range(ast.getNumChildren()):
        ast_child = ast.getChild(k)
        find_names_in_ast(ast_child, names)

    return names
Beispiel #3
0
def ast_info(ast: libsbml.ASTNode) -> None:
    """Print ASTNode information."""
    print(ast)
    print(ast.getType(), ast.getName())