Ejemplo n.º 1
0
def regenerateTable():
    """
    Called to completely regenerate the contents of this table.  First,
    it (generates the sql to) empties the table, and then it repopulates
    it.
    """
    # Anatomy part-of relationships form a directed acyclic graph (DAG)
    #
    # Walk the graph top to bottom, breadth first, generating
    # transitive closure relationships as we go.

    # Delete everything in existing table.
    deleteAll()
    _initialiseGlobals()

    # Start with the root of everything.
    nodeQueue = [Nodes.getRoot()]
    beenQueued = sets.Set(nodeQueue) # what has been put on the Q
    beenProcessed = sets.Set()       # what has been pulled from the Q

    while len(nodeQueue) > 0:
        current = nodeQueue.pop(0)
        currentOid = current.getOid()

        _createImmediateRelsWhereChild(current)

        # This node's children can be added to the queue if all that
        # child's parents have been processed.
        beenProcessed.add(current)
        relsWithChildren = Relationships.getByParentOidRelType(
            currentOid, Relationships.PART_OF)
        for childRel in relsWithChildren:
            childOid = childRel.getChildOid()
            child = Nodes.getByOid(childOid)
            if child not in beenQueued:
                # Get all the child's parents
                childsRelsWithParents = Relationships.getByChildOidRelType(
                    childOid, Relationships.PART_OF)
                allParentsProcessed = True
                for childsRelWithParent in childsRelsWithParents:
                    childsParent = Nodes.getByOid(
                        childsRelWithParent.getParentOid())
                    if childsParent not in beenProcessed:
                        allParentsProcessed = False
                if allParentsProcessed:
                    nodeQueue.append(child)
                    beenQueued.add(child)

    # Add self-referential relationships
    for node in beenProcessed:
        createRelationshipTransitive(Relationships.PART_OF,
                                     descendentOid = node.getOid(),
                                     ancestorOid = node.getOid())

    return None