IS_SAO_SYSTEM = ":Flow"

list_of_DSO = ["DSO", "DSO-AOCS", "DSO-AOCS-instances"]
list_of_RMO = ["RMO", "RMO-instances", "DSO-AOCS-instances", "SAO"]


'''Return the Systems and Items from the Ontologies'''
def get_keyword(keyword):
    system_list = []
    for dso in list_of_DSO :
        flag_1, flag_2 = 0, 0
        with open("../Ontologies/Mokos_18_1_7_47.ttl") as f:
            for line in f:
                if line.startswith(dso):
                    spliter_1 = line.split(':')
                    spliter_2 = spliter_1[1].split(' ')
                    key = spliter_2[0].strip()
                    flag_1 = 1
                if flag_1 == 1: 
                    if ':' in keyword:   
                        if keyword in line and ("subClassOf" in line) and not ( ':comment' in line or 'disjointWith' in line or 'construct' in line or 'label' in line) :
                            flag_2 = 1
                    elif not ':' in keyword:
                         keyword = ':'+keyword 
                         if keyword in line and ("subClassOf" in line) and not ( ':comment' in line or 'disjointWith' in line or 'construct' in line or 'label' in line) :
                            flag_2 = 1
                if flag_1 == 1 and flag_2 == 1:
                    if (line.strip()).endswith('.'):
                        if not key in system_list:
                            system_list.append(key)
                        flag_1, flag_2 = 0, 0
                if (line.strip()).endswith('.') and not ((line.strip()).startswith('-') or (line.strip()).startswith(':comment')) :
                    flag_1, flag_2 = 0, 0  
    return system_list #tuple((str(n), str(n)) for n in system_list)


'''Return the Flow,  from the Ontologies'''
def get_keyword_2(keyword):
    system_list = []
    for dso in list_of_RMO :
        flag_1, flag_2 = 0, 0
        with open("../Ontologies/Mokos_18_1_7_47.ttl") as f:
            for line in f:
                if line.startswith(dso): 
                    spliter_1 = line.split(':')
                    spliter_2 = spliter_1[1].split(' ')
                    key = spliter_2[0].strip()
                    flag_1 = 1
                if flag_1 == 1: 
                    if ':' in keyword:   
                        if keyword in line and ("domain" in line or "range" in line or "a" in line) and not ( ':comment' in line or 'disjointWith' in line or 'construct' in line or 'label' in line) :
                            flag_2 = 1
                    elif not ':' in keyword:
                         keyword = ':'+keyword 
                         if keyword in line and ("domain" in line or "range" in line or "a" in line) and not ( ':comment' in line or 'disjointWith' in line or 'construct' in line or 'label' in line) :
                            flag_2 = 1
                if flag_1 == 1 and flag_2 == 1:
                    if (line.strip()).endswith('.'):
                        if not key in system_list:
                            system_list.append(key)
                        flag_1, flag_2 = 0, 0
                if (line.strip()).endswith('.') and not ((line.strip()).startswith('-') or (line.strip()).startswith(':comment')) :
                    flag_1, flag_2 = 0, 0  
    return system_list #tuple((str(n), str(n)) for n in system_list)



def find_extra_keywords(keyword, choice):
    third_search = []
    if choice == 1 :
        first_search = get_keyword(keyword)
        second_search = first_search.copy()
        for i in first_search:
            k = get_keyword(i)
            #print(i)
            #print(k)
            for kl in k:
                if not kl in second_search:
                    second_search.append(kl)
        third_search = second_search.copy()
        for i in second_search:
            k = get_keyword(i)
            #print(i)
            #print(k)
            for kl in k:
                if not kl in third_search:
                    third_search.append(kl)
    if choice == 2 :
        first_search = get_keyword_2(keyword)
        second_search = first_search.copy()
        for i in first_search:
            k = get_keyword_2(i)
            #print(i)
            #print(k)
            for kl in k:
                if not kl in second_search:
                    second_search.append(kl)
        third_search = second_search.copy()
        for i in second_search:
            k = get_keyword_2(i)
            #print(i)
            #print(k)
            for kl in k:
                if not kl in third_search:
                    third_search.append(kl)
    return tuple((str(n), str(n)) for n in third_search)




#print(len(find_extra_keywords(IS_SAO_SYSTEM, 2)))
STATE_VALUE_CHOICES = find_extra_keywords(":StateValue", 2)
new = [ x for x in find_extra_keywords(":State", 2) if x not in STATE_VALUE_CHOICES ]
print("this is",new)
#print(get_keyword_2(IS_SAO_SYSTEM))