#!/usr/bin/env python

import coral
import coral.modeler
import coral.modeler.modeler
from coral.addins.constraint.engine import *

TM=coral.getTransactionManager()

loadModelFromFile("testEngineConstraints.xml")
#printConstraints()

#assert len(getConstraintsByName("DiagramaticConstraint")) == 2
assert len(getConstraintsByName("QueryScriptConstraint")) == 1
assert len(getConstraintsByName("PythonConstraint")) == 3

#assert len(getConstraintsByType(coral.lang.CONSTRAINT10.DiagramaticConstraint)) == 2
assert len(getConstraintsByType(coral.lang.CONSTRAINT10.PythonConstraint)) == 3
assert len(getConstraintsByType(coral.lang.CONSTRAINT10.QueryScriptConstraint)) == 1

prj = coral.loadDocument("testEngineTarget.xml")
for elem in prj.contents():
     if elem.oclIsKindOf(coral.lang.UML14.Model):
         target = elem
assert target

results=[]
TM.beginModifyCmd()
try:

     resultCM = L.ConstraintModel()
     for constr in getConstraintsByName("PythonConstraint"):
         print "checking ", constr.name
         results+=checkSingleConstraint(constr, target)
         resultCM.constraint.append(constr)

     for constr in getConstraintsByName("QueryScriptConstraint"):
         print "checking ", constr.name
         results+=checkSingleConstraint(constr, target)
         resultCM.constraint.append(constr)

         if len(results) != 0:
              outputprj = coral.Project()
              for res in results:
                   res.constraintmodel = resultCM
              resultPackage = L.Package()
              resultCM.package = resultPackage
              resultPackage.__setProject__(outputprj)
              outputprj.setFilename("tmp/pc_output.xml")
              outputprj.saveToDocument()

finally:
     TM.endModifyCmd()

assert len(results) == 5, "Incorrect number of result elements created"

