#!/usr/bin/env python

import coral
from coral.util.search.cquery import *

CQUERY=coral.lang.CQUERY10
UML=coral.lang.UML14

### Test prune_ignore_properties ###
def get_meta_property(lang_elem, property_name):
    slot=lang_elem().__getSlotByName__(property_name)
    return slot.__type__().getCorrespondingElement()

uml_cls=UML.Class(name="hejsan")
q_elem=CQUERY.QueryElement(abstractElement=uml_cls,name=uml_cls.name)
q_elem.ignore.append(get_meta_property(UML.Class,"name"))
q_elem.ignore.append(get_meta_property(UML.Class,"isAbstract"))

assert len(uml_cls.name)!=0
new_elem=prune_ignore_properties(q_elem)
assert new_elem.__getSlotByName__("name").size()==0
assert new_elem.__getSlotByName__("isAbstract").size()==0

## Test get_pruned_query_set ###

query=CQUERY.Query()

cls1=coral.lang.UML14.Class(name="Class1")
query.queryelement.append(CQUERY.QueryElement(abstractElement=cls1))

cls2=coral.lang.UML14.Class(name="Class1")
query.queryelement.append(CQUERY.QueryElement(abstractElement=cls1))

ass1=coral.lang.UML14.Association(name="Association1")
assend1=UML.AssociationEnd(participant=cls1,association=ass1,isNavigable=True)
assend2=UML.AssociationEnd(participant=cls2,association=ass1,isNavigable=True)

qe_ae1=CQUERY.QueryElement(abstractElement=cls1)
qe_ae1.ignore.append(get_meta_property(UML.Class,"name"))
query.queryelement.append(qe_ae1)

qe_ae2=CQUERY.QueryElement(abstractElement=cls2)
qe_ae2.ignore.append(get_meta_property(UML.Class,"name"))
query.queryelement.append(qe_ae2)

qe_ass1=CQUERY.QueryElement(abstractElement=ass1)
qe_ass1.ignore.append(get_meta_property(UML.Association,"name"))
query.queryelement.append(qe_ass1)

qe_assend1=CQUERY.QueryElement(abstractElement=assend1)
qe_assend1.ignore.append(get_meta_property(UML.AssociationEnd,"name"))
query.queryelement.append(qe_assend1)

qe_assend2=CQUERY.QueryElement(abstractElement=assend2)
qe_assend2.ignore.append(get_meta_property(UML.AssociationEnd,"name"))
query.queryelement.append(qe_assend2)

#query_set=get_pruned_query_set(query)
query_set=coral.Set()
for elem in query.queryelement:
    query_set.append(elem.abstractElement)

assert cls1 in query_set
assert cls2 in query_set
assert ass1 in query_set
assert assend1 in query_set
assert assend2 in query_set
#assert len(cls1.__getSlotByName__("name"))==0
#assert len(cls2.__getSlotByName__("name"))==0
#assert len(ass1.__getSlotByName__("name"))==0

prj=coral.loadDocument("testCQueryTarget.xml")
target=None
for root in prj.contents():
    if root.oclIsKindOf(UML.Model):
        target=root

matches=csearch(query, target)
assert len(matches)==3
assert qe_ass1.abstractElement.name

assert len(matches)==3,str(len(matches))

# for prop in assend1.__properties__():
#     if prop.isAnonymous():
#         continue
#     print prop.getName(),
#     slot=assend1.__getSlotByName__(prop.getName())
#     if slot:
#         print slot[0]
#     else:
#         print ""

elem=target.__getAllParts__().select(lambda p: p.oclIsKindOf(UML.Class))[0]
    
matches=csearch_elem(query, elem)

assert len(matches)==2

assert cls1.name
assert cls2.name
assert ass1.name != None
assert assend1.name != None
assert assend2.name != None
