#!/usr/bin/env python
#
# This test changes a package's ownedElement and makes sure that
# the GraphEdge is correctly kept in the BodyCompartment.
# For some reason the XMIDI synchronization system removes it
#
# Additionally, it relinks the association and checks that the
# GraphEdge moved accordingly.
#
#
import coral
import time
import qt
from coral.modeler import modeler

DI = coral.lang.XMIDI20
L = coral.lang.UML14


mw = modeler.ModelerWindow()
mw.main(loop=0)

p = coral.loadDocument("association_in_bodycompartment.xml")

diagram = p.contents()[1]
model = p.contents()[0]
package = model.ownedElement.select(lambda x: x.oclIsKindOf(L.Package))[0]

all = model.__getAllParts__()

dv = mw.displayDiagram(diagram)

bodycompartment = diagram.contained[0].contained[1]

assert len(bodycompartment.contained) == 3
assert len(diagram.contained) == 2

#
# Here, do a dummy change that triggers XMIDI synchronization 
#
time.sleep(1)
TM = coral.getTransactionManager()
TM.beginModifyCmd()
try:
	package.ownedElement.add(L.Class())
finally:
	TM.endModifyCmd()

#
# The GraphEdge should still be in the bodycompartment
#
assert len(bodycompartment.contained) == 3
assert len(diagram.contained) == 2

time.sleep(1)

association = all.select(lambda x: x.oclIsKindOf(L.Association) and x.name == "A")[0]
targetClass = all.select(lambda x: x.oclIsKindOf(L.Class) and x.name == "C")[0]
#
# Relink
#
TM.beginModifyCmd()
try:
	association.connection[0].participant = targetClass
finally:
	TM.endModifyCmd()
time.sleep(1)

#
# After relinking, the GraphEdge should've moved.
#
assert len(bodycompartment.contained) == 2
assert len(diagram.contained) == 3


