#!/usr/bin/env python
#
# This test adds stereotypes to a class and checks whether
# they are synchronized properly into the XMIDI
# The same test would work for all similar properties, that
# have exactly the same structure in the XMIDI mapping (generated code)
#
#
from coral.modeler.diagram.degen import *
from coral import modeler
from coral.modeler.modeler import *
DI = coral.lang.XMIDI20
from coral.modeler.xmidigraphbuilder import xmidibuilder
from coral.modeler.diagram import xmidi_diagrams

L=coral.lang.UML14

m=ModelerWindow()
m.main(loop=0)

tm = coral.getTransactionManager()

tm.beginModifyCmd()
try:
    prj=coral.Project()
    
    m1=L.Model()
    m1.__setProject__(prj)
    c1 = L.Class(namespace=m1, name="C1")

    
    #create a Class Diagram for the model
    cd = xmidi_diagrams.createDiagram(prj,owner_elem=m1,typeinfo="ClassDiagram")
    assert cd
    xmidibuilder(m1, cd)
    
    s1 = L.Stereotype(namespace=m1, name="S1")
    s2 = L.Stereotype(namespace=m1, name="S2")
    s3 = L.Stereotype(namespace=m1, name="S3")
    
finally:
    tm.endModifyCmd()

m.getDiagramEditorManager().editorSet.notifyFocusOn(cd)
m.processEvents()


x_c1 = c1.xmidi_get_graphelements()[0]
nc, nc_idx = searchForSimpleGraphNode(x_c1.contained, "NameCompartment")

assert nc

stc, stc_idx = searchForSimpleGraphNode(nc.contained, "StereotypeCompartment")

assert not stc
assert stc_idx == -1

assert len(c1.stereotype) == 0

tm.beginModifyCmd()
try:
    c1.stereotype.append(s1)
finally:
    tm.endModifyCmd()

stereotypes = {}
for s in c1.stereotype:
    stereotypes[s] = True

stc, stc_idx = searchForSimpleGraphNode(nc.contained, "StereotypeCompartment")

assert stc

assert len(c1.stereotype) == 1
assert len(stc.contained) == 3
assert str(stc.contained[0].semanticModel.typeInfo) == "StereotypeStart"
assert stc.contained[1].semanticModel.element in stereotypes.keys()
assert str(stc.contained[2].semanticModel.typeInfo) == "StereotypeEnd"


tm.beginModifyCmd()
try:
    c1.stereotype.append(s2)
finally:
    tm.endModifyCmd()

stereotypes = {}
for s in c1.stereotype:
    stereotypes[s] = True


assert len(c1.stereotype) == 2
assert len(stc.contained) == 5
assert str(stc.contained[0].semanticModel.typeInfo) == "StereotypeStart"
assert stc.contained[1].semanticModel.element in stereotypes.keys()
del stereotypes[stc.contained[1].semanticModel.element]
assert str(stc.contained[2].semanticModel.typeInfo) == "StereotypeSeparator"
assert stc.contained[3].semanticModel.element in stereotypes.keys()
del stereotypes[stc.contained[3].semanticModel.element]
assert str(stc.contained[4].semanticModel.typeInfo) == "StereotypeEnd"


tm.beginModifyCmd()
try:
    c1.stereotype.append(s3)
finally:
    tm.endModifyCmd()

stereotypes = {}
for s in c1.stereotype:
    stereotypes[s] = True


assert len(c1.stereotype) == 3
assert len(stc.contained) == 7
assert str(stc.contained[0].semanticModel.typeInfo) == "StereotypeStart"
assert stc.contained[1].semanticModel.element in stereotypes.keys()
del stereotypes[stc.contained[1].semanticModel.element]
assert str(stc.contained[2].semanticModel.typeInfo) == "StereotypeSeparator"
assert stc.contained[3].semanticModel.element in stereotypes.keys()
del stereotypes[stc.contained[3].semanticModel.element]
assert str(stc.contained[4].semanticModel.typeInfo) == "StereotypeSeparator"
assert stc.contained[5].semanticModel.element in stereotypes.keys()
del stereotypes[stc.contained[5].semanticModel.element]
assert str(stc.contained[6].semanticModel.typeInfo) == "StereotypeEnd"


tm.beginModifyCmd()
try:
    c1.stereotype.remove(s2)
finally:
    tm.endModifyCmd()

stereotypes = {}
for s in c1.stereotype:
    stereotypes[s] = True


assert len(c1.stereotype) == 2
assert len(stc.contained) == 5
assert str(stc.contained[0].semanticModel.typeInfo) == "StereotypeStart"
assert stc.contained[1].semanticModel.element in stereotypes.keys()
del stereotypes[stc.contained[1].semanticModel.element]
assert str(stc.contained[2].semanticModel.typeInfo) == "StereotypeSeparator"
assert stc.contained[3].semanticModel.element in stereotypes.keys()
del stereotypes[stc.contained[3].semanticModel.element]
assert str(stc.contained[4].semanticModel.typeInfo) == "StereotypeEnd"

tm.beginModifyCmd()
try:
    c1.stereotype.remove(s1)
finally:
    tm.endModifyCmd()

stereotypes = {}
for s in c1.stereotype:
    stereotypes[s] = True


assert len(c1.stereotype) == 1
assert len(stc.contained) == 3
assert str(stc.contained[0].semanticModel.typeInfo) == "StereotypeStart"
assert stc.contained[1].semanticModel.element in stereotypes.keys()
del stereotypes[stc.contained[1].semanticModel.element]
assert str(stc.contained[2].semanticModel.typeInfo) == "StereotypeEnd"


tm.beginModifyCmd()
try:
    c1.stereotype.remove(s3)
finally:
    tm.endModifyCmd()


assert len(c1.stereotype) == 0

stc, stc_idx = searchForSimpleGraphNode(nc.contained, "StereotypeCompartment")

assert not stc
assert stc_idx == -1


