def find_all_comm_package(rule_file,package_name):
    if not os.path.isfile(rule_file) :
        return None
    logging.info("Finding communications with "+package_name)
    call = [swipl_path(),'-G4g', '-q', '-g', "( \+ comm({0},AppB,Visited,Path,Length) -> Ps = [] ; setof(({0},AppB,Path),Visited^Length^comm({0},AppB,Visited,Path,Length),Ps)),writeln(Ps),halt".format(package_name), "-t", "'halt(1)'", '-s', rule_file]
    result = check_output(call)
    return parse_returned_app_list(result)
def find_all_comm(rule_file):
    if not os.path.isfile(rule_file) :
        return None
    logging.info("Executing SWIPL command")
    call = [swipl_path(),'-G4g', '-q', '-g', "( \+ comm({0},AppB,Visited,Path,Length) -> Ps = [] ; setof((AppA,AppB,Path),Visited^Length^comm(AppA,AppB,Visited,Path,Length),Ps)),writeln(Ps),halt","-t","'halt(1)'", '-s', rule_file]
    result = check_output(call)
    return parse_returned_app_list(result)
def find_all_comm_package(rule_file,package_name):
    if not os.path.isfile(rule_file) :
        return None
    logging.info("Finding communications with "+package_name)
    call = [swipl_path(),'-G4g', '-q', '-g', "( \+ comm({0},AppB,Visited,Path,Length) -> Ps = [] ; setof(({0},AppB,Path),Visited^Length^comm({0},AppB,Visited,Path,Length),Ps)),writeln(Ps),halt".format(package_name), "-t", "'halt(1)'", '-s', rule_file]
    result = check_output(call)
    return parse_returned_app_list(result)
def find_all_comm(rule_file):
    if not os.path.isfile(rule_file) :
        return None
    logging.info("Executing SWIPL command")
    call = [swipl_path(),'-G4g', '-q', '-g', "( \+ comm({0},AppB,Visited,Path,Length) -> Ps = [] ; setof((AppA,AppB,Path),Visited^Length^comm(AppA,AppB,Visited,Path,Length),Ps)),writeln(Ps),halt","-t","'halt(1)'", '-s', rule_file]
    result = check_output(call)
    return parse_returned_app_list(result)
def find_package_colluding(rule_file,package,colluding_predicate):
    if colluding_predicate not in colluding_predicates:
        return None
    if not os.path.isfile(rule_file) :
        return None
    logging.info("Executing SWIPL command")
    call = [swipl_path(),'-G4g', '-q', '-g', "( \+ {0}({1},AppB,Path) -> Ps = [] ; setof(({1},AppB,Path),{0}({1},AppB,Path),Ps)),writeln(Ps),halt".format(colluding_predicate,package),"-t","'halt(1)'", '-s', rule_file]
    result = check_output(call)
    return parse_returned_app_list(result)
def find_package_colluding(rule_file,package,colluding_predicate):
    if colluding_predicate not in colluding_predicates:
        return None
    if not os.path.isfile(rule_file) :
        return None
    logging.info("Executing SWIPL command")
    call = [swipl_path(),'-G4g', '-q', '-g', "( \+ {0}({1},AppB,Path) -> Ps = [] ; setof(({1},AppB,Path),{0}({1},AppB,Path),Ps)),writeln(Ps),halt".format(colluding_predicate,package),"-t","'halt(1)'", '-s', rule_file]
    result = check_output(call)
    return parse_returned_app_list(result)
def find_package_colluding_length(rule_file,colluding_predicate,package="AppA",length=2):
    if colluding_predicate not in colluding_predicates:
        return None
    if not os.path.isfile(rule_file) :
        return None
    colluding_predicate = colluding_predicate+"_length"
    logging.info("Executing SWIPL command")
    call = [swipl_path(),'-G4g', '-q', '-g', "( \+ {0}({2},AppB,Path,{1}) -> Ps = [] ; setof(({2},AppB,Path),{0}({2},AppB,Path,{1}),Ps)),writeln(Ps),halt".format(colluding_predicate,length,package),"-t","'halt(1)'", '-s', rule_file]
    logging.debug("Call is :"+str(call))
    result = check_output(call)
    logging.info("Ids of apps obtained :"+result)
    return parse_returned_app_list(result)
def find_package_colluding_length(rule_file,colluding_predicate,package="AppA",length=2):
    if colluding_predicate not in colluding_predicates:
        return None
    if not os.path.isfile(rule_file) :
        return None
    colluding_predicate = colluding_predicate+"_length"
    logging.info("Executing SWIPL command")
    call = [swipl_path(),'-G4g', '-q', '-g', "( \+ {0}({2},AppB,Path,{1}) -> Ps = [] ; setof(({2},AppB,Path),{0}({2},AppB,Path,{1}),Ps)),writeln(Ps),halt".format(colluding_predicate,length,package),"-t","'halt(1)'", '-s', rule_file]
    logging.debug("Call is :"+str(call))
    result = check_output(call)
    logging.info("Ids of apps obtained :"+result)
    return parse_returned_app_list(result)
def communication_channels(rule_file,app_set):
    if len(app_set) < 2:
        return []
    app_a = app_set[0]
    app_b = app_set[-1]
    path_string = "[]"
    if len(app_set) > 2:
        path_string = ",".join([str(i) for i in app_set[1:-1]])
        path_string = "[" + path_string+ "]"
    prolog_command = 'setof(C,channel({0},{1},{2},C),Cs),writeln(Cs),halt'.format(app_a,app_b,path_string)
    logging.debug("Executing :"+prolog_command)
    call = [swipl_path(),'-G4g','-q', "-g", prolog_command, "-t","'halt(1)'", "-s", rule_file]
    result = check_output(call)
    return parse_returned_channel_list(result)
def communication_channels(rule_file,app_set):
    if len(app_set) < 2:
        return []
    app_a = app_set[0]
    app_b = app_set[-1]
    path_string = "[]"
    if len(app_set) > 2:
        path_string = ",".join([str(i) for i in app_set[1:-1]])
        path_string = "[" + path_string+ "]"
    prolog_command = 'setof(C,channel({0},{1},{2},C),Cs),writeln(Cs),halt'.format(app_a,app_b,path_string)
    logging.debug("Executing :"+prolog_command)
    call = [swipl_path(),'-G4g','-q', "-g", prolog_command, "-t","'halt(1)'", "-s", rule_file]
    result = check_output(call)
    return parse_returned_channel_list(result)