#!/usr/bin/env python
#

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()
    
    sm=L.StateMachine()
    sm.__setProject__(prj)
    topstate = L.CompositeState()
    sm.top = topstate
    
    ## Create a StateDiagram from the model
    cd1 = xmidi_diagrams.createDiagram(prj,owner_elem=sm,typeinfo="StateDiagram")
    cd2 = xmidi_diagrams.createDiagram(prj,owner_elem=sm,typeinfo="StateDiagram")
    
    assert cd1
    assert cd2
    
finally:
    tm.endModifyCmd()

m.getDiagramEditorManager().editorSet.notifyFocusOn(cd1)

m.processEvents()

m.getDiagramEditorManager().editorSet.notifyFocusOn(cd2)

m.processEvents()


assert len(topstate.subvertex) == 0
assert len(cd1.contained) == 0
assert len(cd2.contained) == 0

tm.beginModifyCmd()
try:
    s1 = L.SimpleState(name="S1")
    topstate.subvertex.insert(s1)
    
finally:
    tm.endModifyCmd()

x_s1 = s1.xmidi_get_graphelements()

assert len(topstate.subvertex) == 1
assert len(x_s1) == 2
assert len(cd1.contained) == 1
assert len(cd2.contained) == 1


tm.beginModifyCmd()
try:
    s2 = L.SimpleState(name="S2")
    topstate.subvertex.insert(s2)
    
finally:
    tm.endModifyCmd()

x_s1 = s1.xmidi_get_graphelements()
x_s2 = s2.xmidi_get_graphelements()

assert len(topstate.subvertex) == 2
assert len(x_s1) == 2
assert len(x_s2) == 2
assert len(cd1.contained) == 2
assert len(cd2.contained) == 2

m.processEvents()


tm.beginModifyCmd()
try:
    s3 = L.SimpleState(name="S3")
    topstate.subvertex.insert(s3)
    
finally:
    tm.endModifyCmd()

x_s1 = s1.xmidi_get_graphelements()
x_s2 = s2.xmidi_get_graphelements()
x_s3 = s3.xmidi_get_graphelements()

assert len(topstate.subvertex) == 3
assert len(x_s1) == 2
assert len(x_s2) == 2
assert len(x_s3) == 2
assert len(cd1.contained) == 3
assert len(cd2.contained) == 3


tm.beginModifyCmd()
try:
    s4 = L.SimpleState(name="S4")
    topstate.subvertex.insert(s4)
    
finally:
    tm.endModifyCmd()

x_s1 = s1.xmidi_get_graphelements()
x_s2 = s2.xmidi_get_graphelements()
x_s3 = s3.xmidi_get_graphelements()
x_s4 = s4.xmidi_get_graphelements()

assert len(topstate.subvertex) == 4
assert len(x_s1) == 2
assert len(x_s2) == 2
assert len(x_s3) == 2
assert len(x_s4) == 2
assert len(cd1.contained) == 4
assert len(cd2.contained) == 4

m.processEvents()


tm.beginModifyCmd()
try:
    topstate.subvertex.remove(s1)
finally:
    tm.endModifyCmd()

x_s1 = s1.xmidi_get_graphelements()
x_s2 = s2.xmidi_get_graphelements()
x_s3 = s3.xmidi_get_graphelements()
x_s4 = s4.xmidi_get_graphelements()

assert len(topstate.subvertex) == 3
assert len(x_s1) == 0
assert len(x_s2) == 2
assert len(x_s3) == 2
assert len(x_s4) == 2
assert len(cd1.contained) == 3
assert len(cd2.contained) == 3


tm.beginModifyCmd()
try:
    topstate.subvertex.remove(s2)
finally:
    tm.endModifyCmd()

x_s1 = s1.xmidi_get_graphelements()
x_s2 = s2.xmidi_get_graphelements()
x_s3 = s3.xmidi_get_graphelements()
x_s4 = s4.xmidi_get_graphelements()

assert len(topstate.subvertex) == 2
assert len(x_s1) == 0
assert len(x_s2) == 0
assert len(x_s3) == 2
assert len(x_s4) == 2
assert len(cd1.contained) == 2
assert len(cd2.contained) == 2

m.processEvents()


tm.beginModifyCmd()
try:
    topstate.subvertex.remove(s3)
    topstate.subvertex.remove(s4)
finally:
    tm.endModifyCmd()

x_s1 = s1.xmidi_get_graphelements()
x_s2 = s2.xmidi_get_graphelements()
x_s3 = s3.xmidi_get_graphelements()
x_s4 = s4.xmidi_get_graphelements()

assert len(topstate.subvertex) == 0
assert len(x_s1) == 0
assert len(x_s2) == 0
assert len(x_s3) == 0
assert len(x_s4) == 0
assert len(cd1.contained) == 0
assert len(cd2.contained) == 0

