#!/usr/bin/env python
import time

import coral

s = coral.Project()

metamodel=coral.lang.UML14
m = metamodel.Model()
m.__setProject__(s)
m.name = "initial name"
m.__setXMIid__("DCE:12345")

TM = coral.getTransactionManager()

TM.beginModifyCmd()
try:
    m.name = "myModel"
    m.__setXMIid__("DCE:98765")
    assert(m.__XMIid__() == "DCE:98765")
finally:
    TM.endModifyCmd()

TM.undo()
assert(m.name == "initial name")
print m.__XMIid__()
assert(m.__XMIid__() == "DCE:12345")
TM.redo()
assert(m.name == "myModel")
TM.undo()

print "Everything ok."
print
print
TM.beginModifyCmd()
try:
    print "Creating class."
    c = metamodel.Class()
    c.__setProject__(s)
finally:
    TM.endModifyCmd()
c = None

print
print "now nothing should happen..."
TM.undo()

print
print 
print "But NOW a class should be deleted"
print
TM.beginModifyCmd()
try:
    m.name = "foo"
finally:
    TM.endModifyCmd()

TM.beginModifyCmd()
try:
    c = metamodel.Class()
    c.namespace = m
    c = None
finally:
    TM.endModifyCmd()

TM.undo()

print "M owns now: ", m.ownedElement
assert(m.ownedElement.__len__() == 0)

TM.trimFuture()

c = metamodel.Class()
c.__setProject__(s)
assert(c.isRoot == "false")
TM.beginModifyCmd()
try:
    c.isRoot = "true"
finally:
    TM.endModifyCmd()
assert(c.isRoot == "true")
TM.undo()
assert(c.isRoot == "false")
TM.redo()
assert(c.isRoot == "true")

print
print "End of test."
print
