#!/usr/bin/env python
import coral
import coral.modeler
import coral.modeler.modeler
import coral.addins.constraint.engine as E
UL = coral.lang.UML14
CL = coral.lang.CONSTRAINT10

# Evaluates constraints for Class. A testmodel is set to target and
# the constraints to be evaluated are appended to the constraintmodels
# constraint slot.
# The result slot must be of appropriate length after constraint evaluation.

constrprj = coral.loadDocument("../UML14Constraints.xml")
targetprj = coral.loadDocument("testClass.xml")

targetModel = None
for part in targetprj.contents():
    if part.oclIsKindOf(UL.Model):
        targetModel = part

testPack = CL.Package()
CM = CL.ConstraintModel(target = targetModel, package = testPack)

for part in constrprj.contents().select(lambda p:p.oclIsKindOf(CL.Package)):
    for elem in part.__getAllParts__().select(lambda l:l.oclIsKindOf(CL.Constraint)):
        if elem.name=="OperationAndMethod":
            CM.constraint.append(elem)
        if elem.name=="OwnedElementsOfAClass":
            CM.constraint.append(elem)

E.loadConstraintModel(CM)
E.activateCM(CM)
assert len(CM.result)==1
for r in CM.result:
    assert len(r.offending) == 1
