0

I am running my Prolog query from Python 3 using pyswip, but the response to the query is not what I am expecting. Instead of: X = us, D = [fact2,ipGeolocation(us)] ? I am getting {'X': 'us', 'D': [Atom('712197'), Atom('711429'), Functor(15831181,1,us)]}. How do I do away with the Atom and Functor keywords and get the actual values, as in the first instance? Look at the two screenshots I have attached.

These are my prolog and python codes respectively:

Prolog

:- compile('/PATH_TO_GORGIAS/gorgias-src-0.6d/lib/gorgias.pl').
:- compile('/PATH_TO_GORGIAS/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

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

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


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

Instead of having the output with these Atom and Functor keywords:

enter image description here

I want the output like this, without the Atom and Functor keywords, but the actual values:

enter image description here

  • Are you asking why you get `'X': 'china'` instead of `X = china` or why you get `china` instead of `us`? – mkrieger1 Jan 01 '21 at 00:33
  • No. I don't want to have Atom and Functor in my query result. I want to have the actual string equivalent. Look at the screenshot I have attached. – Faisal Ali Gama Jan 01 '21 at 00:34
  • What exactly should be the string equivalent of `Functor(15831181,1,china)`? – mkrieger1 Jan 01 '21 at 00:34
  • The query result suppose to give me: ```X = us, D = [fact2,ipGeolocation(us)]``` instead of ```'X': 'us', 'D': [Atom('712197'), Atom('711429'), Functor(15831181,1,china)]``` So I dont' want to have Atom and Functor in my query result. I want to have the actual value which in this case is fact2 and ipGeolocation(us). – Faisal Ali Gama Jan 01 '21 at 00:38
  • I don't understand what is supposed to be the relation between `{'X': 'china', 'D': [Atom('712197'), Atom('711429'), Functor(15831181,1,china)]}` and `X = us, D = [fact2,ipGeolocation(us)]`. – mkrieger1 Jan 01 '21 at 00:40
  • I have edited the question. I want to have the actual string value of Atom and Functor in my result output. – Faisal Ali Gama Jan 01 '21 at 00:45
  • I wish to have the same output just like when I run it with SWI-Prolog from the terminal. Look at the second screenshot. – Faisal Ali Gama Jan 01 '21 at 00:50
  • Does this answer your question: https://stackoverflow.com/questions/27581563/how-to-get-functor-string-in-prolog-using-pyswip – mkrieger1 Jan 01 '21 at 11:11
  • That method is too complex. Is there any more simple and clearer way to achieve that? – Faisal Ali Gama Jan 01 '21 at 19:01

0 Answers0