#!/usr/bin/env python
import os
import coral
L = coral.lang.UML14

s=coral.Project()
m=L.Model(name="m")

m.__setProject__(s)

c1=L.Class(name="c1",namespace=m)
c2=L.Class(name="c2")
c3=L.Class(name="c3")
p=L.Package(name="p",ownedElement=[c2,c3])


for e in [m,c1]:
    print e.name
    assert(e.__isConnectedTo__(m))

for e in [c2,c2,c3]:
    print e.name
    assert(not e.__isConnectedTo__(m))


print "test 3"
p.namespace=m

for e in m.__getAllParts__():
    print e.name
    assert(e.__isConnectedTo__(m))

#
tm = coral.getTransactionManager()

tm.beginModifyCmd()
try:
    p.__resetAttrs__()
finally:
    tm.endModifyCmd()
assert(not p.__isConnectedTo__(m))
tm.undo()
assert(p.__isConnectedTo__(m))
tm.redo()
assert(not p.__isConnectedTo__(m))
tm.undo()
assert(p.__isConnectedTo__(m))

coral.saveModel("tmp/testConnected2.xml", m)


model=coral.loadModel("testConnected.xml")

elements=model.__getAllParts__()

for e in elements:
    print e,e.__XMIid__()
    print "t1"
    assert(model.__isConnectedTo__(e))
    print "t2"
    assert(e.__isConnectedTo__(model))
                          
os.system("rm tmp/testConnected2.xml")
