
import coral
import random
DI = coral.lang.XMIDI20

def clone(transformation):

    inputModel=transformation.inputParameter[0].source

    c=coral.Set()
    c.insert(inputModel.model)
    for d in inputModel.diagram:
        c.insert(d)


    for op in transformation.outputParameter:
        outputModel=op.target

        outputModel.model=None
        outputModel.diagram=[]
      
        copy=coral.ModelCopy(c)
    
        for e in copy:
            if e.oclIsKindOf(DI.Diagram):
                outputModel.diagram.insert(e)
            else:
                assert outputModel.model is None
                outputModel.model=e

def mergeUML14(transformation):

    outputModel=transformation.outputParameter[0].target
    outputModel.model=coral.lang.UML14.Model(name="")
    outputModel.diagram=[]
    
    c=coral.Set()

    for ip in transformation.inputParameter:
        inputModel=ip.source

        if outputModel.model.name=="":
            outputModel.model.name=inputModel.name
        else:
            outputModel.model.name=outputModel.model.name+"+"+inputModel.name
        for e in inputModel.model.ownedElement:        
            c.insert(e)
        for e in inputModel.diagram:        
            c.insert(e)
                
    copy=coral.ModelCopy(c)
    
    outputModel.name=outputModel.model.name
    
    for e in copy:
        if e.oclIsKindOf(DI.Diagram):
            outputModel.diagram.insert(e)
        else:
            outputModel.model.ownedElement.insert(e)
            

def crash(transformatiom):
    if random.random()>0.7:
        print 42 /0
        
    
