#!/usr/bin/env python
import coral
import coral.modeler
import coral.modeler.modeler

## This is a huge relink associations test that
## attempts to test each possible scenario of a relink

TM = coral.getTransactionManager()

## Checking that original model is OK

prj = coral.loadDocument("testAssociationEndsRelinking.xml")

class_diagram = prj.contents()[1]
model = prj.contents()[0]

L = coral.lang.UML14
DI = coral.lang.XMIDI20
assert model.oclIsKindOf(L.Model)
assert class_diagram.oclIsKindOf(DI.Diagram)

a = model.ownedElement.select(lambda x: x.name == "A")[0]
b = model.ownedElement.select(lambda x: x.name == "B")[0]
c = model.ownedElement.select(lambda x: x.name == "C")[0]
d = model.ownedElement.select(lambda x: x.name == "D")[0]
ass = model.ownedElement.select(lambda x: x.oclIsKindOf(L.Association))[0]


assert a
assert b
assert c
assert d
assert ass

assert len(ass.connection) == 2

foo = ass.connection[0]
bar = ass.connection[1]

assert foo
assert bar
assert foo.participant == a
assert bar.participant == b

xa = a.xmidi_get_graphelements()[0]
xb = b.xmidi_get_graphelements()[0]
xc = c.xmidi_get_graphelements()[0]
xd = d.xmidi_get_graphelements()[0]
xass = ass.xmidi_get_graphelements()[0]

assert xa
assert xb
assert xc
assert xd
assert xass

assert len(xass.anchor) == 2
assert xass.anchor[0].graphElement == xa
assert xass.anchor[1].graphElement == xb


#1 Relink first end (foo) from A to C
TM.beginModifyCmd()
try:
    foo.participant = c
finally:
    TM.endModifyCmd()

# Check model
assert foo.participant == c
assert bar.participant == b

# Check XMI-DI
assert len(xass.anchor) == 2
assert xass.anchor[0].graphElement == xc
assert xass.anchor[1].graphElement == xb

# Check we have no left-over graphconnectors
assert len(xa.anchorage) == 0
assert len(xb.anchorage) == 1
assert len(xc.anchorage) == 1
assert len(xd.anchorage) == 0


#2 Relink second end (bar) from B to D
TM.beginModifyCmd()
try:
    bar.participant = d
finally:
    TM.endModifyCmd()

# Check model
assert foo.participant == c
assert bar.participant == d

# Check XMI-DI
assert len(xass.anchor) == 2
assert xass.anchor[0].graphElement == xc
assert xass.anchor[1].graphElement == xd

# Check we have no left-over graphconnectors
assert len(xa.anchorage) == 0
assert len(xb.anchorage) == 0
assert len(xc.anchorage) == 1
assert len(xd.anchorage) == 1

#3 Swap participants for foo and bar
TM.beginModifyCmd()
try:
    foo.participant, bar.participant = bar.participant, foo.participant
finally:
    TM.endModifyCmd()

# Check model
assert foo.participant == d
assert bar.participant == c

# Check XMI-DI
assert len(xass.anchor) == 2
assert xass.anchor[0].graphElement == xd
assert xass.anchor[1].graphElement == xc

# Check we have no left-over graphconnectors
assert len(xa.anchorage) == 0
assert len(xb.anchorage) == 0
assert len(xc.anchorage) == 1
assert len(xd.anchorage) == 1

#4 Move association from D-C to A-B
TM.beginModifyCmd()
try:
    foo.participant, bar.participant = a, b
finally:
    TM.endModifyCmd()

# Check model
assert foo.participant == a
assert bar.participant == b

# Check XMI-DI
assert len(xass.anchor) == 2
assert xass.anchor[0].graphElement == xa
assert xass.anchor[1].graphElement == xb

# Check we have no left-over graphconnectors
assert len(xa.anchorage) == 1
assert len(xb.anchorage) == 1
assert len(xc.anchorage) == 0
assert len(xd.anchorage) == 0


#5 Relink second end (bar) also to A
TM.beginModifyCmd()
try:
    bar.participant = a
finally:
    TM.endModifyCmd()

# Check model
assert foo.participant == a
assert bar.participant == a

# Check XMI-DI
assert len(xass.anchor) == 2
assert xass.anchor[0].graphElement == xa
assert xass.anchor[1].graphElement == xa

# Check we have no left-over graphconnectors
assert len(xa.anchorage) == 2
assert len(xb.anchorage) == 0
assert len(xc.anchorage) == 0
assert len(xd.anchorage) == 0

#6 Move association from A-A to B-B
TM.beginModifyCmd()
try:
    foo.participant, bar.participant = b, b
finally:
    TM.endModifyCmd()

# Check model
assert foo.participant == b
assert bar.participant == b

# Check XMI-DI
assert len(xass.anchor) == 2
assert xass.anchor[0].graphElement == xb
assert xass.anchor[1].graphElement == xb

# Check we have no left-over graphconnectors
assert len(xa.anchorage) == 0
assert len(xb.anchorage) == 2
assert len(xc.anchorage) == 0
assert len(xd.anchorage) == 0

#7 Move second end (bar) to C
TM.beginModifyCmd()
try:
    bar.participant = c
finally:
    TM.endModifyCmd()

# Check model
assert foo.participant == b
assert bar.participant == c

# Check XMI-DI
assert len(xass.anchor) == 2
assert xass.anchor[0].graphElement == xb
assert xass.anchor[1].graphElement == xc

# Check we have no left-over graphconnectors
assert len(xa.anchorage) == 0
assert len(xb.anchorage) == 1
assert len(xc.anchorage) == 1
assert len(xd.anchorage) == 0

