#!/usr/bin/env python
import coral

prj = coral.loadDocument("testCreateResults.xml")
for elem in prj.contents():
    for part in elem.__getAllParts__().select(lambda p:p.oclIsKindOf(coral.lang.CONSTRAINT10.ConstraintModel)):
        for con in part.constraint:
            if con.oclIsKindOf(coral.lang.CONSTRAINT10.QueryScriptConstraint):
                try:
                    part.result.append(coral.lang.CONSTRAINT10.Result(name=con.name+"Result", message=con.script))
                except:
                    assert 0, "Error adding result"
            elif con.oclIsKindOf(coral.lang.CONSTRAINT10.PythonConstraint):
                try:
                    part.result.append(coral.lang.CONSTRAINT10.Result(name=con.name+"Result", message=con.script))
                except:
                    assert 0, "Error adding result"
            else:
                try:
                    part.result.append(coral.lang.CONSTRAINT10.Result(name = con.name+"Result"))
                except:
                    assert 0, "Error adding result"

    for part in elem.__getAllParts__().select(lambda p:p.oclIsKindOf(coral.lang.CONSTRAINT10.ConstraintModel)):
        assert len(part.result) == len(part.constraint), "Incorrect number of result elements in "+part.name

try:                
    prj.setFilename("tmp/testCreateResults_Output.xml")
    prj.saveToDocument()
except:
    assert 0, "Error creating output file"
