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

TM = coral.getTransactionManager()

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

## Checking that original model is OK

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

model = prj.contents()[0]
diagram1 = model.xmidi_get_diagrams()[0]

print diagram1

assert model.oclIsKindOf(L.Model)
assert diagram1.oclIsKindOf(DI.Diagram)

c1 = model.ownedElement.select(lambda x: x.name == "C1")[0]
c2 = model.ownedElement.select(lambda x: x.name == "C2")[0]
c3 = model.ownedElement.select(lambda x: x.name == "C3")[0]
c4 = model.ownedElement.select(lambda x: x.name == "C4")[0]
c5 = model.ownedElement.select(lambda x: x.name == "C5")[0]
c6 = model.ownedElement.select(lambda x: x.name == "C6")[0]
c7 = model.ownedElement.select(lambda x: x.name == "C7")[0]
c8 = model.ownedElement.select(lambda x: x.name == "C8")[0]
p1 = model.ownedElement.select(lambda x: x.name == "P1")[0]


assert c1
assert c2
assert c3
assert c4
assert c5
assert c6
assert c7
assert c8
assert p1

xc1 = c1.xmidi_get_graphelements()[0]
xc2 = c2.xmidi_get_graphelements()[0]
xc3 = c3.xmidi_get_graphelements()[0]
xc4 = c4.xmidi_get_graphelements()[0]
xc5 = c5.xmidi_get_graphelements()[0]
xc6 = c6.xmidi_get_graphelements()[0]
xc7 = c7.xmidi_get_graphelements()[0]
xc8 = c8.xmidi_get_graphelements()[0]
xp1 = p1.xmidi_get_graphelements()[0]

assert xp1
xp1bc = xp1.contained[1]

assert xp1bc

diagram2 = p1.xmidi_get_diagrams()[0]

assert xc1 in diagram1.contained
assert xc2 in diagram1.contained
assert xc3 in diagram1.contained
assert xc4 in diagram1.contained
assert xc5 in diagram1.contained
assert xc6 in diagram1.contained
assert xc7 in diagram1.contained
assert xc8 in diagram1.contained
assert xp1 in diagram1.contained


assert len(diagram1.contained) == 9
assert len(diagram2.contained) == 0

## Then, one by one move the class into the package

## Move C1
TM.beginModifyCmd()
try:
    p1.ownedElement.append(c1)
finally:
    TM.endModifyCmd()

assert xc1 not in xp1bc.contained
assert xc1 in diagram1.contained

assert len(diagram1.contained) == 9
assert len(diagram2.contained) == 0
assert len(xp1bc.contained) == 0

## Move C2
TM.beginModifyCmd()
try:
    p1.ownedElement.append(c2)
finally:
    TM.endModifyCmd()

assert xc2 not in xp1bc.contained
assert xc2 in diagram1.contained

assert len(diagram1.contained) == 9
assert len(diagram2.contained) == 0
assert len(xp1bc.contained) == 0

## Move C3
TM.beginModifyCmd()
try:
    p1.ownedElement.append(c3)
finally:
    TM.endModifyCmd()

assert xc3 not in xp1bc.contained
assert xc3 in diagram1.contained

assert len(diagram1.contained) == 9
assert len(diagram2.contained) == 0
assert len(xp1bc.contained) == 0

## Move C4
TM.beginModifyCmd()
try:
    p1.ownedElement.append(c4)
finally:
    TM.endModifyCmd()

assert xc4 not in xp1bc.contained
assert xc4 in diagram1.contained

assert len(diagram1.contained) == 9
assert len(diagram2.contained) == 0
assert len(xp1bc.contained) == 0

## Move C5
TM.beginModifyCmd()
try:
    p1.ownedElement.append(c5)
finally:
    TM.endModifyCmd()

assert xc5 not in xp1bc.contained
assert xc5 in diagram1.contained

assert len(diagram1.contained) == 9
assert len(diagram2.contained) == 0
assert len(xp1bc.contained) == 0

## Move C6
TM.beginModifyCmd()
try:
    p1.ownedElement.append(c6)
finally:
    TM.endModifyCmd()

assert xc6 not in xp1bc.contained
assert xc6 in diagram1.contained

assert len(diagram1.contained) == 9
assert len(diagram2.contained) == 0
assert len(xp1bc.contained) == 0

## Move C7
TM.beginModifyCmd()
try:
    p1.ownedElement.append(c7)
finally:
    TM.endModifyCmd()

assert xc7 not in xp1bc.contained
assert xc7 in diagram1.contained

assert len(diagram1.contained) == 9
assert len(diagram2.contained) == 0
assert len(xp1bc.contained) == 0

## Move C8
TM.beginModifyCmd()
try:
    p1.ownedElement.append(c8)
finally:
    TM.endModifyCmd()

assert xc8 not in xp1bc.contained
assert xc8 in diagram1.contained

assert len(diagram1.contained) == 9
assert len(diagram2.contained) == 0
assert len(xp1bc.contained) == 0

