#!/usr/bin/env python
import coral
import coral.modeler
import coral.modeler.modeler

TM = coral.getTransactionManager()

prj = coral.loadDocument("ordered_association.xmi")

class_diagram = prj.contents()[1]
model = prj.contents()[0]

L = coral.lang.UML14
DI = coral.lang.XMIDI20
assert model.oclIsKindOf(L.Model)
assert class_diagram.oclIsKindOf(DI.Diagram)

assoc = model.ownedElement.select(lambda x: x.oclIsKindOf(L.Association))[0]

ae1 = assoc.connection[0]
ae2 = assoc.connection[1]

assert ae2.name == "hello"
assert ae2.ordering == "ordered"

x_ae2 = ae2.xmidi_get_graphelements()[0]
assert len(x_ae2.contained) == 4

TM.beginModifyCmd()
try:
    ae2.ordering = "unordered"
finally:
    TM.endModifyCmd()
assert len(x_ae2.contained) == 3


TM.beginModifyCmd()
try:
    ae2.ordering = "ordered"
finally:
    TM.endModifyCmd()
assert len(x_ae2.contained) == 4

comp = x_ae2.contained[3]
assert comp.semanticModel.typeInfo == "ExpressionCompartment"

assert comp.contained[0].semanticModel.typeInfo == "ExpressionStart"
assert comp.contained[1].semanticModel.typeInfo == "Ordering"
assert comp.contained[2].semanticModel.typeInfo == "ExpressionEnd"

