#!/usr/bin/env python
import coral
DI = coral.lang.XMIDI20
TM = coral.getTransactionManager()

case = 0

st = coral.Project()
edge = DI.GraphEdge()
edge.__setProject__(st)

def f(x):
    global TM
    global case
    global DI
    global edge
    
    tr = TM.getCurrentTransaction()
    assert tr
    print "tr.size()", tr.size()
    for cmd in tr:
        print cmd

    case += 1
    if case == 1:
        assert tr.size() == 2
        assert tr[0].getSlot().getProperty() == DI.Point.getPropertyByName("x")
        assert tr[1].getSlot().getProperty() == DI.Point.getPropertyByName("y")
        assert tr[0].getSlot() != getattr(edge, "waypoints")
        assert tr[1].getSlot() != getattr(edge, "waypoints")
        assert tr[0].getSlot().size() == 1
        assert tr[1].getSlot().size() == 1
        assert tr[0].getSlot()[0] == 0
        assert tr[1].getSlot()[0] == 0
        assert edge.waypoints[0].x == 30
        assert edge.waypoints[0].y == 50
    if case == 2:
        assert tr.size() == 2
        assert tr[0].getSlot().getProperty() == DI.Point.getPropertyByName("x")
        assert tr[1].getSlot().getProperty() == DI.Point.getPropertyByName("y")
        assert tr[0].getSlot() != getattr(edge, "waypoints")
        assert tr[1].getSlot() != getattr(edge, "waypoints")
        assert tr[0].getSlot().size() == 1
        assert tr[1].getSlot().size() == 1
        assert tr[0].getSlot()[0] == 30
        assert tr[1].getSlot()[0] == 50
        assert edge.waypoints[0].x == 60
        assert edge.waypoints[0].y == 100



TM.addObserver(f)

for i in xrange(2):
    edge.waypoints.append(DI.Point())

TM.beginModifyCmd()
try:
    edge.waypoints[0].x = 30.0
    edge.waypoints[0].y = 50.0
finally:
    TM.endModifyCmd()

TM.beginModifyCmd()
try:
    edge.waypoints[0].x = 60.0
    edge.waypoints[0].y = 100.0
finally:
    TM.endModifyCmd()
