#!/usr/bin/env python

import coral
import coral.modeler.modeler

import coral.addins.constraint.engine as E

def find_by_name(name, prj):
    for root in prj.contents():
        for part in root.__getAllParts__().select(lambda p: not p.oclIsKindOf(coral.lang.CQUERY10.Query)):
            if hasattr(part, "name"):
                if part.name == name:
                    return part
    print "Did not find ",name

test_model_prj=coral.loadDocument("testSynchState.xml")
constraint_prj=coral.loadDocument("../UML14Constraints.xml")

#            target     constraint      number_of_failing
test_table=[("test1","SynchState[1]",1),
            ("test1","SynchState[2.1]",1),
            ("test1","SynchState[2.2]",1),
            #("test2","UML14StateChartConstraints",9)
            ]

CM=coral.lang.CONSTRAINT10.ConstraintModel()
Package=coral.lang.CONSTRAINT10.Package(element=[CM])

for test_target_name,constraint_name,failing in test_table:

    test_target=find_by_name(test_target_name,test_model_prj)

    CM.target=test_target

    test_constraint=find_by_name(constraint_name, constraint_prj)
    
    CM.constraint=[test_constraint]

    E.loadConstraintModel(CM)
    E.activateCM(CM)

    print test_target_name,len(CM.result)
    assert len(CM.result) == failing,str(len(CM.result))+" != "+ str(failing)

    E.deActivateCM(CM)
