#!/usr/bin/env python
import coral

st = coral.Project()
st.setFilename("tmp/mixing_metamodels2.xmi")
st.setXMIFormat("2.0")

UML15 = coral.lang.UML15
UML14 = coral.lang.UML14
UML13 = coral.lang.UML13
CORE = coral.lang.CORE01

L = coral.Language("Foobar", 0, 1)
L.setXMLNS("http://www.abo.fi/test/test/test/foobar01/")
M = coral.ElementDefinition(L, "MyMetaClass")
M.addSuperclass(coral.AnyElement)
P = coral.Property("owned", coral.AnyElement, "", 0, -1, coral.kind_t.KIND_ORDERED | coral.kind_t.KIND_COMPOSITION)
M.addPropertyRecursive(P)

L2 = coral.Language("Foobar", 0, 2)
L2.setXMLNS("http://www.abo.fi/test/test/test/foobar02/")
M2 = coral.ElementDefinition(L2, "MyMetaClass")
M2.addSuperclass(coral.AnyElement)
P2 = coral.Property("owned", coral.AnyElement, "", 0, -1, coral.kind_t.KIND_ORDERED | coral.kind_t.KIND_COMPOSITION)
M2.addPropertyRecursive(P2)

print "OK OK OK"

m = M()
m.__setProject__(st)
a1 = CORE.AnyElement()
a2 = CORE.AnyAbstractElement()
c1 = UML13.Class()
c1.name = "myclass13"
c2 = UML14.Class()
c2.name = "myclass14"
c3 = UML15.Class()
c3.name = "myclass15"
m2 = M2()
m3 = M()
m4 = M2()

m.owned.append(a1)
m.owned.append(a2)
m.owned.append(c1)
m.owned.append(c2)
m.owned.append(c3)
m.owned.append(m2)
m2.owned.append(m3)
m3.owned.append(m4)

coral.saveDocument(st)
st.invalidate()

st = coral.loadDocument("tmp/mixing_metamodels2.xmi")
m = st.contents()[0]
a1 = m.owned[0]
a2 = m.owned[1]
c1 = m.owned[2]
c2 = m.owned[3]
c3 = m.owned[4]
m2 = m.owned[5]
m3 = m2.owned[0]
m4 = m3.owned[0]

assert a1
assert a1.__type__() == CORE.AnyElement
assert a2
assert a2.__type__() == CORE.AnyAbstractElement
assert c1
assert c1.__type__() == UML13.Class
assert c1.name == "myclass13"
assert c2
assert c2.__type__() == UML14.Class
assert c2.name == "myclass14"
assert c3
assert c3.__type__() == UML15.Class
assert c3.name == "myclass15"
assert m2
assert m2.__type__() == L2.MyMetaClass
assert m3
assert m3.__type__() == L.MyMetaClass
assert m4
assert m4.__type__() == L2.MyMetaClass
