#!/usr/bin/env python
#
#
#
import coral
import time
import qt

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

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

all = coral.Set()

for r in p.contents():
    all.unionWith(r.__getAllParts__())

assoc = all.select(lambda x: x.oclIsKindOf(L.Association))[0]
assert assoc

x_assoc = assoc.__getSlotByOppositeName__(DI.Uml1SemanticModelBridge, "element").graphElement[0]


tm = coral.getTransactionManager()

def assert_stuff(assoc):
    print "Association:  ", assoc
    c0 = assoc.connection[0]
    c1 = assoc.connection[1]
    print "Connection0:  ", c0
    print "Connection1:  ", c1
    if c0:
        print "Association0: ", c0.association
    if c1:
        print "Association1: ", c1.association
    assert len(assoc.connection) == 2
    assert assoc.connection[0].association != None
    assert assoc.connection[1].association != None
    assert assoc.connection[0] != assoc.connection[1]
    assert assoc.connection[0].association == assoc
    assert assoc.connection[1].association == assoc
    print

assert_stuff(assoc)

tm.beginModifyCmd()
try:
    assoc.connection[0], assoc.connection[1] = assoc.connection[1], assoc.connection[0]
finally:
    tm.endModifyCmd()

assert_stuff(assoc)

tm.beginModifyCmd()
try:
    assoc.connection[0], assoc.connection[1] = assoc.connection[1], assoc.connection[0]
finally:
    tm.endModifyCmd()

assert_stuff(assoc)
