#!/usr/bin/env python
#
import coral
from coral.modeler.diagram.degen import *
from coral import modeler
from coral.modeler.modeler import *
DI = coral.lang.XMIDI20
from coral.modeler.xmidigraphbuilder import xmidibuilder
from coral.modeler.diagram import xmidi_diagrams
from coral.addins.constraint import constraintTab
from coral.modeler import extension,action
import copy
import sys
import time

tm=coral.getTransactionManager()

L=coral.lang.CONSTRAINT10
XMIDI=coral.lang.XMIDI20

m=ModelerWindow()
m.main(loop=0)

## Opens the Constraint Tool
assert action.__exists__("openConstraint")
action["openConstraint"].__call__()
m.main(loop=0)

## Digs out the Constraint Tool and the outline editor
for e in m.editorSet.getEditors():
    if isinstance(e,coral.addins.constraint.constraintTab.ConstraintTab):
        ctool= e

for e in action.getCurrentHandler("modeler").editorSet.getEditors():
    if isinstance(e,coral.modeler.outline.outlineeditor.OutlineEditor):
        outline = e
        break
assert ctool
assert outline


## Test to creat a new constraint model
## This new UML14 Model will become the target for the ConstraintModel later on
tm.beginModifyCmd()
try:
    prj=coral.Project()
    
    m1=coral.lang.UML14.Model()
    m1.__setProject__(prj)
    c1 = coral.lang.UML14.Class(namespace=m1, name="C1")
    assert c1
finally:
    tm.endModifyCmd()

## Sets focus on the model so the constraintModel will come into its project
outline.notifyFocusOn(m1)

## Loads the constraints
tm.beginModifyCmd()
try:
    constPro = coral.loadDocument("../UML14Constraints/UML14Constraints.xml")
finally:
    tm.endModifyCmd()

## Creates the constraint package containing the ConstraintModel,
## loads the constraintModel in the engine and makes all visible in the Constraint Tool
ctool.newModel(constPro)
m.main(loop=0)

## Activates the continuous checking (and re-evaluates )
ctool.activateDeActivate()

    
ctool.modelWidget.updateModelWidget()
m.main(loop=0)

## There should be only one result saying that there are no target
assert len(ctool.modelWidget.cr.qlvItems)==1, str(len(ctool.modelWidget.cr.qlvItems))


## Sets the model as the taget to be checked
tm.beginModifyCmd()
try:
    ctool.modelWidget.cm.target = m1
    ctool.modelWidget.cm.result.resetSlot()
finally:
    tm.endModifyCmd()

ctool.modelWidget.updateModelWidget()
m.main(loop=0)


## Very ugly assert !!
## All constraints should be visible now. No failing constraint so far!
assert len(ctool.modelWidget.cr.qlvItems)>1, str(len(ctool.modelWidget.cr.qlvItems))


## Change the abstract model to create a failing constraint
tm.beginModifyCmd()
try:
    gene1=coral.lang.UML14.Generalization(namespace=m1,name="gene1")
    gene2=coral.lang.UML14.Generalization(namespace=m1,name="gene2")
    gene3=coral.lang.UML14.Generalization(namespace=m1,name="gene3")
    cls1=coral.lang.UML14.Class(generalization=[gene1],specialization=[gene2],namespace=m1)
    cls2=coral.lang.UML14.Class(generalization=[gene2],specialization=[gene3],namespace=m1)
    cls3=coral.lang.UML14.Class(generalization=[gene3],specialization=[gene1],namespace=m1)
finally:
    tm.endModifyCmd()

import coral.addins.constraint.engine as E
while E.isEvaluating():
    m.main(loop=0)

ctool.modelWidget.updateModelWidget()
m.main(loop=0)

assert cls1 in ctool.modelWidget.cm.result[0].offending
assert cls2 in ctool.modelWidget.cm.result[0].offending
assert cls3 in ctool.modelWidget.cm.result[0].offending


## Fix the failure
tm.beginModifyCmd()
try:
    cls4=coral.lang.UML14.Class(generalization=[gene2],namespace=m1)
    cls2.generalization.resetSlot()
    
    
finally:
    tm.endModifyCmd()

while E.isEvaluating():
    m.main(loop=0)

ctool.modelWidget.updateModelWidget()
m.main(loop=0)

## No failure any more!!
assert len(ctool.modelWidget.cm.result)==0

#deactivate the continuousObserverChecker
ctool.activateDeActivate()

