#!/usr/bin/env python
#
#
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

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

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

tm = coral.getTransactionManager()

tm.beginModifyCmd()

try:
    prj = coral.loadDocument("testEngineEvaluate.xml")
finally:
    tm.endModifyCmd()

for elem in prj.contents():
    if elem.oclIsKindOf(L.Package):
        package = elem

    elif elem.oclIsKindOf(XMIDI.Diagram) and elem.name == "Target":
        m.displayDiagram(elem)
        
assert package

assert action.__exists__("openConstraint")

action["openConstraint"].__call__()
m.main(loop=0)

for elem in package.element:
    if elem.oclIsKindOf(L.ConstraintModel):
        constraintmodel=elem

assert constraintmodel

for e in m.editorSet.getEditors():
    if isinstance(e,coral.addins.constraint.constraintTab.ConstraintTab):
        ct= e
assert ct


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

ct.loadConstraintModel(constraintmodel)
m.main(loop=0)

#for w in ct.activeModelWidgets.values():
#ct.modelWidget.deActivate()
#ct.modelWidget.activate()

print "Active",coral.addins.constraint.engine.isActive(constraintmodel)

#activate the continuousObserverChecker
ct.activateDeActivate()
m.main(loop=0)

##
print "Active",coral.addins.constraint.engine.isActive(constraintmodel)

outline.notifyFocusOn(ct.modelWidget.cm)
#assert w.tabWidget.parent().focus == ct.focus
    
qlv = ct.modelWidget.cr.qlvItems
offending = ct.modelWidget.ol.olItems
temp  = qlv.copy()
for i in temp:
    ct.modelWidget.cr.clicked(temp[i])
assert len(qlv) == 9

id = 1
list = qlv.values()

## Checks that there are failures
for r in list:#w.cr.qlvItems.values():
    if r.subject.oclIsKindOf(L.Constraint):
        assert r.isExpandable() == True

list = qlv.values()
new = []
org = list[:]
org_result = []
for r in org:
    org_result.append(r.subject)
    
for r in list:
    if r.subject.oclIsKindOf(L.Result):
            
        # Results aren't exandable!
        assert r.isExpandable() == False
        ct.modelWidget.cr.clicked(r)
            
        if r.subject.name == "HasName" or r.subject.name == "UniqueClassNames":
            for of in ct.modelWidget.ol.olItems:
                ct.modelWidget.ol.clicked(ct.modelWidget.ol.olItems[of])
                tm.beginModifyCmd()
                of.name = str(id)
                tm.endModifyCmd()
                id = id+1
                    
        elif r.subject.name == "NoMultipleGene":
            for of in ct.modelWidget.ol.olItems:
                ct.modelWidget.ol.clicked(ct.modelWidget.ol.olItems[of])
                while True:
                    if len(of.generalization)>1:
                        tm.beginModifyCmd()
                        of.generalization[0].namespace = None
                        tm.endModifyCmd()
                    else:
                        break
        m.main(loop=0)

# Trigger a transaction UGLY
tm.beginModifyCmd()
constraintmodel.name="cmcmcmcmcm"
tm.endModifyCmd()
## All results fixed and removed

assert len(ct.modelWidget.cr.qlvItems)==3,str(len(ct.modelWidget.cr.qlvItems))


    
