#!/usr/bin/env python
import coral
from coral.modeler.xmidigraphbuilder import xmidibuilder
from coral.modeler.diagram import xmidi_diagrams

L = coral.lang.UML14
X = coral.lang.XMIDI20

st = coral.Project()
m = L.Model()
m.__setProject__(st)
m.name = "myModel"
p1 = L.Package()
p2 = L.Package()
p3 = L.Package()
p4 = L.Package()
p5 = L.Package()
p1.name = "P1"
p2.name = "P2"
p3.name = "P3"
p4.name = "P4"
p5.name = "P5"
m.ownedElement.add(p1)
p1.ownedElement.add(p2)
p2.ownedElement.add(p3)
p3.ownedElement.add(p4)
p4.ownedElement.add(p5)

# The False prevents spurious creation of the X.SemanticModelBridge element
# D = X.Diagram(False)
# D.__setProject__(st)

D = xmidi_diagrams.createDiagram(st, owner_elem=m, typeinfo="ClassDiagram")

# D.semanticModel = X.SimpleSemanticModelElement(typeInfo = "ClassDiagram")
# D.owner = X.Uml1SemanticModelBridge(element=m)
xmidibuilder(m, D)

print "D.contained:", D.contained
u = X.Uml1SemanticModelBridge
print "AAAARGH", p1.__getSlotByOppositeName__(u, "element")
for i in p1.__all_slots__():
    print i.getProperty().getName(), i
    o = i.getProperty().getOppositeProperty()
    if o:
        print "oppo", o.getOwner().getName(), o.getName()

assert len(D.contained) == 1
d_p1 = D.contained[0].contained[1].contained
print d_p1
assert len(d_p1) == 1
d_p2 = d_p1[0].contained[1].contained
assert len(d_p2) == 1
d_p3 = d_p2[0].contained[1].contained
assert len(d_p3) == 1
d_p4 = d_p3[0].contained[1].contained
assert len(d_p4) == 1

assert len(m.__getSlotByOppositeName__(u, "element")) > 0
assert len(p1.__getSlotByOppositeName__(u, "element")) > 0
assert len(p2.__getSlotByOppositeName__(u, "element")) > 0
assert len(p3.__getSlotByOppositeName__(u, "element")) > 0
assert len(p4.__getSlotByOppositeName__(u, "element")) > 0
assert len(p5.__getSlotByOppositeName__(u, "element")) > 0
