#!/usr/bin/env python
import coral
L = coral.lang.UML14

m = L.Model()
c = L.Class()
c.namespace = m
c2 = L.Class()
c2.namespace = m

i = L.Instance()
i.namespace = m
i2 = L.Instance()
i2.namespace = m
c.instance.append(i)
c.instance.append(i2)
c2.instance.append(i)
c2.instance.append(i2)

# The correct order
o = coral.Set()
o.add(i)
o.add(i2)
assert(c.instance.getCollection().setDifference(o).size() == 0)
assert(c2.instance.getCollection().setDifference(o).size() == 0)

m.isWellFormed()

ss = coral.Set()
ss.add(c)
ss.add(c2)

new_classes = coral.ModelCopy(ss)

print "---------------"
print m.ownedElement
nc1, nc2 = new_classes[0], new_classes[1]

assert(nc1.namespace == m)
assert(nc2.namespace == m)
assert(len(nc1.instance) == 2)
assert(len(nc2.instance) == 2)
assert(nc1.instance[0] == c.instance[0] or nc1.instance[0] == c.instance[1])
assert(nc1.instance[1] == c.instance[1] or nc1.instance[1] == c.instance[0])
assert(nc2.instance[0] == c.instance[0] or nc2.instance[0] == c.instance[1])
assert(nc2.instance[1] == c.instance[1] or nc2.instance[1] == c.instance[0])
#
# This is the problematic part
# Our association in c.instance pointed
# outside out modelcopy subtree.
# And there is nothing currently in modelcopy
# to fix this.
# => i.classifier still only contains [c],
# not [c, c2]
#
print i.classifier
print i2.classifier
assert(len(i.classifier) == 4)
assert(len(i2.classifier) == 4)

## #
## # Furthermore, I think it would be cool to keep
## # the order from the outside association
## # This is implemented by the dict3 feature in modelcopy,
## # but currently it is unused
## #
## print nc1, nc2
## print i.classifier
## assert(i.classifier[2] == nc1)
## assert(i.classifier[3] == nc2)
