Exemplo n.º 1
0
def visualise_triples(triples,
                      triple_args_function=None,
                      ignore_properties=VIS_IGNORE_PROPERTIES):
    """
    Visualise a triples representation of Triples such as retrieved from
    TreeTransformer.get_triples
    @param triple_args_function: a function of Triple to dict that gives
                                 optional arguments for a triple
    """
    g = dot.Graph()
    nodes = {}  # Node -> dot.Node
    # create nodes
    for n in iterate_nodes(triples):
        label = "%s: %s" % (n.position, n.label)
        for k, v in n.__dict__.iteritems():
            if k not in ignore_properties:
                label += "\\n%s: %s" % (k, v)
        node = dot.Node(id="node_%s" % n.position, label=label)
        g.addNode(node)
        nodes[n] = node
    # create edges
    for triple in triples:
        kargs = triple_args_function(triple) if triple_args_function else {}
        if 'label' not in kargs: kargs['label'] = triple.predicate
        g.addEdge(nodes[triple.subject], nodes[triple.object], **kargs)
    # some theme options
    g.theme.graphattrs["rankdir"] = "BT"
    g.theme.shape = "rect"
    g.theme.edgesize = 10
    g.theme.fontsize = 10

    return g
Exemplo n.º 2
0
 def getnode(x):
     if not x in nodes: 
         id = "node_%i_%s" % (len(nodes), re.sub("\W","",x))
         nodes[x] = dot.Node(id, x)
     return nodes[x]