yuce / pyswip

PySwip is a Python - SWI-Prolog bridge enabling to query SWI-Prolog in your Python programs. It features an (incomplete) SWI-Prolog foreign language interface, a utility class that makes it easy querying with Prolog and also a Pythonic interface.
https://pyswip.org
MIT License
472 stars 97 forks source link

How to get the actual Atom and Functor String Equivalent? #116

Closed alifa2try closed 3 years ago

alifa2try commented 3 years ago

I am querying Prolog program from Python Script. But in the result output I am getting Atom and Functor in the result output. How do I get the string equivalent?

These are my codes - Prolog:

` :- compile('/home/faysal/Documents/Attribution/ABR/gorgias-src-0.6d/lib/gorgias.pl'). :- compile('/home/faysal/Documents/Attribution/ABR/gorgias-src-0.6d/ext/lpwnf.pl').

% Rules rule(notGuiltyByDefault(X), (neg(isCulprit(X)), []). rule(ipGeolocation(X), isCulprit(X), [ipGeoloc(X, IP)]). rule(spoofedIp(X), neg(isCulprit(X)), [ipGeoloc(X, IP), spoofedIP(IP)]).

% Facts rule(fact1, ipGeoloc(china, ip1), []). rule(fact2, ipGeoloc(us, ip2), []). rule(fact3, spoofedIP(ip1), []).

%Priority/Preference rule(p1(X), prefer(spoofedIp(X), ipGeolocation(X)), []). rule(p2(X), prefer(ipGeolocation(X), notGuiltyByDefault(X)), []).

`

Python Script:

`def get_argument(): parser = argparse.ArgumentParser(description=display_banner()) parser.add_argument("-q", "--query", dest= "query", help="Query") option = parser.parse_args()

if not option.query:
    parser.error(f"{Red}[-] You need to specify query, enter -h for help")
    raise SystemExit    
return option.query 

def query_knowledge_base(query_term): prolog = Prolog() prolog.consult('code.pl') return list(prolog.query(query_term))

def print_query_result(query_term, query_result_list): print(f'[+] The query submitted is: {Green}%s{Reset}' % (query_term))

if not query_result_list:
    print(f'\n[-] The result of your submitted query: {Red}%s{Reset}' % ('False'))
    raise SystemExit

for query_result in query_result_list:

    if len(query_result) == 0:
        print(f'\n[+] The result of your submitted query: {Green}%s{Reset}' % ('True'))
        raise SystemExit

    #list(query_result)
    #print(f'\n[+] The result of your submitted query: {Green}{query_result}{Reset}')

    for key, value in query_result.items():
        if 'Atom' in value:
            value = list(value)
            print(f'\n[+] The result of your submitted query: {key}:{value}')

        print(f'\n[+] The result of your submitted query: {key}:{value}')   

    #print('\n[+] The result of your submitted query: ', query_result['X'])

query_term = get_argument() query_result_list = query_knowledge_base(query_term) print_query_result(query_term, query_result_list) `

Output

image