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
Ejemplo n.º 2
0
def connectTheDots():
    """
    Called by the Anatomy module (if you are using it) after all of
    the anatomy has been read into memory.  This connects the different
    object types together in many different ways.
    """
    global _partOfParentsForChild, _partOfChildrenForParent

    _partOfParentsForChild = {}
    _partOfChildrenForParent = {}

    for timedNode in Iterator():
        node = Nodes.getByOid(timedNode.getNodeOid())
        for childNode in Nodes.getPartOfChildrenForNode(node):
            childTimedNode = getByNodeStageOids(childNode.getOid(),
                                                timedNode.getStageOid())
            if childTimedNode:
                if childTimedNode in _partOfParentsForChild:
                    _partOfParentsForChild[childTimedNode].add(timedNode)
                else:
                    _partOfParentsForChild[childTimedNode] = sets.Set([timedNode])

        for parentNode in Nodes.getPartOfParentsForNode(node):
            parentTimedNode = getByNodeStageOids(parentNode.getOid(),
                                                 timedNode.getStageOid())
            if parentTimedNode:
                if parentTimedNode in _partOfChildrenForParent:
                    _partOfChildrenForParent[parentTimedNode].add(timedNode)
                else:
                    _partOfChildrenForParent[parentTimedNode] = sets.Set([timedNode])

    return
Ejemplo n.º 3
0
def _relCmp(rel1, rel2):
    """
    Sort comparison routine to compare 2 relationships.
    First sorts by rel sequence (if set), and then by
    child component names if sequence is a tie.
    """
    relType1 = rel1.getRelationshipType()
    relType2 = rel2.getRelationshipType()
    if relType1 != relType2:
        Util.fatalError([
            "Attempting to sort relationships of two different types:",
            "Type 1: " + relType1 + " Type 2: " + relType2
            ])
    if rel1.getParentOid() != rel2.getParentOid():
        Util.fatalError([
            "Attempting to sort relationships that have different parents."])
    seq1 = rel1.getSequence()
    seq2 = rel2.getSequence()
    if seq1 != seq2:
        # sort based purely on sequence
        if seq1 != None and seq2 != None:
            if seq1 < seq2:
                return -1
            elif seq1 > seq2:
                return +1
        elif seq1 != None:
            return -1
        elif seq2 != None:
            return +1
    else:
        # sequence same, sort based on name.
        if relType1 == PART_OF:
            return cmp(Nodes.getByOid(rel1.getChildOid()).getComponentName(),
                       Nodes.getByOid(rel2.getChildOid()).getComponentName())
        else:
            # assume relationship is between timed nodes.
            nodeOid1 = TimedNodes.getByOid(rel1.getChildOid()).getNodeOid()
            nodeOid2 = TimedNodes.getByOid(rel2.getChildOid()).getNodeOid()
            return cmp(Nodes.getByOid(nodeOid1).getComponentName(),
                       Nodes.getByOid(nodeOid2).getComponentName())
Ejemplo n.º 4
0
def _createImmediateRelsWhereChild(currentNode):
    """
    Create transitive relationships for immeditate relationships where the
    given node is the child.
    """
    currentOid = currentNode.getOid()

    # Add relationships from this node to its immediate parents
    relsWithParents = Relationships.getByChildOidRelType(
        currentOid, Relationships.PART_OF)
    for parentRel in relsWithParents:
        parentOid = parentRel.getParentOid()
        if not exists(Relationships.PART_OF, descendentOid = currentOid,
                      ancestorOid = parentOid):
            start, stop = Nodes.getStageWindowForNodeOid(currentOid)

            DbAccess.writeSql("/* direct */")
            createRelationshipTransitive(
                Relationships.PART_OF, descendentOid = currentOid,
                ancestorOid = parentOid)

            # Add relationships based on its parents' already defined
            # transitive relationships.  Transitive relationships
            # do not cross relationship types.
            ancestorRels = _byDescendentOidRelType.get((parentOid,
                                                        Relationships.PART_OF))
            if ancestorRels != None:
                for ancestorRel in ancestorRels.itervalues():
                    ancestorOid = ancestorRel.getAncestorOid()
                    ancestor = Nodes.getByOid(ancestorOid)
                    if not exists(relType = Relationships.PART_OF,
                                  descendentOid = currentOid,
                                  ancestorOid = ancestorOid):
                        # if reltype is part-of then only generate a transitive
                        # relationship if the ancestor and this node overlap in time.
                        # Group ancestors sometimes do not overlap with their
                        # descendents.
                        ancestorStart, ancestorStop = (
                            Nodes.getStageWindowForNodeOid(ancestorOid))
                        if (start.getSequence() > ancestorStop.getSequence() or
                            stop.getSequence()  < ancestorStart.getSequence()):
                            if not ancestor.isGroup():
                                # oops, supposed to be a group
                                Util.fatalError([
                                    "Ancestor stage window does not overlap " +
                                    "with descendent stage window and",
                                    "ancestor is not a group.",
                                    "Ancestor ID:   " + ancestor.getPublicId() +
                                    " Name: " + ancestor.getName(),
                                    "  Start-Stop: " + ancestorStart.getName() +
                                    "-" + ancestorStop.getName(),
                                    "Descendent ID: " + currentNode.getPublicId() +
                                    " Name: " + currentNode.getName(),
                                    "  Start-Stop: " + start.getName() +
                                    "-" + stop.getName()])
                        else:
                            DbAccess.writeSql("/* from ancestor */")
                            createRelationshipTransitive(Relationships.PART_OF,
                                                         descendentOid = currentOid,
                                                         ancestorOid = ancestorOid)
    return
