#!/usr/bin/env python

import coral
import coral.util.search.query as Q
import coral.util.search.util as U

L=coral.lang.UML14

prj=coral.loadDocument("testQueryOnQuery.xml")

queries=[]

for root in prj.contents():
    if root.oclIsKindOf(L.Model) and root.name[:5]=="Query":
        queries.append(root)

assert queries

for query in queries:
    query_set=coral.Set()
    for elem in query.ownedElement:
        query_set.append(elem)

    print "Running",query.name
    num=len(Q.search(U.get_all_parts(query_set), query, None, True))
    assert num > 0
    if num == 1:
        print "Non symetric query => no permutations"
        perm=len(Q.search(query_set, query, None, True))
        noperm=len(Q.search(query_set, query, None, False))
        print ":-) ", perm,"==",noperm
        assert perm == noperm
    else:
        perm=len(Q.search(query_set, query, None, True))
        noperm=len(Q.search(query_set, query, None, False))
        print perm,">=",noperm
        assert perm>=noperm
