Skip to content

Commit

Permalink
Fix problem with expression to DOT conversion
Browse files Browse the repository at this point in the history
When you iterate through the arguments of an expression, they will not
return unique identifiers. Had to put a new function call in the
exprnode module to return the pointer value of the actual BoolExpr
object.
  • Loading branch information
cjdrake committed Mar 1, 2015
1 parent ec9f249 commit 396d52d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 19 deletions.
34 changes: 15 additions & 19 deletions pyeda/boolalg/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -928,35 +928,31 @@ def to_dot(self, name='EXPR'): # pragma: no cover
"""Convert to DOT language representation."""
parts = ['graph', name, '{', 'rankdir=BT;']
for ex in self.iter_dfs():
exid = ex.node.id()
if ex is Zero:
parts += ['n' + str(id(ex)), '[label=0,shape=box]']
parts += ["n{} [label=0,shape=box];".format(exid)]
elif ex is One:
parts += ['n' + str(id(ex)), '[label=1,shape=box]']
parts += ["n{} [label=1,shape=box];".format(exid)]
elif isinstance(ex, Literal):
parts += ['n' + str(id(ex)),
'[label="{}",shape=box]'.format(ex)]
parts += ['n{} [label="{}",shape=box];'.format(exid, ex)]
else:
parts.append('n' + str(id(ex)))
parts.append("[label={0.ASTOP},shape=circle]".format(ex))
parts += ["n{0} [label={1.ASTOP},shape=circle];".format(exid, ex)]
for ex in self.iter_dfs():
exid = ex.node.id()
if isinstance(ex, NotOp):
parts += ['n' + str(id(ex.x)), '--',
'n' + str(id(ex))]
parts += ["n{} -- n{};".format(ex.x.node.id(), exid)]
elif isinstance(ex, ImpliesOp):
parts += ['n' + str(id(ex.p)), '--',
'n' + str(id(ex)), '[label=p]']
parts += ['n' + str(id(ex.q)), '--',
'n' + str(id(ex)), '[label=q]']
p, q = ex.xs
parts += ["n{} -- n{} [label=p];".format(p.node.id(), exid)]
parts += ["n{} -- n{} [label=q];".format(q.node.id(), exid)]
elif isinstance(ex, IfThenElseOp):
parts += ['n' + str(id(ex.s)), '--',
'n' + str(id(ex)), '[label=s]']
parts += ['n' + str(id(ex.d1)), '--',
'n' + str(id(ex)), '[label=d1]']
parts += ['n' + str(id(ex.d0)), '--',
'n' + str(id(ex)), '[label=d0]']
s, d1, d0 = ex.xs
parts += ["n{} -- n{} [label=s];".format(s.node.id(), exid)]
parts += ["n{} -- n{} [label=d1];".format(d1.node.id(), exid)]
parts += ["n{} -- n{} [label=d0];".format(d0.node.id(), exid)]
elif isinstance(ex, NaryOp):
for x in ex.xs:
parts += ['n' + str(id(x)), '--', 'n' + str(id(ex))]
parts += ["n{} -- n{};".format(x.node.id(), exid)]
parts.append('}')
return " ".join(parts)

Expand Down
13 changes: 13 additions & 0 deletions pyeda/boolalg/exprnodemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,18 @@ static PyObject *
ExprNode_compose(ExprNode *self, PyObject *args);


/* ExprNode.id() */
PyDoc_STRVAR(id_doc,
"Return the int id of the expression."
);

static PyObject *
ExprNode_id(ExprNode *self)
{
return PyLong_FromLong((long) self->ex);
}


/* Recursive component of ExprNode_to_ast */
static PyObject *
_node2ast(struct BoolExpr *ex)
Expand Down Expand Up @@ -364,6 +376,7 @@ ExprNode_methods[] = {
{"restrict", (PyCFunction) ExprNode_restrict, METH_VARARGS, restrict_doc},
{"compose", (PyCFunction) ExprNode_compose, METH_VARARGS, compose_doc},

{"id", (PyCFunction) ExprNode_id, METH_NOARGS, id_doc},
{"to_ast", (PyCFunction) ExprNode_to_ast, METH_NOARGS, to_ast_doc},
{"type", (PyCFunction) ExprNode_type, METH_NOARGS, type_doc},
{"data", (PyCFunction) ExprNode_data, METH_NOARGS, data_doc},
Expand Down

0 comments on commit 396d52d

Please sign in to comment.