Ejemplo n.º 5
0
def _relCmp(rel1, rel2):
    """
    Sort comparison routine to compare 2 relationships.
    First sorts by rel sequence (if set), and then by
    child component names if sequence is a tie.
    """
    relType1 = rel1.getRelationshipType()
    relType2 = rel2.getRelationshipType()
    #if relType1 != relType2:
    #    Util.fatalError([
    #        "Attempting to sort relationships of two different types:",
    #        "Type 1: " + relType1 + " Type 2: " + relType2
    #        ])
    if rel1.getParentOid() != rel2.getParentOid():
        Util.fatalError([
            "Attempting to sort relationships that have different parents."])

    relProj1 = AnaRelationshipProjectDb.getByRelationShipFkAndProject(rel1.getOid(), _sortProject)
    relProj2 = AnaRelationshipProjectDb.getByRelationShipFkAndProject(rel2.getOid(), _sortProject)

    seq1 = relProj1.getSequence()
    seq2 = relProj2.getSequence()

    #if seq1 != None and seq2 != None:
        #Util.statusMessage(["Relationship 1: " + str(rel1.getOid()) + "\n" +
        #                    "Type 1: " + relType1 + "\n" +
        #                    "Sequence 1: " + str(seq1) + "\n" +
        #                    "Project: " + _sortProject])
        #Util.statusMessage(["Relationship 2: " + str(rel2.getOid()) + "\n" +
        #                    "Type 2: " + relType2 + "\n" +
        #                    "Sequence 2: " + str(seq2) + "\n" + 
        #                    "Project: " + _sortProject])
    
    if seq1 != seq2:
        # sort based purely on sequence
        if seq1 != None and seq2 != None:
            if seq1 < seq2:
                return -1
            elif seq1 > seq2:
                return +1
        elif seq1 != None:
            return -1
        elif seq2 != None:
            return +1
    else:
        # sequence same, sort based on name.
        #if relType1 == IS_A:
        #if relType1 == PART_OF or relType1 == IS_A or relType1 == DERIVES_FROM or relType1 == DEVELOPS_FROM or relType1 == LOCATED_IN or relType1 == DEVELOPS_IN or relType1 == DISJOINT_FROM or relType1 == ATTACHED_TO or relType1 == HAS_PART:
        if relType1 == PART_OF or relType1 == IS_A or relType1 == DEVELOPS_FROM or relType1 == LOCATED_IN or relType1 == DEVELOPS_IN or relType1 == DISJOINT_FROM or relType1 == ATTACHED_TO or relType1 == HAS_PART:
        #if relType1 == PART_OF or relType1 == DERIVES_FROM or relType1 == DEVELOPS_FROM or relType1 == LOCATED_IN or relType1 == DEVELOPS_IN or relType1 == DISJOINT_FROM or relType1 == ATTACHED_TO or relType1 == HAS_PART:
            #Util.statusMessage(["HERE!"])
            #Util.statusMessage(["Relationship 1: " + str(rel1.getOid()) + "\n" +
            #                "Type 1: " + relType1 + "\n" +
            #                "Sequence 1: " + str(seq1) + "\n" +
            #                "Project: " + _sortProject])
            #Util.statusMessage(["Relationship 2: " + str(rel2.getOid()) + "\n" +
            #                "Type 2: " + relType2 + "\n" +
            #                "Sequence 2: " + str(seq2) + "\n" + 
            #                "Project: " + _sortProject])
            return cmp(Nodes.getByOid(rel1.getChildOid()).getComponentName(),
                       Nodes.getByOid(rel2.getChildOid()).getComponentName())
        else:
            # assume relationship is between timed nodes.
            Util.statusMessage(["THERE"])
            Util.statusMessage(["Relationship 1: " + str(rel1.getOid()) + "\n" +
                            "Type 1: " + relType1 + "\n" +
                            "Sequence 1: " + str(seq1) + "\n" +
                            "Project: " + _sortProject])
            Util.statusMessage(["Relationship 2: " + str(rel2.getOid()) + "\n" +
                            "Type 2: " + relType2 + "\n" +
                            "Sequence 2: " + str(seq2) + "\n" + 
                            "Project: " + _sortProject])
            nodeOid1 = TimedNodes.getByOid(rel1.getChildOid()).getNodeOid()
            nodeOid2 = TimedNodes.getByOid(rel2.getChildOid()).getNodeOid()
            return cmp(Nodes.getByOid(nodeOid1).getComponentName(),
                       Nodes.getByOid(nodeOid2).getComponentName())