# This file is part of the Coral project.
# Please read the copyright notice in the LICENSE file.

__namePool={}
def createUniqueName(eSet, prefix):
    """eSet is a slot with named elements. Prefix is a name prefix.
    Examples:
    createUniqueName(myPackage.ownedElement, "Class")
    """
    global __namePool
    if not __namePool.has_key(prefix):
        __namePool[prefix]=0

    i=1
    while 1:
        __namePool[prefix]+=i
        candidate=prefix+str(__namePool[prefix])
        if not eSet.exists(lambda e: e.name==candidate):
            return candidate
        i=i*2

import coral
#
#
# The following used by language generators and
# similar applications which move between metamodels and models.
#
def createCoralUUID(element_definition):
    """Returns a nicey name for an AbstractElementDefinition
    AAU:coral::UML20::Package1::Package2::AbstractElementDefinitionName
    """
    name = []
    p = element_definition
    while p is not None:
        if isinstance(p, coral.Language):
            name.append(str(p.getName())
                        + str(p.getVersion())
                        + str(p.getRevision()))
        else:
            name.append(str(p.getName()))
        p = p.__package__()
    name.reverse()
    return "AAU:coral::" + '::'.join(name)

def createCoralPropertyUUID(a_property):
    """Returns a nicey name for a Property
    AAU:coral::UML20::Namespace::ownedElement
    """
    name = []
    name.append(str(a_property.getName()))

    p = a_property.getOwner()
    while p is not None:
        if isinstance(p, coral.Language):
            name.append(str(p.getName())
                        + str(p.getVersion())
                        + str(p.getRevision()))
        else:
            name.append(str(p.getName()))
        p = p.__package__()
    name.reverse()
    return "AAU:coral::" + '::'.join(name)

