#!/usr/bin/env python
import coral

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

UML14 = coral.lang.UML14
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")
P = coral.Property("owned", coral.AnyElement, "", 0, -1, coral.kind_t.KIND_ORDERED | coral.kind_t.KIND_COMPOSITION)
M.addPropertyRecursive(P)

m = M()
m.__setProject__(st)
a1 = CORE.AnyElement()
a2 = CORE.AnyAbstractElement()
c1 = UML14.Class()
c1.name = "myclass"
attr = UML14.Attribute()
attr.name = "myattribute"
c1.feature.append(attr)

m.owned.append(a1)
m.owned.append(a2)
m.owned.append(c1)

coral.saveDocument(st)
st.invalidate()

st = coral.loadDocument("tmp/mixing_metamodels.xmi")
m = st.contents()[0]
a1 = m.owned[0]
a2 = m.owned[1]
c1 = m.owned[2]
attr1 = c1.feature[0]
assert a1
assert a1.__type__() == CORE.AnyElement
assert a2
assert a2.__type__() == CORE.AnyAbstractElement
assert c1
assert c1.__type__() == UML14.Class
assert c1.name == "myclass"
assert attr1
assert attr1.__type__() == UML14.Attribute
assert attr1.name == "myattribute"
